Skip to content
Browse files

some index cleaning

  • Loading branch information...
1 parent e33f6fc commit efbf3382cd76ef742e31a75e699c17864929723b @erh erh committed
Showing with 52 additions and 31 deletions.
  1. +28 −10 db/index.cpp
  2. +10 −20 db/index.h
  3. +1 −1 db/matcher.cpp
  4. +13 −0 jstests/indexf.js
View
38 db/index.cpp
@@ -55,7 +55,30 @@ namespace mongo {
wassert( n == 1 );
}
- void getKeys( vector< const char * > fieldNames, vector< BSONElement > fixed, const BSONObj &obj, BSONObjSetDefaultOrder &keys ) {
+ void IndexSpec::_init(){
+ assert( keys.objsize() );
+
+ BSONObjIterator i( keys );
+ BSONObjBuilder nullKeyB;
+ while( i.more() ) {
+ _fieldNames.push_back( i.next().fieldName() );
+ _fixed.push_back( BSONElement() );
+ nullKeyB.appendNull( "" );
+ }
+
+ _nullKey = nullKeyB.obj();
+ }
+
+
+ void IndexSpec::getKeys( const BSONObj &obj, BSONObjSetDefaultOrder &keys ) const {
+ vector<const char*> fieldNames( _fieldNames );
+ vector<BSONElement> fixed( _fixed );
+ _getKeys( fieldNames , fixed , obj, keys );
+ if ( keys.empty() )
+ keys.insert( _nullKey );
+ }
+
+ void IndexSpec::_getKeys( vector<const char*> fieldNames , vector<BSONElement> fixed , const BSONObj &obj, BSONObjSetDefaultOrder &keys ) const {
BSONObjBuilder b;
b.appendNull( "" );
BSONElement nullElt = b.done().firstElement();
@@ -89,7 +112,8 @@ namespace mongo {
for( vector< BSONElement >::iterator i = fixed.begin(); i != fixed.end(); ++i )
b.appendAs( *i, "" );
keys.insert( b.obj() );
- } else {
+ }
+ else {
// terminal array element to expand, so generate all keys
BSONObjIterator i( arrElt.embeddedObject() );
if ( i.more() ){
@@ -123,24 +147,18 @@ namespace mongo {
while( i.more() ) {
BSONElement e = i.next();
if ( e.type() == Object )
- getKeys( fieldNames, fixed, e.embeddedObject(), keys );
+ _getKeys( fieldNames, fixed, e.embeddedObject(), keys );
}
}
}
- void getKeysFromObject( const IndexSpec &spec, const BSONObj &obj, BSONObjSetDefaultOrder &keys ) {
- getKeys( spec.fieldNames, spec.fixed, obj, keys );
- if ( keys.empty() )
- keys.insert( spec.nullKey );
- }
-
/* Pull out the relevant key objects from obj, so we
can index them. Note that the set is multiple elements
only when it's a "multikey" array.
Keys will be left empty if key not found in the object.
*/
void IndexDetails::getKeysFromObject( const BSONObj& obj, BSONObjSetDefaultOrder& keys) const {
- mongo::getKeysFromObject( NamespaceDetailsTransient::get_w( info.obj()["ns"].valuestr() ).getIndexSpec( this ) , obj, keys );
+ NamespaceDetailsTransient::get_w( info.obj()["ns"].valuestr() ).getIndexSpec( this ).getKeys( obj, keys );
}
void setDifference(BSONObjSetDefaultOrder &l, BSONObjSetDefaultOrder &r, vector<BSONObj*> &diff) {
View
30 db/index.h
@@ -27,10 +27,6 @@ namespace mongo {
BSONObj keys;
BSONObj meta;
- vector< const char * > fieldNames;
- vector< BSONElement > fixed;
- BSONObj nullKey;
-
IndexSpec(){
}
@@ -58,24 +54,18 @@ namespace mongo {
_init();
}
+ void getKeys( const BSONObj &obj, BSONObjSetDefaultOrder &keys ) const;
+
private:
- void _init(){
- assert( keys.objsize() );
-
- BSONObjIterator i( keys );
- BSONObjBuilder nullKeyB;
- while( i.more() ) {
- fieldNames.push_back( i.next().fieldName() );
- fixed.push_back( BSONElement() );
- nullKeyB.appendNull( "" );
- }
-
- nullKey = nullKeyB.obj();
- }
-
+
+ void _getKeys( vector<const char*> fieldNames , vector<BSONElement> fixed , const BSONObj &obj, BSONObjSetDefaultOrder &keys ) const;
+
+ vector<const char*> _fieldNames;
+ vector<BSONElement> _fixed;
+ BSONObj _nullKey;
+
+ void _init();
};
-
- void getKeysFromObject( const IndexSpec &spec, const BSONObj &obj, BSONObjSetDefaultOrder &keys );
/* Details about a particular index. There is one of these effectively for each object in
system.namespaces (although this also includes the head pointer, which is not in that
View
2 db/matcher.cpp
@@ -391,7 +391,7 @@ namespace mongo {
if ( bm.myset->size() == 0 )
return -1; // is this desired?
BSONObjSetDefaultOrder actualKeys;
- getKeysFromObject( BSON( fieldName << 1 ), obj, actualKeys );
+ IndexSpec( BSON( fieldName << 1 ) ).getKeys( obj, actualKeys );
if ( actualKeys.size() == 0 )
return 0;
for( set< BSONElement, element_lt >::const_iterator i = bm.myset->begin(); i != bm.myset->end(); ++i ) {
View
13 jstests/indexf.js
@@ -0,0 +1,13 @@
+
+t = db.indexf
+t.drop();
+
+t.ensureIndex( { x : 1 } );
+
+t.save( { x : 2 } );
+t.save( { y : 3 } );
+t.save( { x : 4 } );
+
+assert.eq( 2 , t.findOne( { x : 2 } ).x , "A1" );
+assert.eq( 3 , t.findOne( { x : null } ).y , "A2" );
+assert.eq( 4 , t.findOne( { x : 4 } ).x , "A3" );

0 comments on commit efbf338

Please sign in to comment.
Something went wrong with that request. Please try again.