本文整理汇总了Java中com.yahoo.ycsb.DB类的典型用法代码示例。如果您正苦于以下问题:Java DB类的具体用法?Java DB怎么用?Java DB使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DB类属于com.yahoo.ycsb包,在下文中一共展示了DB类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doTransactionRead
import com.yahoo.ycsb.DB; //导入依赖的package包/类
public void doTransactionRead(DB db)
{
//choose a random key
int keynum = nextKeynum();
String keyname = buildKeyName(keynum);
HashSet<String> fields=null;
if (!readallfields)
{
//read a random field
String fieldname="field"+fieldchooser.nextString();
fields=new HashSet<String>();
fields.add(fieldname);
}
db.read(table,keyname,fields,new HashMap<String,ByteIterator>());
}
示例2: doTransactionScan
import com.yahoo.ycsb.DB; //导入依赖的package包/类
public void doTransactionScan(DB db)
{
//choose a random key
int keynum = nextKeynum();
String startkeyname = buildKeyName(keynum);
//choose a random scan length
int len=scanlength.nextInt();
HashSet<String> fields=null;
if (!readallfields)
{
//read a random field
String fieldname="field"+fieldchooser.nextString();
fields=new HashSet<String>();
fields.add(fieldname);
}
db.scan(table,startkeyname,len,fields,new Vector<HashMap<String,ByteIterator>>());
}
示例3: doTransactionUpdate
import com.yahoo.ycsb.DB; //导入依赖的package包/类
public void doTransactionUpdate(DB db)
{
//choose a random key
int keynum = nextKeynum();
String keyname=buildKeyName(keynum);
HashMap<String,ByteIterator> values;
if (writeallfields)
{
//new data for all the fields
values = buildValues();
}
else
{
//update a random field
values = buildUpdate();
}
db.update(table,keyname,values);
}
示例4: delete
import com.yahoo.ycsb.DB; //导入依赖的package包/类
@Override
/**
* Delete a record from the database.
*
* @param table The name of the table
* @param key The record key of the record to delete.
* @return Zero on success, a non-zero error code on error. See this class's description for a discussion of error codes.
*/
public int delete(String table, String key) {
com.mongodb.DB db=null;
try {
db = mongo.getDB(database);
db.requestStart();
DBCollection collection = db.getCollection(table);
DBObject q = new BasicDBObject().append("_id", key);
WriteResult res = collection.remove(q, writeConcern);
return res.getN() == 1 ? 0 : 1;
} catch (Exception e) {
System.err.println(e.toString());
return 1;
}
finally
{
if (db!=null)
{
db.requestDone();
}
}
}
示例5: insert
import com.yahoo.ycsb.DB; //导入依赖的package包/类
@Override
/**
* Insert a record in the database. Any field/value pairs in the specified values HashMap will be written into the record with the specified
* record key.
*
* @param table The name of the table
* @param key The record key of the record to insert.
* @param values A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error. See this class's description for a discussion of error codes.
*/
public int insert(String table, String key, HashMap<String, ByteIterator> values) {
com.mongodb.DB db = null;
try {
db = mongo.getDB(database);
db.requestStart();
DBCollection collection = db.getCollection(table);
DBObject r = new BasicDBObject().append("_id", key);
for(String k: values.keySet()) {
r.put(k, values.get(k).toArray());
}
WriteResult res = collection.insert(r,writeConcern);
return res.getError() == null ? 0 : 1;
} catch (Exception e) {
System.err.println(e.toString());
return 1;
} finally {
if (db!=null)
{
db.requestDone();
}
}
}
示例6: doInsert
import com.yahoo.ycsb.DB; //导入依赖的package包/类
/**
* Do one insert operation. Because it will be called concurrently from multiple client threads, this
* function must be thread safe. However, avoid synchronized, or the threads will block waiting for each
* other, and it will be difficult to reach the target throughput. Ideally, this function would have no side
* effects other than DB operations.
*/
public boolean doInsert(DB db, Object threadstate)
{
int keynum=keysequence.nextInt();
String dbkey = buildKeyName(keynum);
HashMap<String, ByteIterator> values = buildValues();
if (db.insert(table,dbkey,values) == 0)
return true;
else
return false;
}
示例7: doTransaction
import com.yahoo.ycsb.DB; //导入依赖的package包/类
/**
* Do one transaction operation. Because it will be called concurrently from multiple client threads, this
* function must be thread safe. However, avoid synchronized, or the threads will block waiting for each
* other, and it will be difficult to reach the target throughput. Ideally, this function would have no side
* effects other than DB operations.
*/
public boolean doTransaction(DB db, Object threadstate)
{
String op=operationchooser.nextString();
if (op.compareTo("READ")==0)
{
doTransactionRead(db);
}
else if (op.compareTo("UPDATE")==0)
{
doTransactionUpdate(db);
}
else if (op.compareTo("INSERT")==0)
{
doTransactionInsert(db);
}
else if (op.compareTo("SCAN")==0)
{
doTransactionScan(db);
}
else
{
doTransactionReadModifyWrite(db);
}
return true;
}
示例8: doTransactionInsert
import com.yahoo.ycsb.DB; //导入依赖的package包/类
public void doTransactionInsert(DB db)
{
//choose the next key
int keynum=transactioninsertkeysequence.nextInt();
String dbkey = buildKeyName(keynum);
HashMap<String, ByteIterator> values = buildValues();
db.insert(table,dbkey,values);
}
示例9: insert
import com.yahoo.ycsb.DB; //导入依赖的package包/类
@Override
/**
* Insert a record in the database. Any field/value pairs in the specified values HashMap will be written into the record with the specified
* record key.
*
* @param table The name of the table
* @param key The record key of the record to insert.
* @param values A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error. See this class's description for a discussion of error codes.
*/
public int insert(String table, String key, HashMap<String, ByteIterator> values) {
com.mongodb.DB db = null;
try {
db = mongo.getDB(database);
db.requestStart();
DBCollection collection = db.getCollection(table);
DBObject r = new BasicDBObject().append("_id", key);
for(String k: values.keySet()) {
r.put(k, values.get(k).toArray());
}
WriteResult res = collection.insert(r,writeConcern);
return res.getError() == null ? 0 : 1;
} catch (Exception e) {
e.printStackTrace();
return 1;
} finally {
if (db!=null)
{
db.requestDone();
}
}
}
示例10: read
import com.yahoo.ycsb.DB; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
/**
* Read a record from the database. Each field/value pair from the result will be stored in a HashMap.
*
* @param table The name of the table
* @param key The record key of the record to read.
* @param fields The list of fields to read, or null for all of them
* @param result A HashMap of field/value pairs for the result
* @return Zero on success, a non-zero error code on error or "not found".
*/
public int read(String table, String key, Set<String> fields,
HashMap<String, ByteIterator> result) {
com.mongodb.DB db = null;
try {
db = mongo.getDB(database);
db.requestStart();
DBCollection collection = db.getCollection(table);
DBObject q = new BasicDBObject().append("_id", key);
DBObject fieldsToReturn = new BasicDBObject();
boolean returnAllFields = fields == null;
DBObject queryResult = null;
if (!returnAllFields) {
Iterator<String> iter = fields.iterator();
while (iter.hasNext()) {
fieldsToReturn.put(iter.next(), 1);
}
queryResult = collection.findOne(q, fieldsToReturn);
} else {
queryResult = collection.findOne(q);
}
if (queryResult != null) {
result.putAll(queryResult.toMap());
}
return queryResult != null ? 0 : 1;
} catch (Exception e) {
System.err.println(e.toString());
return 1;
} finally {
if (db!=null)
{
db.requestDone();
}
}
}
示例11: update
import com.yahoo.ycsb.DB; //导入依赖的package包/类
@Override
/**
* Update a record in the database. Any field/value pairs in the specified values HashMap will be written into the record with the specified
* record key, overwriting any existing values with the same field name.
*
* @param table The name of the table
* @param key The record key of the record to write.
* @param values A HashMap of field/value pairs to update in the record
* @return Zero on success, a non-zero error code on error. See this class's description for a discussion of error codes.
*/
public int update(String table, String key, HashMap<String, ByteIterator> values) {
com.mongodb.DB db = null;
try {
db = mongo.getDB(database);
db.requestStart();
DBCollection collection = db.getCollection(table);
DBObject q = new BasicDBObject().append("_id", key);
DBObject u = new BasicDBObject();
DBObject fieldsToSet = new BasicDBObject();
Iterator<String> keys = values.keySet().iterator();
while (keys.hasNext()) {
String tmpKey = keys.next();
fieldsToSet.put(tmpKey, values.get(tmpKey).toArray());
}
u.put("$set", fieldsToSet);
WriteResult res = collection.update(q, u, false, false,
writeConcern);
return res.getN() == 1 ? 0 : 1;
} catch (Exception e) {
System.err.println(e.toString());
return 1;
} finally {
if (db!=null)
{
db.requestDone();
}
}
}
示例12: scan
import com.yahoo.ycsb.DB; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
/**
* Perform a range scan for a set of records in the database. Each field/value pair from the result will be stored in a HashMap.
*
* @param table The name of the table
* @param startkey The record key of the first record to read.
* @param recordcount The number of records to read
* @param fields The list of fields to read, or null for all of them
* @param result A Vector of HashMaps, where each HashMap is a set field/value pairs for one record
* @return Zero on success, a non-zero error code on error. See this class's description for a discussion of error codes.
*/
public int scan(String table, String startkey, int recordcount,
Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
com.mongodb.DB db=null;
try {
db = mongo.getDB(database);
db.requestStart();
DBCollection collection = db.getCollection(table);
// { "_id":{"$gte":startKey, "$lte":{"appId":key+"\uFFFF"}} }
DBObject scanRange = new BasicDBObject().append("$gte", startkey);
DBObject q = new BasicDBObject().append("_id", scanRange);
DBCursor cursor = collection.find(q).limit(recordcount);
while (cursor.hasNext()) {
//toMap() returns a Map, but result.add() expects a Map<String,String>. Hence, the suppress warnings.
result.add(StringByteIterator.getByteIteratorMap((Map<String,String>)cursor.next().toMap()));
}
return 0;
} catch (Exception e) {
System.err.println(e.toString());
return 1;
}
finally
{
if (db!=null)
{
db.requestDone();
}
}
}
示例13: doTransactionReadModifyWrite
import com.yahoo.ycsb.DB; //导入依赖的package包/类
public void doTransactionReadModifyWrite(DB db)
{
//choose a random key
int keynum = nextKeynum();
String keyname = buildKeyName(keynum);
HashSet<String> fields=null;
if (!readallfields)
{
//read a random field
String fieldname="field"+fieldchooser.nextString();
fields=new HashSet<String>();
fields.add(fieldname);
}
HashMap<String,ByteIterator> values;
if (writeallfields)
{
//new data for all the fields
values = buildValues();
}
else
{
//update a random field
values = buildUpdate();
}
//do the transaction
long st=System.currentTimeMillis();
db.read(table,keyname,fields,new HashMap<String,ByteIterator>());
db.update(table,keyname,values);
long en=System.currentTimeMillis();
Measurements.getMeasurements().measure("READ-MODIFY-WRITE", (int)(en-st));
}