Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix array numberic sort order SERVER-833

  • Loading branch information...
commit 31c93582892516f7cf379e25366160a4877b9fad 1 parent 18149f3
@erh erh authored
View
13 db/update.cpp
@@ -24,11 +24,11 @@
#include "repl.h"
#include "update.h"
+//#define DEBUGUPDATE(x) cout << x << endl;
+#define DEBUGUPDATE(x)
+
namespace mongo {
- //#define DEBUGUPDATE(x) cout << x << endl;
-#define DEBUGUPDATE(x)
-
const char* Mod::modNames[] = { "$inc", "$set", "$push", "$pushAll", "$pull", "$pullAll" , "$pop", "$unset" ,
"$bitand" , "$bitor" , "$bit" , "$addToSet" };
unsigned Mod::modNamesNum = sizeof(Mod::modNames)/sizeof(char*);
@@ -310,11 +310,12 @@ namespace mongo {
// Perform this check first, so that we don't leave a partially modified object on uassert.
for ( ModHolder::const_iterator i = _mods.begin(); i != _mods.end(); ++i ) {
+ DEBUGUPDATE( "\t\t prepare : " << i->first );
ModState& ms = mss->_mods[i->first];
const Mod& m = i->second;
BSONElement e = obj.getFieldDotted(m.fieldName);
-
+
ms.m = &m;
ms.old = e;
@@ -406,6 +407,7 @@ namespace mongo {
mss->amIInPlacePossible( false );
}
}
+
return auto_ptr<ModSetState>( mss );
}
@@ -477,6 +479,7 @@ namespace mongo {
template< class Builder >
void ModSetState::createNewFromMods( const string& root , Builder& b , const BSONObj &obj ){
+ DEBUGUPDATE( "\t\t createNewFromMods root: " << root );
BSONObjIteratorSorted es( obj );
BSONElement e = es.next();
@@ -488,6 +491,8 @@ namespace mongo {
while ( e.type() && m != mend ){
string field = root + e.fieldName();
FieldCompareResult cmp = compareDottedFieldNames( m->second.m->fieldName , field );
+
+ DEBUGUPDATE( "\t\t\t" << field << "\t" << m->second.m->fieldName << "\t" << cmp );
switch ( cmp ){
View
5 dbtests/basictests.cpp
@@ -306,11 +306,14 @@ namespace BasicTests {
ASSERT_EQUALS( 1, lexNumCmp( "f12g", "f12f" ) );
ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aab" ) );
ASSERT_EQUALS( 1, lexNumCmp( "aa{", "aa1" ) );
- ASSERT_EQUALS( 1, lexNumCmp( "a1{", "a11" ) );
+ ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a11" ) );
ASSERT_EQUALS( 1, lexNumCmp( "a1{a", "a1{" ) );
ASSERT_EQUALS( -1, lexNumCmp( "a1{", "a1{a" ) );
ASSERT_EQUALS( 1, lexNumCmp("21", "11") );
ASSERT_EQUALS( -1, lexNumCmp("11", "21") );
+
+ ASSERT_EQUALS( -1 , lexNumCmp( "a.0" , "a.1" ) );
+ ASSERT_EQUALS( -1 , lexNumCmp( "a.0.b" , "a.1" ) );
}
};
View
20 jstests/updated.js
@@ -0,0 +1,20 @@
+
+t = db.updated;
+t.drop()
+
+o = { _id : Math.random() ,
+ items:[null,null,null,null]
+ };
+
+t.insert( o );
+assert.eq( o , t.findOne() , "A1" );
+
+o.items[0] = {amount:9000,itemId:1};
+t.update({},{$set:{"items.0":o.items[0]}});
+assert.eq( o , t.findOne() , "A2" );
+
+o.items[0].amount += 1000;
+o.items[1] = {amount:1,itemId:2};
+t.update({},{$inc:{"items.0.amount":1000},$set:{"items.1":o.items[1]}});
+assert.eq( o , t.findOne() , "A3" );
+
View
55 util/goodies.h
@@ -650,43 +650,54 @@ namespace mongo {
// for convenience, '{' is greater than anything and stops number parsing
inline int lexNumCmp( const char *s1, const char *s2 ) {
- int nret = 0;
while( *s1 && *s2 ) {
+
bool p1 = ( *s1 == '{' );
bool p2 = ( *s2 == '{' );
if ( p1 && !p2 )
return 1;
if ( p2 && !p1 )
return -1;
+
bool n1 = isNumber( *s1 );
bool n2 = isNumber( *s2 );
+
if ( n1 && n2 ) {
- if ( nret == 0 ) {
- nret = *s1 > *s2 ? 1 : ( *s1 == *s2 ? 0 : -1 );
- }
- } else if ( n1 ) {
- return 1;
- } else if ( n2 ) {
- return -1;
- } else {
- if ( nret ) {
- return nret;
- }
- if ( *s1 > *s2 ) {
+ char * e1;
+ char * e2;
+ long l1 = strtol( s1 , &e1 , 10 );
+ long l2 = strtol( s2 , &e2 , 10 );
+
+ if ( l1 > l2 )
return 1;
- } else if ( *s2 > *s1 ) {
+ else if ( l1 < l2 )
return -1;
- }
- nret = 0;
- }
- ++s1; ++s2;
+
+ s1 = e1;
+ s2 = e2;
+ continue;
+ }
+
+ if ( n1 )
+ return 1;
+
+ if ( n2 )
+ return -1;
+
+ if ( *s1 > *s2 )
+ return 1;
+
+ if ( *s2 > *s1 )
+ return -1;
+
+ s1++; s2++;
}
- if ( *s1 ) {
+
+ if ( *s1 )
return 1;
- } else if ( *s2 ) {
+ if ( *s2 )
return -1;
- }
- return nret;
+ return 0;
}
} // namespace mongo
Please sign in to comment.
Something went wrong with that request. Please try again.