Thursday, August 4, 2011

Voldemort Database Integration with Didik's Container

package com.bnis.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import voldemort.client.ClientConfig;
import voldemort.client.SocketStoreClientFactory;
import voldemort.client.StoreClient;
import voldemort.client.StoreClientFactory;
import voldemort.client.protocol.admin.AdminClient;
import voldemort.client.protocol.admin.AdminClientConfig;
import voldemort.cluster.Node;
import voldemort.utils.ByteArray;

import com.bnis.qbean.DeployableMXBean;

public class VoldemortBean extends DeployableMXBean {
private StoreClientFactory factory;
private StoreClient client;
private AdminClient admin;
private String dbName, bootstrapURL;

@Override
public void startService() throws Exception {
// TODO Auto-generated method stub
bootstrapURL = param.get("bootstrap-url");
dbName = param.get("db-name");
factory = new SocketStoreClientFactory(new ClientConfig()
.setBootstrapUrls(bootstrapURL));
client = factory.getStoreClient(dbName);

admin = new AdminClient(bootstrapURL, new AdminClientConfig());

}

@Override
public void stopService() throws Exception {
// TODO Auto-generated method stub
client = null;
factory.close();
System.gc();
}

public void put(String key, String value) {
client.put(key, value);
// System.out.println("put:" + key + "\t:" + value);
}

public String get(String key) {
return client.getValue(key);
}

public void delete(String key) {
client.delete(key);
}

public List getKeys() {
List l = new ArrayList();
Collection nodes = admin.getAdminClientCluster().getNodes();
for (Node node : nodes) {
if (factory.getFailureDetector().isAvailable(node)) {
List partitionId = node.getPartitionIds();
Iterator it = admin.fetchKeys(node.getId(), dbName,
partitionId, null, false);
while (it.hasNext()) {
l.add(new String(it.next().get()));
}
}
}
return l.size() == 0 ? null : l;
}

public List cleanIfNotIn(List filter) {
List deleted=new ArrayList();
long l = System.currentTimeMillis();
List lAllKeys = getKeys();
if(lAllKeys==null)
return deleted;
for (String key : lAllKeys) {
if (!filter.contains(key)) {
delete(key);
deleted.add(key);
}
}
l = System.currentTimeMillis() - l;
StringBuffer sb = new StringBuffer();
sb.append("clean up ").append(deleted.size()).append(
" row(s)").append(" in ").append(l).append(" ms");
log.debug(sb.toString());
return deleted;
}

public void cleanIfIn(List filter) {
long l = System.currentTimeMillis();
for (String key : filter) {
delete(key);
}
l = System.currentTimeMillis() - l;
StringBuffer sb = new StringBuffer();
sb.append("clean up ").append(filter.size()).append(" row(s)").append(
" in ").append(l).append(" ms");
log.debug(sb.toString());
}
}


package com.bnis.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import voldemort.client.ClientConfig;
import voldemort.client.SocketStoreClientFactory;
import voldemort.client.StoreClient;
import voldemort.client.StoreClientFactory;
import voldemort.client.protocol.admin.AdminClient;
import voldemort.client.protocol.admin.AdminClientConfig;
import voldemort.cluster.Node;
import voldemort.utils.ByteArray;

import com.bnis.qbean.DeployableMXBean;

public class VoldemortBean extends DeployableMXBean {
private StoreClientFactory factory;
private StoreClient client;
private AdminClient admin;
private String dbName, bootstrapURL;

@Override
public void startService() throws Exception {
// TODO Auto-generated method stub
bootstrapURL = param.get("bootstrap-url");
dbName = param.get("db-name");
factory = new SocketStoreClientFactory(new ClientConfig()
.setBootstrapUrls(bootstrapURL));
client = factory.getStoreClient(dbName);

admin = new AdminClient(bootstrapURL, new AdminClientConfig());

}

@Override
public void stopService() throws Exception {
// TODO Auto-generated method stub
client = null;
factory.close();
System.gc();
}

public void put(String key, String value) {
client.put(key, value);
// System.out.println("put:" + key + "\t:" + value);
}

public String get(String key) {
return client.getValue(key);
}

public void delete(String key) {
client.delete(key);
}

public List getKeys() {
List l = new ArrayList();
Collection nodes = admin.getAdminClientCluster().getNodes();
for (Node node : nodes) {
if (factory.getFailureDetector().isAvailable(node)) {
List partitionId = node.getPartitionIds();
Iterator it = admin.fetchKeys(node.getId(), dbName,
partitionId, null, false);
while (it.hasNext()) {
l.add(new String(it.next().get()));
}
}
}
return l.size() == 0 ? null : l;
}

public List cleanIfNotIn(List filter) {
List deleted=new ArrayList();
long l = System.currentTimeMillis();
List lAllKeys = getKeys();
if(lAllKeys==null)
return deleted;
for (String key : lAllKeys) {
if (!filter.contains(key)) {
delete(key);
deleted.add(key);
}
}
l = System.currentTimeMillis() - l;
StringBuffer sb = new StringBuffer();
sb.append("clean up ").append(deleted.size()).append(
" row(s)").append(" in ").append(l).append(" ms");
log.debug(sb.toString());
return deleted;
}

public void cleanIfIn(List filter) {
long l = System.currentTimeMillis();
for (String key : filter) {
delete(key);
}
l = System.currentTimeMillis() - l;
StringBuffer sb = new StringBuffer();
sb.append("clean up ").append(filter.size()).append(" row(s)").append(
" in ").append(l).append(" ms");
log.debug(sb.toString());
}
}


package com.bnis.voldemort;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.bnis.bean.VoldemortBean;

public class VoldemortMap {
private ConcurrentHashMap data=new ConcurrentHashMap();
private VoldemortBean bean=null;
private ExecutorService executor=null;
public VoldemortMap(VoldemortBean bean,int maxExecutor){
this.bean=bean;
this.executor=Executors.newFixedThreadPool(maxExecutor);
}
public void put(final String key,final String value){
data.put(key, value);
executor.submit(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
bean.put(key, value);
}
});
}
/**
* get Value from 'cache', if doesn't exist get from 'voldemort' and add it to 'cache'
* @param key
* @return String value
*/
public String get(String key){
String value=this.data.get(key);
if(value!=null)
return value;
value=bean.get(key);
//add to cache
this.data.put(key, value);

return value;
}
public boolean contains(String key){
String value=get(key);
return value!=null;
}
public String remove(final String key){
String value=get(key);
if(value==null)
return null;
data.remove(key);
executor.submit(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
bean.delete(key);
}
});
return value;
}
}


package com.bnis;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.bnis.bean.VoldemortBean;
import com.bnis.cmd.Command;
import com.bnis.cmd.ICommandHandler;
import com.bnis.util.NamedRegistrar;
import com.bnis.voldemort.VoldemortMap;

public class Test implements ICommandHandler{
Logger log=Logger.getLogger(getClass());
public static void main(String[] args) {
Main.main(args);
Command.getInstances(new Test());
}

@Override
public boolean doAction(String[] param) {
// TODO Auto-generated method stub
if(param[0].equalsIgnoreCase("put")) {
long l=System.currentTimeMillis();
// ((VoldemortBean)NamedRegistrar.context.get(VoldemortBean.class.getName())).put(param[1], param[2]);
NamedRegistrar.get(VoldemortMap.class).put(param[1], param[2]);
System.out.println("put done in "+(System.currentTimeMillis()-l));
return true;
}else if(param[0].equalsIgnoreCase("get")) {
long l=System.currentTimeMillis();
// System.out.println(((VoldemortBean)NamedRegistrar.context.get(VoldemortBean.class.getName())).get(param[1]));;
System.out.println(NamedRegistrar.get(VoldemortMap.class).get(param[1]));;
System.out.println("get done in "+(System.currentTimeMillis()-l));
return true;
}else if(param[0].equalsIgnoreCase("getall")) {
long l=System.currentTimeMillis();
System.out.println(((VoldemortBean)NamedRegistrar.context.get(VoldemortBean.class.getName())).getKeys());;
System.out.println("getall done in "+(System.currentTimeMillis()-l));
return true;
}else if(param[0].equalsIgnoreCase("delall")) {
long l=System.currentTimeMillis();
System.out.println(((VoldemortBean)NamedRegistrar.context.get(VoldemortBean.class.getName())).cleanIfNotIn(new ArrayList()));;
System.out.println("delall done in "+(System.currentTimeMillis()-l));
return true;
}
return false;
}

@Override
public String help() {
// TODO Auto-generated method stub
return "put [key] [value]\nget [key]";
}
}



Voldemort Database Integration

Voldemort is very useful, and fast key value database. you can download voldemort from here .

Example:

package voldemort.examples;

import voldemort.client.ClientConfig;
import voldemort.client.SocketStoreClientFactory;
import voldemort.client.StoreClient;
import voldemort.client.StoreClientFactory;
import voldemort.versioning.Versioned;

public class ClientExample {

public static void main(String[] args) {

// In real life this stuff would get wired in
String bootstrapUrl = "tcp://localhost:6666";
StoreClientFactory factory = new SocketStoreClientFactory(new ClientConfig().setBootstrapUrls(bootstrapUrl));

StoreClient client = factory.getStoreClient("my_store_name");

// get the value
Versioned version = client.get("some_key");

// modify the value
version.setObject("new_value");

// update the value
client.put("some_key", version);
}

}

really easy isn't it? confuse? let's discuss it here.

Didik TCP Tunnel

This tcp tunnel can forward packet and logged message. download it http://www.didiksoft.com/download/DidikTCPTunnel.rar

edit launch.bat

java -jar dist/DidikTCPTunnel.jar [your local port] [ip destination] [port destination]