Pages

Experimenting on Space4j , an in memory java database system

Its always challenging to develop scalable and high performance applications in java.There are different form of data persistence made available in all programming platforms.Considering the back end data persistence , when there is another layer of mapping ... Object Relational Mapping layer to map java objects and data store , then it will be easy for providing maintenance, supporting multi-vendor products at the cost of performance.I am not expertise in the vast numbered ORM tools (I had written an article before ).In java its a hack .. using the Reflection and collection APIs effectively.This article is about a new form of database system for java.I think its better suitable for real time ,standalone applications .We know the key value pair data structure is used mostly in applications.So the said to be database system is based on maps in Java Collections API.This is an in memory database.It reads and persists data using serialization.For always on application data is always in the memory.If there is any crash then the data is regenerated from the commands previously exectued.
I have read an article in InfoQ regarding the primary in-RAM application.The hardware level changes could tend to rapidly redesign all the framework paradigms, tools ,design patterns etc

So I decided to play around with the Space4j .The data is the space.We have to create a space in memory and we assign data to it.The map stores the serializable objects.Clients will use the Space4J object to execute commands in the underlying space. Space4J will be running in the same JVM the client is running, directly accessing from memory.

ie space4jObj.exec(Command)

Commands can be
- CreateMapCmd that will initiate the map.Like creating table.
- PutCmd to insert data
- RemoveCmd to remove data

We create a map and store in the space.There are different implementations for space.I tried with SimpleSpace4j I have created a CURD operation for space4j sample.Create a movie list and
store.Add jar to classpath.

Movie.java

package com.space4j.sample;
import java.io.Serializable;
public class Movie implements Serializable {
private static final long serialVersionUID = -1L;
private String name ;
private String id;
public Movie(String name,String id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public String getId(){
return id;
}
@Override
public String toString(){
return id+":"+name;
}
@Override
public boolean equals(Object obj){
boolean b = false;
if(obj instanceof Movie){
Movie mov = (Movie) obj;
if (mov.getId() == this.id){b = true;}
}
return b;
}

}
SpaceDAO.java

package com.space4j.sample;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.space4j.CommandException;
import org.space4j.LoggerException;
import org.space4j.Space4J;
import org.space4j.Space;
import org.space4j.command.CreateMapCmd;
import org.space4j.command.PutCmd;
import org.space4j.command.RemoveCmd;
import org.space4j.implementation.SimpleSpace4J;


public class SpaceDAO {


private static String MAP_NAME = "movies";
private Space4J space4j = null;
private Space space = null;
public SpaceDAO(){
try {
space4j = new SimpleSpace4J("SpaceDAO");
space4j.start();
space = space4j.getSpace();
if(!space.check(MAP_NAME)){
space4j.exec(new CreateMapCmd(MAP_NAME));
}


} catch (LoggerException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (CommandException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnknownHostException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void create(Movie mov){


try {
space4j.exec(new PutCmd(MAP_NAME,mov.getId(), mov));

} catch (CommandException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (LoggerException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void update(Movie mov){

try {
space4j.exec(new PutCmd(MAP_NAME, mov.getId(), mov));

} catch (CommandException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (LoggerException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
}


}

public Iterator read(){
return space.getIterator(MAP_NAME);
}
public void delete(Movie mov){
try {
space4j.exec(new RemoveCmd(MAP_NAME, mov.getId()));
} catch (CommandException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
} catch (LoggerException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
}

}

public void createSnapshot(){
try {
space4j.executeSnapshot();
} catch (LoggerException ex) {
Logger.getLogger(SpaceDAO.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

Test.java

package com.space4j.sample;

import java.util.Iterator;

/**
*
* @author Haris
*/
public class Test {

public static void main(String ars[]){
SpaceDAO sp = new SpaceDAO();
Movie mov1 = new Movie("Shawshank Redemption","M1");
sp.create(mov1);
Iterator iter = sp.read();
Movie mov = null;
while(iter.hasNext()){
mov = iter.next();
System.out.println(mov.getName());
}
mov1 = new Movie ("No Country for Old Men","M1");
sp.update(mov1);
sp.createSnapshot();

}
}


There will be a space4j_db folder created in your work directly automatically storing all the log files based on the persistent class names.When we take a snapshot a file with extension ".snap" is created.Things i donn like: No package structure for files.Files are named by numbers. like 000000000001.log etc.Creating too many files.
It supports indexing , sequence numbers,clustering,db replication etc.
I did this for a study purpose only.Actually read an article from InfoQ .. so tried out hows it .. Article
Project site

No comments:

Post a Comment