Support for ARRAY type.

This commit is contained in:
Jim Menard 2008-12-08 15:27:20 -05:00
parent 8fd56ed92c
commit e7019a63f2
3 changed files with 32 additions and 2 deletions

2
README
View File

@ -52,6 +52,8 @@ type
= To Do = To Do
* Support more types: REGEX, etc.
* Study src/main/ed/db/{dbcollection,dbcursor,db}.js in the Babble code. * Study src/main/ed/db/{dbcollection,dbcursor,db}.js in the Babble code.
That's what I should be writing to. That's what I should be writing to.

View File

@ -1,4 +1,5 @@
require 'mongo/util/byte_buffer' require 'mongo/util/byte_buffer'
require 'mongo/util/ordered_hash'
require 'mongo/objectid' require 'mongo/objectid'
# See http://github.com/10gen/mongo/tree/master/db/jsobj.h # See http://github.com/10gen/mongo/tree/master/db/jsobj.h
@ -56,6 +57,8 @@ class BSON
serialize_object_element(@buf, k, v) serialize_object_element(@buf, k, v)
when OID when OID
serialize_oid_element(@buf, k, v) serialize_oid_element(@buf, k, v)
when ARRAY
serialize_array_element(@buf, k, v)
when BOOLEAN when BOOLEAN
serialize_boolean_element(@buf, k, v) serialize_boolean_element(@buf, k, v)
when DATE when DATE
@ -91,6 +94,9 @@ class BSON
when OID when OID
key = deserialize_element_name(@buf) key = deserialize_element_name(@buf)
doc[key] = deserialize_oid_data(@buf) doc[key] = deserialize_oid_data(@buf)
when ARRAY
key = deserialize_element_name(@buf)
doc[key] = deserialize_array_data(@buf)
when OBJECT when OBJECT
key = deserialize_element_name(@buf) key = deserialize_element_name(@buf)
doc[key] = deserialize_object_data(@buf) doc[key] = deserialize_object_data(@buf)
@ -149,6 +155,13 @@ class BSON
BSON.new.deserialize(buf.get(size)) BSON.new.deserialize(buf.get(size))
end end
def deserialize_array_data(buf)
h = deserialize_object_data(buf)
a = []
h.each { |k, v| a[k.to_i] = v }
a
end
def deserialize_string_data(buf) def deserialize_string_data(buf)
len = buf.get_int len = buf.get_int
bytes = buf.get(len) bytes = buf.get(len)
@ -193,12 +206,20 @@ class BSON
end end
end end
def serialize_object_element(buf, key, val) def serialize_object_element(buf, key, val, opcode=OBJECT)
buf.put(OBJECT) buf.put(opcode)
self.class.serialize_cstr(buf, key) self.class.serialize_cstr(buf, key)
buf.put_array(BSON.new.serialize(val).to_a) buf.put_array(BSON.new.serialize(val).to_a)
end end
def serialize_array_element(buf, key, val)
# Turn array into hash with integer indices as keys
h = OrderedHash.new
i = 0
val.each { |v| h[i] = v; i += 1 }
serialize_object_element(buf, key, h, ARRAY)
end
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)

View File

@ -94,4 +94,11 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 'index_name', info[:name] assert_equal 'index_name', info[:name]
assert_equal 1, info[:keys]['a'] assert_equal 1, info[:keys]['a']
end end
def test_array
@coll << {'b' => [1, 2, 3]}
rows = @coll.find({}, {'b' => 1}).collect
assert_equal 1, rows.length
assert_equal [1, 2, 3], rows[0]['b']
end
end end