diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 67346a0..6ba8022 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -13,6 +13,7 @@ # along with this program. If not, see . require 'socket' +require 'mongo/objectid' require 'mongo/collection' require 'mongo/message' require 'mongo/query' @@ -152,6 +153,7 @@ module XGen end def insert_into_db(collection, objects) + objects.each { |o| o['_id'] ||= ObjectID.new } # TODO synchronize objects.each { |o| send_to_db(InsertMessage.new(@name, collection, o)) } end @@ -177,4 +179,3 @@ module XGen end end end - diff --git a/lib/mongo/objectid.rb b/lib/mongo/objectid.rb index bdbf310..cd96941 100644 --- a/lib/mongo/objectid.rb +++ b/lib/mongo/objectid.rb @@ -20,12 +20,18 @@ module XGen class ObjectID + UUID_STRING_LENGTH = 24 + + @@uuid_generator = UUID.new + # String UUID attr_reader :uuid - # uuid is a string + # +uuid+ is a string. If nil, a new UUID will be generated. def initialize(uuid=nil) - @uuid ||= UUID.new.generate + # The Babble server expects 12-byte (24 hex character) keys, which + # is why we throw away part of the UUID. + @uuid ||= @@uuid_generator.generate(:compact).sub(/(.{12}).{8}(.{12})/, '\1\2') end def to_s diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index 5f945ce..49f4fbd 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -155,8 +155,8 @@ class BSON bytes[0..-2].pack("C*") end - def deserialize_oid_data - XGen::Mongo::Driver::ObjectID.new(buf.get(12).pack("C*")) + def deserialize_oid_data(buf) + XGen::Mongo::Driver::ObjectID.new(buf.get(XGen::Mongo::Driver::ObjectID::UUID_STRING_LENGTH).pack("C*")) end def serialize_eoo_element(buf) @@ -199,7 +199,7 @@ class BSON def serialize_oid_element(buf, key, val) buf.put(OID) self.class.serialize_cstr(buf, key) - buf.put_array(val.to_a) + buf.put_array(val.to_s.unpack("C*")) end def serialize_string_element(buf, key, val, type)