Permalink
Browse files

Voldemort DB!

  • Loading branch information...
1 parent a87ab25 commit 3f8adb07104a774d8623786cec993af3180f715c @rsumbaly rsumbaly committed Sep 26, 2010
View
@@ -40,7 +40,12 @@
<antcall target="dbcompile"/>
</target>
- <target name="compile">
+ <target name="dbcompile-voldemort" depends="compile">
+ <property name="db.dir" value="db/voldemort"/>
+ <antcall target="dbcompile"/>
+ </target>
+
+ <target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="build.classpath" deprecation="on">
<compilerarg value="-Xlint:unchecked"/>
@@ -0,0 +1,11 @@
+<cluster>
+ <name>mycluster</name>
+ <server>
+ <id>0</id>
+ <host>localhost</host>
+ <http-port>8081</http-port>
+ <socket-port>6666</socket-port>
+ <partitions>0, 1</partitions>
+ </server>
+</cluster>
+
@@ -0,0 +1,26 @@
+# The ID of *this* particular cluster node
+node.id=0
+
+max.threads=100
+
+############### DB options ######################
+
+http.enable=true
+socket.enable=true
+
+# BDB
+bdb.write.transactions=false
+bdb.flush.transactions=false
+bdb.cache.size=1G
+
+# Mysql
+mysql.host=localhost
+mysql.port=1521
+mysql.user=root
+mysql.password=3306
+mysql.database=test
+
+#NIO connector settings.
+enable.nio.connector=false
+
+storage.configs=voldemort.store.bdb.BdbStorageConfiguration, voldemort.store.readonly.ReadOnlyStorageConfiguration
@@ -0,0 +1,16 @@
+<stores>
+ <store>
+ <name>usertable</name>
+ <persistence>bdb</persistence>
+ <routing>client</routing>
+ <replication-factor>1</replication-factor>
+ <required-reads>1</required-reads>
+ <required-writes>1</required-writes>
+ <key-serializer>
+ <type>string</type>
+ </key-serializer>
+ <value-serializer>
+ <type>java-serialization</type>
+ </value-serializer>
+ </store>
+</stores>
@@ -0,0 +1,148 @@
+package com.yahoo.ycsb.db;
+
+import java.util.HashMap;
+import java.util.Set;
+import java.util.Vector;
+import java.util.Map.Entry;
+
+import org.apache.log4j.Logger;
+
+import voldemort.client.ClientConfig;
+import voldemort.client.SocketStoreClientFactory;
+import voldemort.client.StoreClient;
+import voldemort.versioning.VectorClock;
+import voldemort.versioning.Versioned;
+
+import com.yahoo.ycsb.DB;
+import com.yahoo.ycsb.DBException;
+
+public class VoldemortClient extends DB {
+
+ private StoreClient<String, HashMap<String, String>> storeClient;
+ private SocketStoreClientFactory socketFactory;
+ private String storeName;
+ private final Logger logger = Logger.getLogger(VoldemortClient.class);
+
+ public static final int OK = 0;
+ public static final int ERROR = -1;
+
+ /**
+ * Initialize the DB layer. This accepts all properties allowed by the Voldemort client.
+ * A store maps to a table.
+ * Required : bootstrap_urls
+ * Additional property : store_name -> to preload once, should be same as -t <table>
+ *
+ * {@linktourl http://project-voldemort.com/javadoc/client/voldemort/client/ClientConfig.html}
+ */
+ public void init() throws DBException {
+ ClientConfig clientConfig = new ClientConfig(getProperties());
+ socketFactory = new SocketStoreClientFactory(clientConfig);
+
+ // Retrieve store name
+ storeName = getProperties().getProperty("store_name", "usertable");
+
+ // Use store name to retrieve client
+ storeClient = socketFactory.getStoreClient(storeName);
+ if ( storeClient == null )
+ throw new DBException("Unable to instantiate store client");
+
+ }
+
+ public void cleanup() throws DBException {
+ socketFactory.close();
+ }
+
+ @Override
+ public int delete(String table, String key) {
+ if ( checkStore(table) == ERROR ) {
+ return ERROR;
+ }
+
+ if ( storeClient.delete(key) )
+ return OK;
+ else
+ return ERROR;
+ }
+
+ @Override
+ public int insert(String table, String key, HashMap<String, String> values) {
+ if ( checkStore(table) == ERROR ) {
+ return ERROR;
+ }
+ storeClient.put(key, values);
+ return OK;
+ }
+
+ @Override
+ public int read(String table, String key, Set<String> fields,
+ HashMap<String, String> result) {
+ if ( checkStore(table) == ERROR ) {
+ return ERROR;
+ }
+
+ Versioned<HashMap<String, String>> versionedValue = storeClient.get(key);
+
+ if (versionedValue == null)
+ return OK;
+
+
+ if ( fields != null ) {
+ for (String field : fields) {
+ String val = versionedValue.getValue().get(field);
+ if ( val != null)
+ result.put(field, val);
+ }
+ } else {
+ result.putAll( versionedValue.getValue());
+ }
+ return OK;
+ }
+
+ @Override
+ public int scan(String table, String startkey, int recordcount,
+ Set<String> fields, Vector<HashMap<String, String>> result) {
+ logger.warn("Voldemort does not support Scan semantics");
+ return OK;
+ }
+
+ @Override
+ public int update(String table, String key, HashMap<String, String> values) {
+ if ( checkStore(table) == ERROR ) {
+ return ERROR;
+ }
+
+ Versioned<HashMap<String, String>> versionedValue = storeClient.get(key);
+ HashMap<String, String> value = new HashMap<String, String>();
+ VectorClock version;
+ if ( versionedValue != null) {
+ version = ((VectorClock) versionedValue.getVersion()).incremented(0, 1);
+ value = versionedValue.getValue();
+ for ( Entry<String, String> entry : values.entrySet()) {
+ value.put(entry.getKey(), entry.getValue());
+ }
+ } else {
+ version = new VectorClock();
+ value.putAll(values);
+ }
+
+ storeClient.put(key, Versioned.value(value, version));
+ return OK;
+ }
+
+ private int checkStore(String table) {
+ if ( table.compareTo(storeName) != 0) {
+ try {
+ storeClient = socketFactory.getStoreClient(table);
+ if ( storeClient == null ) {
+ logger.error("Could not instantiate storeclient for " + table);
+ return ERROR;
+ }
+ storeName = table;
+ } catch ( Exception e ) {
+ return ERROR;
+ }
+ }
+ return OK;
+ }
+
+}

0 comments on commit 3f8adb0

Please sign in to comment.