From a75f602db17e890a5659b9321bdb4b95a94223f0 Mon Sep 17 00:00:00 2001
From: Jaemyoun <jaemyoun@gmail.com>
Date: Fri, 11 Mar 2016 17:41:44 +0900
Subject: [PATCH] add files

---
 rados/pom.xml                                 |  28 ++++
 .../java/com/yahoo/ycsb/db/RadosClient.java   | 136 ++++++++++++++++++
 .../java/com/yahoo/ycsb/db/package-info.java  |   1 +
 3 files changed, 165 insertions(+)
 create mode 100644 rados/pom.xml
 create mode 100644 rados/src/main/java/com/yahoo/ycsb/db/RadosClient.java
 create mode 100644 rados/src/main/java/com/yahoo/ycsb/db/package-info.java

diff --git a/rados/pom.xml b/rados/pom.xml
new file mode 100644
index 00000000..f536bb27
--- /dev/null
+++ b/rados/pom.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>com.yahoo.ycsb</groupId>
+    <artifactId>binding-parent</artifactId>
+    <version>0.8.0-SNAPSHOT</version>
+    <relativePath>../binding-parent</relativePath>
+  </parent>
+  
+  <artifactId>rados-binding</artifactId>
+  <name>rados of Ceph FS binding</name>
+  <packaging>jar</packaging>
+  
+  <dependencies>
+    <dependency>
+      <groupId>com.ceph</groupId>
+      <artifactId>rados</artifactId>
+      <version>${rados.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.yahoo.ycsb</groupId>
+      <artifactId>core</artifactId>
+      <version>${project.version}</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/rados/src/main/java/com/yahoo/ycsb/db/RadosClient.java b/rados/src/main/java/com/yahoo/ycsb/db/RadosClient.java
new file mode 100644
index 00000000..6fd2b885
--- /dev/null
+++ b/rados/src/main/java/com/yahoo/ycsb/db/RadosClient.java
@@ -0,0 +1,136 @@
+package com.yahoo.ycsb.db;
+
+import com.yahoo.ycsb.ByteIterator;
+import com.yahoo.ycsb.DB;
+import com.yahoo.ycsb.DBException;
+import com.yahoo.ycsb.Status;
+import com.yahoo.ycsb.StringByteIterator;
+
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.Protocol;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.Vector;
+
+/**
+ * YCSB binding for <a href="http://redis.io/">Redis</a>.
+ *
+ * See {@code redis/README.md} for details.
+ */
+public class RadosClient extends DB {
+
+  private static String ENV_CONFIG_FILE = System.getenv("RADOS_JAVA_CONFIG_FILE");
+  private static String ENV_ID = System.getenv("RADOS_JAVA_ID");
+  private static String ENV_POOL = System.getenv("RADOS_JAVA_POOL");
+
+  private static final String CONFIG_FILE = ENV_CONFIG_FILE == null ? "/etc/ceph/ceph.conf" : ENV_CONFIG_FILE;
+  private static final String ID = ENV_ID == null ? "admin" : ENV_ID;
+  private static final String POOL = ENV_POOL == null ? "data" : ENV_POOL;
+
+  private static Rados rados;
+  private static IoCTX ioctx;
+
+  public void init() throws DBException {
+    Properties props = getProperties();
+    int port;
+
+    String portString = props.getProperty(PORT_PROPERTY);
+    if (portString != null) {
+      port = Integer.parseInt(portString);
+    } else {
+      port = Protocol.DEFAULT_PORT;
+    }
+    String host = props.getProperty(HOST_PROPERTY);
+
+    jedis = new Jedis(host, port);
+    jedis.connect();
+
+    String password = props.getProperty(PASSWORD_PROPERTY);
+    if (password != null) {
+      jedis.auth(password);
+    }
+  }
+
+  public void cleanup() throws DBException {
+    jedis.disconnect();
+  }
+
+  /*
+   * Calculate a hash for a key to store it in an index. The actual return value
+   * of this function is not interesting -- it primarily needs to be fast and
+   * scattered along the whole space of doubles. In a real world scenario one
+   * would probably use the ASCII values of the keys.
+   */
+  private double hash(String key) {
+    return key.hashCode();
+  }
+
+  // XXX jedis.select(int index) to switch to `table`
+
+  @Override
+  public Status read(String table, String key, Set<String> fields,
+      HashMap<String, ByteIterator> result) {
+    if (fields == null) {
+      StringByteIterator.putAllAsByteIterators(result, jedis.hgetAll(key));
+    } else {
+      String[] fieldArray =
+          (String[]) fields.toArray(new String[fields.size()]);
+      List<String> values = jedis.hmget(key, fieldArray);
+
+      Iterator<String> fieldIterator = fields.iterator();
+      Iterator<String> valueIterator = values.iterator();
+
+      while (fieldIterator.hasNext() && valueIterator.hasNext()) {
+        result.put(fieldIterator.next(),
+            new StringByteIterator(valueIterator.next()));
+      }
+      assert !fieldIterator.hasNext() && !valueIterator.hasNext();
+    }
+    return result.isEmpty() ? Status.ERROR : Status.OK;
+  }
+
+  @Override
+  public Status insert(String table, String key,
+      HashMap<String, ByteIterator> values) {
+    if (jedis.hmset(key, StringByteIterator.getStringMap(values))
+        .equals("OK")) {
+      jedis.zadd(INDEX_KEY, hash(key), key);
+      return Status.OK;
+    }
+    return Status.ERROR;
+  }
+
+  @Override
+  public Status delete(String table, String key) {
+    return jedis.del(key) == 0 && jedis.zrem(INDEX_KEY, key) == 0 ? Status.ERROR
+        : Status.OK;
+  }
+
+  @Override
+  public Status update(String table, String key,
+      HashMap<String, ByteIterator> values) {
+    return jedis.hmset(key, StringByteIterator.getStringMap(values))
+        .equals("OK") ? Status.OK : Status.ERROR;
+  }
+
+  @Override
+  public Status scan(String table, String startkey, int recordcount,
+      Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
+    Set<String> keys = jedis.zrangeByScore(INDEX_KEY, hash(startkey),
+        Double.POSITIVE_INFINITY, 0, recordcount);
+
+    HashMap<String, ByteIterator> values;
+    for (String key : keys) {
+      values = new HashMap<String, ByteIterator>();
+      read(table, key, fields, values);
+      result.add(values);
+    }
+
+    return Status.OK;
+  }
+
+}
diff --git a/rados/src/main/java/com/yahoo/ycsb/db/package-info.java b/rados/src/main/java/com/yahoo/ycsb/db/package-info.java
new file mode 100644
index 00000000..cacae13b
--- /dev/null
+++ b/rados/src/main/java/com/yahoo/ycsb/db/package-info.java
@@ -0,0 +1 @@
+package com.yahoo.ycsb.db;
-- 
GitLab