mongo-ruby-driver/lib/bson.rb

102 lines
3.0 KiB
Ruby
Raw Permalink Normal View History

# encoding: UTF-8
2011-01-17 17:26:32 +00:00
#
# --
# Copyright (C) 2008-2011 10gen Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ++
2012-02-28 17:04:49 +00:00
require 'bson/version'
2010-04-05 18:09:06 +00:00
module BSON
2011-01-03 20:09:07 +00:00
if defined? Mongo::DEFAULT_MAX_BSON_SIZE
DEFAULT_MAX_BSON_SIZE = Mongo::DEFAULT_MAX_BSON_SIZE
else
DEFAULT_MAX_BSON_SIZE = 4 * 1024 * 1024
end
2010-04-05 18:09:06 +00:00
def self.serialize(obj, check_keys=false, move_id=false)
BSON_CODER.serialize(obj, check_keys, move_id)
end
def self.deserialize(buf=nil)
BSON_CODER.deserialize(buf)
end
# Reads a single BSON document from an IO object.
2010-05-28 18:04:07 +00:00
# This method is used in the executable b2json, bundled with
2010-05-28 18:04:33 +00:00
# the bson gem, for reading a file of bson documents.
#
# @param [IO] io an io object containing a bson object.
#
# @return [ByteBuffer]
def self.read_bson_document(io)
bytebuf = BSON::ByteBuffer.new
sz = io.read(4).unpack("V")[0]
bytebuf.put_int(sz)
bytebuf.put_array(io.read(sz-4).unpack("C*"))
bytebuf.rewind
return BSON.deserialize(bytebuf)
end
2010-04-05 18:09:06 +00:00
end
2010-09-30 13:43:17 +00:00
if RUBY_PLATFORM =~ /java/
jar_dir = File.join(File.dirname(__FILE__), '..', 'ext', 'java', 'jar')
2011-08-10 18:14:52 +00:00
require File.join(jar_dir, 'mongo-2.6.5.jar')
2010-09-30 13:43:17 +00:00
require File.join(jar_dir, 'jbson.jar')
require 'bson/bson_java'
2010-04-05 18:09:06 +00:00
module BSON
2010-09-30 13:43:17 +00:00
BSON_CODER = BSON_JAVA
2010-04-05 18:09:06 +00:00
end
2010-09-30 13:43:17 +00:00
else
begin
# Need this for running test with and without c ext in Ruby 1.9.
raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT']
2010-11-11 20:02:16 +00:00
2012-02-28 17:04:49 +00:00
# Raise LoadError unless little endian, since the C extensions
# only work on little-endian architectures.
raise LoadError unless "\x01\x00\x00\x00".unpack("i").first == 1
2010-11-11 20:02:16 +00:00
2010-09-30 13:43:17 +00:00
require 'bson_ext/cbson'
raise LoadError unless defined?(CBson::VERSION)
require 'bson/bson_c'
module BSON
BSON_CODER = BSON_C
end
2010-10-04 19:19:14 +00:00
rescue LoadError
2010-09-30 13:43:17 +00:00
require 'bson/bson_ruby'
module BSON
BSON_CODER = BSON_RUBY
end
unless ENV['TEST_MODE']
warn "\n**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance."
warn " You can install the extension as follows:\n gem install bson_ext\n"
warn " If you continue to receive this message after installing, make sure that the"
warn " bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.\n"
end
2010-04-05 18:09:06 +00:00
end
end
require 'bson/types/binary'
require 'bson/types/code'
require 'bson/types/dbref'
require 'bson/types/object_id'
2010-04-05 18:09:06 +00:00
require 'bson/types/min_max_keys'
2011-03-28 18:36:49 +00:00
require 'bson/types/timestamp'
2010-04-05 18:09:06 +00:00
require 'base64'
require 'bson/ordered_hash'
require 'bson/byte_buffer'
require 'bson/bson_ruby'
require 'bson/exceptions'