-
Notifications
You must be signed in to change notification settings - Fork 13
/
MapReduceIndex.cc
445 lines (376 loc) · 16.2 KB
/
MapReduceIndex.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//
// MapReduceIndex.cc
// CBForest
//
// Created by Jens Alfke on 5/15/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 "MapReduceIndex.hh"
#include "Collatable.hh"
#include "GeoIndex.hh"
#include "Tokenizer.hh"
#include "LogInternal.hh"
#include <algorithm>
namespace cbforest {
static int64_t kMinFormatVersion = 4;
static int64_t kCurFormatVersion = 5;
MapReduceIndex::MapReduceIndex(Database* db, std::string name, Database *sourceDatabase)
:Index(db, name),
_sourceDatabase(sourceDatabase)
{
readState();
}
void MapReduceIndex::readState() {
CollatableBuilder stateKey;
stateKey.addNull();
Document state = _store.get(stateKey);
CollatableReader reader(state.body());
if (reader.peekTag() == CollatableReader::kArray) {
reader.beginArray();
_lastSequenceIndexed = reader.readInt();
_lastSequenceChangedAt = reader.readInt();
_lastMapVersion = std::string(reader.readString());
_indexType = (int)reader.readInt();
_rowCount = (uint64_t)reader.readInt();
if (reader.peekTag() == CollatableTypes::kEndSequence
|| reader.readInt() < kMinFormatVersion) {
// Obsolete index version
deleted();
_indexType = 0;
}
if (reader.peekTag() != CollatableTypes::kEndSequence)
_lastPurgeCount = (uint64_t)reader.readInt();
}
Debug("MapReduceIndex<%p>: Read state (lastSeq=%lld, lastChanged=%lld, lastMapVersion='%s', indexType=%d, rowCount=%d, lastPurgeCount=%llu)",
this, _lastSequenceIndexed, _lastSequenceChangedAt, _lastMapVersion.c_str(), _indexType, _rowCount, _lastPurgeCount);
}
void MapReduceIndex::saveState(Transaction& t) {
CBFAssert(t.database()->contains(_store));
_lastMapVersion = _mapVersion;
CollatableBuilder stateKey;
stateKey.addNull();
CollatableBuilder state;
state.beginArray();
state << _lastSequenceIndexed << _lastSequenceChangedAt << _lastMapVersion << _indexType
<< _rowCount << kCurFormatVersion << _lastPurgeCount;
state.endArray();
_stateReadAt = t(_store).set(stateKey, state);
Debug("MapReduceIndex<%p>: Saved state (lastSeq=%lld, lastChanged=%lld, lastMapVersion='%s', indexType=%d, rowCount=%d, lastPurgeCount=%llu)",
this, _lastSequenceIndexed, _lastSequenceChangedAt, _lastMapVersion.c_str(), _indexType, _rowCount, _lastPurgeCount);
}
void MapReduceIndex::deleted() {
_lastSequenceIndexed = 0;
_lastSequenceChangedAt = 0;
_lastMapVersion = "";
_lastPurgeCount = 0;
_stateReadAt = 0;
_rowCount = 0;
}
sequence MapReduceIndex::lastSequenceIndexed() const {
const_cast<MapReduceIndex*>(this)->readState();
return _lastSequenceIndexed;
}
sequence MapReduceIndex::lastSequenceChangedAt() const {
const_cast<MapReduceIndex*>(this)->readState();
return _lastSequenceChangedAt;
}
uint64_t MapReduceIndex::rowCount() const {
const_cast<MapReduceIndex*>(this)->readState();
return _rowCount;
}
// Checks the index's saved purgeCount against the db's current purgeCount;
// if they don't match, the index is invalidated (erased).
bool MapReduceIndex::checkForPurge() {
readState();
auto dbPurgeCount = _sourceDatabase->purgeCount();
if (dbPurgeCount == _lastPurgeCount)
return false;
invalidate();
_lastPurgeCount = dbPurgeCount;
return true;
}
void MapReduceIndex::setup(int indexType, std::string mapVersion) {
Debug("MapReduceIndex<%p>: Setup (indexType=%ld, mapVersion='%s')",
this, indexType, mapVersion.c_str());
readState();
_mapVersion = mapVersion;
if (indexType != _indexType || mapVersion != _lastMapVersion) {
_indexType = indexType;
invalidate();
}
}
void MapReduceIndex::invalidate() {
if (_lastSequenceIndexed > 0) {
Debug("MapReduceIndex: Erasing invalidated index");
_store.erase();
}
_lastSequenceIndexed = _lastSequenceChangedAt = _lastPurgeCount = 0;
_rowCount = 0;
_stateReadAt = 0;
}
void MapReduceIndex::erase() {
Debug("MapReduceIndex: Erasing");
_store.erase();
_lastSequenceIndexed = _lastSequenceChangedAt = _lastPurgeCount = 0;
_rowCount = 0;
_stateReadAt = 0;
}
alloc_slice MapReduceIndex::getSpecialEntry(slice docID, sequence seq, unsigned entryID) const
{
// This data was written by emitter::emitTextTokens, below
CollatableBuilder key;
key.addNull();
return getEntry(docID, seq, key, entryID);
}
alloc_slice MapReduceIndex::readFullText(slice docID, sequence seq, unsigned fullTextID) const {
alloc_slice entry = getSpecialEntry(docID, seq, fullTextID);
CollatableReader reader(entry);
reader.beginArray();
return reader.readString();
}
alloc_slice MapReduceIndex::readFullTextValue(slice docID, sequence seq, unsigned fullTextID) const {
// This data was written by emitter::emitTextTokens, below
alloc_slice entry = getSpecialEntry(docID, seq, fullTextID);
CollatableReader reader(entry);
reader.beginArray();
(void)reader.read(); // skip text
if (reader.peekTag() == Collatable::kEndSequence)
return alloc_slice();
return alloc_slice(reader.readString());
}
void MapReduceIndex::readGeoArea(slice docID, sequence seq, unsigned geoID,
geohash::area &outArea,
alloc_slice& outGeoJSON,
alloc_slice& outValue)
{
// Reads data written by emitter::emit(const geohash::area&,...), below
alloc_slice entry = getSpecialEntry(docID, seq, geoID);
CollatableReader reader(entry);
reader.beginArray();
outArea = ::cbforest::readGeoArea(reader);
outGeoJSON = outValue = slice::null;
if (reader.peekTag() != CollatableReader::kEndSequence) {
if (reader.peekTag() == CollatableReader::kString)
outGeoJSON = alloc_slice(reader.readString());
else
(void)reader.read();
if (reader.peekTag() != CollatableReader::kEndSequence)
outValue = alloc_slice(reader.readString());
}
}
#pragma mark - EMITTER:
// Collects key/value pairs being emitted
class Emitter {
public:
std::vector<Collatable> keys;
std::vector<alloc_slice> values;
void emit(Collatable key, alloc_slice value) {
CollatableReader keyReader(key);
switch (keyReader.peekTag()) {
case CollatableTypes::kFullTextKey: {
auto textAndLang = keyReader.readFullTextKey();
emitTextTokens(textAndLang.first, std::string(textAndLang.second), value);
break;
}
case CollatableTypes::kGeoJSONKey: {
geohash::area bbox;
alloc_slice geoJSON = keyReader.readGeoKey(bbox);
emit(bbox, geoJSON, value);
break;
}
default:
_emit(key, value);
break;
}
}
void reset() {
keys.clear();
values.clear();
// _tokenizer is stateless
}
private:
void _emit(Collatable key, alloc_slice value) {
keys.push_back(key);
values.push_back(value);
}
void emitTextTokens(slice text, std::string languageCode, slice value) {
if (!_tokenizer || _tokenizer->stemmer() != languageCode) {
_tokenizer = std::unique_ptr<Tokenizer> {
new Tokenizer(languageCode, (languageCode == "en")) };
}
std::unordered_map<std::string, CollatableBuilder> tokens;
int specialKey = -1;
for (TokenIterator i(*_tokenizer, slice(text), false); i; ++i) {
if (specialKey < 0) {
// Emit the full text being indexed, and the value, under a special key.
specialKey = emitSpecial(text, value);
}
// Add the word position to the value array for this token:
CollatableBuilder& tokValue = tokens[i.token()];
if (tokValue.empty()) {
tokValue.beginArray();
tokValue << specialKey;
}
tokValue << i.wordOffset() << i.wordLength();
}
// Emit each token string and value array as a key:
for (auto kv = tokens.begin(); kv != tokens.end(); ++kv) {
CollatableBuilder collKey(kv->first);
CollatableBuilder& collValue = kv->second;
collValue.endArray();
_emit(collKey, collValue);
}
}
static const unsigned kMaxCoveringHashes = 4;
void emit(const geohash::area& boundingBox, slice geoJSON, slice value) {
Debug("emit {%g ... %g, %g ... %g}",
boundingBox.latitude.min, boundingBox.latitude.max,
boundingBox.longitude.min, boundingBox.longitude.max);
// Emit the bbox, geoJSON, and value, under a special key:
unsigned specialKey = emitSpecial(boundingBox, geoJSON, value);
CollatableBuilder collValue(specialKey);
// Now emit a set of geohashes that cover the given area:
auto hashes = boundingBox.coveringHashes();
for (auto iHash = hashes.begin(); iHash != hashes.end(); ++iHash) {
Debug(" hash='%s'", (const char*)(*iHash));
CollatableBuilder collKey(*iHash);
_emit(collKey, collValue);
}
}
// Saves a special key-value pair in the index that can store auxiliary data associated
// with an emit, such as the full text or the geo-JSON. This data is read by
// MapReduceIndex::getSpecialEntry
template <typename KEY>
unsigned emitSpecial(const KEY &key, slice value1, slice value2 = slice::null) {
CollatableBuilder collKey;
collKey.addNull();
CollatableBuilder collValue;
collValue.beginArray();
collValue << key;
// Write value1 (or a null placeholder) then value2
if (value1.size > 0 || value2.size > 0) {
if (value1.size > 0)
collValue << value1;
else
collValue.addNull();
if (value2.size > 0)
collValue << value2;
}
collValue.endArray();
auto result = keys.size();
emit(collKey, collValue.extractOutput());
return (unsigned)result;
}
std::unique_ptr<Tokenizer> _tokenizer;
};
#pragma mark - INDEX WRITER:
// In charge of updating one view's index. Owned by a MapReduceIndexer.
class MapReduceIndexWriter: IndexWriter {
public:
MapReduceIndexWriter(MapReduceIndex *idx, Transaction *t)
:IndexWriter(idx, *t),
index(idx),
_documentType(index->documentType()),
_transaction(t)
{ }
MapReduceIndex* const index;
bool shouldIndexDocument(const Document& doc) const {
return doc.sequence() > index->_lastSequenceIndexed;
}
bool shouldIndexDocumentType(slice docType) {
return _documentType.buf == NULL || _documentType == docType;
}
// Writes the given rows to the index.
bool indexDocument(slice docID,
sequence docSequence,
const std::vector<Collatable> &keys,
const std::vector<alloc_slice> &values)
{
if (docSequence <= index->_lastSequenceIndexed)
return false;
_emitter.reset();
for (unsigned i = 0; i < keys.size(); ++i)
_emitter.emit(keys[i], values[i]);
index->_lastSequenceIndexed = docSequence;
if (update(docID, docSequence, _emitter.keys, _emitter.values, index->_rowCount)) {
index->_lastSequenceChangedAt = index->_lastSequenceIndexed;
return true;
}
return false;
}
void finish(bool success) {
if (success)
index->saveState(*_transaction);
else
_transaction->abort();
}
private:
alloc_slice const _documentType;
Emitter _emitter;
std::unique_ptr<Transaction> _transaction;
};
#pragma mark - MAP-REDUCE INDEXER
void MapReduceIndexer::addIndex(MapReduceIndex* index) {
CBFAssert(index);
index->checkForPurge(); // has to be called before creating the transaction
auto writer = new MapReduceIndexWriter(index, new Transaction(index->database()));
_writers.push_back(writer);
if (index->documentType().buf)
_docTypes.insert(index->documentType());
else
_allDocTypes = true;
}
sequence MapReduceIndexer::startingSequence() {
_latestDbSequence = _writers[0]->index->sourceStore().lastSequence();
// First find the minimum sequence that not all indexes have indexed yet.
sequence startSequence = _latestDbSequence+1;
for (auto writer = _writers.begin(); writer != _writers.end(); ++writer) {
sequence lastSequence = (*writer)->index->lastSequenceIndexed();
if (lastSequence < _latestDbSequence) {
startSequence = std::min(startSequence, lastSequence+1);
} else if ((*writer)->index == _triggerIndex) {
return UINT64_MAX; // The trigger index doesn't need to be updated, so abort
}
}
if (startSequence > _latestDbSequence)
startSequence = UINT64_MAX; // no updating needed
return startSequence;
}
std::set<slice>* MapReduceIndexer::documentTypes() {
return _allDocTypes ? NULL : &_docTypes;
}
MapReduceIndexer::~MapReduceIndexer() {
for (auto writer = _writers.begin(); writer != _writers.end(); ++writer) {
(*writer)->finish(_finished);
delete *writer;
}
}
bool MapReduceIndexer::shouldMapDocIntoView(const Document &doc, unsigned viewNumber) {
return _writers[viewNumber]->shouldIndexDocument(doc);
}
bool MapReduceIndexer::shouldMapDocTypeIntoView(slice docType, unsigned viewNumber) {
return _writers[viewNumber]->shouldIndexDocumentType(docType);
}
void MapReduceIndexer::emitDocIntoView(slice docID,
sequence docSequence,
unsigned viewNumber,
const std::vector<Collatable> &keys,
const std::vector<alloc_slice> &values)
{
_writers[viewNumber]->indexDocument(docID, docSequence, keys, values);
}
void MapReduceIndexer::skipDoc(slice docID, sequence docSequence) {
for (auto i = _writers.begin(); i != _writers.end(); ++i)
(*i)->indexDocument(docID, docSequence, _noKeys, _noValues);
}
void MapReduceIndexer::skipDocInView(slice docID, sequence docSequence, unsigned viewNumber) {
_writers[viewNumber]->indexDocument(docID, docSequence, _noKeys, _noValues);
}
}