Wednesday, August 14, 2013

Distributed Semaphore (Lock) with memcached

Hi, today I want to show a simple technique how to build a synchronization primitive when you have compare-and-swap operation (in fact you can apply those technique to Mongo or Zokeeper and get the semaphore backed by Mongo or ZooKeeper, actually I really like ZK-based locks and semaphores since there is a built-in notification for value changes).
!!! Note, that the code I provide is just for demonstration purposes it's not production-ready. Please do not use these distributed semaphores as is.

Wednesday, August 7, 2013

Recursively resize images

A couple of days ago I was trying to share my photos to my relatives. The thing is that there was 25Gb of photos and there was a bunch of nested folders in my photo collection. I didn't want to share the photos in such a high resolution and that was a problem #1. Admittedly I did have a image converter that could be used as image resizer but it only worked with one folder, completely ignoring the nested, so there was a problem #2.

I decided to create a simple image resizer that could recursively traverse through nested folders and resize the images. As usual, sources and binary (github). Note that You need JRE to run the recursive image resizer.

I should say I find that utility pretty useful (at least for me :)
  1. The first good thing is that you invoke it with SOURCE_DIR and DESTINATION_DIR parameters and it creates 100% identical copy of SOURCE_DIR, resizes the images and places them in the same place (ralative to source dir). 
  2. The second good thing is that it utilizes mutiple CPU's of your machine, so, hopefully it the recursive conversion of your images should complete faster. 

The usage of  recursive image resizer is simple, just run to get help:
java -Xmx1024m -jar image-resize-app.jar

I'll give you a simple example:
java -Xmx1024m -jar image-resize-app.jar -r 1920,1440 /home/username/photos /home/username/photos_resized

Friday, August 2, 2013

Learn Hibernate By Example

Hey, I should say I'm a very big fan of learning by example. The thing is, once you have a solid background you don't need words to be said: the code tells everything. So I've added a couple of projects which are dedicated to Hibernate and related stuff. And as always github: hibernate-samples and hibernate-ehcache. All the examples are implemented as JUnit tests.

Thursday, July 25, 2013

» Post-Order Iterative DFS Traversal using Stack

Several days ago I had to traverse a huge graph in depth (depth-first-search) visiting edges in post-order. I was working on Kasaraju's algorithm to find strongly connected components of a graph.

Of course the default (natural) implementation of post-order DFS is very easy to be read and understand. In my implementation of I'm using callback in order to change the behaviour when node is visited - for example I can just print the nodes or add the into a collection without changing the algorithm.

    public void dfsPostOrderRecursive(AdjGraph graph, AdjGraph.Node vertex, 
                                                 Callback callback) {
        if (vertex.isVisited())


        List<AdjGraph.Node> edges = graph.edges(vertex);
        for (AdjGraph.Node node : edges) {
            if (!node.isVisited())
                dfsPostOrderRecursive(graph, node, callback);

        callback.nodeVisited(graph, vertex);  //post order call to callback

The thing is, there were about a million of nodes in the graph. So it's possible to reach a stack depth of 1 million of nested calls while doing recursive DFS. The Java stack by default is not configured to work with such a huge amount of recursive calls...

Tuesday, July 23, 2013

Software Engineering: Java Tuples

This post is related to Java Tuples. Every time I'm switching from Scala or Python back to Java what I'm missing are tuples. Simple but effective idea of tuples minimizes the amount of code needed to express my ideas in code.

Sure thing you don't want to publish an API with Tuples as a return type of your methods. But if the tuples are used between your methods - they can significantly reduce the amount of work and a number of classes needed. So I've implemented my own library to add Tuples to Java. Feel free to try it. Sources on GitHub


Saturday, June 15, 2013

Programming Puzzle: Multiply Matrixes with MongoDB

Today I want to share an idea on how to multiply matrixes using MondoDB's facilities. It's not the way you usually multiply them and more likely you never need to do this with Mongo. However the idea provided here can be applied to any MapReduce framework (for example: Hadoop). Frankly speaking it's not a clean and robust way of multiplying the matrixes, it's done just for fun, so don't take this article to seriously.

I'm going to consider two approaches, the first one - using only Mongo's MapReduce facilities and the second - MapReduce + Aggregation Framework. Intrigued? :) Continue reading then...

Wednesday, June 5, 2013

How to calculate PValue from ChiSquare in Java

Several days ago I faced a problem: I needed to calculate pValue for a given ChiSquare with a given Degrees of Freedom. I didn't have time to do the research and to implement the algorithm from the scratch. In fact I had just a couple of hours to solve the problem. So I started searching...

I was unable to find PValue calculator written in Java but was able to find one written in JavaScript there. So I converted the code into Java and want to share it to you (just call pochisq(chiSquareValue, degreesOfFreedom)):