summaryrefslogtreecommitdiff
path: root/src/mongo/db
diff options
context:
space:
mode:
authorAlberto Lerner <alerner@10gen.com>2013-01-16 10:36:40 -0500
committerAlberto Lerner <alerner@10gen.com>2013-01-16 10:36:40 -0500
commitfd1bc4895b51dc65185ac5fa633bb0435860b936 (patch)
treef6594a3da5b2b975daced141c862544b98171def /src/mongo/db
parent45772b7c149307803a3b0052b2468e2b254e19be (diff)
downloadmongo-fd1bc4895b51dc65185ac5fa633bb0435860b936.tar.gz
SERVER-5710 Changed KeyPattern.hasField() sematics to consider dotted field prefixes.
Diffstat (limited to 'src/mongo/db')
-rw-r--r--src/mongo/db/SConscript15
-rw-r--r--src/mongo/db/field_ref.cpp (renamed from src/mongo/db/ops/field_ref.cpp)2
-rw-r--r--src/mongo/db/field_ref.h (renamed from src/mongo/db/ops/field_ref.h)0
-rw-r--r--src/mongo/db/field_ref_test.cpp (renamed from src/mongo/db/ops/field_ref_test.cpp)2
-rw-r--r--src/mongo/db/keypattern.cpp17
-rw-r--r--src/mongo/db/keypattern.h36
-rw-r--r--src/mongo/db/ops/SConscript9
-rw-r--r--src/mongo/db/ops/update_internal.cpp4
8 files changed, 66 insertions, 19 deletions
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
new file mode 100644
index 00000000000..2d3a4f65734
--- /dev/null
+++ b/src/mongo/db/SConscript
@@ -0,0 +1,15 @@
+# -*- mode: python -*-
+
+Import("env")
+
+#
+# We'd like 'common' to have the abstractions that are shared by several components of the
+# server. Ideally, many of the object in 'coredb' should be moved here when their dependencies
+# get resolved.
+#
+
+env.StaticLibrary('common', ['field_ref.cpp'],
+ LIBDEPS=['$BUILD_DIR/mongo/bson',
+ '$BUILD_DIR/mongo/foundation'])
+
+env.CppUnitTest('field_ref_test', ['field_ref_test.cpp'], LIBDEPS=['common'])
diff --git a/src/mongo/db/ops/field_ref.cpp b/src/mongo/db/field_ref.cpp
index f4dac8f9097..7b9f5631140 100644
--- a/src/mongo/db/ops/field_ref.cpp
+++ b/src/mongo/db/field_ref.cpp
@@ -14,7 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "mongo/db/ops/field_ref.h"
+#include "mongo/db/field_ref.h"
#include "mongo/util/assert_util.h"
namespace mongo {
diff --git a/src/mongo/db/ops/field_ref.h b/src/mongo/db/field_ref.h
index ea7b7dab34d..ea7b7dab34d 100644
--- a/src/mongo/db/ops/field_ref.h
+++ b/src/mongo/db/field_ref.h
diff --git a/src/mongo/db/ops/field_ref_test.cpp b/src/mongo/db/field_ref_test.cpp
index e6645acffc1..e84a9d1158a 100644
--- a/src/mongo/db/ops/field_ref_test.cpp
+++ b/src/mongo/db/field_ref_test.cpp
@@ -18,7 +18,7 @@
#include "mongo/base/error_codes.h"
#include "mongo/base/status.h"
#include "mongo/base/string_data.h"
-#include "mongo/db/ops/field_ref.h"
+#include "mongo/db/field_ref.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/mongoutils/str.h"
diff --git a/src/mongo/db/keypattern.cpp b/src/mongo/db/keypattern.cpp
index 5123048d816..89eca1f16c6 100644
--- a/src/mongo/db/keypattern.cpp
+++ b/src/mongo/db/keypattern.cpp
@@ -17,14 +17,29 @@
*/
#include "mongo/db/keypattern.h"
-#include "mongo/util/mongoutils/str.h"
+
#include "mongo/db/hasher.h"
#include "mongo/db/queryutil.h"
+#include "mongo/util/mongoutils/str.h"
using namespace mongoutils;
namespace mongo {
+ KeyPattern::KeyPattern( const BSONObj& pattern ): _pattern( pattern ) {
+
+ // Extract all prefixes of each field in pattern.
+ BSONForEach( field, _pattern ) {
+ StringData fieldName = field.fieldName();
+ size_t pos = fieldName.find( '.' );
+ while ( pos != string::npos ) {
+ _prefixes.insert( StringData( field.fieldName(), pos ) );
+ pos = fieldName.find( '.', pos+1 );
+ }
+ _prefixes.insert( fieldName );
+ }
+ }
+
BSONObj KeyPattern::extractSingleKey(const BSONObj& doc ) const {
if ( _pattern.isEmpty() )
return BSONObj();
diff --git a/src/mongo/db/keypattern.h b/src/mongo/db/keypattern.h
index fae49bb931a..a25ce2b87e5 100644
--- a/src/mongo/db/keypattern.h
+++ b/src/mongo/db/keypattern.h
@@ -20,6 +20,7 @@
#include "mongo/base/string_data.h"
#include "mongo/db/jsobj.h"
+#include "mongo/platform/unordered_set.h"
#include "mongo/util/mongoutils/str.h"
namespace mongo {
@@ -47,7 +48,10 @@ namespace mongo {
*/
class KeyPattern {
public:
- KeyPattern( const BSONObj& pattern ): _pattern( pattern ) {}
+ /*
+ * We are allowing implicit conversion from BSON
+ */
+ KeyPattern( const BSONObj& pattern );
/*
* Returns a BSON representation of this KeyPattern.
@@ -55,10 +59,12 @@ namespace mongo {
BSONObj toBSON() const { return _pattern; }
/*
- * Returns true if the given fieldname is the name of one element of the (potentially)
- * compound key described by this KeyPattern.
+ * Returns true if the given fieldname is the (dotted prefix of the) name of one
+ * element of the (potentially) compound key described by this KeyPattern.
*/
- bool hasField( const StringData& fieldname ) const { return _pattern.hasField( fieldname ); }
+ bool hasField( const StringData& fieldname ) const {
+ return _prefixes.find( fieldname ) != _prefixes.end();
+ }
/*
* Gets the element of this pattern corresponding to the given fieldname.
@@ -159,12 +165,33 @@ namespace mongo {
private:
BSONObj _pattern;
+
+ // Each field in the '_pattern' may be itself a dotted field. We store all the prefixes
+ // of each field here. For instance, if a pattern is { 'a.b.c': 1, x: 1 }, we'll store
+ // here 'a', 'a.b', 'a.b.c', and 'x'.
+ //
+ // Since we're indexing into '_pattern's field names, it must stay constant after
+ // constructed.
+ struct PrefixHasher {
+ size_t operator()( const StringData& strData ) const {
+ size_t result = 0;
+ const char* p = strData.rawData();
+ for (size_t len = strData.size(); len > 0; len-- ) {
+ result = ( result * 131 ) + *p++;
+ }
+ return result;
+ }
+ };
+ unordered_set<StringData, PrefixHasher> _prefixes;
+
bool isAscending( const BSONElement& fieldExpression ) const {
return ( fieldExpression.isNumber() && fieldExpression.numberInt() == 1 );
}
+
bool isDescending( const BSONElement& fieldExpression ) const {
return ( fieldExpression.isNumber() && fieldExpression.numberInt() == -1 );
}
+
bool isHashed( const BSONElement& fieldExpression ) const {
return mongoutils::str::equals( fieldExpression.valuestrsafe() , "hashed" );
}
@@ -178,5 +205,4 @@ namespace mongo {
};
-
} // namespace mongo
diff --git a/src/mongo/db/ops/SConscript b/src/mongo/db/ops/SConscript
deleted file mode 100644
index eb33dfb5680..00000000000
--- a/src/mongo/db/ops/SConscript
+++ /dev/null
@@ -1,9 +0,0 @@
-# -*- mode: python -*-
-
-Import("env")
-
-env.StaticLibrary('update', ['field_ref.cpp'],
- LIBDEPS=['$BUILD_DIR/mongo/bson',
- '$BUILD_DIR/mongo/foundation'])
-
-env.CppUnitTest('field_ref_test', ['field_ref_test.cpp'], LIBDEPS=['update'])
diff --git a/src/mongo/db/ops/update_internal.cpp b/src/mongo/db/ops/update_internal.cpp
index 0dfb7993ec1..5e6a2a7b32c 100644
--- a/src/mongo/db/ops/update_internal.cpp
+++ b/src/mongo/db/ops/update_internal.cpp
@@ -20,10 +20,10 @@
#include <algorithm> // for max
-#include "mongo/db/oplog.h"
-#include "mongo/db/ops/field_ref.h"
+#include "mongo/db/field_ref.h"
#include "mongo/db/jsobjmanipulator.h"
#include "mongo/db/pdfile.h"
+#include "mongo/db/oplog.h"
#include "mongo/util/mongoutils/str.h"
#include "update_internal.h"