diff options
author | Kyle Suarez <kyle.suarez@mongodb.com> | 2016-06-24 15:36:49 -0400 |
---|---|---|
committer | Kyle Suarez <kyle.suarez@mongodb.com> | 2016-07-07 10:35:40 -0400 |
commit | e39c46b27f4ae58f96b685dd625b0165761fee0d (patch) | |
tree | 147d97c8a4989f50e4fe40c0a82b765b4a14aa76 /src/mongo/db/db_raii.h | |
parent | 21e0ff71b1693f42e1b44885a57de3b7b3cdacba (diff) | |
download | mongo-e39c46b27f4ae58f96b685dd625b0165761fee0d.tar.gz |
SERVER-24766 implement basic views library
Adds the basic infrastructure for read-only non-materialized views, as well as
the ability to create them (but not use them).
Views are disabled by default unless mongod is given the setParameter
enableViews=1.
Diffstat (limited to 'src/mongo/db/db_raii.h')
-rw-r--r-- | src/mongo/db/db_raii.h | 78 |
1 files changed, 73 insertions, 5 deletions
diff --git a/src/mongo/db/db_raii.h b/src/mongo/db/db_raii.h index 654996b676c..275a5e2c3fe 100644 --- a/src/mongo/db/db_raii.h +++ b/src/mongo/db/db_raii.h @@ -35,6 +35,7 @@ #include "mongo/db/concurrency/d_concurrency.h" #include "mongo/db/namespace_string.h" #include "mongo/db/operation_context.h" +#include "mongo/db/views/view.h" #include "mongo/util/timer.h" namespace mongo { @@ -73,13 +74,29 @@ private: class AutoGetCollection { MONGO_DISALLOW_COPYING(AutoGetCollection); + enum class ViewMode; + public: - AutoGetCollection(OperationContext* txn, const NamespaceString& nss, LockMode modeAll); + AutoGetCollection(OperationContext* txn, const NamespaceString& nss, LockMode modeAll) + : AutoGetCollection(txn, nss, modeAll, modeAll, ViewMode::kViewsForbidden) {} + + AutoGetCollection(OperationContext* txn, + const NamespaceString& nss, + LockMode modeDB, + LockMode modeColl) + : AutoGetCollection(txn, nss, modeDB, modeColl, ViewMode::kViewsForbidden) {} + /** + * This constructor is inteded for internal use and should not be used outside this file. + * AutoGetCollectionForRead and AutoGetCollectionOrViewForRead use ViewMode to determine whether + * or not it is permissible to obtain a handle on a view namespace. Use another constructor or + * another AutoGet class instead. + */ AutoGetCollection(OperationContext* txn, const NamespaceString& nss, LockMode modeDB, - LockMode modeColl); + LockMode modeColl, + ViewMode viewMode); Database* getDb() const { return _autoDb.getDb(); @@ -90,9 +107,15 @@ public: } private: + enum class ViewMode { kViewsPermitted, kViewsForbidden }; + + const ViewMode _viewMode; const AutoGetDb _autoDb; const Lock::CollectionLock _collLock; Collection* const _coll; + + friend class AutoGetCollectionForRead; + friend class AutoGetCollectionOrViewForRead; }; /** @@ -143,8 +166,13 @@ class AutoGetCollectionForRead { MONGO_DISALLOW_COPYING(AutoGetCollectionForRead); public: - AutoGetCollectionForRead(OperationContext* txn, const std::string& ns); - AutoGetCollectionForRead(OperationContext* txn, const NamespaceString& nss); + AutoGetCollectionForRead(OperationContext* txn, const std::string& ns) + : AutoGetCollectionForRead( + txn, NamespaceString(ns), AutoGetCollection::ViewMode::kViewsForbidden) {} + + AutoGetCollectionForRead(OperationContext* txn, const NamespaceString& nss) + : AutoGetCollectionForRead(txn, nss, AutoGetCollection::ViewMode::kViewsForbidden) {} + ~AutoGetCollectionForRead(); Database* getDb() const { @@ -156,16 +184,56 @@ public: } private: - void _init(const std::string& ns, StringData coll); void _ensureMajorityCommittedSnapshotIsValid(const NamespaceString& nss); const Timer _timer; OperationContext* const _txn; const ScopedTransaction _transaction; + +protected: + AutoGetCollectionForRead(OperationContext* txn, + const NamespaceString& nss, + AutoGetCollection::ViewMode viewMode); + + /** + * This protected section must come after the private section because + * AutoGetCollectionOrViewForRead needs access to _autoColl, but _autoColl must be initialized + * after _transaction. + */ boost::optional<AutoGetCollection> _autoColl; }; /** + * RAII-style class for obtaining a collection or view for reading. The pointer to a view definition + * is nullptr if it does not exist. + */ +class AutoGetCollectionOrViewForRead final : public AutoGetCollectionForRead { + MONGO_DISALLOW_COPYING(AutoGetCollectionOrViewForRead); + +public: + AutoGetCollectionOrViewForRead(OperationContext* txn, const std::string& ns) + : AutoGetCollectionOrViewForRead(txn, NamespaceString(ns)) {} + + AutoGetCollectionOrViewForRead(OperationContext* txn, const NamespaceString& nss); + + ViewDefinition* getView() const { + return _view; + } + + /** + * Unlock this view or collection and release all resources. After calling this function, it is + * illegal to access this object's database, collection and view pointers. + * + * TODO(SERVER-24909): Consider having the constructor release locks instead, or otherwise + * remove the need for this method. + */ + void releaseLocksForView() noexcept; + +private: + ViewDefinition* _view; +}; + +/** * Opens the database that we want to use and sets the appropriate namespace on the * current operation. */ |