Skip to content

Commit ef3d06f

Browse files
author
Hari Khalsa
committed
migrate fts/haystack + add/del logic SERVER-8791 SERVER-9164 SERVER-9165
1 parent 7797f45 commit ef3d06f

20 files changed

+685
-245
lines changed

src/mongo/SConscript

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,10 @@ serverOnlyFiles = [ "db/curop.cpp",
398398
"db/index/btree_index_cursor.cpp",
399399
"db/index/btree_interface.cpp",
400400
"db/index/btree_key_generator.cpp",
401+
"db/index/fts_access_method.cpp",
401402
"db/index/hash_access_method.cpp",
402403
"db/index/hash_index_cursor.cpp",
404+
"db/index/haystack_access_method.cpp",
403405
"db/index/s2_access_method.cpp",
404406
"db/index/s2_index_cursor.cpp",
405407
"db/index/s2_near_cursor.cpp",

src/mongo/db/btreecursor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ namespace mongo {
6666
_order = indexDetails.keyPattern();
6767
}
6868

69-
void BtreeCursor::init( const BSONObj& sk, const BSONObj& ek, bool endKeyInclusive, int direction ) {
69+
void BtreeCursor::init(const BSONObj& sk, const BSONObj& ek, bool endKeyInclusive,
70+
int direction ) {
7071
_finishConstructorInit();
7172
startKey = sk;
7273
endKey = ek;

src/mongo/db/geo/haystack.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// XXX THIS FILE IS DEPRECATED. PLEASE DON'T MODIFY.
12
// db/geo/haystack.cpp
23

34
/**
@@ -23,6 +24,10 @@
2324
#include "mongo/db/auth/action_set.h"
2425
#include "mongo/db/auth/action_type.h"
2526
#include "mongo/db/auth/privilege.h"
27+
#include "mongo/db/index/catalog_hack.h"
28+
#include "mongo/db/index/haystack_access_method.h"
29+
#include "mongo/db/index/index_descriptor.h"
30+
#include "mongo/db/index/index_access_method.h"
2631
#include "mongo/db/namespace-inl.h"
2732
#include "mongo/db/jsobj.h"
2833
#include "mongo/db/index.h"
@@ -343,13 +348,6 @@ namespace mongo {
343348
return false;
344349
}
345350

346-
int idxNum = idxs[0];
347-
348-
IndexDetails& id = nsd->idx(idxNum);
349-
GeoHaystackSearchIndex *si =
350-
static_cast<GeoHaystackSearchIndex*>(id.getSpec().getType());
351-
verify(&id == si->getDetails());
352-
353351
BSONElement nearElt = cmdObj["near"];
354352
BSONElement maxDistance = cmdObj["maxDistance"];
355353
BSONElement search = cmdObj["search"];
@@ -362,8 +360,20 @@ namespace mongo {
362360
if (cmdObj["limit"].isNumber())
363361
limit = static_cast<unsigned>(cmdObj["limit"].numberInt());
364362

365-
si->searchCommand(nsd, nearElt.Obj(), maxDistance.numberDouble(), search.Obj(),
366-
result, limit);
363+
int idxNum = idxs[0];
364+
IndexDetails& id = nsd->idx(idxNum);
365+
if (CatalogHack::testIndexMigration()) {
366+
auto_ptr<IndexDescriptor> desc(CatalogHack::getDescriptor(nsd, idxNum));
367+
auto_ptr<HaystackAccessMethod> ham(new HaystackAccessMethod(desc.get()));
368+
ham->searchCommand(nearElt.Obj(), maxDistance.numberDouble(), search.Obj(),
369+
&result, limit);
370+
} else {
371+
GeoHaystackSearchIndex *si =
372+
static_cast<GeoHaystackSearchIndex*>(id.getSpec().getType());
373+
verify(&id == si->getDetails());
374+
si->searchCommand(nsd, nearElt.Obj(), maxDistance.numberDouble(), search.Obj(),
375+
result, limit);
376+
}
367377
return 1;
368378
}
369379
} nameSearchCommand;

src/mongo/db/index/btree_access_method.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ namespace mongo {
5252
options.dupsAllowed, _descriptor->getOnDisk(), true);
5353
++*numInserted;
5454
} catch (AssertionException& e) {
55-
if (10287 == e.getCode() && options.dupsAllowed) {
56-
// Duplicate key, but our options say to ignore it.
55+
if (10287 == e.getCode() && _descriptor->isBackgroundIndex()) {
56+
// This is the duplicate key exception. We ignore it for some reason in BG
57+
// indexing.
5758
DEV log() << "info: key already in index during bg indexing (ok)\n";
5859
} else if (!options.dupsAllowed) {
5960
// Assuming it's a duplicate key exception. Clean up any inserted keys.
@@ -71,6 +72,10 @@ namespace mongo {
7172
}
7273
}
7374

75+
if (*numInserted > 1) {
76+
_descriptor->setMultikey();
77+
}
78+
7479
return ret;
7580
}
7681

@@ -161,8 +166,6 @@ namespace mongo {
161166
data->loc = record;
162167
data->dupsAllowed = options.dupsAllowed;
163168

164-
status->_isMultiKey = data->newKeys.size() > 1;
165-
166169
setDifference(data->oldKeys, data->newKeys, &data->removed);
167170
setDifference(data->newKeys, data->oldKeys, &data->added);
168171

@@ -193,6 +196,10 @@ namespace mongo {
193196
BtreeBasedPrivateUpdateData* data =
194197
static_cast<BtreeBasedPrivateUpdateData*>(ticket._indexSpecificUpdateData.get());
195198

199+
if (data->oldKeys.size() + data->added.size() - data->removed.size() > 1) {
200+
_descriptor->setMultikey();
201+
}
202+
196203
for (size_t i = 0; i < data->added.size(); ++i) {
197204
_interface->bt_insert(_descriptor->getHead(), data->loc, *data->added[i], _ordering,
198205
data->dupsAllowed, _descriptor->getOnDisk(), true);

src/mongo/db/index/catalog_hack.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616

1717
#include "mongo/db/index/btree_access_method.h"
18+
#include "mongo/db/index/fts_access_method.h"
1819
#include "mongo/db/index/hash_access_method.h"
20+
#include "mongo/db/index/haystack_access_method.h"
1921
#include "mongo/db/index/index_access_method.h"
2022
#include "mongo/db/index/index_descriptor.h"
2123
#include "mongo/db/index/s2_access_method.h"
@@ -35,15 +37,28 @@ namespace mongo {
3537

3638
static bool isIndexMigrated(const BSONObj& keyPattern) {
3739
string type = KeyPattern::findPluginName(keyPattern);
38-
return "hashed" == type || "2dsphere" == type;
40+
return "" == type || "fts" == type || "hashed" == type || "2dsphere" == type
41+
|| "geoHaystack" == type;
3942
}
4043

41-
static IndexAccessMethod* getSpecialIndex(IndexDescriptor* desc) {
44+
// If true, use EmulatedCursor + IndexCursor when answering "special" queries.
45+
static bool testCursorMigration() { return true; }
46+
47+
// If true, use IndexAccessMethod for insert/delete when possible.
48+
static bool testIndexMigration() { return true; }
49+
50+
static IndexAccessMethod* getIndex(IndexDescriptor* desc) {
4251
string type = KeyPattern::findPluginName(desc->keyPattern());
4352
if ("hashed" == type) {
4453
return new HashAccessMethod(desc);
4554
} else if ("2dsphere" == type) {
4655
return new S2AccessMethod(desc);
56+
} else if ("fts" == type) {
57+
return new FTSAccessMethod(desc);
58+
} else if ("geoHaystack" == type) {
59+
return new HaystackAccessMethod(desc);
60+
} else if ("" == type) {
61+
return new BtreeAccessMethod(desc);
4762
} else {
4863
verify(0);
4964
return NULL;

src/mongo/db/index/emulated_cursor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ namespace mongo {
5050
int numWanted,
5151
const BSONObj& keyPattern) {
5252

53-
EmulatedCursor* ret = new EmulatedCursor(descriptor, indexAccessMethod,
54-
order, numWanted, keyPattern);
53+
auto_ptr<EmulatedCursor> ret(new EmulatedCursor(descriptor, indexAccessMethod,
54+
order, numWanted, keyPattern));
5555
// Why do we have to do this? No reading from disk is allowed in constructors,
5656
// and seeking involves reading from disk.
5757
ret->seek(query);
58-
return ret;
58+
return ret.release();
5959
}
6060

6161
// We defer everything we can to the underlying cursor.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (C) 2013 10gen Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include "mongo/db/index/fts_access_method.h"
18+
#include "mongo/db/fts/fts_index_format.h"
19+
20+
namespace mongo {
21+
22+
FTSAccessMethod::FTSAccessMethod(IndexDescriptor* descriptor)
23+
: BtreeBasedAccessMethod(descriptor), _ftsSpec(descriptor->infoObj()) { }
24+
25+
void FTSAccessMethod::getKeys(const BSONObj& obj, BSONObjSet* keys) {
26+
fts::FTSIndexFormat::getKeys(_ftsSpec, obj, keys);
27+
}
28+
29+
Status FTSAccessMethod::newCursor(IndexCursor** out) {
30+
return Status::OK();
31+
}
32+
33+
} // namespace mongo
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (C) 2013 10gen Inc.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License, version 3,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU Affero General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Affero General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#pragma once
18+
19+
#include "mongo/base/status.h"
20+
#include "mongo/db/fts/fts_spec.h"
21+
#include "mongo/db/index/index_descriptor.h"
22+
#include "mongo/db/index/btree_access_method_internal.h"
23+
#include "mongo/db/jsobj.h"
24+
25+
namespace mongo {
26+
27+
class FTSAccessMethod : public BtreeBasedAccessMethod {
28+
public:
29+
FTSAccessMethod(IndexDescriptor* descriptor);
30+
virtual ~FTSAccessMethod() { }
31+
32+
// Not implemented:
33+
virtual Status newCursor(IndexCursor** out);
34+
35+
private:
36+
// Implemented:
37+
virtual void getKeys(const BSONObj& obj, BSONObjSet* keys);
38+
39+
fts::FTSSpec _ftsSpec;
40+
};
41+
42+
} //namespace mongo

src/mongo/db/index/hash_access_method.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace mongo {
3535
using BtreeBasedAccessMethod::_ordering;
3636

3737
HashAccessMethod(IndexDescriptor* descriptor);
38+
virtual ~HashAccessMethod() { }
3839

3940
virtual Status newCursor(IndexCursor** out);
4041

0 commit comments

Comments
 (0)