Storage Engine API
namespace.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <boost/filesystem/path.hpp>
32 #include <cstring>
33 #include <string>
34 
35 #include "mongo/base/string_data.h"
36 #include "mongo/db/namespace_string.h"
37 #include "mongo/util/assert_util.h"
38 #include "mongo/util/mongoutils/str.h"
39 
40 namespace mongo {
41 
42 #pragma pack(1)
43 
49 class Namespace {
50 public:
51  Namespace(StringData ns) {
52  *this = ns;
53  }
54 
55  Namespace& operator=(StringData ns) {
56  // We fill the remaining space with all zeroes here. As the full Namespace struct is in the
57  // datafiles (the .ns files specifically), that is helpful as then they are deterministic in
58  // the bytes they have for a given sequence of operations. This makes testing and debugging
59  // the data files easier.
60  //
61  // If profiling indicates this method is a significant bottleneck, we could have a version
62  // we use for reads which does not fill with zeroes, and keep the zeroing behavior on
63  // writes.
64  memset(buf, 0, sizeof(buf));
65  uassert(10080,
66  str::stream() << "ns name " << ns << " (size: " << ns.size()
67  << ") too long, max size is 127 bytes",
68  ns.size() <= MaxNsLen);
69  uassert(
70  17380, "ns name can't contain embedded '\0' byte", ns.find('\0') == std::string::npos);
71  ns.copyTo(buf, true);
72  return *this;
73  }
74 
75  void kill() {
76  buf[0] = 0x7f;
77  }
78 
79  bool operator==(const char* r) const {
80  return strcmp(buf, r) == 0;
81  }
82  bool operator==(const Namespace& r) const {
83  return strcmp(buf, r.buf) == 0;
84  }
85  bool operator!=(const char* r) const {
86  return strcmp(buf, r) != 0;
87  }
88  bool operator!=(const Namespace& r) const {
89  return strcmp(buf, r.buf) != 0;
90  }
91 
92  bool hasDollarSign() const {
93  return strchr(buf, '$') != NULL;
94  }
95 
99  int hash() const {
100  unsigned x = 0;
101  const char* p = buf;
102  while (*p) {
103  x = x * 131 + *p;
104  p++;
105  }
106  return (x & 0x7fffffff) | 0x8000000; // must be > 0
107  }
108 
109  size_t size() const {
110  return strlen(buf);
111  }
112 
113  std::string toString() const {
114  return buf;
115  }
116  operator std::string() const {
117  return buf;
118  }
119 
125  std::string extraName(int i) const {
126  char ex[] = "$extra";
127  ex[5] += i;
128  const std::string s = std::string(buf) + ex;
129  massert(10348, "$extra: ns name too long", s.size() <= MaxNsLen);
130  return s;
131  }
132 
137  bool isExtra() const {
138  const char* p = strstr(buf, "$extr");
139  return p && p[5] &&
140  p[6] == 0; //== 0 is important in case an index uses name "$extra_1" for example
141  }
142 
144  // Maximum possible length of name any namespace, including special ones like $extra. This
145  // includes room for the NUL byte so it can be used when sizing buffers.
147 
148  // MaxNsLenWithNUL excluding the NUL byte. Use this when comparing std::string lengths.
150 
151  // Maximum allowed length of fully qualified namespace name of any real collection. Does not
152  // include NUL so it can be directly compared to std::string lengths.
153  MaxNsCollectionLen = MaxNsLen - 7 /*strlen(".$extra")*/,
154  };
155 
156 private:
158 };
159 #pragma pack()
160 
161 namespace {
162 MONGO_STATIC_ASSERT(sizeof(Namespace) == 128);
163 MONGO_STATIC_ASSERT(Namespace::MaxNsLenWithNUL == MaxDatabaseNameLen);
164 MONGO_STATIC_ASSERT((int)Namespace::MaxNsLenWithNUL == (int)NamespaceString::MaxNsLenWithNUL);
165 MONGO_STATIC_ASSERT((int)Namespace::MaxNsLen == (int)NamespaceString::MaxNsLen);
166 MONGO_STATIC_ASSERT((int)Namespace::MaxNsCollectionLen == (int)NamespaceString::MaxNsCollectionLen);
167 } // namespace
168 } // namespace mongo
bool operator!=(const Namespace &r) const
Definition: namespace.h:88
Collection *const const NamespaceString & ns
Definition: collection_info_cache_impl.cpp:53
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
void kill()
Definition: namespace.h:75
Namespace(StringData ns)
Definition: namespace.h:51
bool operator==(const Namespace &r) const
Definition: namespace.h:82
MONGO_STATIC_ASSERT(sizeof(void *)==sizeof(size_t))
std::string toString() const
Definition: namespace.h:113
Definition: namespace.h:153
size_t size() const
Definition: namespace.h:109
Definition: namespace.h:146
int hash() const
Value returned is always > 0.
Definition: namespace.h:99
MaxNsLenValue
Definition: namespace.h:143
bool operator!=(const char *r) const
Definition: namespace.h:85
This is used for storing a namespace on disk in a fixed witdh form and should only be used for that...
Definition: namespace.h:49
std::string extraName(int i) const
NamespaceDetails::Extra was added after fact to allow chaining of data blocks to support more than 10...
Definition: namespace.h:125
bool hasDollarSign() const
Definition: namespace.h:92
char buf[MaxNsLenWithNUL]
Definition: namespace.h:157
Namespace & operator=(StringData ns)
Definition: namespace.h:55
Definition: namespace.h:149
bool operator==(const char *r) const
Definition: namespace.h:79
bool isExtra() const
Returns whether the namespace ends with "$extr...".
Definition: namespace.h:137