Permalink
Browse files

partial support for temporal queries

enables the following queries

* only current (default)
* transaction: {all: true}
* transaction: {at: Timestamp(123, 1)}
  • Loading branch information...
1 parent c353ba8 commit 026b510c8b2920d00527b775c45ce155163586b4 @stefankoegl committed Nov 7, 2012
Showing with 62 additions and 1 deletion.
  1. +9 −1 src/mongo/db/ops/query.cpp
  2. +51 −0 src/mongo/db/ttime.cpp
  3. +2 −0 src/mongo/db/ttime.h
View
@@ -30,6 +30,7 @@
#include "../../server.h"
#include "../queryoptimizercursor.h"
#include "../pagefault.h"
+#include "../ttime.h"
namespace mongo {
@@ -992,6 +993,14 @@ namespace mongo {
query = query.getOwned();
order = order.getOwned();
+ Client::ReadContext ctx( ns , dbpath ); // read locks
+ NamespaceDetails *d = nsdetails( ns );
+
+ if( d && d->hasTransactionTime() )
+ {
+ query = addTemporalCriteria(query);
+ }
+
bool hasRetried = false;
scoped_ptr<PageFaultRetryableSection> pgfs;
scoped_ptr<NoPageFaultsAllowed> npfe;
@@ -1003,7 +1012,6 @@ namespace mongo {
}
try {
- Client::ReadContext ctx( ns , dbpath ); // read locks
const ConfigVersion shardingVersionAtStart = shardingState.getVersion( ns );
replVerifyReadsOk(&pq);
View
@@ -68,5 +68,56 @@ namespace mongo {
bb.appendElementsUnique(newObj);
return wrapObjectId(bb.obj(), endTimestampTime, endTimestampInc);
}
+
+ BSONObj addTemporalCriteria(BSONObj query)
+ {
+ if( !query.hasElement("transaction") )
+ {
+ return addCurrentVersionCriterion(query);
+ }
+
+ BSONElement allElem = query.getFieldDotted("transaction.all");
+ if( !allElem.eoo() )
+ {
+ /* TODO: assert allElem.trueValue() */
+ return query;
+ }
+
+ BSONElement atElem = query.getFieldDotted("transaction.at");
+ cout << atElem.type() << endl;
+ if (!atElem.eoo())
+ {
+ BSONObjBuilder bb;
+
+ // the transaction started before the at-timestamp
+ BSONObjBuilder startT(bb.subobjStart(StringData("_id.transaction_start")));
+ startT.appendAs(atElem, "$lte");
+ startT.done();
+
+ // and either is still current
+ BSONArrayBuilder arr( bb.subarrayStart( "$or" ) );
+
+ BSONObjBuilder upperNull(arr.subobjStart());
+ upperNull.appendNull(StringData("_id.transaction_end"));
+ upperNull.done();
+
+ // ... or ended after the at-timestamp
+ BSONObjBuilder upper(arr.subobjStart());
+ BSONObjBuilder upperVal(upper.subobjStart(StringData("_id.transaction_end")));
+ upperVal.appendAs(atElem, "$gte");
+ upperVal.done();
+ upper.done();
+
+ arr.done();
+ bb.done();
+
+ // all other conditions are inserted afterwards
+ query = query.removeField("transaction");
+ bb.appendElementsUnique(query);
+ query = bb.obj();
+ }
+
+ return query;
+ }
}
View
@@ -21,6 +21,8 @@ namespace mongo {
BSONObj setTransactionEndTimestamp(BSONObj obj);
BSONObj setTransactionStartTimestamp(BSONObj newObj, BSONObj prevObj);
+
+ BSONObj addTemporalCriteria(BSONObj query);
}
#endif /* TTIME_H_ */

0 comments on commit 026b510

Please sign in to comment.