1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
/**
* Copyright (C) 2017 MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include <algorithm>
#include <boost/optional.hpp>
#include <iosfwd>
#include <string>
#include "mongo/base/status_with.h"
#include "mongo/base/string_data.h"
#include "mongo/db/repl/optime.h"
#include "mongo/platform/hash_namespace.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/uuid.h"
namespace mongo {
const size_t MaxDatabaseNameLen = 128; // max str len for the db name, including null char
class NamespaceString {
public:
// Reserved system namespaces
// Namespace for the admin database
static constexpr StringData kAdminDb = "admin"_sd;
// Namespace for the local database
static constexpr StringData kLocalDb = "local"_sd;
// Namespace for the sharding config database
static constexpr StringData kConfigDb = "config"_sd;
// Name for the system views collection
static constexpr StringData kSystemDotViewsCollectionName = "system.views"_sd;
// Namespace for storing configuration data, which needs to be replicated if the server is
// running as a replica set. Documents in this collection should represent some configuration
// state of the server, which needs to be recovered/consulted at startup. Each document in this
// namespace should have its _id set to some string, which meaningfully describes what it
// represents. For example, 'shardIdentity' and 'featureCompatibilityVersion'.
static const NamespaceString kServerConfigurationNamespace;
// Namespace for storing the transaction information for each session
static const NamespaceString kSessionTransactionsTableNamespace;
// Name for a shard's collections metadata collection, each document of which indicates the
// state of a specific collection
static const NamespaceString kShardConfigCollectionsNamespace;
// Name for a shard's databases metadata collection, each document of which indicates the state
// of a specific database
static const NamespaceString kShardConfigDatabasesNamespace;
// Name for causal consistency's key collection.
static const NamespaceString kSystemKeysNamespace;
// Namespace of the the oplog collection.
static const NamespaceString kRsOplogNamespace;
/**
* Constructs an empty NamespaceString.
*/
NamespaceString() : _ns(), _dotIndex(std::string::npos) {}
/**
* Constructs a NamespaceString from the fully qualified namespace named in "ns".
*/
explicit NamespaceString(StringData ns) {
_ns = ns.toString(); // copy to our buffer
_dotIndex = _ns.find('.');
uassert(ErrorCodes::InvalidNamespace,
"namespaces cannot have embedded null characters",
_ns.find('\0') == std::string::npos);
}
/**
* Constructs a NamespaceString for the given database and collection names.
* "dbName" must not contain a ".", and "collectionName" must not start with one.
*/
NamespaceString(StringData dbName, StringData collectionName)
: _ns(dbName.size() + collectionName.size() + 1, '\0') {
uassert(ErrorCodes::InvalidNamespace,
"'.' is an invalid character in a database name",
dbName.find('.') == std::string::npos);
uassert(ErrorCodes::InvalidNamespace,
"Collection names cannot start with '.'",
collectionName.empty() || collectionName[0] != '.');
std::string::iterator it = std::copy(dbName.begin(), dbName.end(), _ns.begin());
*it = '.';
++it;
it = std::copy(collectionName.begin(), collectionName.end(), it);
_dotIndex = dbName.size();
dassert(it == _ns.end());
dassert(_ns[_dotIndex] == '.');
uassert(ErrorCodes::InvalidNamespace,
"namespaces cannot have embedded null characters",
_ns.find('\0') == std::string::npos);
}
/**
* Constructs the namespace '<dbName>.$cmd.aggregate', which we use as the namespace for
* aggregation commands with the format {aggregate: 1}.
*/
static NamespaceString makeCollectionlessAggregateNSS(StringData dbName);
/**
* Constructs a NamespaceString representing a listCollections namespace. The format for this
* namespace is "<dbName>.$cmd.listCollections".
*/
static NamespaceString makeListCollectionsNSS(StringData dbName);
/**
* Constructs a NamespaceString representing a listIndexes namespace. The format for this
* namespace is "<dbName>.$cmd.listIndexes.<collectionName>".
*/
static NamespaceString makeListIndexesNSS(StringData dbName, StringData collectionName);
/**
* Note that these values are derived from the mmap_v1 implementation and that is the only
* reason they are constrained as such.
*/
enum MaxNsLenValue {
// Maximum possible length of name any namespace, including special ones like $extra.
// This includes rum for the NUL byte so it can be used when sizing buffers.
MaxNsLenWithNUL = 128,
// MaxNsLenWithNUL excluding the NUL byte. Use this when comparing std::string lengths.
MaxNsLen = MaxNsLenWithNUL - 1,
// Maximum allowed length of fully qualified namespace name of any real collection.
// Does not include NUL so it can be directly compared to std::string lengths.
MaxNsCollectionLen = MaxNsLen - 7 /*strlen(".$extra")*/,
};
/**
* NOTE: DollarInDbNameBehavior::allow is deprecated.
*
* Please use DollarInDbNameBehavior::disallow and check explicitly for any DB names that must
* contain a $.
*/
enum class DollarInDbNameBehavior {
Disallow,
Allow, // Deprecated
};
StringData db() const {
return _dotIndex == std::string::npos ? _ns : StringData(_ns.data(), _dotIndex);
}
StringData coll() const {
return _dotIndex == std::string::npos
? StringData()
: StringData(_ns.c_str() + _dotIndex + 1, _ns.size() - 1 - _dotIndex);
}
const std::string& ns() const {
return _ns;
}
const std::string& toString() const {
return ns();
}
size_t size() const {
return _ns.size();
}
bool isEmpty() const {
return _ns.empty();
}
struct Hasher {
size_t operator()(const NamespaceString& nss) const {
return std::hash<std::string>()(nss._ns);
}
};
//
// The following methods assume isValid() is true for this NamespaceString.
//
bool isHealthlog() const {
return isLocal() && coll() == "system.healthlog";
}
bool isSystem() const {
return coll().startsWith("system.");
}
bool isAdminDB() const {
return db() == kAdminDb;
}
bool isLocal() const {
return db() == kLocalDb;
}
bool isSystemDotIndexes() const {
return coll() == "system.indexes";
}
bool isSystemDotProfile() const {
return coll() == "system.profile";
}
bool isSystemDotViews() const {
return coll() == kSystemDotViewsCollectionName;
}
bool isServerConfigurationCollection() const {
return (db() == kAdminDb) && (coll() == "system.version");
}
bool isConfigDB() const {
return db() == kConfigDb;
}
bool isCommand() const {
return coll() == "$cmd";
}
bool isOplog() const {
return oplog(_ns);
}
bool isSpecial() const {
return special(_ns);
}
bool isOnInternalDb() const {
if (db() == kAdminDb)
return true;
if (db() == kLocalDb)
return true;
if (db() == kConfigDb)
return true;
return false;
}
bool isNormal() const {
return normal(_ns);
}
/**
* Returns whether the NamespaceString references a special collection that cannot be used for
* generic data storage.
*/
bool isVirtualized() const {
return virtualized(_ns);
}
/**
* Returns whether a namespace is replicated, based only on its string value. One notable
* omission is that map reduce `tmp.mr` collections may or may not be replicated. Callers must
* decide how to handle that case separately.
*/
bool isReplicated() const;
/**
* Returns true if cursors for this namespace are registered with the global cursor manager.
*/
bool isGloballyManagedNamespace() const {
return coll().startsWith("$cmd."_sd);
}
bool isCollectionlessAggregateNS() const;
bool isListCollectionsCursorNS() const;
bool isListIndexesCursorNS() const;
/**
* Returns true if a client can modify this namespace even though it is under ".system."
* For example <dbname>.system.users is ok for regular clients to update.
*/
bool isLegalClientSystemNS() const;
/**
* Given a NamespaceString for which isGloballyManagedNamespace() returns true, returns the
* namespace the command targets, or boost::none for commands like 'listCollections' which
* do not target a collection.
*/
boost::optional<NamespaceString> getTargetNSForGloballyManagedNamespace() const;
/**
* Returns true if this namespace refers to a drop-pending collection.
*/
bool isDropPendingNamespace() const;
/**
* Returns the drop-pending namespace name for this namespace, provided the given optime.
*
* Example:
* test.foo -> test.system.drop.<timestamp seconds>i<timestamp increment>t<term>.foo
*
* Original collection name may be truncated so that the generated namespace length does not
* exceed MaxNsCollectionLen.
*/
NamespaceString makeDropPendingNamespace(const repl::OpTime& opTime) const;
/**
* Returns the optime used to generate the drop-pending namespace.
* Returns an error if this namespace is not drop-pending.
*/
StatusWith<repl::OpTime> getDropPendingNamespaceOpTime() const;
/**
* Checks if this namespace is valid as a target namespace for a rename operation, given
* the length of the longest index name in the source collection.
*/
Status checkLengthForRename(const std::string::size_type longestIndexNameLength) const;
/**
* Given a NamespaceString for which isListIndexesCursorNS() returns true, returns the
* NamespaceString for the collection that the "listIndexes" targets.
*/
NamespaceString getTargetNSForListIndexes() const;
/**
* Returns true if the namespace is valid. Special namespaces for internal use are considered as
* valid.
*/
bool isValid() const {
return validDBName(db(), DollarInDbNameBehavior::Allow) && !coll().empty();
}
/**
* NamespaceString("foo.bar").getSisterNS("blah") returns "foo.blah".
*/
std::string getSisterNS(StringData local) const;
std::string getSystemIndexesCollection() const {
return db().toString() + ".system.indexes";
}
NamespaceString getCommandNS() const {
return {db(), "$cmd"};
}
/**
* @return true if ns is 'normal'. A "$" is used for namespaces holding index data,
* which do not contain BSON objects in their records. ("oplog.$main" is the exception)
*/
static bool normal(StringData ns) {
return !virtualized(ns);
}
/**
* @return true if the ns is an oplog one, otherwise false.
*/
static bool oplog(StringData ns) {
return ns.startsWith("local.oplog.");
}
static bool special(StringData ns) {
return !normal(ns) || ns.substr(ns.find('.')).startsWith(".system.");
}
/**
* Check if `ns` references a special collection that cannot be used for generic data storage.
*/
static bool virtualized(StringData ns) {
return ns.find('$') != std::string::npos && ns != "local.oplog.$main";
}
/**
* samples:
* good
* foo
* bar
* foo-bar
* bad:
* foo bar
* foo.bar
* foo"bar
*
* @param db - a possible database name
* @param DollarInDbNameBehavior - please do not change the default value. DB names that must
* contain a $ should be checked explicitly.
* @return if db is an allowed database name
*/
static bool validDBName(StringData db,
DollarInDbNameBehavior behavior = DollarInDbNameBehavior::Disallow);
/**
* Takes a fully qualified namespace (ie dbname.collectionName), and returns true if
* the collection name component of the namespace is valid.
* samples:
* good:
* foo.bar
* bad:
* foo.
*
* @param ns - a full namespace (a.b)
* @return if db.coll is an allowed collection name
*/
static bool validCollectionComponent(StringData ns);
/**
* Takes a collection name and returns true if it is a valid collection name.
* samples:
* good:
* foo
* system.indexes
* bad:
* $foo
* @param coll - a collection name component of a namespace
* @return if the input is a valid collection name
*/
static bool validCollectionName(StringData coll);
// Relops among `NamespaceString`.
friend bool operator==(const NamespaceString& a, const NamespaceString& b) {
return a.ns() == b.ns();
}
friend bool operator!=(const NamespaceString& a, const NamespaceString& b) {
return a.ns() != b.ns();
}
friend bool operator<(const NamespaceString& a, const NamespaceString& b) {
return a.ns() < b.ns();
}
friend bool operator>(const NamespaceString& a, const NamespaceString& b) {
return a.ns() > b.ns();
}
friend bool operator<=(const NamespaceString& a, const NamespaceString& b) {
return a.ns() <= b.ns();
}
friend bool operator>=(const NamespaceString& a, const NamespaceString& b) {
return a.ns() >= b.ns();
}
private:
std::string _ns;
size_t _dotIndex;
};
/**
* This class is intented to be used by commands which can accept either a collection name or
* database + collection UUID. It will never be initialized with both.
*/
class NamespaceStringOrUUID {
public:
NamespaceStringOrUUID(NamespaceString nss) : _nss(std::move(nss)) {}
NamespaceStringOrUUID(std::string dbname, UUID uuid)
: _uuid(std::move(uuid)), _dbname(std::move(dbname)) {}
const boost::optional<NamespaceString>& nss() const {
return _nss;
}
const boost::optional<UUID>& uuid() const {
return _uuid;
}
const std::string& dbname() const {
return _dbname;
}
std::string toString() const;
private:
// At any given time exactly one of these optionals will be initialized
boost::optional<NamespaceString> _nss;
boost::optional<UUID> _uuid;
// Empty string when '_nss' is non-none, and contains the database name when '_uuid' is
// non-none. Although the UUID specifies a collection uniquely, we must later verify that the
// collection belongs to the database named here.
std::string _dbname;
};
std::ostream& operator<<(std::ostream& stream, const NamespaceString& nss);
std::ostream& operator<<(std::ostream& stream, const NamespaceStringOrUUID& nsOrUUID);
/**
* "database.a.b.c" -> "database"
*/
inline StringData nsToDatabaseSubstring(StringData ns) {
size_t i = ns.find('.');
if (i == std::string::npos) {
massert(10078, "nsToDatabase: db too long", ns.size() < MaxDatabaseNameLen);
return ns;
}
massert(10088, "nsToDatabase: db too long", i < static_cast<size_t>(MaxDatabaseNameLen));
return ns.substr(0, i);
}
/**
* "database.a.b.c" -> "database"
*
* TODO: make this return a StringData
*/
inline std::string nsToDatabase(StringData ns) {
return nsToDatabaseSubstring(ns).toString();
}
/**
* "database.a.b.c" -> "a.b.c"
*/
inline StringData nsToCollectionSubstring(StringData ns) {
size_t i = ns.find('.');
massert(16886, "nsToCollectionSubstring: no .", i != std::string::npos);
return ns.substr(i + 1);
}
/**
* foo = false
* foo. = false
* foo.a = true
*/
inline bool nsIsFull(StringData ns) {
size_t i = ns.find('.');
if (i == std::string::npos)
return false;
if (i == ns.size() - 1)
return false;
return true;
}
/**
* foo = true
* foo. = false
* foo.a = false
*/
inline bool nsIsDbOnly(StringData ns) {
size_t i = ns.find('.');
if (i == std::string::npos)
return true;
return false;
}
inline bool NamespaceString::validDBName(StringData db, DollarInDbNameBehavior behavior) {
if (db.size() == 0 || db.size() >= 64)
return false;
for (StringData::const_iterator iter = db.begin(), end = db.end(); iter != end; ++iter) {
switch (*iter) {
case '\0':
case '/':
case '\\':
case '.':
case ' ':
case '"':
return false;
case '$':
if (behavior == DollarInDbNameBehavior::Disallow)
return false;
continue;
#ifdef _WIN32
// We prohibit all FAT32-disallowed characters on Windows
case '*':
case '<':
case '>':
case ':':
case '|':
case '?':
return false;
#endif
default:
continue;
}
}
return true;
}
inline bool NamespaceString::validCollectionComponent(StringData ns) {
size_t idx = ns.find('.');
if (idx == std::string::npos)
return false;
return validCollectionName(ns.substr(idx + 1)) || oplog(ns);
}
inline bool NamespaceString::validCollectionName(StringData coll) {
if (coll.empty())
return false;
if (coll[0] == '.')
return false;
for (StringData::const_iterator iter = coll.begin(), end = coll.end(); iter != end; ++iter) {
switch (*iter) {
case '\0':
case '$':
return false;
default:
continue;
}
}
return true;
}
} // namespace mongo
MONGO_HASH_NAMESPACE_START
template <>
struct hash<mongo::NamespaceString> {
size_t operator()(const mongo::NamespaceString& nss) const {
mongo::NamespaceString::Hasher hasher;
return hasher(nss);
}
};
MONGO_HASH_NAMESPACE_END
|