Storage Engine API
mmap_v1_database_catalog_entry.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <map>
32 #include <string>
33 
34 #include "mongo/base/status.h"
35 #include "mongo/base/string_data.h"
40 
41 namespace mongo {
42 
43 class CollectionCatalogEntry;
44 struct CollectionOptions;
45 class IndexAccessMethod;
46 class IndexCatalogEntry;
47 class IndexDescriptor;
48 class RecordId;
49 class RecordStore;
50 class RecordStoreV1Base;
51 class RecoveryUnit;
52 class OperationContext;
53 
55 public:
56  MMAPV1DatabaseCatalogEntry(OperationContext* opCtx,
57  StringData name,
58  StringData path,
59  bool directoryperdb,
60  bool transient,
61  std::unique_ptr<ExtentManager> extentManager);
62 
64 
68  virtual void close(OperationContext* opCtx) {
69  _extentManager->close(opCtx);
70  _namespaceIndex.close(opCtx);
71  }
72 
73  // these two seem the same and yet different
74  // TODO(ERH): consolidate into one ideally
75  virtual bool exists() const {
76  return _namespaceIndex.pathExists();
77  }
78  virtual bool isEmpty() const {
79  return !_namespaceIndex.allocated();
80  }
81  virtual bool hasUserData() const {
82  // The two collections which exist and can't be removed are:
83  // system.indexes
84  // system.namespaces
85  return _collections.size() > 2;
86  }
87 
88  virtual int64_t sizeOnDisk(OperationContext* opCtx) const;
89 
90  virtual bool isOlderThan24(OperationContext* opCtx) const;
91  virtual void markIndexSafe24AndUp(OperationContext* opCtx);
92 
93  // Records in the data file version bits that an index or collection may have an associated
94  // collation.
95  void markCollationFeatureAsInUse(OperationContext* opCtx);
96 
97  virtual Status currentFilesCompatible(OperationContext* opCtx) const;
98 
99  virtual void appendExtraStats(OperationContext* opCtx, BSONObjBuilder* out, double scale) const;
100 
101  Status createCollection(OperationContext* opCtx,
102  StringData ns,
103  const CollectionOptions& options,
104  bool allocateDefaultSpace);
105 
106  Status dropCollection(OperationContext* opCtx, StringData ns);
107 
108  Status renameCollection(OperationContext* opCtx,
109  StringData fromNS,
110  StringData toNS,
111  bool stayTemp);
112 
113  void getCollectionNamespaces(std::list<std::string>* tofill) const;
114 
119 
120  RecordStore* getRecordStore(StringData ns) const;
121 
122  IndexAccessMethod* getIndex(OperationContext* opCtx,
124  IndexCatalogEntry* index);
125 
127  return _extentManager.get();
128  }
130  return _extentManager.get();
131  }
132 
133  CollectionOptions getCollectionOptions(OperationContext* opCtx, StringData ns) const;
134 
135  CollectionOptions getCollectionOptions(OperationContext* opCtx, RecordId nsRid) const;
136 
142  void createNamespaceForIndex(OperationContext* opCtx, StringData name);
143  static void invalidateSystemCollectionRecord(OperationContext* opCtx,
144  NamespaceString systemCollectionNamespace,
145  RecordId record);
146 
147 private:
148  class EntryInsertion;
150 
152 
153  // The _collections map is a cache for efficiently looking up namespace information. Access
154  // to the cache is protected by holding the appropriate DB lock. Regular operations
155  // (insert/update/delete/query) hold intent locks on the database and they access the cache
156  // directly. Metadata operations, such as create db/collection, etc acquire exclusive lock
157  // on the database, which protects against concurrent readers of the cache.
158  //
159  // Once initialized, the cache must remain consistent with the data in the memory-mapped
160  // database files through _removeFromCache and _insertInCache. These methods use the
161  // RecoveryUnit to ensure correct handling of rollback.
162 
163  struct Entry {
164  std::unique_ptr<NamespaceDetailsCollectionCatalogEntry> catalogEntry;
165  std::unique_ptr<RecordStoreV1Base> recordStore;
166  };
167 
168  typedef std::map<std::string, Entry*> CollectionMap;
169 
170 
173  RecordStoreV1Base* _getRecordStore(StringData ns) const;
174 
175  RecordId _addNamespaceToNamespaceCollection(OperationContext* opCtx,
176  StringData ns,
177  const BSONObj* options);
178 
179  void _removeNamespaceFromNamespaceCollection(OperationContext* opCtx, StringData ns);
180 
181  Status _renameSingleNamespace(OperationContext* opCtx,
182  StringData fromNS,
183  StringData toNS,
184  bool stayTemp);
185 
186  void _ensureSystemCollection(OperationContext* opCtx, StringData ns);
187 
188  void _init(OperationContext* opCtx);
189 
193  void _insertInCache(OperationContext* opCtx, StringData ns, RecordId rid, Entry* entry);
194 
199  void _removeFromCache(RecoveryUnit* ru, StringData ns);
200 
201 
202  const std::string _path;
203 
205  std::unique_ptr<ExtentManager> _extentManager;
206  CollectionMap _collections;
207 };
208 }
virtual bool isOlderThan24(OperationContext *opCtx) const
Definition: mmap_v1_database_catalog_entry.cpp:446
ExtentManager * getExtentManager()
Definition: mmap_v1_database_catalog_entry.h:129
std::unique_ptr< ExtentManager > _extentManager
Definition: mmap_v1_database_catalog_entry.h:205
RecordStoreV1Base * _getIndexRecordStore()
Definition: mmap_v1_database_catalog_entry.cpp:816
void markCollationFeatureAsInUse(OperationContext *opCtx)
Definition: mmap_v1_database_catalog_entry.cpp:470
static void invalidateSystemCollectionRecord(OperationContext *opCtx, NamespaceString systemCollectionNamespace, RecordId record)
Definition: mmap_v1_database_catalog_entry.cpp:405
bool allocated() const
Definition: namespace_index.h:79
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
virtual void markIndexSafe24AndUp(OperationContext *opCtx)
Definition: mmap_v1_database_catalog_entry.cpp:456
Definition: collection_options.h:57
const std::string & name() const
Definition: database_catalog_entry.h:56
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
Definition: collection_catalog_entry.h:47
Collection *const collection
Definition: collection_info_cache_impl.cpp:53
Definition: record_store_v1_base.h:152
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
Status dropCollection(OperationContext *opCtx, StringData ns)
Definition: mmap_v1_database_catalog_entry.cpp:232
Status _renameSingleNamespace(OperationContext *opCtx, StringData fromNS, StringData toNS, bool stayTemp)
Definition: mmap_v1_database_catalog_entry.cpp:328
Definition: namespace_index.h:50
CollectionOptions getCollectionOptions(OperationContext *opCtx, StringData ns) const
Definition: mmap_v1_database_catalog_entry.cpp:880
void _init(OperationContext *opCtx)
Definition: mmap_v1_database_catalog_entry.cpp:509
const std::string _path
Definition: mmap_v1_database_catalog_entry.h:202
NamespaceIndex _namespaceIndex
Definition: mmap_v1_database_catalog_entry.h:204
Registers the removal of an entry from the _collections cache with the RecoveryUnit, delaying actual deletion of the information until the change is commited.
Definition: mmap_v1_database_catalog_entry.cpp:137
Definition: namespace_details_collection_entry.h:48
IndexAccessMethod * getIndex(OperationContext *opCtx, const CollectionCatalogEntry *collection, IndexCatalogEntry *index)
Definition: mmap_v1_database_catalog_entry.cpp:775
Status renameCollection(OperationContext *opCtx, StringData fromNS, StringData toNS, bool stayTemp)
Definition: mmap_v1_database_catalog_entry.cpp:261
void _removeFromCache(RecoveryUnit *ru, StringData ns)
Drop cached information for specified namespace.
Definition: mmap_v1_database_catalog_entry.cpp:217
Registers the insertion of a new entry in the _collections cache with the RecoveryUnit, allowing for rollback.
Definition: mmap_v1_database_catalog_entry.cpp:116
virtual ~MMAPV1DatabaseCatalogEntry()
Definition: mmap_v1_database_catalog_entry.cpp:204
virtual bool hasUserData() const
Definition: mmap_v1_database_catalog_entry.h:81
CollectionMap _collections
Definition: mmap_v1_database_catalog_entry.h:206
virtual int64_t sizeOnDisk(OperationContext *opCtx) const
Definition: mmap_v1_database_catalog_entry.cpp:213
std::unique_ptr< NamespaceDetailsCollectionCatalogEntry > catalogEntry
Definition: mmap_v1_database_catalog_entry.h:164
RecordStoreV1Base * _getNamespaceRecordStore() const
Definition: mmap_v1_database_catalog_entry.cpp:824
Definition: mmap_v1_database_catalog_entry.h:163
const ExtentManager * getExtentManager() const
Definition: mmap_v1_database_catalog_entry.h:126
virtual bool isEmpty() const
Definition: mmap_v1_database_catalog_entry.h:78
void _insertInCache(OperationContext *opCtx, StringData ns, RecordId rid, Entry *entry)
Populate the _collections cache.
Definition: mmap_v1_database_catalog_entry.cpp:739
Definition: index_catalog_entry.h:56
A RecoveryUnit is responsible for ensuring that data is persisted.
Definition: recovery_unit.h:51
std::map< std::string, Entry * > CollectionMap
Definition: mmap_v1_database_catalog_entry.h:168
void getCollectionNamespaces(std::list< std::string > *tofill) const
Definition: mmap_v1_database_catalog_entry.cpp:491
An abstraction used for storing documents in a collection or entries in an index. ...
Definition: record_store.h:282
RecordId _addNamespaceToNamespaceCollection(OperationContext *opCtx, StringData ns, const BSONObj *options)
Definition: mmap_v1_database_catalog_entry.cpp:832
virtual bool exists() const
Definition: mmap_v1_database_catalog_entry.h:75
virtual Status currentFilesCompatible(OperationContext *opCtx) const
Returns whethers the data files are compatible with the current code:
Definition: mmap_v1_database_catalog_entry.cpp:484
Definition: database_catalog_entry.h:50
virtual void close(OperationContext *opCtx)
Must be called before destruction.
Definition: mmap_v1_database_catalog_entry.h:68
RecordStoreV1Base * _getRecordStore(StringData ns) const
Definition: mmap_v1_database_catalog_entry.cpp:765
void createNamespaceForIndex(OperationContext *opCtx, StringData name)
Creates a CollectionCatalogEntry in the form of an index rather than a collection.
Definition: mmap_v1_database_catalog_entry.cpp:714
NamespaceDetailsCollectionCatalogEntry * getCollectionCatalogEntry(StringData ns) const
will return NULL if ns does not exist
Definition: mmap_v1_database_catalog_entry.cpp:728
virtual void appendExtraStats(OperationContext *opCtx, BSONObjBuilder *out, double scale) const
Definition: mmap_v1_database_catalog_entry.cpp:416
std::unique_ptr< RecordStoreV1Base > recordStore
Definition: mmap_v1_database_catalog_entry.h:165
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
RecoveryUnit * ru
Definition: recovery_unit_test_harness.cpp:50
ExtentManager basics.
Definition: extent_manager.h:64
MMAPV1DatabaseCatalogEntry(OperationContext *opCtx, StringData name, StringData path, bool directoryperdb, bool transient, std::unique_ptr< ExtentManager > extentManager)
Definition: mmap_v1_database_catalog_entry.cpp:158
bool pathExists() const
Definition: namespace_index.cpp:119
void close(OperationContext *opCtx)
Must be called before destruction.
Definition: namespace_index.h:60
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
Definition: mmap_v1_database_catalog_entry.h:54
void _removeNamespaceFromNamespaceCollection(OperationContext *opCtx, StringData ns)
Definition: mmap_v1_database_catalog_entry.cpp:857
RecordStore * getRecordStore(StringData ns) const
Definition: mmap_v1_database_catalog_entry.cpp:761
Status createCollection(OperationContext *opCtx, StringData ns, const CollectionOptions &options, bool allocateDefaultSpace)
Definition: mmap_v1_database_catalog_entry.cpp:651
void _ensureSystemCollection(OperationContext *opCtx, StringData ns)
Definition: mmap_v1_database_catalog_entry.cpp:495