Storage Engine API
kv_catalog.h
Go to the documentation of this file.
1 // kv_catalog.h
2 
31 #pragma once
32 
33 #include <map>
34 #include <memory>
35 #include <string>
36 
37 #include "mongo/base/string_data.h"
39 #include "mongo/db/record_id.h"
42 #include "mongo/stdx/mutex.h"
43 
44 namespace mongo {
45 
46 class OperationContext;
47 class RecordStore;
48 
49 class KVCatalog {
50 public:
51  class FeatureTracker;
52 
59  KVCatalog(RecordStore* rs, bool directoryPerDb, bool directoryForIndexes);
60  ~KVCatalog();
61 
62  void init(OperationContext* opCtx);
63 
64  void getAllCollections(std::vector<std::string>* out) const;
65 
69  Status newCollection(OperationContext* opCtx,
70  StringData ns,
72  KVPrefix prefix);
73 
74  std::string getCollectionIdent(StringData ns) const;
75 
76  std::string getIndexIdent(OperationContext* opCtx, StringData ns, StringData idName) const;
77 
78  BSONCollectionCatalogEntry::MetaData getMetaData(OperationContext* opCtx, StringData ns) const;
79  void putMetaData(OperationContext* opCtx,
80  StringData ns,
82 
83  Status renameCollection(OperationContext* opCtx,
84  StringData fromNS,
85  StringData toNS,
86  bool stayTemp);
87 
88  Status dropCollection(OperationContext* opCtx, StringData ns);
89 
90  std::vector<std::string> getAllIdentsForDB(StringData db) const;
91  std::vector<std::string> getAllIdents(OperationContext* opCtx) const;
92 
93  bool isUserDataIdent(StringData ident) const;
94 
96  invariant(_featureTracker);
97  return _featureTracker.get();
98  }
99 
101  return _rs;
102  }
103 
104 private:
105  class AddIdentChange;
106  class RemoveIdentChange;
107 
108  BSONObj _findEntry(OperationContext* opCtx, StringData ns, RecordId* out = NULL) const;
109 
115  std::string _newUniqueIdent(StringData ns, const char* kind);
116 
117  // Helpers only used by constructor and init(). Don't call from elsewhere.
118  static std::string _newRand();
119  bool _hasEntryCollidingWithRand() const;
120 
121  RecordStore* _rs; // not owned
122  const bool _directoryPerDb;
124 
125  // These two are only used for ident generation inside _newUniqueIdent.
126  std::string _rand; // effectively const after init() returns
127  AtomicUInt64 _next;
128 
129  struct Entry {
130  Entry() {}
131  Entry(std::string i, RecordId l) : ident(i), storedLoc(l) {}
132  std::string ident;
133  RecordId storedLoc;
134  };
135  typedef std::map<std::string, Entry> NSToIdentMap;
136  NSToIdentMap _idents;
137  mutable stdx::mutex _identsLock;
138 
139  // Manages the feature document that may be present in the KVCatalog. '_featureTracker' is
140  // guaranteed to be non-null after KVCatalog::init() is called.
141  std::unique_ptr<FeatureTracker> _featureTracker;
142 };
143 }
std::map< std::string, Entry > NSToIdentMap
Definition: kv_catalog.h:135
RecordStore * getRecordStore()
Definition: kv_catalog.h:100
KVCatalog(RecordStore *rs, bool directoryPerDb, bool directoryForIndexes)
Definition: kv_catalog.cpp:324
bool isUserDataIdent(StringData ident) const
Definition: kv_catalog.cpp:630
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
static std::string _newRand()
Definition: kv_catalog.cpp:334
Definition: collection_options.h:57
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
RecordStore * _rs
Definition: kv_catalog.h:121
std::string _rand
Definition: kv_catalog.h:126
OperationContext Database StringData BSONObj CollectionOptions::ParseKind bool const BSONObj &idIndex Status
Definition: database_impl.cpp:956
FeatureTracker * getFeatureTracker() const
Definition: kv_catalog.h:95
void init(OperationContext *opCtx)
Definition: kv_catalog.cpp:359
Status dropCollection(OperationContext *opCtx, StringData ns)
Definition: kv_catalog.cpp:569
bool _hasEntryCollidingWithRand() const
Definition: kv_catalog.cpp:338
void putMetaData(OperationContext *opCtx, StringData ns, BSONCollectionCatalogEntry::MetaData &md)
Definition: kv_catalog.cpp:492
std::unique_ptr< FeatureTracker > _featureTracker
Definition: kv_catalog.h:141
std::unique_ptr< RecordStore > rs
Definition: kv_engine_test_timestamps.cpp:207
std::vector< std::string > getAllIdentsForDB(StringData db) const
Definition: kv_catalog.cpp:586
Definition: kv_catalog.h:49
const bool _directoryForIndexes
Definition: kv_catalog.h:123
std::string getIndexIdent(OperationContext *opCtx, StringData ns, StringData idName) const
Definition: kv_catalog.cpp:447
AtomicUInt64 _next
Definition: kv_catalog.h:127
Entry()
Definition: kv_catalog.h:130
const bool _directoryPerDb
Definition: kv_catalog.h:122
stdx::mutex _identsLock
Definition: kv_catalog.h:137
Definition: kv_catalog.cpp:152
std::string _newUniqueIdent(StringData ns, const char *kind)
Generates a new unique identifier for a new "thing".
Definition: kv_catalog.cpp:347
Entry(std::string i, RecordId l)
Definition: kv_catalog.h:131
std::string ident
Definition: kv_catalog.h:132
An abstraction used for storing documents in a collection or entries in an index. ...
Definition: record_store.h:282
void getAllCollections(std::vector< std::string > *out) const
Definition: kv_catalog.cpp:393
RecordId storedLoc
Definition: kv_catalog.h:133
Status newCollection(OperationContext *opCtx, StringData ns, const CollectionOptions &options, KVPrefix prefix)
Definition: kv_catalog.cpp:400
OperationContext Database * db
Definition: database_impl.cpp:949
OperationContext Database StringData BSONObj options
Definition: database_impl.cpp:949
Manages the contents of a document in the KVCatalog used to restrict downgrade compatibility.
Definition: kv_catalog_feature_tracker.h:53
A KVPrefix may be prepended to the keys of entries in an underlying KV store.
Definition: kv_prefix.h:44
Definition: kv_catalog.h:129
BSONCollectionCatalogEntry::MetaData getMetaData(OperationContext *opCtx, StringData ns) const
Definition: kv_catalog.cpp:479
NSToIdentMap _idents
Definition: kv_catalog.h:136
Collection *const OperationContext *const opCtx
Definition: collection_impl.cpp:80
Definition: bson_collection_catalog_entry.h:110
Definition: kv_catalog.cpp:137
Status renameCollection(OperationContext *opCtx, StringData fromNS, StringData toNS, bool stayTemp)
Definition: kv_catalog.cpp:531
std::string getCollectionIdent(StringData ns) const
Definition: kv_catalog.cpp:440
std::vector< std::string > getAllIdents(OperationContext *opCtx) const
Definition: kv_catalog.cpp:602
~KVCatalog()
Definition: kv_catalog.cpp:330
BSONObj _findEntry(OperationContext *opCtx, StringData ns, RecordId *out=NULL) const
Definition: kv_catalog.cpp:455