summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Rassi <rassi@10gen.com>2015-03-20 16:51:37 -0400
committerJason Rassi <rassi@10gen.com>2015-03-23 11:37:21 -0400
commit1b7ee0b6d045c5f99036507915f29684cee59b29 (patch)
tree73b5f03af2623671e81611e2b0496f2c61c03d7d
parentff5b0db8db277d7e54c64619c12093f7d0e17125 (diff)
downloadmongo-1b7ee0b6d045c5f99036507915f29684cee59b29.tar.gz
SERVER-17672 Helpers::getSingleton()/getLast() return owned object
(cherry picked from commit 8f931d4443e07e3bfdfab97377b98bb1fdd53548)
-rw-r--r--src/mongo/db/dbhelpers.cpp23
-rw-r--r--src/mongo/db/dbhelpers.h28
2 files changed, 31 insertions, 20 deletions
diff --git a/src/mongo/db/dbhelpers.cpp b/src/mongo/db/dbhelpers.cpp
index 32fa31f3725..16d1b8fe809 100644
--- a/src/mongo/db/dbhelpers.cpp
+++ b/src/mongo/db/dbhelpers.cpp
@@ -205,19 +205,18 @@ namespace mongo {
return accessMethod->findSingle( txn, idquery["_id"].wrap() );
}
- /* Get the first object from a collection. Generally only useful if the collection
- only ever has a single object -- which is a "singleton collection". Note that the
- BSONObj returned is *not* owned and will become invalid if the database is closed.
-
- Returns: true if object exists.
- */
bool Helpers::getSingleton(OperationContext* txn, const char *ns, BSONObj& result) {
AutoGetCollectionForRead ctx(txn, ns);
auto_ptr<PlanExecutor> exec(InternalPlanner::collectionScan(txn, ns, ctx.getCollection()));
-
PlanExecutor::ExecState state = exec->getNext(&result, NULL);
+
txn->getCurOp()->done();
- return PlanExecutor::ADVANCED == state;
+
+ if (PlanExecutor::ADVANCED == state) {
+ result = result.getOwned();
+ return true;
+ }
+ return false;
}
bool Helpers::getLast(OperationContext* txn, const char *ns, BSONObj& result) {
@@ -226,9 +225,13 @@ namespace mongo {
ns,
autoColl.getCollection(),
InternalPlanner::BACKWARD));
-
PlanExecutor::ExecState state = exec->getNext(&result, NULL);
- return PlanExecutor::ADVANCED == state;
+
+ if (PlanExecutor::ADVANCED == state) {
+ result = result.getOwned();
+ return true;
+ }
+ return false;
}
void Helpers::upsert( OperationContext* txn,
diff --git a/src/mongo/db/dbhelpers.h b/src/mongo/db/dbhelpers.h
index f9ead76bc20..29236a0ef75 100644
--- a/src/mongo/db/dbhelpers.h
+++ b/src/mongo/db/dbhelpers.h
@@ -107,23 +107,31 @@ namespace mongo {
static RecordId findById(OperationContext* txn,
Collection* collection, const BSONObj& query);
- /** Get/put the first (or last) object from a collection. Generally only useful if the collection
- only ever has a single object -- which is a "singleton collection".
-
- You do not need to set the database (Context) before calling.
-
- @return true if object exists.
- */
+ /**
+ * Get the first object generated from a forward natural-order scan on "ns". Callers do not
+ * have to lock "ns".
+ *
+ * Returns true if there is such an object. An owned copy of the object is placed into the
+ * out-argument "result".
+ *
+ * Returns false if there is no such object.
+ */
static bool getSingleton(OperationContext* txn, const char *ns, BSONObj& result);
- static void putSingleton(OperationContext* txn, const char *ns, BSONObj obj);
- static void putSingletonGod(OperationContext* txn, const char *ns, BSONObj obj, bool logTheOp);
/**
- * get last object int he collection; e.g. {$natural : -1}
+ * Same as getSingleton, but with a reverse natural-order scan on "ns".
*/
static bool getLast(OperationContext* txn, const char *ns, BSONObj& result);
/**
+ * Performs an upsert of "obj" into the collection "ns", with an empty update predicate.
+ * Callers must have "ns" locked.
+ */
+ static void putSingleton(OperationContext* txn, const char *ns, BSONObj obj);
+
+ static void putSingletonGod(OperationContext* txn, const char *ns, BSONObj obj, bool logTheOp);
+
+ /**
* you have to lock
* you do not have to have Context set
* o has to have an _id field or will assert