minor: ObjectId optimization

This commit is contained in:
Kyle Banker 2010-10-04 12:09:29 -04:00
parent 7ad280c2cb
commit bf10db9b21
6 changed files with 14 additions and 12 deletions

Binary file not shown.

View File

@ -264,10 +264,11 @@ public class RubyBSONCallback implements BSONCallback {
}
public void gotObjectId( String name , ObjectId id ){
IRubyObject arg = (IRubyObject)RubyString.newString(_runtime, id.toString());
byte[] jbytes = id.toByteArray();
RubyArray arg = ja2ra(jbytes);
Object[] args = new Object[] { arg };
Object result = JavaEmbedUtils.invokeMethod(_runtime, _rbclsObjectId, "from_string", args, Object.class);
Object result = JavaEmbedUtils.invokeMethod(_runtime, _rbclsObjectId, "new", args, Object.class);
_put( name, (RubyObject)result );
}
@ -281,7 +282,7 @@ public class RubyBSONCallback implements BSONCallback {
// TODO: I know that this is horrible. To be optimized.
private RubyArray ja2ra( byte[] b ) {
RubyArray result = RubyArray.newArray( _runtime, b.length );
for ( int i=0; i<b.length; i++ ) {
result.store( i, _runtime.newFixnum(b[i]) );
}

View File

@ -319,7 +319,8 @@ public class RubyBSONEncoder extends BSONEncoder {
}
else {
long jval = big.longValue();
putLong(name, (Number)jval );
_put( NUMBER_LONG , name );
_buf.writeLong( jval );
}
}
@ -530,7 +531,7 @@ public class RubyBSONEncoder extends BSONEncoder {
_put( OID , name );
RubyArray roid = (RubyArray)JavaEmbedUtils.invokeMethod(_runtime, oid,
"to_a", new Object[] {}, Object.class);
"data", new Object[] {}, Object.class);
byte[] joid = ra2ba( (RubyArray)roid );
_buf.writeInt( convertToInt(joid, 0) );

View File

@ -108,6 +108,13 @@ module BSON
@data.dup
end
# Get the array representation without cloning.
#
# @return [Array]
def data
@data
end
# Given a string representation of an ObjectId, return a new ObjectId
# with that value.
#
@ -134,13 +141,6 @@ module BSON
str
end
def to_e
@data.each do |i|
print i
print ' '
end
end
def inspect
"BSON::ObjectId('#{to_s}')"
end