Add _id to inserted records. Fixed ObjectID load, save, and generation.
This commit is contained in:
parent
cf03cd53f9
commit
51dde79ba4
|
@ -13,6 +13,7 @@
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
require 'socket'
|
require 'socket'
|
||||||
|
require 'mongo/objectid'
|
||||||
require 'mongo/collection'
|
require 'mongo/collection'
|
||||||
require 'mongo/message'
|
require 'mongo/message'
|
||||||
require 'mongo/query'
|
require 'mongo/query'
|
||||||
|
@ -152,6 +153,7 @@ module XGen
|
||||||
end
|
end
|
||||||
|
|
||||||
def insert_into_db(collection, objects)
|
def insert_into_db(collection, objects)
|
||||||
|
objects.each { |o| o['_id'] ||= ObjectID.new }
|
||||||
# TODO synchronize
|
# TODO synchronize
|
||||||
objects.each { |o| send_to_db(InsertMessage.new(@name, collection, o)) }
|
objects.each { |o| send_to_db(InsertMessage.new(@name, collection, o)) }
|
||||||
end
|
end
|
||||||
|
@ -177,4 +179,3 @@ module XGen
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,12 +20,18 @@ module XGen
|
||||||
|
|
||||||
class ObjectID
|
class ObjectID
|
||||||
|
|
||||||
|
UUID_STRING_LENGTH = 24
|
||||||
|
|
||||||
|
@@uuid_generator = UUID.new
|
||||||
|
|
||||||
# String UUID
|
# String UUID
|
||||||
attr_reader :uuid
|
attr_reader :uuid
|
||||||
|
|
||||||
# uuid is a string
|
# +uuid+ is a string. If nil, a new UUID will be generated.
|
||||||
def initialize(uuid=nil)
|
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
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|
|
@ -155,8 +155,8 @@ class BSON
|
||||||
bytes[0..-2].pack("C*")
|
bytes[0..-2].pack("C*")
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize_oid_data
|
def deserialize_oid_data(buf)
|
||||||
XGen::Mongo::Driver::ObjectID.new(buf.get(12).pack("C*"))
|
XGen::Mongo::Driver::ObjectID.new(buf.get(XGen::Mongo::Driver::ObjectID::UUID_STRING_LENGTH).pack("C*"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_eoo_element(buf)
|
def serialize_eoo_element(buf)
|
||||||
|
@ -199,7 +199,7 @@ class BSON
|
||||||
def serialize_oid_element(buf, key, val)
|
def serialize_oid_element(buf, key, val)
|
||||||
buf.put(OID)
|
buf.put(OID)
|
||||||
self.class.serialize_cstr(buf, key)
|
self.class.serialize_cstr(buf, key)
|
||||||
buf.put_array(val.to_a)
|
buf.put_array(val.to_s.unpack("C*"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_string_element(buf, key, val, type)
|
def serialize_string_element(buf, key, val, type)
|
||||||
|
|
Loading…
Reference in New Issue