MongoDB C++ Driver  legacy-1.1.2
namespace_string.h
1 // @file namespacestring.h
2 
3 /* Copyright 2014 MongoDB Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <algorithm>
21 #include <string>
22 
23 #include "mongo/base/string_data.h"
24 #include "mongo/util/assert_util.h"
25 
26 namespace mongo {
27 
28 /* in the mongo source code, "client" means "database". */
29 
30 const size_t MaxDatabaseNameLen = 128; // max str len for the db name, including null char
31 
36 bool legalClientSystemNS(const StringData& ns, bool write);
37 
38 /* e.g.
39  NamespaceString ns("acme.orders");
40  cout << ns.coll; // "orders"
41 */
43 public:
48 
52  explicit NamespaceString(const StringData& ns);
53 
58  NamespaceString(const StringData& dbName, const StringData& collectionName);
59 
65  // Maximum possible length of name any namespace, including special ones like $extra.
66  // This includes rum for the NUL byte so it can be used when sizing buffers.
67  MaxNsLenWithNUL = 128,
68 
69  // MaxNsLenWithNUL excluding the NUL byte. Use this when comparing std::string lengths.
70  MaxNsLen = MaxNsLenWithNUL - 1,
71 
72  // Maximum allowed length of fully qualified namespace name of any real collection.
73  // Does not include NUL so it can be directly compared to std::string lengths.
74  MaxNsCollectionLen = MaxNsLen - 7 /*strlen(".$extra")*/,
75  };
76 
77  StringData db() const;
78  StringData coll() const;
79 
80  const std::string& ns() const {
81  return _ns;
82  }
83 
84  operator const std::string&() const {
85  return ns();
86  }
87  const std::string& toString() const {
88  return ns();
89  }
90 
91  size_t size() const {
92  return _ns.size();
93  }
94 
95  bool isSystem() const {
96  return coll().startsWith("system.");
97  }
98  bool isSystemDotIndexes() const {
99  return coll() == "system.indexes";
100  }
101  bool isConfigDB() const {
102  return db() == "config";
103  }
104  bool isCommand() const {
105  return coll() == "$cmd";
106  }
107  bool isOplog() const {
108  return oplog(_ns);
109  }
110  bool isSpecialCommand() const {
111  return coll().startsWith("$cmd.sys");
112  }
113  bool isSpecial() const {
114  return special(_ns);
115  }
116  bool isNormal() const {
117  return normal(_ns);
118  }
119  bool isListCollectionsGetMore() const;
120  bool isListIndexesGetMore() const;
121 
127 
132  bool isValid() const {
133  return validDBName(db()) && !coll().empty();
134  }
135 
136  bool operator==(const std::string& nsIn) const {
137  return nsIn == _ns;
138  }
139  bool operator==(const StringData& nsIn) const {
140  return nsIn == _ns;
141  }
142  bool operator==(const NamespaceString& nsIn) const {
143  return nsIn._ns == _ns;
144  }
145 
146  bool operator!=(const std::string& nsIn) const {
147  return nsIn != _ns;
148  }
149  bool operator!=(const NamespaceString& nsIn) const {
150  return nsIn._ns != _ns;
151  }
152 
153  bool operator<(const NamespaceString& rhs) const {
154  return _ns < rhs._ns;
155  }
156 
159  std::string getSisterNS(const StringData& local) const;
160 
161  // @return db() + ".system.indexes"
162  std::string getSystemIndexesCollection() const;
163 
164  // @return db() + ".$cmd"
165  std::string getCommandNS() const;
166 
171  static bool normal(const StringData& ns);
172 
176  static bool oplog(const StringData& ns);
177 
178  static bool special(const StringData& ns);
179 
194  static bool validDBName(const StringData& dbin);
195 
208  static bool validCollectionComponent(const StringData& ns);
209 
221  static bool validCollectionName(const StringData& coll);
222 
223 private:
224  std::string _ns;
225  size_t _dotIndex;
226 };
227 
228 
229 // "database.a.b.c" -> "database"
230 inline StringData nsToDatabaseSubstring(const StringData& ns) {
231  size_t i = ns.find('.');
232  if (i == std::string::npos) {
233  massert(10078, "nsToDatabase: db too long", ns.size() < MaxDatabaseNameLen);
234  return ns;
235  }
236  massert(10088, "nsToDatabase: db too long", i < static_cast<size_t>(MaxDatabaseNameLen));
237  return ns.substr(0, i);
238 }
239 
240 // "database.a.b.c" -> "database"
241 inline void nsToDatabase(const StringData& ns, char* database) {
242  StringData db = nsToDatabaseSubstring(ns);
243  db.copyTo(database, true);
244 }
245 
246 // TODO: make this return a StringData
247 inline std::string nsToDatabase(const StringData& ns) {
248  return nsToDatabaseSubstring(ns).toString();
249 }
250 
251 // "database.a.b.c" -> "a.b.c"
252 inline StringData nsToCollectionSubstring(const StringData& ns) {
253  size_t i = ns.find('.');
254  massert(16886, "nsToCollectionSubstring: no .", i != std::string::npos);
255  return ns.substr(i + 1);
256 }
257 
263 inline bool nsIsFull(const StringData& ns) {
264  size_t i = ns.find('.');
265  if (i == std::string::npos)
266  return false;
267  if (i == ns.size() - 1)
268  return false;
269  return true;
270 }
271 
277 inline bool nsIsDbOnly(const StringData& ns) {
278  size_t i = ns.find('.');
279  if (i == std::string::npos)
280  return true;
281  return false;
282 }
283 
294 int nsDBHash(const std::string& ns);
295 
296 bool nsDBEquals(const std::string& a, const std::string& b);
297 
299  int operator()(const std::string& ns) const {
300  return nsDBHash(ns);
301  }
302 };
303 
305  bool operator()(const std::string& a, const std::string& b) const {
306  return nsDBEquals(a, b);
307  }
308 };
309 }
310 
311 
312 #include "mongo/db/namespace_string-inl.h"
bool nsIsDbOnly(const StringData &ns)
foo = true foo.
Definition: namespace_string.h:277
static bool validCollectionComponent(const StringData &ns)
Takes a fully qualified namespace (ie dbname.collectionName), and returns true if the collection name...
Definition: namespace_string-inl.h:76
static bool oplog(const StringData &ns)
Definition: namespace_string-inl.h:38
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
static bool normal(const StringData &ns)
Definition: namespace_string-inl.h:32
bool legalClientSystemNS(const StringData &ns, bool write)
static bool validDBName(const StringData &dbin)
samples: good foo bar foo-bar bad: foo bar foo.bar foo"bar
Definition: namespace_string-inl.h:46
NamespaceString getTargetNSForListIndexesGetMore() const
Given a NamespaceString for which isListIndexesGetMore() returns true, returns the NamespaceString fo...
NamespaceString()
Constructs an empty NamespaceString.
Definition: namespace_string-inl.h:104
Definition: namespace_string.h:42
MaxNsLenValue
Note that these values are derived from the mmap_v1 implementation and that is the only reason they a...
Definition: namespace_string.h:64
static bool validCollectionName(const StringData &coll)
Takes a collection name and returns true if it is a valid collection name.
Definition: namespace_string-inl.h:84
std::string getSisterNS(const StringData &local) const
( foo.bar ).getSisterNS( "blah" ) == foo.blah
Definition: namespace_string-inl.h:172
int nsDBHash(const std::string &ns)
NamespaceDBHash and NamespaceDBEquals allow you to do something like unordered_map and use the full namespace for the string but comparisons are done only on the db piece.
Definition: namespace_string-inl.h:130
bool isValid() const
Definition: namespace_string.h:132
bool startsWith(const StringData &prefix) const
Returns true if 'prefix' is a substring of this instance, anchored at position 0. ...
Definition: string_data-inl.h:109
Definition: namespace_string.h:304
bool nsIsFull(const StringData &ns)
foo = false foo.
Definition: namespace_string.h:263
Definition: namespace_string.h:298