dbrefs only need an oid and a collection, not all this extra cruft...

This commit is contained in:
Mike Dirolf 2009-03-10 14:17:01 -04:00
parent a3f4e4565a
commit 56fddf79d4
6 changed files with 20 additions and 22 deletions

View File

@ -218,7 +218,7 @@ module XGen
raise "BAD SIZE" unless read2.length == size - 4
@n_remaining -= 1
buf.rewind
BSON.new(@db).deserialize(buf)
BSON.new.deserialize(buf)
end
def send_query_if_needed

View File

@ -20,11 +20,11 @@ module XGen
class DBRef
attr_reader :parent, :field_name, :db, :namespace, :object_id
attr_reader :namespace, :object_id
def initialize(parent, field_name, db, namespace, object_id)
@parent, @field_name, @db, @namespace, @object_id =
parent, field_name, db, namespace, object_id
def initialize(namespace, object_id)
@namespace, @object_id =
namespace, object_id
end
def to_s

View File

@ -62,9 +62,7 @@ class BSON
buf.put_array(to_utf8(val.to_s).unpack("C*") + [0])
end
def initialize(db=nil)
# db is only needed during deserialization when the data contains a DBRef
@db = db
def initialize()
@buf = ByteBuffer.new
end
@ -133,7 +131,7 @@ class BSON
begin
require 'mongo/ext/cbson'
def deserialize(buf=nil, parent=nil)
def deserialize(buf=nil)
if buf.is_a? String
@buf = ByteBuffer.new(buf) if buf
else
@ -143,7 +141,7 @@ class BSON
CBson.deserialize(@buf.to_s)
end
rescue LoadError
def deserialize(buf=nil, parent=nil)
def deserialize(buf=nil)
# If buf is nil, use @buf, assumed to contain already-serialized BSON.
# This is only true during testing.
if buf.is_a? String
@ -174,13 +172,13 @@ class BSON
doc[key] = deserialize_oid_data(@buf)
when ARRAY
key = deserialize_cstr(@buf)
doc[key] = deserialize_array_data(@buf, doc)
doc[key] = deserialize_array_data(@buf)
when REGEX
key = deserialize_cstr(@buf)
doc[key] = deserialize_regex_data(@buf)
when OBJECT
key = deserialize_cstr(@buf)
doc[key] = deserialize_object_data(@buf, doc)
doc[key] = deserialize_object_data(@buf)
when BOOLEAN
key = deserialize_cstr(@buf)
doc[key] = deserialize_boolean_data(@buf)
@ -195,7 +193,7 @@ class BSON
doc[key] = Undefined.new
when REF
key = deserialize_cstr(@buf)
doc[key] = deserialize_dbref_data(@buf, key, parent)
doc[key] = deserialize_dbref_data(@buf)
when BINARY
key = deserialize_cstr(@buf)
doc[key] = deserialize_binary_data(@buf)
@ -245,14 +243,14 @@ class BSON
buf.get_int
end
def deserialize_object_data(buf, parent)
def deserialize_object_data(buf)
size = buf.get_int
buf.position -= 4
BSON.new(@db).deserialize(buf.get(size), parent)
BSON.new().deserialize(buf.get(size))
end
def deserialize_array_data(buf, parent)
h = deserialize_object_data(buf, parent)
def deserialize_array_data(buf)
h = deserialize_object_data(buf)
a = []
h.each { |k, v| a[k.to_i] = v }
a
@ -286,10 +284,10 @@ class BSON
ObjectID.new(buf.get(12))
end
def deserialize_dbref_data(buf, key, parent)
def deserialize_dbref_data(buf)
ns = deserialize_string_data(buf)
oid = deserialize_oid_data(buf)
DBRef.new(parent, key, @db, ns, oid)
DBRef.new(ns, oid)
end
def deserialize_binary_data(buf)

View File

@ -99,7 +99,7 @@ class XMLToRuby
def dbref_to_ruby(elements)
ns = elements['ns'].text
oid_str = elements['oid'].text
DBRef.new(nil, nil, nil, ns, ObjectID.from_string(oid_str))
DBRef.new(ns, ObjectID.from_string(oid_str))
end
end

View File

@ -108,7 +108,7 @@ class BSONTest < Test::Unit::TestCase
def test_dbref
oid = ObjectID.new
doc = {}
doc['dbref'] = DBRef.new(doc, 'dbref', nil, 'namespace', oid)
doc['dbref'] = DBRef.new('namespace', oid)
@b.serialize(doc)
doc2 = @b.deserialize
assert_equal 'namespace', doc2['dbref'].namespace

View File

@ -95,7 +95,7 @@ EOS
# We're passing a nil db to the contructor here, but that's OK because
# the BSON DBFef bytes don't contain the db object in any case, and we
# don't care what the database is.
obj_from_bson = BSON.new(nil).deserialize(ByteBuffer.new(bson_from_ruby))
obj_from_bson = BSON.new.deserialize(ByteBuffer.new(bson_from_ruby))
assert_kind_of OrderedHash, obj_from_bson
# Turn that Ruby object into BSON and compare it to the original BSON