Skip to content

Commit

Permalink
fix ObjectId.compareTo JAVA-85
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Jan 30, 2011
1 parent c4ada42 commit 04aa553
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 13 deletions.
12 changes: 12 additions & 0 deletions src/main/com/mongodb/MapReduceOutput.java
Expand Up @@ -9,6 +9,8 @@
public class MapReduceOutput {

MapReduceOutput( DBCollection from , BasicDBObject raw ){
_raw = raw;

_collname = raw.getString( "result" );
_coll = from._db.getCollection( _collname );
_counts = (BasicDBObject)raw.get( "counts" );
Expand Down Expand Up @@ -37,6 +39,16 @@ public DBCollection getOutputCollection(){
return _coll;
}

public BasicDBObject getRaw(){
return _raw;
}

public String toString(){
return _raw.toString();
}

final BasicDBObject _raw;

final String _collname;
final DBCollection _coll;
final BasicDBObject _counts;
Expand Down
31 changes: 19 additions & 12 deletions src/main/org/bson/types/ObjectId.java
Expand Up @@ -254,26 +254,33 @@ public static String babbleToMongod( String b ){
public String toString(){
return toStringMongod();
}

int _compare( int i , int j ){
i = _flip(i);
j = _flip(j);

final int diff = j - i;

if ( i >= 0 ){
return j >= 0 ? -diff : -1;
}

return j < 0 ? -diff : 1;
}

public int compareTo( ObjectId id ){
if ( id == null )
return -1;

long xx = id.getTime() - getTime();
if ( xx > 0 )
return -1;
else if ( xx < 0 )
return 1;

int x = id._machine - _machine;
int x = _compare( _time , id._time );
if ( x != 0 )
return -x;
return x;

x = id._inc - _inc;
x = _compare( _machine , id._machine );
if ( x != 0 )
return -x;

return 0;
return x;
return _compare( _inc , id._inc );
}

public int getMachine(){
Expand Down
47 changes: 46 additions & 1 deletion src/test/com/mongodb/ByteTest.java
Expand Up @@ -431,12 +431,57 @@ public void testMany()
assertEquals( -1 , in.read() );
}

int _fix( int x ){
if ( x < 0 )
return -1;
if ( x > 0 )
return 1;
return 0;
}

@Test
public void testObjcetIdCompare(){
Random r = new Random( 171717 );

List<ObjectId> l = new ArrayList<ObjectId>();
for ( int i=0; i<10000; i++ ){
l.add( new ObjectId( new Date( Math.abs( r.nextLong() ) ) , Math.abs( r.nextInt() ) , Math.abs( r.nextInt() ) ) );
}

for ( int i=1; i<l.size(); i++ ){
int a = _fix( l.get(0).compareTo( l.get(i) ) );
int b = _fix( l.get(0).toString().compareTo( l.get(i).toString() ) );
if ( a == b )
continue;
throw new RuntimeException( "broken [" + l.get(0) + "] [" + l.get(i) + "] a: " + a + " b: " + b );
}

DBCollection c = _db.getCollection( "testObjcetIdCompare" );
c.drop();

for ( ObjectId o : l ){
c.insert( new BasicDBObject( "_id" , o ) );
}

Collections.sort( l );

List<DBObject> out = c.find().sort( new BasicDBObject( "_id" , 1 ) ).toArray();

assertEquals( l.size() , out.size() );

for ( int i=0; i<l.size(); i++ ){
assertEquals( l.get(i) , out.get(i).get( "_id" ) );
}


}

final DB _db;

public static void main( String args[] )
throws Exception {
(new ByteTest()).runConsole();

}

}

0 comments on commit 04aa553

Please sign in to comment.