Storage Engine API
btree_ondisk.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include "mongo/base/static_assert.h"
32 #include "mongo/db/jsobj.h"
35 
36 namespace mongo {
37 
38 const int OldBucketSize = 8192;
39 
40 //
41 // On-disk index format
42 //
43 
44 #pragma pack(1)
45 
53 template <class LocType>
54 struct FixedWidthKey {
55  //
56  // Data
57  //
58 
63  LocType prevChildBucket;
64 
68  LocType recordLoc;
69 
73  unsigned short _kdo;
74 
75  //
76  // Accessors / mutators
77  //
78 
79  short keyDataOfs() const {
80  return static_cast<short>(_kdo);
81  }
82 
83  void setKeyDataOfs(short s) {
84  _kdo = s;
85  invariant(s >= 0);
86  }
87 
88  void setKeyDataOfsSavingUse(short s) {
89  // XXX kill this func
90  setKeyDataOfs(s);
91  }
92 
107  void setUnused() {
108  recordLoc.GETOFS() |= 1;
109  }
110 
111  void setUsed() {
112  recordLoc.GETOFS() &= ~1;
113  }
114 
115  int isUnused() const {
116  return recordLoc.getOfs() & 1;
117  }
118 
119  int isUsed() const {
120  return !isUnused();
121  }
122 };
123 
146 
151 
155  unsigned short _wasSize;
156 
160  unsigned short _reserved1;
161 
162  int flags;
163 
168 
170  int topSize;
171 
172  /* Number of keys in the bucket. */
173  int n;
174 
175  int reserved;
176 
177  /* Beginning of the bucket's body */
178  char data[4];
179 
180  // Precalculated size constants
181  enum { HeaderSize = 40 };
182 };
183 
184 // BtreeBucketV0 is part of the on-disk format, so it should never be changed
185 MONGO_STATIC_ASSERT(sizeof(BtreeBucketV0) - sizeof(static_cast<BtreeBucketV0*>(NULL)->data) ==
187 
191 struct DiskLoc56Bit {
192  //
193  // Data
194  //
195 
196  int ofs;
197 
198  unsigned char _a[3];
199 
200  //
201  // Accessors XXX rename these, this is terrible
202  //
203 
204  int& GETOFS() {
205  return ofs;
206  }
207 
208  int getOfs() const {
209  return ofs;
210  }
211 
212  //
213  // Comparison
214  //
215 
216  bool isNull() const {
217  return ofs < 0;
218  }
219 
220  unsigned long long toLongLong() const {
221  // endian
222  unsigned long long result = ofs;
223  char* cursor = reinterpret_cast<char*>(&result);
224  *reinterpret_cast<uint16_t*>(cursor + 4) = *reinterpret_cast<const uint16_t*>(&_a[0]);
225  *reinterpret_cast<uint8_t*>(cursor + 6) = *reinterpret_cast<const uint8_t*>(&_a[2]);
226  *reinterpret_cast<uint8_t*>(cursor + 7) = uint8_t(0);
227  return result;
228  }
229 
230  bool operator<(const DiskLoc56Bit& rhs) const {
231  // the orderering of dup keys in btrees isn't too critical, but we'd like to put items
232  // that are close together on disk close together in the tree, so we do want the file #
233  // to be the most significant bytes
234  return toLongLong() < rhs.toLongLong();
235  }
236 
237  int compare(const DiskLoc56Bit& rhs) const {
238  unsigned long long a = toLongLong();
239  unsigned long long b = rhs.toLongLong();
240  if (a < b) {
241  return -1;
242  } else {
243  return a == b ? 0 : 1;
244  }
245  }
246 
247  bool operator==(const DiskLoc56Bit& rhs) const {
248  return toLongLong() == rhs.toLongLong();
249  }
250 
251  bool operator!=(const DiskLoc56Bit& rhs) const {
252  return toLongLong() != rhs.toLongLong();
253  }
254 
255  bool operator==(const DiskLoc& rhs) const {
256  return DiskLoc(*this) == rhs;
257  }
258 
259  bool operator!=(const DiskLoc& rhs) const {
260  return !(*this == rhs);
261  }
262 
263  //
264  // Mutation
265  //
266 
267  enum {
268  OurNullOfs = -2, // first bit of offsets used in _KeyNode we don't use -1 here
269  OurMaxA = 0xffffff, // highest 3-byte value
270  };
271 
272  void Null() {
273  ofs = OurNullOfs;
274  _a[0] = _a[1] = _a[2] = 0;
275  }
276 
277  void operator=(const DiskLoc& loc);
278 
279  //
280  // Type Conversion
281  //
282 
283  RecordId toRecordId() const {
284  return DiskLoc(*this).toRecordId();
285  }
286 
287  operator DiskLoc() const {
288  // endian
289  if (isNull())
290  return DiskLoc();
291  unsigned a = *((unsigned*)(_a - 1));
292  return DiskLoc(a >> 8, ofs);
293  }
294 
295  std::string toString() const {
296  return DiskLoc(*this).toString();
297  }
298 };
299 
303 
306 
307  unsigned short flags;
308 
310  unsigned short emptySize;
311 
313  unsigned short topSize;
314 
315  /* Number of keys in the bucket. */
316  unsigned short n;
317 
318  /* Beginning of the bucket's body */
319  char data[4];
320 
321  // Precalculated size constants
322  enum { HeaderSize = 22 };
323 };
324 
325 // BtreeBucketV1 is part of the on-disk format, so it should never be changed
326 MONGO_STATIC_ASSERT(sizeof(BtreeBucketV1) - sizeof(static_cast<BtreeBucketV1*>(NULL)->data) ==
328 
329 enum Flags { Packed = 1 };
330 
333  typedef DiskLoc LocType;
334  typedef KeyBson KeyType;
337 
338  enum { BucketSize = 8192, BucketBodySize = BucketSize - BucketType::HeaderSize };
339 
340  // largest key size we allow. note we very much need to support bigger keys (somehow) in
341  // the future.
342 
343  static const int KeyMax = OldBucketSize / 10;
344 
345  // A sentinel value sometimes used to identify a deallocated bucket.
346  static const int INVALID_N_SENTINEL = -1;
347 
348  static void initBucket(BucketType* bucket) {
349  bucket->_reserved1 = 0;
350  bucket->_wasSize = BucketSize;
351  bucket->reserved = 0;
352  }
353 };
354 
357  typedef KeyV1 KeyType;
361 
362  enum {
363  BucketSize = 8192 - 16, // The -16 is to leave room for the MmapV1RecordHeader header
364  BucketBodySize = BucketSize - BucketType::HeaderSize
365  };
366 
367  static const int KeyMax = 1024;
368 
369  // A sentinel value sometimes used to identify a deallocated bucket.
370  static const unsigned short INVALID_N_SENTINEL = 0xffff;
371 
372  static void initBucket(BucketType* bucket) {}
373 };
374 
375 #pragma pack()
376 
377 } // namespace mongo
void setUsed()
Definition: btree_ondisk.h:111
int topSize
Size used for bson storage, including storage of old keys.
Definition: btree_ondisk.h:170
Definition: key.h:83
bool operator<(const DiskLoc56Bit &rhs) const
Definition: btree_ondisk.h:230
unsigned short _wasSize
Can be reused, value is 8192 in current pdfile version Apr2010.
Definition: btree_ondisk.h:155
unsigned short _reserved1
zero
Definition: btree_ondisk.h:160
int ofs
Definition: btree_interface.cpp:337
LocType prevChildBucket
The &#39;left&#39; child bucket of this key.
Definition: btree_ondisk.h:63
int ofs
Definition: btree_ondisk.h:196
int compare(const DiskLoc56Bit &rhs) const
Definition: btree_ondisk.h:237
Definition: btree_ondisk.h:331
FixedWidthKey< DiskLoc56Bit > FixedWidthKeyType
Definition: btree_ondisk.h:356
BtreeBucketV1 BucketType
Definition: btree_ondisk.h:360
Definition: btree_ondisk.h:329
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
LocType recordLoc
The location of the record associated with this key.
Definition: btree_ondisk.h:68
int n
Definition: btree_ondisk.h:173
int isUnused() const
Definition: btree_ondisk.h:115
RecordId toRecordId() const
Definition: btree_ondisk.h:283
void setUnused()
Unused keys are not returned by read operations.
Definition: btree_ondisk.h:107
void setKeyDataOfs(short s)
Definition: btree_ondisk.h:83
int emptySize
basicInsert() assumes the next three members are consecutive and in this order:
Definition: btree_ondisk.h:167
int reserved
Definition: btree_ondisk.h:175
unsigned long long toLongLong() const
Definition: btree_ondisk.h:220
BtreeBucketV0 BucketType
Definition: btree_ondisk.h:336
int & GETOFS()
Definition: btree_ondisk.h:204
KeyBson KeyType
Definition: btree_ondisk.h:334
KeyV1 KeyType
Definition: btree_ondisk.h:357
unsigned short flags
Definition: btree_ondisk.h:307
MONGO_STATIC_ASSERT(sizeof(void *)==sizeof(size_t))
FixedWidthKey< DiskLoc > FixedWidthKeyType
Definition: btree_ondisk.h:332
int isUsed() const
Definition: btree_ondisk.h:119
represents a disk location/offset on disk in a database.
Definition: diskloc.h:53
This structure represents header data for a btree bucket.
Definition: btree_ondisk.h:141
RecordId toRecordId() const
Definition: diskloc.h:185
unsigned short topSize
Size used for bson storage, including storage of old keys.
Definition: btree_ondisk.h:313
void setKeyDataOfsSavingUse(short s)
Definition: btree_ondisk.h:88
This is the fixed width data component for storage of a key within a bucket.
Definition: btree_ondisk.h:54
static void initBucket(BucketType *bucket)
Definition: btree_ondisk.h:372
DiskLoc56Bit nextChild
Given that there are n keys, this is the n index child.
Definition: btree_ondisk.h:305
const int OldBucketSize
Definition: btree_ondisk.h:38
std::shared_ptr< void > data
Definition: ephemeral_for_test_record_store_test.cpp:74
Key class for precomputing a small format index key that is denser than a traditional BSONObj...
Definition: key.h:44
short keyDataOfs() const
Definition: btree_ondisk.h:79
bool operator!=(const DiskLoc &rhs) const
Definition: btree_ondisk.h:259
A variant of DiskLoc Used by the V1 bucket type.
Definition: btree_ondisk.h:191
std::string toString() const
Definition: diskloc.h:119
DiskLoc bucket
Definition: btree_interface.cpp:336
unsigned short emptySize
Size of the empty region.
Definition: btree_ondisk.h:310
KeyV1Owned KeyOwnedType
Definition: btree_ondisk.h:358
bool isNull() const
Definition: btree_ondisk.h:216
Flags
Definition: btree_ondisk.h:329
Definition: btree_ondisk.h:300
DiskLoc nextChild
Given that there are n keys, this is the n index child.
Definition: btree_ondisk.h:150
int flags
Definition: btree_ondisk.h:162
Definition: btree_ondisk.h:355
unsigned short _kdo
Offset within current bucket of the variable width bson key for this _KeyNode.
Definition: btree_ondisk.h:73
bool operator==(const DiskLoc56Bit &rhs) const
Definition: btree_ondisk.h:247
unsigned short n
Definition: btree_ondisk.h:316
static void initBucket(BucketType *bucket)
Definition: btree_ondisk.h:348
Definition: btree_ondisk.h:181
DiskLoc LocType
Definition: btree_ondisk.h:333
std::string toString() const
Definition: btree_ondisk.h:295
KeyBson KeyOwnedType
Definition: btree_ondisk.h:335
DiskLoc56Bit parent
Parent bucket of this bucket, which isNull() for the root bucket.
Definition: btree_ondisk.h:302
bool operator!=(const DiskLoc56Bit &rhs) const
Definition: btree_ondisk.h:251
int getOfs() const
Definition: btree_ondisk.h:208
bool operator==(const DiskLoc &rhs) const
Definition: btree_ondisk.h:255
DiskLoc parent
Parent bucket of this bucket, which isNull() for the root bucket.
Definition: btree_ondisk.h:145
DiskLoc56Bit LocType
Definition: btree_ondisk.h:359
Definition: btree_ondisk.h:322
Definition: key.h:150
void Null()
Definition: btree_ondisk.h:272