From 18d7d1c6999f31a9129ecbef621c4ab8be2820a2 Mon Sep 17 00:00:00 2001 From: Charles Remes Date: Wed, 17 Mar 2010 14:30:33 -0500 Subject: [PATCH] Split the BSON logic out to a separate gem - changed the BSON constant to BSON_CODER and scoped it inside of a module - changed the directory layout for all of the BSON related files - updated the C extension to find the BSON files at their new directory locations - updated the C extension to use better/safer macros for accessing the C API; extension now compiles cleanly under rubinius/rbx - changed directory layout for BSON related tests - modified the Rakefile to understand the new layout --- lib/mongo_bson.rb | 40 +++++++++++++++++++ lib/{mongo/util => mongo_bson}/bson_c.rb | 0 lib/{mongo/util => mongo_bson}/bson_ruby.rb | 0 lib/{mongo/util => mongo_bson}/byte_buffer.rb | 0 lib/mongo_bson/exceptions.rb | 36 +++++++++++++++++ .../util => mongo_bson}/ordered_hash.rb | 0 lib/{mongo => mongo_bson}/types/binary.rb | 0 lib/{mongo => mongo_bson}/types/code.rb | 0 lib/{mongo => mongo_bson}/types/dbref.rb | 0 .../types/min_max_keys.rb | 0 lib/{mongo => mongo_bson}/types/objectid.rb | 0 .../types/regexp_of_holding.rb | 0 mongo-bson.gemspec | 23 +++++++++++ test/{ => mongo_bson}/binary_test.rb | 0 test/{ => mongo_bson}/bson_test.rb | 0 test/{ => mongo_bson}/byte_buffer_test.rb | 0 test/{ => mongo_bson}/objectid_test.rb | 0 test/{ => mongo_bson}/ordered_hash_test.rb | 0 18 files changed, 99 insertions(+) create mode 100644 lib/mongo_bson.rb rename lib/{mongo/util => mongo_bson}/bson_c.rb (100%) rename lib/{mongo/util => mongo_bson}/bson_ruby.rb (100%) rename lib/{mongo/util => mongo_bson}/byte_buffer.rb (100%) create mode 100644 lib/mongo_bson/exceptions.rb rename lib/{mongo/util => mongo_bson}/ordered_hash.rb (100%) rename lib/{mongo => mongo_bson}/types/binary.rb (100%) rename lib/{mongo => mongo_bson}/types/code.rb (100%) rename lib/{mongo => mongo_bson}/types/dbref.rb (100%) rename lib/{mongo => mongo_bson}/types/min_max_keys.rb (100%) rename lib/{mongo => mongo_bson}/types/objectid.rb (100%) rename lib/{mongo => mongo_bson}/types/regexp_of_holding.rb (100%) create mode 100644 mongo-bson.gemspec rename test/{ => mongo_bson}/binary_test.rb (100%) rename test/{ => mongo_bson}/bson_test.rb (100%) rename test/{ => mongo_bson}/byte_buffer_test.rb (100%) rename test/{ => mongo_bson}/objectid_test.rb (100%) rename test/{ => mongo_bson}/ordered_hash_test.rb (100%) diff --git a/lib/mongo_bson.rb b/lib/mongo_bson.rb new file mode 100644 index 0000000..e88591b --- /dev/null +++ b/lib/mongo_bson.rb @@ -0,0 +1,40 @@ +$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) + +module Mongo + module BSON + VERSION = "0.19.2" + end +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' + raise LoadError unless defined?(CBson::VERSION) && CBson::VERSION == Mongo::BSON::VERSION + require 'mongo_bson/bson_c' + module Mongo + BSON_CODER = BSON_C + end +rescue LoadError + require 'mongo_bson/bson_ruby' + module Mongo + BSON_CODER = BSON_RUBY + end + 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 mongo_ext\n" + warn " If you continue to receive this message after installing, make sure that the" + warn " mongo_ext gem is in your load path and that the mongo_ext and mongo gems are of the same version.\n" +end + +require 'mongo_bson/types/binary' +require 'mongo_bson/types/code' +require 'mongo_bson/types/dbref' +require 'mongo_bson/types/objectid' +require 'mongo_bson/types/regexp_of_holding' +require 'mongo_bson/types/min_max_keys' + +require 'base64' +require 'mongo_bson/ordered_hash' +require 'mongo_bson/byte_buffer' +require 'mongo_bson/bson_ruby' +require 'mongo_bson/exceptions' diff --git a/lib/mongo/util/bson_c.rb b/lib/mongo_bson/bson_c.rb similarity index 100% rename from lib/mongo/util/bson_c.rb rename to lib/mongo_bson/bson_c.rb diff --git a/lib/mongo/util/bson_ruby.rb b/lib/mongo_bson/bson_ruby.rb similarity index 100% rename from lib/mongo/util/bson_ruby.rb rename to lib/mongo_bson/bson_ruby.rb diff --git a/lib/mongo/util/byte_buffer.rb b/lib/mongo_bson/byte_buffer.rb similarity index 100% rename from lib/mongo/util/byte_buffer.rb rename to lib/mongo_bson/byte_buffer.rb diff --git a/lib/mongo_bson/exceptions.rb b/lib/mongo_bson/exceptions.rb new file mode 100644 index 0000000..10f5dcb --- /dev/null +++ b/lib/mongo_bson/exceptions.rb @@ -0,0 +1,36 @@ +# -- +# Copyright (C) 2008-2010 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. +# ++ + +module Mongo + # Generic Mongo Ruby Driver exception class. + class MongoRubyError < StandardError; end + + # Raised when MongoDB itself has returned an error. + class MongoDBError < RuntimeError; end + + # Raised when given a string is not valid utf-8 (Ruby 1.8 only). + class InvalidStringEncoding < MongoRubyError; end + + # Raised when attempting to initialize an invalid ObjectID. + class InvalidObjectID < MongoRubyError; end + + # Raised when trying to insert a document that exceeds the 4MB limit or + # when the document contains objects that can't be serialized as BSON. + class InvalidDocument < MongoDBError; end + + # Raised when an invalid name is used. + class InvalidName < RuntimeError; end +end diff --git a/lib/mongo/util/ordered_hash.rb b/lib/mongo_bson/ordered_hash.rb similarity index 100% rename from lib/mongo/util/ordered_hash.rb rename to lib/mongo_bson/ordered_hash.rb diff --git a/lib/mongo/types/binary.rb b/lib/mongo_bson/types/binary.rb similarity index 100% rename from lib/mongo/types/binary.rb rename to lib/mongo_bson/types/binary.rb diff --git a/lib/mongo/types/code.rb b/lib/mongo_bson/types/code.rb similarity index 100% rename from lib/mongo/types/code.rb rename to lib/mongo_bson/types/code.rb diff --git a/lib/mongo/types/dbref.rb b/lib/mongo_bson/types/dbref.rb similarity index 100% rename from lib/mongo/types/dbref.rb rename to lib/mongo_bson/types/dbref.rb diff --git a/lib/mongo/types/min_max_keys.rb b/lib/mongo_bson/types/min_max_keys.rb similarity index 100% rename from lib/mongo/types/min_max_keys.rb rename to lib/mongo_bson/types/min_max_keys.rb diff --git a/lib/mongo/types/objectid.rb b/lib/mongo_bson/types/objectid.rb similarity index 100% rename from lib/mongo/types/objectid.rb rename to lib/mongo_bson/types/objectid.rb diff --git a/lib/mongo/types/regexp_of_holding.rb b/lib/mongo_bson/types/regexp_of_holding.rb similarity index 100% rename from lib/mongo/types/regexp_of_holding.rb rename to lib/mongo_bson/types/regexp_of_holding.rb diff --git a/mongo-bson.gemspec b/mongo-bson.gemspec new file mode 100644 index 0000000..84b69ac --- /dev/null +++ b/mongo-bson.gemspec @@ -0,0 +1,23 @@ +require "lib/mongo_bson" + +Gem::Specification.new do |s| + s.name = 'mongo_bson' + + s.version = Mongo::BSON::VERSION + + s.platform = Gem::Platform::RUBY + s.summary = 'Ruby implementation of BSON' + s.description = 'A Ruby BSON implementation for MongoDB. For more information about Mongo, see http://www.mongodb.org. For more information on BSON, see http://www.bsonspec.org.' + + s.require_paths = ['lib'] + + s.files = ['Rakefile', 'mongo-bson.gemspec', 'LICENSE.txt'] + s.files += ['lib/mongo_bson.rb'] + Dir['lib/mongo_bson/**/*.rb'] + s.test_files = Dir['test/mongo_bson/*.rb'] + + s.has_rdoc = true + + s.authors = ['Jim Menard', 'Mike Dirolf', 'Kyle Banker'] + s.email = 'mongodb-dev@googlegroups.com' + s.homepage = 'http://www.mongodb.org' +end diff --git a/test/binary_test.rb b/test/mongo_bson/binary_test.rb similarity index 100% rename from test/binary_test.rb rename to test/mongo_bson/binary_test.rb diff --git a/test/bson_test.rb b/test/mongo_bson/bson_test.rb similarity index 100% rename from test/bson_test.rb rename to test/mongo_bson/bson_test.rb diff --git a/test/byte_buffer_test.rb b/test/mongo_bson/byte_buffer_test.rb similarity index 100% rename from test/byte_buffer_test.rb rename to test/mongo_bson/byte_buffer_test.rb diff --git a/test/objectid_test.rb b/test/mongo_bson/objectid_test.rb similarity index 100% rename from test/objectid_test.rb rename to test/mongo_bson/objectid_test.rb diff --git a/test/ordered_hash_test.rb b/test/mongo_bson/ordered_hash_test.rb similarity index 100% rename from test/ordered_hash_test.rb rename to test/mongo_bson/ordered_hash_test.rb