Thursday, 26 April 2012

Spring-MongoDB: Checking if data member is of array type in map function.

If you'll ever have to do things like that, following code worked out perfectly for me:
if( Object.prototype.toString.call( myObj ) === '[object Array]' ) {}
source: http://stackoverflow.com/questions/4775722/javascript-check-if-object-is-array 

Spring-MongoDB: debugging map\reduce on ubuntu.

while using Spring-data-MongoDB on ubuntu system, with default mongo installation you migh be experiencing troubles with error messages, as they might look similar to this:
InvalidDataAccessApiUsageException: Command execution failed:  Error [db assertion failure], Command = { "mapreduce" : ... }
The error message will include whole command, an will give you no clues about what actually went wrong. Try executing following command from terminal and repeat issue:
 tail -f /var/log/mongodb/mongodb.log 
trace information will get printed to log and you'll see it in terminal.  You should be looking for messages as following:

 JS Error: TypeError: myVar has no properties nofile_b:69
You can also add print to log in map\reduce functions with print("...") method invocation.
print('variable state is : ' + myVar) ;

 

Tuesday, 24 April 2012

Spring-Data-MongoDB passing scope variables to map\reduce

Spring-Data-MongoDB passing scope variables to map\reduce:

It's becoming a little bit tricky if you want to pass something like wrapper that contains Map of Map's as a parameter to map\reduce.

Anyhow, If you'll find yourself in trouble passing parameters to map\reduce here is one way to handle it:


MapReduceOptions options = MapReduceOptions.options();
Map vars = new HashMap();
        try {
            ObjectMapper mapper = new ObjectMapper();
            DBObject dbo = new BasicDBObject(mapper.convertValue(myobject, Map.class));
            vars.put("myparamName",dbo);
        } catch (Exception e) {
            logger.error(e);
        }
        options.scopeVariables(vars);
this way scope parameter will be passed as a pure JSON map object, which will be automatically mapped to js object by MongoDB.

ObjectMapper is a Jackson framework class.

Saturday, 21 April 2012

JSON serialization of JS objects with Spring.

JSON serialization of JS objects with Spring.

Problem: pass JS object from spring MVC enabled backend to HTML page, use eval (...) on object and invoke function that have been passed from backend.

Solution: By default Spring framework is using Jackson to serialize Java objects. During that serialization all members are passed as strings, while we'd like to have data structure as following:

{
    member1 : "value1",
    action : function() {
             ...
    }
}
with such an object it would be possible to do the following to invoke function:
 obj.action()
 In order to do that one would have to implement custom serializer class

public class MySerializer extends JsonSerializer<MyTO> {
    public void serialize(MyTO value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeObjectField("member1", value.getMember1());
        jgen.writeFieldName("action");
        jgen.writeRaw(":" + value.getAction());
    }
}

Done. use eval on front end to convert JSON into actual JS object and enjoy. 

Ubuntu 11.10 installing CMU Sphinx 4.

Ubuntu 11.10 installing CMU Sphinx 4.

The process is fairly straightforward:

1. Install Oracle JDK (I've used version 1.6)
2. install ant
sudo apt-get install ant
3. install sharutils
sudo apt-get install sharutils
4. navigate to desired folder
cd /usr/share
5. install svn
sudo apt-get install subversion
6. checkout files
sudo svn co https://cmusphinx.svn.sourceforge.net/svnroot/cmusphinx/trunk/sphinx4
7. run jsapi.sh
sudo sh ./sphinx4/lib/jsapi.sh
8. build project
cd sphinx4 sudo ant

Next steps will take you to the journey of creating a real project.