diff --git a/README b/README index 4a24f1d..8e19986 100644 --- a/README +++ b/README @@ -52,6 +52,8 @@ type = To Do +* Support more types: REGEX, etc. + * Study src/main/ed/db/{dbcollection,dbcursor,db}.js in the Babble code. That's what I should be writing to. @@ -79,7 +81,9 @@ type Adrian Madrid, aemadrid@gmail.com * examples/benchmarks.rb * examples/irb.rb -* modifications to examples/simple.rb +* Modifications to examples/simple.rb +* Found plenty of bugs and missing features. +* Many other code suggestions and improvements. = License diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 13e6f1d..2b31bdb 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -36,7 +36,11 @@ module XGen res = @db.insert_into_db(@name, objects) res.size > 1 ? res : res.first end +<<<<<<< HEAD:lib/mongo/collection.rb alias :<< :insert +======= + alias_method :<<, :insert +>>>>>>> e7019a63f28331913c12e5c145d1f49af10b54ac:lib/mongo/collection.rb def remove(selector={}) @db.remove_from_db(@name, selector) diff --git a/lib/mongo/util/bson.rb b/lib/mongo/util/bson.rb index 2897abb..286c59e 100644 --- a/lib/mongo/util/bson.rb +++ b/lib/mongo/util/bson.rb @@ -1,4 +1,5 @@ require 'mongo/util/byte_buffer' +require 'mongo/util/ordered_hash' require 'mongo/objectid' # See http://github.com/10gen/mongo/tree/master/db/jsobj.h @@ -56,6 +57,8 @@ class BSON serialize_object_element(@buf, k, v) when OID serialize_oid_element(@buf, k, v) + when ARRAY + serialize_array_element(@buf, k, v) when BOOLEAN serialize_boolean_element(@buf, k, v) when DATE @@ -91,6 +94,9 @@ class BSON when OID key = deserialize_element_name(@buf) doc[key] = deserialize_oid_data(@buf) + when ARRAY + key = deserialize_element_name(@buf) + doc[key] = deserialize_array_data(@buf) when OBJECT key = deserialize_element_name(@buf) doc[key] = deserialize_object_data(@buf) @@ -149,6 +155,13 @@ class BSON BSON.new.deserialize(buf.get(size)) 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) len = buf.get_int bytes = buf.get(len) @@ -193,12 +206,20 @@ class BSON end end - def serialize_object_element(buf, key, val) - buf.put(OBJECT) + def serialize_object_element(buf, key, val, opcode=OBJECT) + buf.put(opcode) self.class.serialize_cstr(buf, key) buf.put_array(BSON.new.serialize(val).to_a) 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) buf.put(OID) self.class.serialize_cstr(buf, key) diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 0c03b7a..1cd22cd 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -36,6 +36,11 @@ class DBAPITest < Test::Unit::TestCase assert docs.detect { |row| row['a'] == 1 } assert docs.detect { |row| row['a'] == 2 } assert docs.detect { |row| row['b'] == 3 } + + @coll << {'b' => 4} + docs = @coll.find().collect + assert_equal 4, docs.length + assert docs.detect { |row| row['b'] == 4 } end def test_inserted_id @@ -155,7 +160,14 @@ class DBAPITest < Test::Unit::TestCase assert_equal 'index_name', info[:name] assert_equal 1, info[:keys]['a'] 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 + private def new_oid