Fixes for 1.9
This commit is contained in:
parent
4b849d4b4b
commit
d40f445c19
1
Rakefile
1
Rakefile
|
@ -10,6 +10,7 @@ rescue LoadError
|
||||||
end
|
end
|
||||||
require 'rbconfig'
|
require 'rbconfig'
|
||||||
include Config
|
include Config
|
||||||
|
ENV['TEST_MODE'] = 'TRUE'
|
||||||
|
|
||||||
gem_command = "gem"
|
gem_command = "gem"
|
||||||
gem_command = "gem1.9" if $0.match(/1\.9$/) # use gem1.9 if we used rake1.9
|
gem_command = "gem1.9" if $0.match(/1\.9$/) # use gem1.9 if we used rake1.9
|
||||||
|
|
|
@ -8,6 +8,8 @@ module Mongo
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
# Need this for running test with and without c ext in Ruby 1.9.
|
||||||
|
raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT']
|
||||||
require 'mongo_ext/cbson'
|
require 'mongo_ext/cbson'
|
||||||
require 'mongo/util/bson_c'
|
require 'mongo/util/bson_c'
|
||||||
BSON = BSON_C
|
BSON = BSON_C
|
||||||
|
@ -31,6 +33,7 @@ require 'mongo/types/regexp_of_holding'
|
||||||
require 'mongo/util/support'
|
require 'mongo/util/support'
|
||||||
require 'mongo/util/conversions'
|
require 'mongo/util/conversions'
|
||||||
require 'mongo/util/server_version'
|
require 'mongo/util/server_version'
|
||||||
|
require 'mongo/util/bson_ruby'
|
||||||
|
|
||||||
require 'mongo/errors'
|
require 'mongo/errors'
|
||||||
require 'mongo/constants'
|
require 'mongo/constants'
|
||||||
|
|
|
@ -216,7 +216,7 @@ module Mongo
|
||||||
def remove(selector={})
|
def remove(selector={})
|
||||||
message = ByteBuffer.new
|
message = ByteBuffer.new
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
BSON.serialize_cstr(message, "#{@db.name}.#{@name}")
|
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
message.put_array(BSON_SERIALIZER.serialize(selector, false).unpack("C*"))
|
message.put_array(BSON_SERIALIZER.serialize(selector, false).unpack("C*"))
|
||||||
@connection.send_message(Mongo::Constants::OP_DELETE, message,
|
@connection.send_message(Mongo::Constants::OP_DELETE, message,
|
||||||
|
@ -243,7 +243,7 @@ module Mongo
|
||||||
def update(selector, document, options={})
|
def update(selector, document, options={})
|
||||||
message = ByteBuffer.new
|
message = ByteBuffer.new
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
BSON.serialize_cstr(message, "#{@db.name}.#{@name}")
|
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
|
||||||
update_options = 0
|
update_options = 0
|
||||||
update_options += 1 if options[:upsert]
|
update_options += 1 if options[:upsert]
|
||||||
update_options += 2 if options[:multi]
|
update_options += 2 if options[:multi]
|
||||||
|
@ -507,7 +507,7 @@ EOS
|
||||||
def insert_documents(documents, collection_name=@name, check_keys=true, safe=false)
|
def insert_documents(documents, collection_name=@name, check_keys=true, safe=false)
|
||||||
message = ByteBuffer.new
|
message = ByteBuffer.new
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
BSON.serialize_cstr(message, "#{@db.name}.#{collection_name}")
|
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{collection_name}")
|
||||||
documents.each { |doc| message.put_array(BSON_SERIALIZER.serialize(doc, check_keys).unpack("C*")) }
|
documents.each { |doc| message.put_array(BSON_SERIALIZER.serialize(doc, check_keys).unpack("C*")) }
|
||||||
if safe
|
if safe
|
||||||
@connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name,
|
@connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name,
|
||||||
|
|
|
@ -467,7 +467,7 @@ module Mongo
|
||||||
def last_error_message(db_name)
|
def last_error_message(db_name)
|
||||||
message = ByteBuffer.new
|
message = ByteBuffer.new
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
BSON.serialize_cstr(message, "#{db_name}.$cmd")
|
BSON_RUBY.serialize_cstr(message, "#{db_name}.$cmd")
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
message.put_int(-1)
|
message.put_int(-1)
|
||||||
message.put_array(BSON_SERIALIZER.serialize({:getlasterror => 1}, false).unpack("C*"))
|
message.put_array(BSON_SERIALIZER.serialize({:getlasterror => 1}, false).unpack("C*"))
|
||||||
|
|
|
@ -286,7 +286,7 @@ module Mongo
|
||||||
|
|
||||||
# DB name.
|
# DB name.
|
||||||
db_name = @admin ? 'admin' : @db.name
|
db_name = @admin ? 'admin' : @db.name
|
||||||
BSON.serialize_cstr(message, "#{db_name}.#{@collection.name}")
|
BSON_RUBY.serialize_cstr(message, "#{db_name}.#{@collection.name}")
|
||||||
|
|
||||||
# Number of results to return; db decides for now.
|
# Number of results to return; db decides for now.
|
||||||
message.put_int(0)
|
message.put_int(0)
|
||||||
|
@ -317,7 +317,7 @@ module Mongo
|
||||||
message = ByteBuffer.new
|
message = ByteBuffer.new
|
||||||
message.put_int(query_opts)
|
message.put_int(query_opts)
|
||||||
db_name = @admin ? 'admin' : @db.name
|
db_name = @admin ? 'admin' : @db.name
|
||||||
BSON.serialize_cstr(message, "#{db_name}.#{@collection.name}")
|
BSON_RUBY.serialize_cstr(message, "#{db_name}.#{@collection.name}")
|
||||||
message.put_int(@skip)
|
message.put_int(@skip)
|
||||||
message.put_int(@limit)
|
message.put_int(@limit)
|
||||||
selector = @selector
|
selector = @selector
|
||||||
|
|
|
@ -1,25 +1,6 @@
|
||||||
# A thin wrapper for the CBson class
|
# A thin wrapper for the CBson class
|
||||||
class BSON_C
|
class BSON_C
|
||||||
|
|
||||||
if RUBY_VERSION >= '1.9'
|
|
||||||
def self.to_utf8(str)
|
|
||||||
str.encode("utf-8")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
def self.to_utf8(str)
|
|
||||||
begin
|
|
||||||
str.unpack("U*")
|
|
||||||
rescue => ex
|
|
||||||
raise InvalidStringEncoding, "String not valid utf-8: #{str}"
|
|
||||||
end
|
|
||||||
str
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.serialize_cstr(buf, val)
|
|
||||||
buf.put_array(to_utf8(val.to_s).unpack("C*") + [0])
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.serialize(obj, check_keys=false)
|
def self.serialize(obj, check_keys=false)
|
||||||
ByteBuffer.new(CBson.serialize(obj, check_keys))
|
ByteBuffer.new(CBson.serialize(obj, check_keys))
|
||||||
end
|
end
|
||||||
|
@ -34,12 +15,4 @@ class BSON_C
|
||||||
CBson.deserialize(buf.to_s)
|
CBson.deserialize(buf.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def deserialize(buf=nil)
|
|
||||||
self.class.deserialize(buf)
|
|
||||||
end
|
|
||||||
|
|
||||||
def serialize(buf, check_keys=false)
|
|
||||||
self.class.serialize(buf, check_keys)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -69,7 +69,7 @@ class BSON_RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.serialize_cstr(buf, val)
|
def self.serialize_cstr(buf, val)
|
||||||
buf.put_array(to_utf8(val.to_s).unpack("C*") + [0])
|
buf.put_array(to_utf8(val.to_s).unpack("C*") << 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_a
|
def to_a
|
||||||
|
|
|
@ -1,35 +1,31 @@
|
||||||
|
# coding:utf-8
|
||||||
require 'test/test_helper'
|
require 'test/test_helper'
|
||||||
require 'iconv'
|
|
||||||
|
|
||||||
class BSONTest < Test::Unit::TestCase
|
class BSONTest < Test::Unit::TestCase
|
||||||
|
|
||||||
include Mongo
|
include Mongo
|
||||||
|
|
||||||
def setup
|
|
||||||
# We don't pass a DB to the constructor, even though we are about to test
|
|
||||||
# deserialization. This means that when we deserialize, any DBRefs will
|
|
||||||
# have nil @db ivars. That's fine for now.
|
|
||||||
#BSON = BSON.new
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_string
|
def test_string
|
||||||
doc = {'doc' => 'hello, world'}
|
doc = {'doc' => 'hello, world'}
|
||||||
bson = bson = BSON.serialize(doc)
|
bson = bson = BSON.serialize(doc)
|
||||||
assert_equal doc, BSON.deserialize(bson)
|
assert_equal doc, BSON.deserialize(bson)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def test_valid_utf8_string
|
def test_valid_utf8_string
|
||||||
doc = {'doc' => "aéあ"}
|
doc = {'doc' => "aé"}
|
||||||
bson = bson = BSON.serialize(doc)
|
bson = bson = BSON.serialize(doc)
|
||||||
assert_equal doc, BSON.deserialize(bson)
|
assert_equal doc, BSON.deserialize(bson)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_invalid_string
|
# We'll raise this exception only in 1.8 since 1.9 forces UTF-8 conversion.
|
||||||
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé').first
|
if RUBY_VERSION < '1.9'
|
||||||
doc = {'doc' => string}
|
require 'iconv'
|
||||||
assert_raise InvalidStringEncoding do
|
def test_invalid_string
|
||||||
BSON.serialize(doc)
|
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé').first
|
||||||
|
doc = {'doc' => string}
|
||||||
|
assert_raise InvalidStringEncoding do
|
||||||
|
BSON.serialize(doc)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue