0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

$push replay

This commit is contained in:
Aaron 2009-03-25 15:17:30 -04:00
parent 7ba4140d5c
commit 4b7f917bf5
2 changed files with 47 additions and 1 deletions

View File

@ -221,7 +221,6 @@ namespace mongo {
double getn() const {
return ndouble ? *ndouble : *nint;
}
int type;
bool operator<( const Mod &other ) const {
return strcmp( fieldName, other.fieldName ) < 0;
}
@ -306,6 +305,31 @@ namespace mongo {
for ( vector<Mod>::const_iterator i = mods_.begin(); i != mods_.end(); i++ )
b.append(i->fieldName, i->getn());
}
bool havePush() const {
for ( vector<Mod>::const_iterator i = mods_.begin(); i != mods_.end(); i++ )
if ( i->op == Mod::PUSH )
return true;
return false;
}
void appendSizeSpecForPushes( BSONObjBuilder &b, const BSONObj &original ) const {
for ( vector<Mod>::const_iterator i = mods_.begin(); i != mods_.end(); i++ ) {
if ( i->op == Mod::PUSH ) {
BSONElement arrElt = original.getFieldDotted( i->fieldName );
if ( arrElt.type() != Array )
continue;
BSONObj arr = arrElt.embeddedObject();
int count = 0;
BSONObjIterator j( arr );
while( j.more() ) {
BSONElement e = j.next();
if ( e.eoo() )
break;
++count;
}
b << i->fieldName << BSON( "$size" << count );
}
}
}
};
bool ModSet::applyModsInPlace(const BSONObj &obj) const {
@ -589,6 +613,12 @@ namespace mongo {
if ( profile )
ss << " fastmod ";
} else {
if ( logop && mods.havePush() ) {
BSONObjBuilder patternBuilder;
patternBuilder.appendElements( pattern );
mods.appendSizeSpecForPushes( patternBuilder, c->currLoc().obj() );
pattern = patternBuilder.obj();
}
BSONObj newObj = mods.createNewFromMods( c->currLoc().obj() );
theDataFileMgr.update(ns, r, c->currLoc(), newObj.objdata(), newObj.objsize(), ss);
}

View File

@ -609,6 +609,21 @@ namespace ReplTests {
}
};
class Push : public Base {
public:
void doIt() const {
client()->update( ns(), BSON( "_id" << 0 ), BSON( "$push" << BSON( "a" << 5.0 ) ) );
}
void check() const {
ASSERT_EQUALS( 1, count() );
checkOne( fromjson( "{'_id':0,a:[4,5]}" ) );
}
void reset() const {
deleteAll( ns() );
insert( fromjson( "{'_id':0,a:[4]}" ) );
}
};
} // namespace Idempotence
class All : public UnitTest::Suite {
@ -638,6 +653,7 @@ namespace ReplTests {
add< Idempotence::RemoveOne >();
add< Idempotence::FailingUpdate >();
add< Idempotence::SetNumToStr >();
add< Idempotence::Push >();
}
};