Storage Engine API
key.h
Go to the documentation of this file.
1 // @file key.h class(es) representing individual keys in a btree
2 
31 #pragma once
32 
33 #include "mongo/db/jsobj.h"
34 #include "mongo/util/debug_util.h"
35 
36 namespace mongo {
37 
44 class KeyBson /* "KeyV0" */ {
45 public:
46  KeyBson() {}
47  explicit KeyBson(const char* keyData) : _o(keyData) {}
48  explicit KeyBson(const BSONObj& obj) : _o(obj) {}
49  int woCompare(const KeyBson& r, const Ordering& o) const;
50  BSONObj toBson() const {
51  return _o;
52  }
53  std::string toString() const {
54  return _o.toString();
55  }
56  int dataSize() const {
57  return _o.objsize();
58  }
59  const char* data() const {
60  return _o.objdata();
61  }
62  BSONElement _firstElement() const {
63  return _o.firstElement();
64  }
65  bool isCompactFormat() const {
66  return false;
67  }
68  bool woEqual(const KeyBson& r) const;
69  void assign(const KeyBson& rhs) {
70  *this = rhs;
71  }
72  bool isValid() const {
73  return true;
74  }
75 
76 private:
77  BSONObj _o;
78 };
79 
80 class KeyV1Owned;
81 
82 // corresponding to BtreeData_V1
83 class KeyV1 {
84  // disallowed just to make people be careful as we don't own the buffer
85  void operator=(const KeyV1&);
86  // disallowed as this is not a great idea as KeyV1Owned likely will go out of scope
87  KeyV1(const KeyV1Owned&);
88 
89 public:
90  KeyV1() {
91  _keyData = 0;
92  }
93  ~KeyV1() {
94  DEV _keyData = (const unsigned char*)1;
95  }
96 
97  KeyV1(const KeyV1& rhs) : _keyData(rhs._keyData) {
98  dassert(_keyData > (const unsigned char*)1);
99  }
100 
101  // explicit version of operator= to be safe
102  void assign(const KeyV1& rhs) {
103  _keyData = rhs._keyData;
104  }
105 
109  explicit KeyV1(const char* keyData) : _keyData((unsigned char*)keyData) {}
110 
111  int woCompare(const KeyV1& r, const Ordering& o) const;
112  bool woEqual(const KeyV1& r) const;
113  BSONObj toBson() const;
114  std::string toString() const {
115  return toBson().toString();
116  }
117 
119  const char* data() const {
120  return (const char*)_keyData;
121  }
122 
124  int dataSize() const;
125 
127  BSONElement _firstElement() const {
128  return bson().firstElement();
129  }
130  bool isCompactFormat() const {
131  return *_keyData != IsBSON;
132  }
133 
134  bool isValid() const {
135  return _keyData > (const unsigned char*)1;
136  }
137 
138 protected:
139  enum { IsBSON = 0xff };
140  const unsigned char* _keyData;
141  BSONObj bson() const {
142  dassert(!isCompactFormat());
143  return BSONObj((const char*)_keyData + 1);
144  }
145 
146 private:
147  int compareHybrid(const KeyV1& right, const Ordering& order) const;
148 };
149 
150 class KeyV1Owned : public KeyV1 {
151  void operator=(const KeyV1Owned&);
152 
153 public:
158  KeyV1Owned(const BSONObj& obj);
159 
161  KeyV1Owned(const KeyV1& rhs);
162 
163 private:
164  StackBufBuilder b;
165  void traditional(const BSONObj& obj); // store as traditional bson not as compact format
166 };
167 };
BSONObj bson() const
Definition: key.h:141
int dataSize() const
Definition: key.h:56
const char * data() const
get the key data we want to store in the btree bucket
Definition: key.h:119
bool isCompactFormat() const
Definition: key.h:130
Definition: key.h:83
~KeyV1()
Definition: key.h:93
std::string toString() const
Definition: key.h:114
Copyright (C) 2014 MongoDB Inc.
Definition: bson_collection_catalog_entry.cpp:38
BSONElement _firstElement() const
only used by geo, which always has bson keys
Definition: key.h:127
KeyBson(const char *keyData)
Definition: key.h:47
KeyBson()
Definition: key.h:46
KeyV1(const char *keyData)
Definition: key.h:109
bool isValid() const
Definition: key.h:72
KeyV1(const KeyV1 &rhs)
Definition: key.h:97
BSONObj toBson() const
Definition: key.h:50
bool isValid() const
Definition: key.h:134
BSONElement _firstElement() const
Definition: key.h:62
Key class for precomputing a small format index key that is denser than a traditional BSONObj...
Definition: key.h:44
void assign(const KeyBson &rhs)
Definition: key.h:69
KeyBson(const BSONObj &obj)
Definition: key.h:48
const char * data() const
Definition: key.h:59
bool isCompactFormat() const
Definition: key.h:65
const unsigned char * _keyData
Definition: key.h:140
int woCompare(const KeyBson &r, const Ordering &o) const
Definition: key.cpp:203
BSONObj _o
Definition: key.h:77
KeyV1()
Definition: key.h:90
StackBufBuilder b
Definition: key.h:164
void assign(const KeyV1 &rhs)
Definition: key.h:102
bool woEqual(const KeyBson &r) const
Definition: key.cpp:209
std::string toString() const
Definition: key.h:53
Definition: key.h:150