diff options
author | samantharitter <samantha.ritter@10gen.com> | 2017-08-01 15:50:37 -0400 |
---|---|---|
committer | samantharitter <samantha.ritter@10gen.com> | 2017-08-02 19:21:34 -0400 |
commit | 68051585b17682c1e13acdf440b2c78868054513 (patch) | |
tree | 1b677a81d5ef5d801a69cd9ce7fa7a9d7aac6295 /src/mongo/db/sessions_collection.cpp | |
parent | 0eea3fc035718c7ae6fc570670f1c51f8f3d71a5 (diff) | |
download | mongo-68051585b17682c1e13acdf440b2c78868054513.tar.gz |
SERVER-29201 Implement SessionsCollectionStandalone
Diffstat (limited to 'src/mongo/db/sessions_collection.cpp')
-rw-r--r-- | src/mongo/db/sessions_collection.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/mongo/db/sessions_collection.cpp b/src/mongo/db/sessions_collection.cpp index 1838d0cfe0e..b5c66195afb 100644 --- a/src/mongo/db/sessions_collection.cpp +++ b/src/mongo/db/sessions_collection.cpp @@ -30,8 +30,125 @@ #include "mongo/db/sessions_collection.h" +#include <memory> + +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/db/logical_session_id.h" +#include "mongo/stdx/functional.h" +#include "mongo/stdx/memory.h" + namespace mongo { +namespace { + +BSONObj lsidQuery(const LogicalSessionId& lsid) { + return BSON(LogicalSessionRecord::kIdFieldName << lsid.toBSON()); +} + +BSONObj lsidQuery(const LogicalSessionRecord& record) { + return lsidQuery(record.getId()); +} + +BSONObj updateQuery(const LogicalSessionRecord& record, Date_t refreshTime) { + // { $max : { lastUse : <time> }, $setOnInsert : { user : <user> } } + + // Build our update doc. + BSONObjBuilder updateBuilder; + + { + BSONObjBuilder maxBuilder(updateBuilder.subobjStart("$max")); + maxBuilder.append(LogicalSessionRecord::kLastUseFieldName, refreshTime); + } + + if (record.getUser()) { + BSONObjBuilder setBuilder(updateBuilder.subobjStart("$setOnInsert")); + setBuilder.append(LogicalSessionRecord::kUserFieldName, record.getUser()->toBSON()); + } + + return updateBuilder.obj(); +} + +template <typename InitBatchFn, typename AddLineFn, typename SendBatchFn, typename Container> +Status runBulkCmd(StringData label, + InitBatchFn&& initBatch, + AddLineFn&& addLine, + SendBatchFn&& sendBatch, + const Container& items) { + int i = 0; + BufBuilder buf; + + boost::optional<BSONObjBuilder> batchBuilder; + boost::optional<BSONArrayBuilder> entries; + + auto setupBatchBuilder = [&] { + buf.reset(); + batchBuilder.emplace(buf); + initBatch(&(batchBuilder.get())); + entries.emplace(batchBuilder->subarrayStart(label)); + }; + + auto sendLocalBatch = [&] { + entries->done(); + return sendBatch(batchBuilder->done()); + }; + + setupBatchBuilder(); + + for (const auto& item : items) { + addLine(&(entries.get()), item); + + if (++i >= 1000) { + auto res = sendLocalBatch(); + if (!res.isOK()) { + return res; + } + + setupBatchBuilder(); + i = 0; + } + } + + return sendLocalBatch(); +} + +} // namespace + + +constexpr StringData SessionsCollection::kSessionsDb; +constexpr StringData SessionsCollection::kSessionsCollection; +constexpr StringData SessionsCollection::kSessionsFullNS; + + SessionsCollection::~SessionsCollection() = default; +Status SessionsCollection::doRefresh(const LogicalSessionRecordSet& sessions, + Date_t refreshTime, + SendBatchFn send) { + auto init = [](BSONObjBuilder* batch) { + batch->append("update", kSessionsCollection); + batch->append("ordered", false); + }; + + auto add = [&refreshTime](BSONArrayBuilder* entries, const LogicalSessionRecord& record) { + entries->append(BSON("q" << lsidQuery(record) << "u" << updateQuery(record, refreshTime) + << "upsert" + << true)); + }; + + return runBulkCmd("updates", init, add, send, sessions); +} + +Status SessionsCollection::doRemove(const LogicalSessionIdSet& sessions, SendBatchFn send) { + auto init = [](BSONObjBuilder* batch) { + batch->append("delete", kSessionsCollection); + batch->append("ordered", false); + }; + + auto add = [](BSONArrayBuilder* builder, const LogicalSessionId& lsid) { + builder->append(BSON("q" << lsidQuery(lsid) << "limit" << 0)); + }; + + return runBulkCmd("deletes", init, add, send, sessions); +} + } // namespace mongo |