This commit is contained in:
Jaemyoun 2016-03-11 17:41:44 +09:00
Родитель 4a4e15b3bf
Коммит a75f602db1
3 изменённых файлов: 165 добавлений и 0 удалений

28
rados/pom.xml Normal file
Просмотреть файл

@ -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>

Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -0,0 +1 @@
package com.yahoo.ycsb.db;