lib/bson.rb in bson-1.12.5-java vs lib/bson.rb in bson-2.0.0.alpha
- old
+ new
@@ -1,113 +1,124 @@
-# Copyright (C) 2009-2013 MongoDB, Inc.
+# encoding: utf-8
+
+# Determine if we are using JRuby or not.
#
-# 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
+# @example Are we running with JRuby?
+# jruby?
#
-# http://www.apache.org/licenses/LICENSE-2.0
+# @return [ true, false ] If JRuby is our vm.
#
-# 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.
+# @since 2.0.0
+def jruby?
+ defined?(JRUBY_VERSION)
+end
-module BSON
- DEFAULT_MAX_BSON_SIZE = 4 * 1024 * 1024
+# Does the Ruby runtime we are using support ordered hashes?
+#
+# @example Does the runtime support ordered hashes?
+# ordered_hash_support?
+#
+# @return [ true, false ] If the runtime has ordered hashes.
+#
+# @since 2.0.0
+def ordered_hash_support?
+ jruby? || RUBY_VERSION > "1.9.1"
+end
- def self.serialize(obj, check_keys=false, move_id=false)
- BSON_CODER.serialize(obj, check_keys, move_id)
- end
+# Are we running in a ruby runtime that is version 1.8.x?
+#
+# @since 2.0.0
+def ruby_18?
+ RUBY_VERSION < "1.9"
+end
- def self.deserialize(buf=nil, opts={})
- BSON_CODER.deserialize(buf, opts)
- end
+# In the case where we don't have encoding, we need to monkey
+# patch string to ignore the encoding directives.
+#
+# @since 2.0.0
+if ruby_18?
- # Reads a single BSON document from an IO object.
- # This method is used in the executable b2json, bundled with
- # 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
+ class String
- def self.extension?
- !((ENV.key?('BSON_EXT_DISABLED') && RUBY_PLATFORM =~ /java/) ||
- (ENV.key?('BSON_EXT_DISABLED') || "\x01\x00\x00\x00".unpack("i")[0] != 1))
+ # In versions prior to 1.9 we need to ignore the encoding requests.
+ #
+ # @since 2.0.0
+ def chr; self; end
+ def force_encoding(*); self; end
+ def encode(*); self; end
+ def encode!(*); self; end
end
+
+ class EncodingError < RuntimeError; end
end
-begin
- # Skips loading extensions if one of the following is true:
- # 1) JRuby and BSON_EXT_DISABLED is set.
- # -OR-
- # 2) Ruby MRI and big endian or BSON_EXT_DISABLED is set.
- raise LoadError unless BSON.extension?
+#
+# The core namespace for all BSON related behaviour.
+#
+# @since 0.0.0
+module BSON
- if RUBY_PLATFORM =~ /java/
- require 'bson/bson_java'
- module BSON
- BSON_CODER = BSON_JAVA
- end
- else
- require 'bson_ext/cbson'
- raise LoadError unless defined?(CBson::VERSION)
- require 'bson/bson_c'
- module BSON
- BSON_CODER = BSON_C
- end
- end
-rescue LoadError
- require 'bson/bson_ruby'
- module BSON
- BSON_CODER = BSON_RUBY
- end
+ # Constant for binary string encoding.
+ #
+ # @since 2.0.0
+ BINARY = "BINARY".freeze
- if RUBY_PLATFORM =~ /java/
- unless ENV['TEST_MODE']
- warn <<-NOTICE
- ** Notice: The BSON extension was not loaded. **
+ # Constant for bson types that don't actually serialize a value.
+ #
+ # @since 2.0.0
+ NO_VALUE = "".force_encoding(BINARY).freeze
- For optimal performance, use of the BSON extension is recommended. To
- enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set.
- NOTICE
- end
- else
- unless ENV['TEST_MODE']
- warn <<-NOTICE
- ** Notice: The native BSON extension was not loaded. **
+ # Constant for a null byte (0x00).
+ #
+ # @since 2.0.0
+ NULL_BYTE = 0.chr.force_encoding(BINARY).freeze
- For optimal performance, use of the BSON extension is recommended.
+ # Constant for UTF-8 string encoding.
+ #
+ # @since 2.0.0
+ UTF8 = "UTF-8".freeze
+end
- To enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set
- and run the following command:
+require "bson/registry"
+require "bson/specialized"
+require "bson/json"
+require "bson/int32"
+require "bson/int64"
+require "bson/integer"
+require "bson/encodable"
+require "bson/array"
+require "bson/binary"
+require "bson/boolean"
+require "bson/code"
+require "bson/code_with_scope"
+require "bson/document"
+require "bson/false_class"
+require "bson/float"
+require "bson/hash"
+require "bson/max_key"
+require "bson/min_key"
+require "bson/nil_class"
+require "bson/object_id"
+require "bson/regexp"
+require "bson/string"
+require "bson/symbol"
+require "bson/time"
+require "bson/timestamp"
+require "bson/true_class"
+require "bson/undefined"
+require "bson/version"
- gem install bson_ext
-
- If you continue to receive this message after installing, make sure that
- the bson_ext gem is in your load path.
- NOTICE
- end
+# If we are using JRuby, attempt to load the Java extensions, if we are using
+# MRI or Rubinius, attempt to load the C extenstions. If either of these fail,
+# we revert back to a pure Ruby implementation of the Buffer class.
+#
+# @since 2.0.0
+begin
+ if jruby?
+ require "bson/NativeService.jar"
+ org.bson.NativeService.new.basicLoad(JRuby.runtime)
+ else
+ require "bson/native"
end
+rescue LoadError
+ $stderr.puts("BSON is using the pure Ruby implementation.")
end
-
-require 'base64'
-require 'bson/bson_ruby'
-require 'bson/byte_buffer'
-require 'bson/exceptions'
-require 'bson/ordered_hash'
-require 'bson/types/binary'
-require 'bson/types/code'
-require 'bson/types/dbref'
-require 'bson/types/min_max_keys'
-require 'bson/types/regex'
-require 'bson/types/object_id'
-require 'bson/types/timestamp'