Start of DBRef support
This commit is contained in:
parent
4e44aeea45
commit
c8200c4cc6
|
@ -1,5 +1,6 @@
|
|||
require 'mongo/mongo'
|
||||
require 'mongo/objectid'
|
||||
require 'mongo/dbref'
|
||||
require 'mongo/message'
|
||||
require 'mongo/db'
|
||||
require 'mongo/cursor'
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
# --
|
||||
# Copyright (C) 2008-2009 10gen Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify it
|
||||
# under the terms of the GNU Affero General Public License, version 3, as
|
||||
# published by the Free Software Foundation.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
||||
# for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
# ++
|
||||
|
||||
module XGen
|
||||
module Mongo
|
||||
module Driver
|
||||
|
||||
class DBRef
|
||||
|
||||
attr_reader :parent, :field_name, :db, :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
|
||||
end
|
||||
|
||||
def to_s
|
||||
"ns: #{namespace}, id: #{object_id}"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -17,6 +17,7 @@
|
|||
require 'mongo/util/byte_buffer'
|
||||
require 'mongo/util/ordered_hash'
|
||||
require 'mongo/objectid'
|
||||
require 'mongo/dbref'
|
||||
|
||||
# A BSON seralizer/deserializer.
|
||||
class BSON
|
||||
|
@ -81,7 +82,9 @@ class BSON
|
|||
serialize_date_element(@buf, k, v)
|
||||
when NULL
|
||||
serialize_null_element(@buf, k)
|
||||
when BINARY, UNDEFINED, REF, SYMBOL, CODE_W_SCOPE
|
||||
when REF
|
||||
serialize_dbref_element(@buf, k, v)
|
||||
when BINARY, UNDEFINED, SYMBOL, CODE_W_SCOPE
|
||||
# TODO
|
||||
raise "unimplemented type #{type}"
|
||||
else
|
||||
|
@ -133,7 +136,10 @@ class BSON
|
|||
when NULL
|
||||
key = deserialize_element_name(@buf)
|
||||
doc[key] = nil
|
||||
when BINARY, UNDEFINED, REF, SYMBOL, CODE_W_SCOPE
|
||||
when REF
|
||||
key = deserialize_element_name(@buf)
|
||||
doc[key] = deserialize_dbref_data(@buf)
|
||||
when BINARY, UNDEFINED, SYMBOL, CODE_W_SCOPE
|
||||
# TODO
|
||||
raise "unimplemented type #{type}"
|
||||
when EOO
|
||||
|
@ -210,6 +216,14 @@ class BSON
|
|||
XGen::Mongo::Driver::ObjectID.new(buf.get(12))
|
||||
end
|
||||
|
||||
def deserialize_dbref_data(buf)
|
||||
ns = deserialize_string_data(buf)
|
||||
oid = deserialize_oid_data(buf)
|
||||
# TODO fix parent, field_name, db of DBRef. Does that need to be done here
|
||||
# or by the caller?
|
||||
XGen::Mongo::Driver::DBRef.new(nil, nil, nil, ns, oid)
|
||||
end
|
||||
|
||||
def serialize_eoo_element(buf)
|
||||
buf.put(EOO)
|
||||
end
|
||||
|
@ -219,6 +233,11 @@ class BSON
|
|||
self.class.serialize_cstr(buf, key)
|
||||
end
|
||||
|
||||
def serialize_dbref_element(buf, key, val)
|
||||
serialize_string_element(buf, key, val.namespace, REF)
|
||||
buf.put_array(val.object_id.to_a)
|
||||
end
|
||||
|
||||
def serialize_boolean_element(buf, key, val)
|
||||
buf.put(BOOLEAN)
|
||||
self.class.serialize_cstr(buf, key)
|
||||
|
@ -325,6 +344,8 @@ class BSON
|
|||
REGEX
|
||||
when XGen::Mongo::Driver::ObjectID
|
||||
OID
|
||||
when XGen::Mongo::Driver::DBRef
|
||||
REF
|
||||
when true, false
|
||||
BOOLEAN
|
||||
when Time
|
||||
|
|
|
@ -76,4 +76,14 @@ class BSONTest < Test::Unit::TestCase
|
|||
|
||||
def test_null
|
||||
end
|
||||
|
||||
def test_dbref
|
||||
oid = ObjectID.new
|
||||
doc = {'dbref' => DBRef.new(nil, nil, nil, 'namespace', oid)}
|
||||
@b.serialize(doc)
|
||||
doc2 = @b.deserialize
|
||||
assert_equal 'namespace', doc2['dbref'].namespace
|
||||
assert_equal oid, doc2['dbref'].object_id
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue