REGEX type support.

This commit is contained in:
Jim Menard 2008-12-08 15:57:20 -05:00
parent e7019a63f2
commit 1431cb3ec3
3 changed files with 43 additions and 9 deletions

12
README
View File

@ -52,18 +52,11 @@ 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.
* Capped collection support.
* More code comments. More text in this file.
* Rake task for rdoc generation.
* Remove default _id generation.
* Support more types: REF, SYMBOL, CODE_W_SCOPE, etc.
* Introduce optional per-database and per-collection PKInjector.
@ -73,7 +66,8 @@ type
* Implement Admin.
* See FIXME in db test.
* Study src/main/ed/db/{dbcollection,dbcursor,db}.js and ByteEncoder.java in
the Babble code. That's what I should be writing to.
= Credits

View File

@ -59,6 +59,8 @@ class BSON
serialize_oid_element(@buf, k, v)
when ARRAY
serialize_array_element(@buf, k, v)
when REGEX
serialize_regex_element(@buf, k, v)
when BOOLEAN
serialize_boolean_element(@buf, k, v)
when DATE
@ -97,6 +99,9 @@ class BSON
when ARRAY
key = deserialize_element_name(@buf)
doc[key] = deserialize_array_data(@buf)
when REGEX
key = deserialize_element_name(@buf)
doc[key] = deserialize_regex_data(@buf)
when OBJECT
key = deserialize_element_name(@buf)
doc[key] = deserialize_object_data(@buf)
@ -162,6 +167,16 @@ class BSON
a
end
def deserialize_regex_data(buf)
str = deserialize_element_name(buf)
options_str = deserialize_element_name(buf)
options = 0
options |= Regexp::IGNORECASE if options_str.include?('i')
options |= Regexp::MULTILINE if options_str.include?('m')
options |= Regexp::EXTENDED if options_str.include?('x')
Regexp.new(str, options)
end
def deserialize_string_data(buf)
len = buf.get_int
bytes = buf.get(len)
@ -220,6 +235,21 @@ class BSON
serialize_object_element(buf, key, h, ARRAY)
end
def serialize_regex_element(buf, key, val)
buf.put(REGEX)
self.class.serialize_cstr(buf, key)
str = val.to_s.sub(/.*?:/, '')[0..-2] # Turn "(?xxx:yyy)" into "yyy"
self.class.serialize_cstr(buf, str)
options = val.options
options_str = ''
options_str << 'i' if ((options & Regexp::IGNORECASE) != 0)
options_str << 'm' if ((options & Regexp::MULTILINE) != 0)
options_str << 'x' if ((options & Regexp::EXTENDED) != 0)
self.class.serialize_cstr(buf, options_str)
end
def serialize_oid_element(buf, key, val)
buf.put(OID)
self.class.serialize_cstr(buf, key)
@ -274,6 +304,8 @@ class BSON
key == "$where" ? CODE : STRING
when Array
ARRAY
when Regexp
REGEX
when XGen::Mongo::Driver::ObjectID
OID
when true, false

View File

@ -101,4 +101,12 @@ class DBAPITest < Test::Unit::TestCase
assert_equal 1, rows.length
assert_equal [1, 2, 3], rows[0]['b']
end
def test_regex
regex = /foobar/i
@coll << {'b' => regex}
rows = @coll.find({}, {'b' => 1}).collect
assert_equal 1, rows.length
assert_equal regex, rows[0]['b']
end
end