Permalink
Browse files

SERVER-1606: don't duplicate update names

Conflicts:

	src/mongo/dbtests/updatetests.cpp
  • Loading branch information...
1 parent b80968f commit 679219a64948a9702c639d2a3bdbd20b6be68723 @erh erh committed with monkey101 Jan 28, 2013
Showing with 68 additions and 21 deletions.
  1. +48 −10 src/mongo/db/ops/update_internal.cpp
  2. +2 −6 src/mongo/db/ops/update_internal.h
  3. +18 −5 src/mongo/dbtests/updatetests.cpp
View
58 src/mongo/db/ops/update_internal.cpp
@@ -550,7 +550,28 @@ namespace mongo {
return mss;
}
- void ModState::appendForOpLog( BSONObjBuilder& b ) const {
+ const char* ModState::getOpLogName() const {
+ if ( dontApply ) {
+ return NULL;
+ }
+
+ if ( incType ) {
+ return "$set";
+ }
+
+ if ( m->op == Mod::RENAME_FROM ) {
+ return "$unset";
+ }
+
+ if ( m->op == Mod::RENAME_TO ) {
+ return "$set";
+ }
+
+ return fixedOpName ? fixedOpName : Mod::modNames[op()];
+ }
+
+
+ void ModState::appendForOpLog( BSONObjBuilder& bb ) const {
// dontApply logic is deprecated for all but $rename.
if ( dontApply ) {
return;
@@ -559,23 +580,18 @@ namespace mongo {
if ( incType ) {
DEBUGUPDATE( "\t\t\t\t\t appendForOpLog inc fieldname: " << m->fieldName
<< " short:" << m->shortFieldName );
- BSONObjBuilder bb( b.subobjStart( "$set" ) );
appendIncValue( bb , true );
- bb.done();
return;
}
if ( m->op == Mod::RENAME_FROM ) {
DEBUGUPDATE( "\t\t\t\t\t appendForOpLog RENAME_FROM fieldName:" << m->fieldName );
- BSONObjBuilder bb( b.subobjStart( "$unset" ) );
bb.append( m->fieldName, 1 );
- bb.done();
return;
}
if ( m->op == Mod::RENAME_TO ) {
DEBUGUPDATE( "\t\t\t\t\t appendForOpLog RENAME_TO fieldName:" << m->fieldName );
- BSONObjBuilder bb( b.subobjStart( "$set" ) );
bb.appendAs( newVal, m->fieldName );
return;
}
@@ -586,13 +602,10 @@ namespace mongo {
<< " fn: " << m->fieldName );
if (strcmp(name, "$unset") == 0) {
- BSONObjBuilder bb(b.subobjStart(name));
bb.append(m->fieldName, 1);
- bb.done();
return;
}
- BSONObjBuilder bb( b.subobjStart( name ) );
if ( fixed ) {
bb.appendAs( *fixed , m->fieldName );
}
@@ -606,7 +619,32 @@ namespace mongo {
else {
bb.appendAs( m->elt , m->fieldName );
}
- bb.done();
+
+ }
+
+ typedef map<string, vector<ModState*> > NamedModMap;
+
+ BSONObj ModSetState::getOpLogRewrite() const {
+ NamedModMap names;
+ for ( ModStateHolder::const_iterator i = _mods.begin(); i != _mods.end(); ++i ) {
+ const char* name = i->second->getOpLogName();
+ if ( ! name )
+ continue;
+ names[name].push_back( i->second.get() );
+ }
+
+ BSONObjBuilder b;
+ for ( NamedModMap::const_iterator i = names.begin();
+ i != names.end();
+ ++i ) {
+ BSONObjBuilder bb( b.subobjStart( i->first ) );
+ const vector<ModState*>& mods = i->second;
+ for ( unsigned j = 0; j < mods.size(); j++ ) {
+ mods[j]->appendForOpLog( bb );
+ }
+ bb.doneFast();
+ }
+ return b.obj();
}
string ModState::toString() const {
View
8 src/mongo/db/ops/update_internal.h
@@ -436,6 +436,7 @@ namespace mongo {
}
}
+ const char* getOpLogName() const;
void appendForOpLog( BSONObjBuilder& b ) const;
void apply( BSONBuilderBase& b , BSONElement in ) {
@@ -616,12 +617,7 @@ namespace mongo {
return false;
}
- BSONObj getOpLogRewrite() const {
- BSONObjBuilder b;
- for ( ModStateHolder::const_iterator i = _mods.begin(); i != _mods.end(); i++ )
- i->second->appendForOpLog( b );
- return b.obj();
- }
+ BSONObj getOpLogRewrite() const;
bool DEPRECATED_haveArrayDepMod() const {
for ( ModStateHolder::const_iterator i = _mods.begin(); i != _mods.end(); i++ )
View
23 src/mongo/dbtests/updatetests.cpp
@@ -691,7 +691,7 @@ namespace UpdateTests {
auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
ASSERT_FALSE( modSetState->canApplyInPlace() );
modSetState->createNewFromMods();
- ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 3 ) << "$set" << BSON("b" << 2)),
+ ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 3 << "b" << 2)),
modSetState->getOpLogRewrite() );
}
};
@@ -705,7 +705,7 @@ namespace UpdateTests {
auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
ASSERT_FALSE( modSetState->canApplyInPlace() );
modSetState->createNewFromMods();
- ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 1 ) << "$set" << BSON("b" << 2)),
+ ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 1 << "b" << 2)),
modSetState->getOpLogRewrite() );
}
};
@@ -861,7 +861,7 @@ namespace UpdateTests {
auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
ASSERT_FALSE( modSetState->canApplyInPlace() );
modSetState->createNewFromMods();
- ASSERT_EQUALS( fromjson( "{ $set:{ 'a.b':[ 1 ] }, $set:{ 'a.c':[ 1 ] } }" ),
+ ASSERT_EQUALS( fromjson( "{ $set:{ 'a.b':[ 1 ] , 'a.c':[ 1 ] } }" ),
modSetState->getOpLogRewrite() );
}
};
@@ -1017,7 +1017,7 @@ namespace UpdateTests {
auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
ASSERT_FALSE( modSetState->canApplyInPlace() );
modSetState->createNewFromMods();
- ASSERT_EQUALS( BSON( "$unset" << BSON( "a" << 1 ) << "$set" << BSON ( "b" << 100 ) ),
+ ASSERT_EQUALS( BSON( "$set" << BSON( "b" << 100 ) << "$unset" << BSON ( "a" << 1 ) ),
modSetState->getOpLogRewrite() );
}
};
@@ -1031,7 +1031,7 @@ namespace UpdateTests {
auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
ASSERT_FALSE( modSetState->canApplyInPlace() );
modSetState->createNewFromMods();
- ASSERT_EQUALS( BSON( "$unset" << BSON( "a" << 1 ) << "$set" << BSON ( "b" << 100 ) ),
+ ASSERT_EQUALS( BSON( "$set" << BSON( "b" << 100 ) << "$unset" << BSON ( "a" << 1 ) ),
modSetState->getOpLogRewrite() );
}
};
@@ -1092,6 +1092,18 @@ namespace UpdateTests {
modSetState->getOpLogRewrite() );
}
};
+
+ class MultiSets {
+ public:
+ void run() {
+ BSONObj obj = BSON( "_id" << 1 << "a" << 1 << "b" << 1 );
+ BSONObj mod = BSON( "$set" << BSON( "a" << 2 << "b" << 2 ) );
+ ModSet modSet( mod );
+ auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
+ ASSERT_TRUE( modSetState->canApplyInPlace() );
+ ASSERT_EQUALS( mod, modSetState->getOpLogRewrite() );
+ }
+ };
};
namespace basic {
@@ -1421,6 +1433,7 @@ namespace UpdateTests {
// add< ModSetTests::BitRewriteNonExistingField >();
add< ModSetTests::SetIsNotRewritten >();
add< ModSetTests::UnsetIsNotRewritten >();
+ add< ModSetTests::MultiSets >();
add< basic::inc1 >();
add< basic::inc2 >();

0 comments on commit 679219a

Please sign in to comment.