Merge branch 'master' of git://github.com/jimm/mongo-ruby-driver

This commit is contained in:
Adrian Madrid 2008-12-08 13:33:30 -07:00
commit 366668679e
4 changed files with 45 additions and 4 deletions

6
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.
@ -79,7 +81,9 @@ type
Adrian Madrid, aemadrid@gmail.com Adrian Madrid, aemadrid@gmail.com
* examples/benchmarks.rb * examples/benchmarks.rb
* examples/irb.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 = License

View File

@ -36,7 +36,11 @@ module XGen
res = @db.insert_into_db(@name, objects) res = @db.insert_into_db(@name, objects)
res.size > 1 ? res : res.first res.size > 1 ? res : res.first
end end
<<<<<<< HEAD:lib/mongo/collection.rb
alias :<< :insert alias :<< :insert
=======
alias_method :<<, :insert
>>>>>>> e7019a63f28331913c12e5c145d1f49af10b54ac:lib/mongo/collection.rb
def remove(selector={}) def remove(selector={})
@db.remove_from_db(@name, selector) @db.remove_from_db(@name, selector)

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

@ -36,6 +36,11 @@ class DBAPITest < Test::Unit::TestCase
assert docs.detect { |row| row['a'] == 1 } assert docs.detect { |row| row['a'] == 1 }
assert docs.detect { |row| row['a'] == 2 } assert docs.detect { |row| row['a'] == 2 }
assert docs.detect { |row| row['b'] == 3 } 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 end
def test_inserted_id def test_inserted_id
@ -155,7 +160,14 @@ 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
private private
def new_oid def new_oid