Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

SERVER-1606: don't duplicate update names

Conflicts:

	src/mongo/dbtests/updatetests.cpp
  • Loading branch information...
commit 679219a64948a9702c639d2a3bdbd20b6be68723 1 parent b80968f
Eliot erh authored monkey101 committed
58 src/mongo/db/ops/update_internal.cpp
@@ -550,7 +550,28 @@ namespace mongo {
550 550 return mss;
551 551 }
552 552
553   - void ModState::appendForOpLog( BSONObjBuilder& b ) const {
  553 + const char* ModState::getOpLogName() const {
  554 + if ( dontApply ) {
  555 + return NULL;
  556 + }
  557 +
  558 + if ( incType ) {
  559 + return "$set";
  560 + }
  561 +
  562 + if ( m->op == Mod::RENAME_FROM ) {
  563 + return "$unset";
  564 + }
  565 +
  566 + if ( m->op == Mod::RENAME_TO ) {
  567 + return "$set";
  568 + }
  569 +
  570 + return fixedOpName ? fixedOpName : Mod::modNames[op()];
  571 + }
  572 +
  573 +
  574 + void ModState::appendForOpLog( BSONObjBuilder& bb ) const {
554 575 // dontApply logic is deprecated for all but $rename.
555 576 if ( dontApply ) {
556 577 return;
@@ -559,23 +580,18 @@ namespace mongo {
559 580 if ( incType ) {
560 581 DEBUGUPDATE( "\t\t\t\t\t appendForOpLog inc fieldname: " << m->fieldName
561 582 << " short:" << m->shortFieldName );
562   - BSONObjBuilder bb( b.subobjStart( "$set" ) );
563 583 appendIncValue( bb , true );
564   - bb.done();
565 584 return;
566 585 }
567 586
568 587 if ( m->op == Mod::RENAME_FROM ) {
569 588 DEBUGUPDATE( "\t\t\t\t\t appendForOpLog RENAME_FROM fieldName:" << m->fieldName );
570   - BSONObjBuilder bb( b.subobjStart( "$unset" ) );
571 589 bb.append( m->fieldName, 1 );
572   - bb.done();
573 590 return;
574 591 }
575 592
576 593 if ( m->op == Mod::RENAME_TO ) {
577 594 DEBUGUPDATE( "\t\t\t\t\t appendForOpLog RENAME_TO fieldName:" << m->fieldName );
578   - BSONObjBuilder bb( b.subobjStart( "$set" ) );
579 595 bb.appendAs( newVal, m->fieldName );
580 596 return;
581 597 }
@@ -586,13 +602,10 @@ namespace mongo {
586 602 << " fn: " << m->fieldName );
587 603
588 604 if (strcmp(name, "$unset") == 0) {
589   - BSONObjBuilder bb(b.subobjStart(name));
590 605 bb.append(m->fieldName, 1);
591   - bb.done();
592 606 return;
593 607 }
594 608
595   - BSONObjBuilder bb( b.subobjStart( name ) );
596 609 if ( fixed ) {
597 610 bb.appendAs( *fixed , m->fieldName );
598 611 }
@@ -606,7 +619,32 @@ namespace mongo {
606 619 else {
607 620 bb.appendAs( m->elt , m->fieldName );
608 621 }
609   - bb.done();
  622 +
  623 + }
  624 +
  625 + typedef map<string, vector<ModState*> > NamedModMap;
  626 +
  627 + BSONObj ModSetState::getOpLogRewrite() const {
  628 + NamedModMap names;
  629 + for ( ModStateHolder::const_iterator i = _mods.begin(); i != _mods.end(); ++i ) {
  630 + const char* name = i->second->getOpLogName();
  631 + if ( ! name )
  632 + continue;
  633 + names[name].push_back( i->second.get() );
  634 + }
  635 +
  636 + BSONObjBuilder b;
  637 + for ( NamedModMap::const_iterator i = names.begin();
  638 + i != names.end();
  639 + ++i ) {
  640 + BSONObjBuilder bb( b.subobjStart( i->first ) );
  641 + const vector<ModState*>& mods = i->second;
  642 + for ( unsigned j = 0; j < mods.size(); j++ ) {
  643 + mods[j]->appendForOpLog( bb );
  644 + }
  645 + bb.doneFast();
  646 + }
  647 + return b.obj();
610 648 }
611 649
612 650 string ModState::toString() const {
8 src/mongo/db/ops/update_internal.h
@@ -436,6 +436,7 @@ namespace mongo {
436 436 }
437 437 }
438 438
  439 + const char* getOpLogName() const;
439 440 void appendForOpLog( BSONObjBuilder& b ) const;
440 441
441 442 void apply( BSONBuilderBase& b , BSONElement in ) {
@@ -616,12 +617,7 @@ namespace mongo {
616 617 return false;
617 618 }
618 619
619   - BSONObj getOpLogRewrite() const {
620   - BSONObjBuilder b;
621   - for ( ModStateHolder::const_iterator i = _mods.begin(); i != _mods.end(); i++ )
622   - i->second->appendForOpLog( b );
623   - return b.obj();
624   - }
  620 + BSONObj getOpLogRewrite() const;
625 621
626 622 bool DEPRECATED_haveArrayDepMod() const {
627 623 for ( ModStateHolder::const_iterator i = _mods.begin(); i != _mods.end(); i++ )
23 src/mongo/dbtests/updatetests.cpp
@@ -691,7 +691,7 @@ namespace UpdateTests {
691 691 auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
692 692 ASSERT_FALSE( modSetState->canApplyInPlace() );
693 693 modSetState->createNewFromMods();
694   - ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 3 ) << "$set" << BSON("b" << 2)),
  694 + ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 3 << "b" << 2)),
695 695 modSetState->getOpLogRewrite() );
696 696 }
697 697 };
@@ -705,7 +705,7 @@ namespace UpdateTests {
705 705 auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
706 706 ASSERT_FALSE( modSetState->canApplyInPlace() );
707 707 modSetState->createNewFromMods();
708   - ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 1 ) << "$set" << BSON("b" << 2)),
  708 + ASSERT_EQUALS( BSON( "$set" << BSON( "a" << 1 << "b" << 2)),
709 709 modSetState->getOpLogRewrite() );
710 710 }
711 711 };
@@ -861,7 +861,7 @@ namespace UpdateTests {
861 861 auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
862 862 ASSERT_FALSE( modSetState->canApplyInPlace() );
863 863 modSetState->createNewFromMods();
864   - ASSERT_EQUALS( fromjson( "{ $set:{ 'a.b':[ 1 ] }, $set:{ 'a.c':[ 1 ] } }" ),
  864 + ASSERT_EQUALS( fromjson( "{ $set:{ 'a.b':[ 1 ] , 'a.c':[ 1 ] } }" ),
865 865 modSetState->getOpLogRewrite() );
866 866 }
867 867 };
@@ -1017,7 +1017,7 @@ namespace UpdateTests {
1017 1017 auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
1018 1018 ASSERT_FALSE( modSetState->canApplyInPlace() );
1019 1019 modSetState->createNewFromMods();
1020   - ASSERT_EQUALS( BSON( "$unset" << BSON( "a" << 1 ) << "$set" << BSON ( "b" << 100 ) ),
  1020 + ASSERT_EQUALS( BSON( "$set" << BSON( "b" << 100 ) << "$unset" << BSON ( "a" << 1 ) ),
1021 1021 modSetState->getOpLogRewrite() );
1022 1022 }
1023 1023 };
@@ -1031,7 +1031,7 @@ namespace UpdateTests {
1031 1031 auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
1032 1032 ASSERT_FALSE( modSetState->canApplyInPlace() );
1033 1033 modSetState->createNewFromMods();
1034   - ASSERT_EQUALS( BSON( "$unset" << BSON( "a" << 1 ) << "$set" << BSON ( "b" << 100 ) ),
  1034 + ASSERT_EQUALS( BSON( "$set" << BSON( "b" << 100 ) << "$unset" << BSON ( "a" << 1 ) ),
1035 1035 modSetState->getOpLogRewrite() );
1036 1036 }
1037 1037 };
@@ -1092,6 +1092,18 @@ namespace UpdateTests {
1092 1092 modSetState->getOpLogRewrite() );
1093 1093 }
1094 1094 };
  1095 +
  1096 + class MultiSets {
  1097 + public:
  1098 + void run() {
  1099 + BSONObj obj = BSON( "_id" << 1 << "a" << 1 << "b" << 1 );
  1100 + BSONObj mod = BSON( "$set" << BSON( "a" << 2 << "b" << 2 ) );
  1101 + ModSet modSet( mod );
  1102 + auto_ptr<ModSetState> modSetState = modSet.prepare( obj );
  1103 + ASSERT_TRUE( modSetState->canApplyInPlace() );
  1104 + ASSERT_EQUALS( mod, modSetState->getOpLogRewrite() );
  1105 + }
  1106 + };
1095 1107 };
1096 1108
1097 1109 namespace basic {
@@ -1421,6 +1433,7 @@ namespace UpdateTests {
1421 1433 // add< ModSetTests::BitRewriteNonExistingField >();
1422 1434 add< ModSetTests::SetIsNotRewritten >();
1423 1435 add< ModSetTests::UnsetIsNotRewritten >();
  1436 + add< ModSetTests::MultiSets >();
1424 1437
1425 1438 add< basic::inc1 >();
1426 1439 add< basic::inc2 >();

0 comments on commit 679219a

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