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
|
// namespace.h
#pragma once
#include "../util/hashtab.h"
#include "../util/mmap.h"
class Cursor;
#pragma pack(push)
#pragma pack(1)
class Namespace {
public:
Namespace(const char *ns) {
*this = ns;
}
Namespace& operator=(const char *ns) {
memset(buf, 0, 128); /* this is just to keep stuff clean in the files for easy dumping and reading */
strcpy_s(buf, 128, ns); return *this;
}
void kill() {
buf[0] = 0x7f;
}
bool operator==(const Namespace& r) { return strcmp(buf, r.buf) == 0; }
int hash() const {
unsigned x = 0;
const char *p = buf;
while( *p ) {
x = x * 131 + *p;
p++;
}
return (x & 0x7fffffff) | 0x8000000; // must be > 0
}
char buf[128];
};
const int Buckets = 19;
const int MaxBucket = 18;
const int MaxIndexes = 10;
class IndexDetails {
public:
DiskLoc head; /* btree head */
/* index info object.
{ name:"nameofindex", ns:"parentnsname", key: {keypattobject} }
*/
DiskLoc info;
/* pull out the relevant key objects from obj, so we
can index them. Note that the set is multiple elements
only when it's a "multikey" array.
keys will be left empty if key not found in the object.
*/
void getKeysFromObject(JSObj& obj, set<JSObj>& keys);
// returns name of this index's storage area
// client.table.$index
string indexNamespace() {
JSObj io = info.obj();
string s;
s.reserve(128);
s = io.getStringField("ns");
assert( !s.empty() );
s += ".$";
s += io.getStringField("name");
return s;
}
};
extern int bucketSizes[];
class NamespaceDetails {
public:
NamespaceDetails() {
/* be sure to initialize new fields here, otherwise they will contain garbage by default.
*/
datasize = nrecords = 0;
lastExtentSize = 0;
nIndexes = 0;
capped = 0;
max = 0x7fffffff;
paddingFactor = 1.0;
memset(reserved, 0, sizeof(reserved));
}
DiskLoc firstExtent;
DiskLoc lastExtent;
DiskLoc deletedList[Buckets];
long long datasize;
long long nrecords;
int lastExtentSize;
int nIndexes;
IndexDetails indexes[MaxIndexes];
int capped;
int max; // max # of objects for a capped table.
double paddingFactor; // 1.0 = no padding.
char reserved[256-16-4-4-8*MaxIndexes-8-8-8];
void paddingFits() {
double x = paddingFactor - 0.01;
if( x >= 1.0 )
paddingFactor = x;
}
void paddingTooSmall() {
double x = paddingFactor + 0.6;
if( x <= 2.0 )
paddingFactor = x;
}
//returns offset in indexes[]
int findIndexByName(const char *name) {
for( int i = 0; i < nIndexes; i++ ) {
if( strcmp(indexes[i].info.obj().getStringField("name"),name) == 0 )
return i;
}
return -1;
}
/* return which "deleted bucket" for this size object */
static int bucket(int n) {
for( int i = 0; i < Buckets; i++ )
if( bucketSizes[i] > n )
return i;
return Buckets-1;
}
/* allocate a new record. lenToAlloc includes headers. */
DiskLoc alloc(const char *ns, int lenToAlloc, DiskLoc& extentLoc);
/* add a given record to the deleted chains for this NS */
void addDeletedRec(DeletedRecord *d, DiskLoc dloc);
void dumpDeleted(set<DiskLoc> *extents = 0);
private:
DiskLoc __stdAlloc(int len);
DiskLoc _alloc(const char *ns, int len);
void compact();
};
#pragma pack(pop)
class NamespaceIndex {
friend class NamespaceCursor;
public:
NamespaceIndex() { }
void init(const char *dir, const char *client) {
string path = dir;
path += client;
path += ".ns";
const int LEN = 16 * 1024 * 1024;
void *p = f.map(path.c_str(), LEN);
if( p == 0 ) {
problem() << "couldn't open namespace.idx " << path.c_str() << " terminating" << endl;
exit(-3);
}
ht = new HashTable<Namespace,NamespaceDetails>(p, LEN, "namespace index");
}
void add(const char *ns, DiskLoc& loc) {
Namespace n(ns);
NamespaceDetails details;
details.lastExtent = details.firstExtent = loc;
ht->put(n, details);
}
NamespaceDetails* details(const char *ns) {
Namespace n(ns);
return ht->get(n);
}
void kill(const char *ns) {
Namespace n(ns);
ht->kill(n);
}
bool find(const char *ns, DiskLoc& loc) {
NamespaceDetails *l = details(ns);
if( l ) {
loc = l->firstExtent;
return true;
}
return false;
}
private:
MemoryMappedFile f;
HashTable<Namespace,NamespaceDetails> *ht;
};
extern const char *dbpath;
/*
class NamespaceIndexMgr {
public:
NamespaceIndexMgr() { }
NamespaceIndex* get(const char *client) {
map<string,NamespaceIndex*>::iterator it = m.find(client);
if( it != m.end() )
return it->second;
NamespaceIndex *ni = new NamespaceIndex();
ni->init(dbpath, client);
m[client] = ni;
return ni;
}
private:
map<string,NamespaceIndex*> m;
};
extern NamespaceIndexMgr namespaceIndexMgr;
*/
// "client.a.b.c" -> "client"
inline void nsToClient(const char *ns, char *client) {
const char *p = ns;
char *q = client;
while( *p != '.' ) {
if( *p == 0 ) {
assert(false);
*client = 0;
return;
}
*q++ = *p++;
}
*q = 0;
assert(q-client<256);
}
/*
inline NamespaceIndex* nsindex(const char *ns) {
char client[256];
nsToClient(ns, client);
return namespaceIndexMgr.get(client);
}
inline NamespaceDetails* nsdetails(const char *ns) {
return nsindex(ns)->details(ns);
}
*/
//auto_ptr<Cursor> makeNamespaceCursor();
|