Permalink
Browse files

Put db.requestDone() in finally block; other fixes suggested by ypai

  • Loading branch information...
1 parent c4422d7 commit 607a4556fc001fe5fbd7ed7f5c87c87f4bcbb99f @brianfrankcooper brianfrankcooper committed May 27, 2010
Showing with 71 additions and 58 deletions.
  1. +71 −58 db/mongodb/src/com/yahoo/ycsb/db/MongoDbClient.java
@@ -107,17 +107,18 @@ public int delete(String table, String key) {
// see if record was deleted
DBObject errors = db.getLastError();
- db.requestDone();
-
return (Long) errors.get("n") == 1 ? 0 : 1;
} catch (Exception e) {
logger.error(e + "", e);
return 1;
}
-
+ finally
+ {
+ db.requestDone();
+ }
}
- @Override
+@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.
@@ -128,9 +129,12 @@ public int delete(String table, String key) {
* @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, String> values) {
+ com.mongodb.DB db = null;
try {
- com.mongodb.DB db = mongo.getDB(database);
- db.requestStart();
+ db = mongo.getDB(database);
+
+ db.requestStart();
+
DBCollection collection = db.getCollection(table);
DBObject r = new BasicDBObject().append("_id", key);
r.putAll(values);
@@ -139,21 +143,22 @@ public int insert(String table, String key, HashMap<String, String> values) {
collection.insert(r);
- // determine if record was inserted
+ // determine if record was inserted, does not seem to return
+ // n=<records affected> for insert
DBObject errors = db.getLastError();
- db.requestDone();
-
- return (Long) errors.get("n") == 1 ? 0 : 1;
+ return (Boolean) errors.get("ok") && errors.get("err") == null ? 0
+ : 1;
} catch (Exception e) {
logger.error(e + "", e);
return 1;
+ } finally {
+ db.requestDone();
}
}
@Override
- @SuppressWarnings ("unchecked")
/**
* Read a record from the database. Each field/value pair from the result will be stored in a HashMap.
*
@@ -165,9 +170,12 @@ public int insert(String table, String key, HashMap<String, String> values) {
*/
public int read(String table, String key, Set<String> fields,
HashMap<String, String> result) {
+ com.mongodb.DB db = null;
try {
- com.mongodb.DB db = mongo.getDB(database);
- db.requestStart();
+ db = mongo.getDB(database);
+
+ db.requestStart();
+
DBCollection collection = db.getCollection(table);
DBObject q = new BasicDBObject().append("_id", key);
DBObject fieldsToReturn = new BasicDBObject();
@@ -185,56 +193,18 @@ public int read(String table, String key, Set<String> fields,
}
if (queryResult != null) {
- //toMap() returns a Map, but result.putAll() expects a Map<String,String>. Hence, the suppress warnings.
result.putAll(queryResult.toMap());
}
-
- db.requestDone();
-
return queryResult != null ? 0 : 1;
} catch (Exception e) {
logger.error(e + "", e);
return 1;
+ } finally {
+ db.requestDone();
}
}
- @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, String>> result) {
- try {
- com.mongodb.DB 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((HashMap<String, String>) cursor.next().toMap());
- }
-
- db.requestDone();
-
- return 0;
- } catch (Exception e) {
- logger.error(e + "", e);
- return 1;
- }
-
- }
@Override
/**
@@ -247,9 +217,12 @@ public int scan(String table, String startkey, int recordcount,
* @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, String> values) {
+ com.mongodb.DB db = null;
try {
- com.mongodb.DB db = mongo.getDB(database);
- db.requestStart();
+ db = mongo.getDB(database);
+
+ db.requestStart();
+
DBCollection collection = db.getCollection(table);
DBObject q = new BasicDBObject().append("_id", key);
DBObject u = new BasicDBObject();
@@ -271,15 +244,55 @@ public int update(String table, String key, HashMap<String, String> values) {
// determine if record was inserted
DBObject errors = db.getLastError();
- db.requestDone();
-
- return (Long) errors.get("n") == 1 ? 0 : 1;
+ return (Integer) errors.get("n") == 1 ? 0 : 1;
} catch (Exception e) {
logger.error(e + "", e);
return 1;
+ } finally {
+ db.requestDone();
}
}
+
+ @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, String>> result) {
+ try {
+ com.mongodb.DB 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((HashMap<String, String>) cursor.next().toMap());
+ }
+
+ return 0;
+ } catch (Exception e) {
+ logger.error(e + "", e);
+ return 1;
+ }
+ finally
+ {
+ db.requestDone();
+ }
+
+ }
+
}

0 comments on commit 607a455

Please sign in to comment.