-
Notifications
You must be signed in to change notification settings - Fork 13
/
Index.cc
376 lines (326 loc) · 13.1 KB
/
Index.cc
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// Index.cc
// CBForest
//
// Created by Jens Alfke on 5/14/14.
// Copyright (c) 2014 Couchbase. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
#include "Index.hh"
#include "Collatable.hh"
#include "varint.hh"
#include "LogInternal.hh"
namespace cbforest {
const slice Index::kSpecialValue("*", 1);
bool KeyRange::isKeyPastEnd(slice key) const {
return inclusiveEnd ? (key > end) : !(key < end);
}
Index::Index(Database* db, std::string name)
:_store(db->getKeyStore(name)),
_indexDB(db),
_userCount(0)
{ }
Index::~Index() {
if (isBusy()) Warn("Index %p being destructed during enumeration", this);
}
IndexWriter::IndexWriter(Index* index, Transaction& t)
:KeyStoreWriter(index->_store, t),
_index(index)
{
CBFDebugAssert(t.database()->contains(index->_store));
index->addUser();
}
IndexWriter::~IndexWriter() {
_index->removeUser();
}
// djb2 hash function:
static const uint32_t kInitialHash = 5381;
static inline void addHash(uint32_t &hash, slice value) {
for (auto i = 0; i < value.size; ++i)
hash = ((hash << 5) + hash) + value[i];
}
void IndexWriter::getKeysForDoc(slice docID, std::vector<Collatable> &keys, uint32_t &hash) {
Document doc = get(docID);
if (doc.body().size > 0) {
CollatableReader reader(doc.body());
hash = (uint32_t)reader.readInt();
while (!reader.atEnd()) {
keys.push_back( Collatable::withData(reader.read()) );
}
} else {
hash = kInitialHash;
}
}
void IndexWriter::setKeysForDoc(slice docID, const std::vector<Collatable> &keys, uint32_t hash) {
if (keys.size() > 0) {
CollatableBuilder writer;
writer << hash;
for (auto i=keys.begin(); i != keys.end(); ++i)
writer << *i;
set(docID, writer);
} else {
del(docID);
}
}
bool IndexWriter::update(slice docID, sequence docSequence,
const std::vector<Collatable> &keys,
const std::vector<alloc_slice> &values,
uint64_t &rowCount)
{
CollatableBuilder collatableDocID;
collatableDocID << docID;
// Metadata of emitted rows contains doc sequence as varint:
uint8_t metaBuf[10];
slice meta(metaBuf, PutUVarInt(metaBuf, docSequence));
// Get the previously emitted keys:
std::vector<Collatable> oldStoredKeys, newStoredKeys;
uint32_t oldStoredHash;
getKeysForDoc(collatableDocID, oldStoredKeys, oldStoredHash);
// Compute a hash of the values and see whether it's the same as the previous values' hash:
uint32_t newStoredHash = kInitialHash;
for (auto value = values.begin(); value != values.end(); ++value) {
if (*value == Index::kSpecialValue) {
// kSpecialValue is placeholder for entire doc, and always considered changed.
oldStoredHash = newStoredHash - 1; // force comparison to fail
break;
}
addHash(newStoredHash, *value);
}
bool valuesMightBeUnchanged = (newStoredHash == oldStoredHash);
bool keysChanged = false;
int64_t rowsRemoved = 0, rowsAdded = 0;
auto value = values.begin();
unsigned emitIndex = 0;
auto oldKey = oldStoredKeys.begin();
for (auto key = keys.begin(); key != keys.end(); ++key,++value,++emitIndex) {
// Create a key for the index db by combining the emitted key, doc ID, and emit#:
CollatableBuilder realKey;
realKey.beginArray() << *key << collatableDocID;
if (emitIndex > 0)
realKey << emitIndex;
realKey.endArray();
if (realKey.size() > Document::kMaxKeyLength
|| value->size > Document::kMaxBodyLength) {
Warn("Index key or value too long"); //FIX: Need more-official warning
continue;
}
// Is this a key that was previously emitted last time we indexed this document?
if (keysChanged || oldKey == oldStoredKeys.end() || !(*oldKey == *key)) {
// no; note that the set of keys is different
keysChanged = true;
} else {
// yes
++oldKey;
if (valuesMightBeUnchanged) {
// read the old row so we can compare the value too:
Document oldRow = get(realKey);
if (oldRow.exists()) {
if (oldRow.body() == *value) {
Log("Old k/v pair (%s, %s) unchanged",
key->toJSON().c_str(), ((std::string)*value).c_str());
continue; // Value is unchanged, so this is a no-op; skip to next key!
}
} else {
Warn("Old emitted k/v pair unexpectedly missing");
}
}
++rowsRemoved; // more like "overwritten"
}
// Store the key & value:
Log("**** update: realKey = %s", realKey.toJSON().c_str());
set(realKey, meta, *value);
newStoredKeys.push_back(*key);
++rowsAdded;
}
// If there are any old keys that weren't emitted this time, we need to delete those rows:
for (; oldKey != oldStoredKeys.end(); ++oldKey) {
CollatableBuilder realKey;
realKey.beginArray() << *oldKey << collatableDocID;
auto oldEmitIndex = oldKey - oldStoredKeys.begin();
if (oldEmitIndex > 0)
realKey << oldEmitIndex;
realKey.endArray();
bool deleted = del(realKey);
if (!deleted) {
Warn("Failed to delete old emitted k/v pair");
}
++rowsRemoved;
keysChanged = true;
}
// Store the keys that were emitted for this doc, and the hash of the values:
if (keysChanged)
setKeysForDoc(collatableDocID, newStoredKeys, newStoredHash);
if (rowsRemoved==0 && rowsAdded==0)
return false;
rowCount += rowsAdded - rowsRemoved;
return true;
}
alloc_slice Index::getEntry(slice docID, sequence docSequence,
Collatable key, unsigned emitIndex) const {
CollatableBuilder collatableDocID;
collatableDocID << docID;
// realKey matches the key generated in update(), above
CollatableBuilder realKey;
realKey.beginArray();
realKey << key << collatableDocID;
if (emitIndex > 0)
realKey << emitIndex;
realKey.endArray();
Log("**** getEntry: realKey = %s", realKey.toJSON().c_str());
Document doc = _store.get(realKey);
CBFAssert(doc.exists());
return alloc_slice(doc.body());
}
#pragma mark - ENUMERATOR:
// Converts an index key into the actual key used in the index db (key + docID)
static Collatable makeRealKey(Collatable key, slice docID, bool isEnd, bool descending) {
bool addEllipsis = (isEnd != descending);
if (key.empty() && addEllipsis)
return Collatable();
CollatableBuilder realKey;
realKey.beginArray();
if (!key.empty()) {
realKey << key;
if (docID.buf)
realKey << docID;
}
if (addEllipsis) {
realKey.beginMap();
realKey.endMap();
}
realKey.endArray();
return realKey;
}
static DocEnumerator::Options docOptions(DocEnumerator::Options options) {
options.limit = DocEnumerator::Options::kDefault.limit;
options.skip = DocEnumerator::Options::kDefault.skip;
options.includeDeleted = false;
options.contentOptions = KeyStore::kDefaultContent; // read() method needs the doc bodies
return options;
}
IndexEnumerator::IndexEnumerator(Index* index,
Collatable startKey, slice startKeyDocID,
Collatable endKey, slice endKeyDocID,
const DocEnumerator::Options& options)
:_index(index),
_options(options),
_inclusiveStart(options.inclusiveStart),
_inclusiveEnd(options.inclusiveEnd),
_dbEnum(_index->_store,
(slice)makeRealKey(startKey, startKeyDocID, false, options.descending),
(slice)makeRealKey(endKey, endKeyDocID, true, options.descending),
docOptions(options))
{
Debug("IndexEnumerator(%p)", this);
index->addUser();
if (!_inclusiveStart)
_startKey = (slice)startKey;
if (!_inclusiveEnd)
_endKey = (slice)endKey;
}
IndexEnumerator::IndexEnumerator(Index* index,
std::vector<KeyRange> keyRanges,
const DocEnumerator::Options& options)
:_index(index),
_options(options),
_inclusiveStart(true),
_inclusiveEnd(true),
_keyRanges(keyRanges),
_dbEnum(_index->_store, slice::null, slice::null, docOptions(options))
{
Debug("IndexEnumerator(%p), key ranges:", this);
index->addUser();
for (auto i = _keyRanges.begin(); i != _keyRanges.end(); ++i)
Debug(" key range: %s -- %s (%d)", i->start.toJSON().c_str(), i->end.toJSON().c_str(), i->inclusiveEnd);
nextKeyRange();
}
bool IndexEnumerator::read() {
while(true) {
if (!_dbEnum) {
if (_currentKeyIndex < 0)
return false; // at end
else {
nextKeyRange();
if (_dbEnum.next())
continue;
else
return false;
}
}
const Document& doc = _dbEnum.doc();
// Decode the key from collatable form:
CollatableReader keyReader(doc.key());
keyReader.beginArray();
_key = keyReader.read();
if (!_inclusiveEnd && _key == _endKey) {
_dbEnum.close();
return false;
} else if (!_inclusiveStart && _key == _startKey) {
_dbEnum.next();
continue;
}
if (_currentKeyIndex >= 0 && _keyRanges[_currentKeyIndex].isKeyPastEnd(_key)) {
// While enumerating through _keys, advance to the next key:
nextKeyRange();
if (_dbEnum.next())
continue;
else
return false;
}
_docID = keyReader.readString();
GetUVarInt(doc.meta(), &_sequence);
_value = doc.body();
// Subclasses can ignore rows:
if (!this->approve(_key)) {
_dbEnum.next();
continue;
}
// OK, this is a candidate. First honor the skip and limit:
if (_options.skip > 0) {
--_options.skip;
_dbEnum.next();
continue;
}
if (_options.limit-- == 0) {
_dbEnum.close();
return false;
}
// Return it as the next row:
Debug("IndexEnumerator: found key=%s",
cbforest::CollatableReader(_key).toJSON().c_str());
return true;
}
}
std::vector<size_t> IndexEnumerator::getTextTokenInfo(unsigned &fullTextID) {
CollatableReader reader(value());
reader.beginArray();
fullTextID = (unsigned)reader.readInt();
std::vector<size_t> tokens;
do {
tokens.push_back((size_t)reader.readInt());
tokens.push_back((size_t)reader.readInt());
} while (reader.peekTag() != CollatableReader::kEndSequence);
return tokens;
}
void IndexEnumerator::nextKeyRange() {
if (++_currentKeyIndex >= _keyRanges.size()) {
_dbEnum.close();
return;
}
Collatable& startKey = _keyRanges[_currentKeyIndex].start;
Debug("IndexEnumerator: Advance to key '%s'", startKey.toJSON().c_str());
if (!_dbEnum)
_dbEnum = DocEnumerator(_index->_store, slice::null, slice::null, docOptions(_options));
_dbEnum.seek(makeRealKey(startKey, slice::null, false, _options.descending));
}
bool IndexEnumerator::next() {
_dbEnum.next();
return read();
}
}