Skip to content

Commit

Permalink
Part 4: fixes brianfrankcooper#2 - Update MongoDB client
Browse files Browse the repository at this point in the history
  • Loading branch information
lyubent committed Jul 14, 2014
1 parent c5e6fec commit 54af8b1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 34 deletions.
122 changes: 89 additions & 33 deletions mongodb/src/main/java/com/yahoo/ycsb/db/MongoDbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,40 @@ public int insert(String table, String key, Map<String, ByteIterator> values) {
@Override
@SuppressWarnings("unchecked")
/**
* Read a record from the database. Each field/value pair from the result will be stored in a HashMap.
* Read a record from the database. Each field/value pair from the result will be stored in a Map.
*
* @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
* @param field The field to read
* @param result A Map 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,
Map<String, ByteIterator> result) {
public int readOne(String table, String key, String field, Map<String,ByteIterator> result) {

DBObject fieldsToReturn = new BasicDBObject();
fieldsToReturn.put(field, 1);

return read(table, key, result, fieldsToReturn);
}

@Override
@SuppressWarnings("unchecked")
/**
* Read a record from the database. Each field/value pair from the result will be stored in a Map.
*
* @param table The name of the table
* @param key The record key of the record to read.
* @param result A Map of field/value pairs for the result
* @return Zero on success, a non-zero error code on error or "not found".
*/
public int readAll(String table, String key, Map<String,ByteIterator> result) {

return read(table, key, result, null);
}


public int read(String table, String key, Map<String, ByteIterator> result,
DBObject fieldsToReturn) {
com.mongodb.DB db = null;
try {
db = mongo.getDB(database);
Expand All @@ -277,19 +301,7 @@ public int read(String table, String key, Set<String> fields,

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, readPreference);
} else {
queryResult = collection.findOne(q, null, readPreference);
}
DBObject queryResult = collection.findOne(q, fieldsToReturn, readPreference);

if (queryResult != null) {
result.putAll(resultify(queryResult));
Expand All @@ -309,15 +321,44 @@ public int read(String table, String key, Set<String> fields,

@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
* Update a record in the database. Any field/value pairs in the specified values Map 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.
* @param value The value to update in the key 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, Map<String, ByteIterator> values) {
public int updateOne(String table, String key, String field, ByteIterator value) {

DBObject fieldsToSet = new BasicDBObject();
fieldsToSet.put(key, value.toArray());

return update(table, key, fieldsToSet);
}

/**
* Update a record in the database. Any field/value pairs in the specified values Map 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 Map 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 updateAll(String table, String key, Map<String,ByteIterator> values) {

DBObject fieldsToSet = new BasicDBObject();
Iterator<String> keys = values.keySet().iterator();
while (keys.hasNext()) {
String tmpKey = keys.next();
fieldsToSet.put(tmpKey, values.get(tmpKey).toArray());
}

return update(table, key, fieldsToSet);
}

public int update(String table, String key, DBObject fieldsToSet) {
com.mongodb.DB db = null;
try {
db = mongo.getDB(database);
Expand All @@ -327,13 +368,7 @@ public int update(String table, String key, Map<String, ByteIterator> values) {
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);
Expand All @@ -356,17 +391,38 @@ public int update(String table, String key, Map<String, ByteIterator> values) {
@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.
* Perform a range scan for a set of records in the database. Each field/value pair from the result will be stored in a Map.
*
* @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.
* @param result A List of Maps, where each Map 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 scanAll(String table, String startkey, int recordcount, List<Map<String, ByteIterator>> result) {

return scan(table, startkey, recordcount, result);
}

@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 Map.
*
* @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 field The field to read
* @param result A List of Maps, where each Map 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 scanOne(String table, String startkey, int recordcount, String field, List<Map<String, ByteIterator>> result) {

return scan(table, startkey, recordcount, result);
}

public int scan(String table, String startkey, int recordcount,
Set<String> fields, List<Map<String, ByteIterator>> result) {
List<Map<String, ByteIterator>> result) {
com.mongodb.DB db=null;
try {
db = mongo.getDB(database);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<!--<module>dynamodb</module>-->
<!--<module>jdbc</module>-->
<!--<module>memcached</module>-->
<!--<module>mongodb</module>-->
<module>mongodb</module>
<module>distribution</module>
</modules>

Expand Down

0 comments on commit 54af8b1

Please sign in to comment.