From ecda90ce4ac91aea66f076aacbbe28f7286a7351 Mon Sep 17 00:00:00 2001 From: Jason Rassi Date: Sat, 15 Feb 2014 11:40:13 -0500 Subject: SERVER-12175 Add 2dsphere index option "2dsphereIndexVersion" - Defines version 1 and 2; both are supported. - Indexes without this option are assumed to be version 1. - New builds get version 2 (if version unspecified). - Version 1 throws in getKeys if the geometry is one of the types introduced since 2.4.0 (MultiPoint, MultiLineString, MultiPolygon, GeometryCollection). --- src/mongo/db/index/s2_access_method.cpp | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/mongo/db/index/s2_access_method.cpp') diff --git a/src/mongo/db/index/s2_access_method.cpp b/src/mongo/db/index/s2_access_method.cpp index 328c7031550..fff6fbba7f3 100644 --- a/src/mongo/db/index/s2_access_method.cpp +++ b/src/mongo/db/index/s2_access_method.cpp @@ -38,6 +38,8 @@ #include "mongo/db/jsobj.h" namespace mongo { + static const string kIndexVersionFieldName("2dsphereIndexVersion"); + static int configValueWithDefault(const IndexDescriptor *desc, const string& name, int def) { BSONElement e = desc->getInfoElement(name); if (e.isNumber()) { return e.numberInt(); } @@ -60,10 +62,21 @@ namespace mongo { S2::kAvgEdge.GetClosestLevel(500.0 / _params.radius)); _params.coarsestIndexedLevel = configValueWithDefault(descriptor, "coarsestIndexedLevel", S2::kAvgEdge.GetClosestLevel(100 * 1000.0 / _params.radius)); + // Determine which version of this index we're using. If none was set in the descriptor, + // assume S2_INDEX_VERSION_1 (alas, the first version predates the existence of the version + // field). + _params.indexVersion = static_cast(configValueWithDefault( + descriptor, kIndexVersionFieldName, S2_INDEX_VERSION_1)); uassert(16747, "coarsestIndexedLevel must be >= 0", _params.coarsestIndexedLevel >= 0); uassert(16748, "finestIndexedLevel must be <= 30", _params.finestIndexedLevel <= 30); uassert(16749, "finestIndexedLevel must be >= coarsestIndexedLevel", _params.finestIndexedLevel >= _params.coarsestIndexedLevel); + massert(17395, + str::stream() << "unsupported geo index version { " << kIndexVersionFieldName + << " : " << _params.indexVersion << " }, only support versions: [" + << S2_INDEX_VERSION_1 << "," << S2_INDEX_VERSION_2 << "]", + _params.indexVersion == S2_INDEX_VERSION_2 + || _params.indexVersion == S2_INDEX_VERSION_1); int geoFields = 0; @@ -85,6 +98,29 @@ namespace mongo { geoFields >= 1); } + // static + BSONObj S2AccessMethod::fixSpec(const BSONObj& specObj) { + // If the spec object has the field "2dsphereIndexVersion", validate it. If it doesn't, add + // {2dsphereIndexVersion: 2}, which is the default for newly-built indexes. + + BSONElement indexVersionElt = specObj[kIndexVersionFieldName]; + if (indexVersionElt.eoo()) { + BSONObjBuilder bob; + bob.appendElements(specObj); + bob.append(kIndexVersionFieldName, S2_INDEX_VERSION_2); + return bob.obj(); + } + + const int indexVersion = indexVersionElt.numberInt(); + uassert(17394, + str::stream() << "unsupported geo index version { " << kIndexVersionFieldName + << " : " << indexVersionElt << " }, only support versions: [" + << S2_INDEX_VERSION_1 << "," << S2_INDEX_VERSION_2 << "]", + indexVersionElt.isNumber() && (indexVersion == S2_INDEX_VERSION_2 + || indexVersion == S2_INDEX_VERSION_1)); + return specObj; + } + void S2AccessMethod::getKeys(const BSONObj& obj, BSONObjSet* keys) { BSONObjSet keysToAdd; // We output keys in the same order as the fields we index. -- cgit v1.2.1