Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes for 1.9
  • Loading branch information
banker committed Dec 1, 2009
1 parent 4b849d4 commit d40f445
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 49 deletions.
1 change: 1 addition & 0 deletions Rakefile
Expand Up @@ -10,6 +10,7 @@ rescue LoadError
end
require 'rbconfig'
include Config
ENV['TEST_MODE'] = 'TRUE'

gem_command = "gem"
gem_command = "gem1.9" if $0.match(/1\.9$/) # use gem1.9 if we used rake1.9
Expand Down
3 changes: 3 additions & 0 deletions lib/mongo.rb
Expand Up @@ -8,6 +8,8 @@ module Mongo
end

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/util/bson_c'
BSON = BSON_C
Expand All @@ -31,6 +33,7 @@ module Mongo
require 'mongo/util/support'
require 'mongo/util/conversions'
require 'mongo/util/server_version'
require 'mongo/util/bson_ruby'

require 'mongo/errors'
require 'mongo/constants'
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo/collection.rb
Expand Up @@ -216,7 +216,7 @@ def insert(doc_or_docs, options={})
def remove(selector={})
message = ByteBuffer.new
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_array(BSON_SERIALIZER.serialize(selector, false).unpack("C*"))
@connection.send_message(Mongo::Constants::OP_DELETE, message,
Expand All @@ -243,7 +243,7 @@ def remove(selector={})
def update(selector, document, options={})
message = ByteBuffer.new
message.put_int(0)
BSON.serialize_cstr(message, "#{@db.name}.#{@name}")
BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
update_options = 0
update_options += 1 if options[:upsert]
update_options += 2 if options[:multi]
Expand Down Expand Up @@ -507,7 +507,7 @@ def normalize_hint_fields(hint)
def insert_documents(documents, collection_name=@name, check_keys=true, safe=false)
message = ByteBuffer.new
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*")) }
if safe
@connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name,
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/connection.rb
Expand Up @@ -467,7 +467,7 @@ def read_documents(number_received, cursor_id, sock)
def last_error_message(db_name)
message = ByteBuffer.new
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(-1)
message.put_array(BSON_SERIALIZER.serialize({:getlasterror => 1}, false).unpack("C*"))
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/cursor.rb
Expand Up @@ -286,7 +286,7 @@ def refill_via_get_more

# 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.
message.put_int(0)
Expand Down Expand Up @@ -317,7 +317,7 @@ def construct_query_message
message = ByteBuffer.new
message.put_int(query_opts)
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(@limit)
selector = @selector
Expand Down
27 changes: 0 additions & 27 deletions lib/mongo/util/bson_c.rb
@@ -1,25 +1,6 @@
# A thin wrapper for the CBson class
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)
ByteBuffer.new(CBson.serialize(obj, check_keys))
end
Expand All @@ -34,12 +15,4 @@ def self.deserialize(buf=nil)
CBson.deserialize(buf.to_s)
end

def deserialize(buf=nil)
self.class.deserialize(buf)
end

def serialize(buf, check_keys=false)
self.class.serialize(buf, check_keys)
end

end
2 changes: 1 addition & 1 deletion lib/mongo/util/bson_ruby.rb
Expand Up @@ -69,7 +69,7 @@ def self.to_utf8(str)
end

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

def to_a
Expand Down
26 changes: 11 additions & 15 deletions test/test_bson.rb
@@ -1,35 +1,31 @@
# coding:utf-8
require 'test/test_helper'
require 'iconv'

class BSONTest < Test::Unit::TestCase

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
doc = {'doc' => 'hello, world'}
bson = bson = BSON.serialize(doc)
assert_equal doc, BSON.deserialize(bson)
end


def test_valid_utf8_string
doc = {'doc' => "aéあ"}
doc = {'doc' => ""}
bson = bson = BSON.serialize(doc)
assert_equal doc, BSON.deserialize(bson)
end

def test_invalid_string
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé').first
doc = {'doc' => string}
assert_raise InvalidStringEncoding do
BSON.serialize(doc)
# We'll raise this exception only in 1.8 since 1.9 forces UTF-8 conversion.
if RUBY_VERSION < '1.9'
require 'iconv'
def test_invalid_string
string = Iconv.conv('iso-8859-1', 'utf-8', 'aé').first
doc = {'doc' => string}
assert_raise InvalidStringEncoding do
BSON.serialize(doc)
end
end
end

Expand Down

0 comments on commit d40f445

Please sign in to comment.