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
|
// @file key.h class(es) representing individual keys in a btree
/**
* Copyright (C) 2011 10gen 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/>.
*/
#pragma once
#include "jsobj.h"
namespace mongo {
/** Key class for precomputing a small format index key that is denser than a traditional BSONObj.
KeyBson is a legacy wrapper implementation for old BSONObj style keys for v:0 indexes.
KeyV1 is the new implementation.
*/
class KeyBson /* "KeyV0" */ {
public:
KeyBson() { }
explicit KeyBson(const char *keyData) : _o(keyData) { }
explicit KeyBson(const BSONObj& obj) : _o(obj) { }
int woCompare(const KeyBson& r, const Ordering &o) const;
BSONObj toBson() const { return _o; }
string toString() const { return _o.toString(); }
int dataSize() const { return _o.objsize(); }
const char * data() const { return _o.objdata(); }
BSONElement _firstElement() const { return _o.firstElement(); }
bool isCompactFormat() const { return false; }
bool woEqual(const KeyBson& r) const;
void assign(const KeyBson& rhs) { *this = rhs; }
private:
BSONObj _o;
};
class KeyV1Owned;
// corresponding to BtreeData_V1
class KeyV1 {
void operator=(const KeyV1&); // disallowed just to make people be careful as we don't own the buffer
KeyV1(const KeyV1Owned&); // disallowed as this is not a great idea as KeyV1Owned likely will go out of scope
public:
KeyV1() { _keyData = 0; }
~KeyV1() { DEV _keyData = (const unsigned char *) 1; }
KeyV1(const KeyV1& rhs) : _keyData(rhs._keyData) {
dassert( _keyData > (const unsigned char *) 1 );
}
// explicit version of operator= to be safe
void assign(const KeyV1& rhs) {
_keyData = rhs._keyData;
}
/** @param keyData can be a buffer containing data in either BSON format, OR in KeyV1 format.
when BSON, we are just a wrapper
*/
explicit KeyV1(const char *keyData) : _keyData((unsigned char *) keyData) { }
int woCompare(const KeyV1& r, const Ordering &o) const;
bool woEqual(const KeyV1& r) const;
BSONObj toBson() const;
string toString() const { return toBson().toString(); }
/** get the key data we want to store in the btree bucket */
const char * data() const { return (const char *) _keyData; }
/** @return size of data() */
int dataSize() const;
/** only used by geo, which always has bson keys */
BSONElement _firstElement() const { return bson().firstElement(); }
bool isCompactFormat() const { return *_keyData != IsBSON; }
protected:
enum { IsBSON = 0xff };
const unsigned char *_keyData;
BSONObj bson() const {
dassert( !isCompactFormat() );
return BSONObj((const char *) _keyData+1);
}
private:
int compareHybrid(const KeyV1& right, const Ordering& order) const;
};
class KeyV1Owned : public KeyV1 {
KeyV1Owned(const KeyV1Owned&); // not copyable -- StackBufBuilder is not copyable and that owns our buffer
void operator=(const KeyV1Owned&);
public:
/** @obj a BSON object to be translated to KeyV1 format. If the object isn't
representable in KeyV1 format (which happens, intentionally, at times)
it will stay as bson herein.
*/
KeyV1Owned(const BSONObj& obj);
private:
StackBufBuilder b;
void traditional(const BSONObj& obj); // store as traditional bson not as compact format
};
};
|