bson 1.12.5 → 2.0.0.alpha

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of bson might be problematic. Click here for more details.

Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +0 -0
  3. data/CONTRIBUTING.md +0 -0
  4. data/LICENSE.md +13 -0
  5. data/README.md +185 -0
  6. data/Rakefile +83 -0
  7. data/ext/bson/extconf.rb +3 -0
  8. data/lib/bson.rb +105 -94
  9. data/lib/bson/array.rb +74 -0
  10. data/lib/bson/binary.rb +125 -0
  11. data/lib/bson/boolean.rb +35 -0
  12. data/lib/bson/code.rb +96 -0
  13. data/lib/bson/code_with_scope.rb +105 -0
  14. data/lib/bson/document.rb +536 -0
  15. data/lib/bson/encodable.rb +73 -0
  16. data/lib/bson/false_class.rb +48 -0
  17. data/lib/bson/float.rb +69 -0
  18. data/lib/bson/hash.rb +71 -0
  19. data/lib/bson/int32.rb +46 -0
  20. data/lib/bson/int64.rb +46 -0
  21. data/lib/bson/integer.rb +172 -0
  22. data/lib/bson/json.rb +26 -0
  23. data/lib/bson/max_key.rb +57 -0
  24. data/lib/bson/min_key.rb +57 -0
  25. data/lib/bson/nil_class.rb +57 -0
  26. data/lib/bson/object_id.rb +309 -0
  27. data/lib/bson/regexp.rb +111 -0
  28. data/lib/bson/registry.rb +57 -0
  29. data/lib/bson/specialized.rb +61 -0
  30. data/lib/bson/string.rb +173 -0
  31. data/lib/bson/symbol.rb +74 -0
  32. data/lib/bson/time.rb +59 -0
  33. data/lib/bson/timestamp.rb +100 -0
  34. data/lib/bson/true_class.rb +48 -0
  35. data/lib/bson/undefined.rb +61 -0
  36. data/lib/bson/version.rb +4 -0
  37. metadata +52 -40
  38. data/LICENSE +0 -190
  39. data/VERSION +0 -1
  40. data/bin/b2json +0 -63
  41. data/bin/j2bson +0 -64
  42. data/bson.gemspec +0 -34
  43. data/lib/bson/bson_c.rb +0 -37
  44. data/lib/bson/bson_java.rb +0 -49
  45. data/lib/bson/bson_ruby.rb +0 -645
  46. data/lib/bson/byte_buffer.rb +0 -241
  47. data/lib/bson/exceptions.rb +0 -37
  48. data/lib/bson/grow.rb +0 -173
  49. data/lib/bson/ordered_hash.rb +0 -197
  50. data/lib/bson/support/hash_with_indifferent_access.rb +0 -174
  51. data/lib/bson/types/binary.rb +0 -52
  52. data/lib/bson/types/code.rb +0 -55
  53. data/lib/bson/types/dbref.rb +0 -40
  54. data/lib/bson/types/min_max_keys.rb +0 -56
  55. data/lib/bson/types/object_id.rb +0 -226
  56. data/lib/bson/types/regex.rb +0 -116
  57. data/lib/bson/types/timestamp.rb +0 -72
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09ee8db8844e09b37a0367f29804bca04f24fd42
4
- data.tar.gz: e8c27a49f6d3ddc608958c8527597ec5f9b3b3c6
3
+ metadata.gz: e50654bfd852cf0daad41abc4d7249bf6dc293fc
4
+ data.tar.gz: 13709ff7d3d55d1990df6ac2f9c92acf193ae242
5
5
  SHA512:
6
- metadata.gz: 79305ba921fc09f772b97e07d7e98da6f8c3af9f52e3589b4bfcae460c5cc01f2c7cc3022c995265e1b2ca03c051faf7fa85dd2c96d0ef51cb3d97e955a6497c
7
- data.tar.gz: cb70dcb29e040f4ab2391c6ad0ce97322f23e39e72e32c949b60d3ae3086f6fc6e93983e59788d78f6f56898c84c271a9d9bfc77905ea8c17fb52faf6420c3c4
6
+ metadata.gz: 7caafece5f3d7fd32f470e928c9ef94297cefc953ba4a8b656b68258db73ecc397f1ff3f27d79d735e713e5cb586ec7882b17fba114172f1b67a739108984f06
7
+ data.tar.gz: aad6c2fea3db480b2e8b763e45c08cf9c663e28713f8322020921bce35a9aede9c32cbfcd4e324df9798b4fa6986ac146ebd0299d6be7eb29c6f650ed0e5a810
File without changes
File without changes
@@ -0,0 +1,13 @@
1
+ Copyright (C) 2013 10gen Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,185 @@
1
+ BSON [![Build Status](https://secure.travis-ci.org/mongodb/bson-ruby.png?branch=master&.png)](http://travis-ci.org/mongodb/bson-ruby) [![Code Climate](https://codeclimate.com/github/mongodb/bson-ruby.png)](https://codeclimate.com/github/mongodb/bson-ruby)
2
+ ====
3
+
4
+ An implementation of the BSON specification in Ruby.
5
+
6
+ Compatibility
7
+ -------------
8
+
9
+ BSON is tested against MRI (1.8.7+), JRuby (1.5.0+), Rubinius (2.0.0+), and REE.
10
+
11
+ Installation
12
+ ------------
13
+
14
+ With bundler, add the `bson` gem to your `Gemfile`. As of 2.0.0 native extensions
15
+ are bundled with the `bson` gem and `bson_ext` is no longer needed.
16
+
17
+ ```ruby
18
+ gem "bson", "~> 2.0"
19
+ ```
20
+
21
+ Require the `bson` gem in your application.
22
+
23
+ ```ruby
24
+ require "bson"
25
+ ```
26
+
27
+ Usage
28
+ -----
29
+
30
+ ### BSON Serialization
31
+
32
+ Getting a Ruby object's raw BSON representation is done by calling `to_bson`
33
+ on the Ruby object. For example:
34
+
35
+ ```ruby
36
+ "Shall I compare thee to a summer's day".to_bson
37
+ 1024.to_bson
38
+ ```
39
+
40
+ Generating an object from BSON is done via calling `from_bson` on the class
41
+ you wish to instantiate and passing it the `StringIO` bytes.
42
+
43
+ ```ruby
44
+ String.from_bson(string_io)
45
+ Int32.from_bson(string_io)
46
+ ```
47
+
48
+ Core Ruby object's that have representations in the BSON specification and
49
+ will have a `to_bson` method defined for them are:
50
+
51
+ - `Array`
52
+ - `FalseClass`
53
+ - `Float`
54
+ - `Hash`
55
+ - `Integer`
56
+ - `NilClass`
57
+ - `Regexp`
58
+ - `String`
59
+ - `Symbol` (deprecated)
60
+ - `Time`
61
+ - `TrueClass`
62
+
63
+ In addition to the core Ruby objects, BSON also provides some special types
64
+ specific to the specification:
65
+
66
+ #### `BSON::Binary`
67
+
68
+ This is a representation of binary data, and must provide the raw data and
69
+ a subtype when constructing.
70
+
71
+ ```ruby
72
+ BSON::Binary.new(binary_data, :md5)
73
+ ```
74
+
75
+ Valid subtypes are: `:generic`, `:function`, `:old`, `:uuid_old`, `:uuid`,
76
+ `:md5`, `:user`.
77
+
78
+ #### `BSON::Code`
79
+
80
+ Represents a string of Javascript code.
81
+
82
+ ```ruby
83
+ BSON::Code.new("this.value = 5;")
84
+ ```
85
+
86
+ #### `BSON::CodeWithScope`
87
+
88
+ Represents a string of Javascript code with a hash of values.
89
+
90
+ ```ruby
91
+ BSON::CodeWithScope.new("this.value = age;", age: 5)
92
+ ```
93
+
94
+ #### `BSON::Document`
95
+
96
+ This is a special ordered hash for use with Ruby below 1.9, and is simply
97
+ a subclass of a Ruby hash in 1.9 and higher.
98
+
99
+ ```ruby
100
+ BSON::Document[:key, "value"]
101
+ BSON::Document.new
102
+ ```
103
+
104
+ #### `BSON::MaxKey`
105
+
106
+ Represents a value in BSON that will always compare higher to another value.
107
+
108
+ ```ruby
109
+ BSON::MaxKey.new
110
+ ```
111
+
112
+ #### `BSON::MinKey`
113
+
114
+ Represents a value in BSON that will always compare lower to another value.
115
+
116
+ ```ruby
117
+ BSON::MinKey.new
118
+ ```
119
+
120
+ #### `BSON::ObjectId`
121
+
122
+ Represents a 12 byte unique identifier for an object on a given machine.
123
+
124
+ ```ruby
125
+ BSON::ObjectId.new
126
+ ```
127
+
128
+ #### `BSON::Timestamp`
129
+
130
+ Represents a special time with a start and increment value.
131
+
132
+ ```ruby
133
+ BSON::Timestamp.new(5, 30)
134
+ ```
135
+
136
+ #### `BSON::Undefined`
137
+
138
+ Represents a placeholder for a value that was not provided.
139
+
140
+ ```ruby
141
+ BSON::Undefined.new
142
+ ```
143
+
144
+ ### JSON Serialization
145
+
146
+ Some BSON types have special representations in JSON. These are as follows
147
+ and will be automatically serialized in the form when calling `to_json` on
148
+ them.
149
+
150
+ - `BSON::Binary`: `{ "$binary" : "\x01", "$type" : "md5" }`
151
+ - `BSON::Code`: `{ "$code" : "this.v = 5 }`
152
+ - `BSON::CodeWithScope`: `{ "$code" : "this.v = value", "$scope" : { v => 5 }}`
153
+ - `BSON::MaxKey`: `{ "$maxKey" : 1 }`
154
+ - `BSON::MinKey`: `{ "$minKey" : 1 }`
155
+ - `BSON::ObjectId`: `{ "$oid" : "4e4d66343b39b68407000001" }`
156
+ - `BSON::Timestamp`: `{ "t" : 5, "i" : 30 }`
157
+ - `Regexp`: `{ "$regex" : "[abc]", "$options" : "i" }`
158
+
159
+ API Documentation
160
+ -----------------
161
+
162
+ The [API Documentation](http://rdoc.info/github/mongodb/bson-ruby/master/frames) is
163
+ located at rdoc.info.
164
+
165
+ BSON Specification
166
+ ------------------
167
+
168
+ The [BSON specification](http://bsonspec.org) is at bsonspec.org.
169
+
170
+ License
171
+ -------
172
+
173
+ Copyright (C) 2013 10gen Inc.
174
+
175
+ Licensed under the Apache License, Version 2.0 (the "License");
176
+ you may not use this file except in compliance with the License.
177
+ You may obtain a copy of the License at
178
+
179
+ http://www.apache.org/licenses/LICENSE-2.0
180
+
181
+ Unless required by applicable law or agreed to in writing, software
182
+ distributed under the License is distributed on an "AS IS" BASIS,
183
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
184
+ See the License for the specific language governing permissions and
185
+ limitations under the License.
@@ -0,0 +1,83 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ $LOAD_PATH.unshift(File.expand_path("../lib", __FILE__))
5
+
6
+ require "rake"
7
+ require "rake/extensiontask"
8
+ require "rspec/core/rake_task"
9
+
10
+ def jruby?
11
+ defined?(JRUBY_VERSION)
12
+ end
13
+
14
+ if jruby?
15
+ require "rake/javaextensiontask"
16
+ Rake::JavaExtensionTask.new do |ext|
17
+ ext.name = "NativeService"
18
+ ext.ext_dir = "src"
19
+ ext.lib_dir = "lib/bson"
20
+ end
21
+ else
22
+ require "rake/extensiontask"
23
+ Rake::ExtensionTask.new do |ext|
24
+ ext.name = "native"
25
+ ext.ext_dir = "ext/bson"
26
+ ext.lib_dir = "lib/bson"
27
+ end
28
+ end
29
+
30
+ require "bson/version"
31
+
32
+ def extension
33
+ RUBY_PLATFORM =~ /darwin/ ? "bundle" : "so"
34
+ end
35
+
36
+ if RUBY_VERSION < "1.9"
37
+ require "perf/bench"
38
+ else
39
+ require_relative "perf/bench"
40
+ end
41
+
42
+ RSpec::Core::RakeTask.new(:spec)
43
+ RSpec::Core::RakeTask.new(:rspec)
44
+
45
+ task :build do
46
+ system "gem build bson.gemspec"
47
+ end
48
+
49
+ task :clean_all => :clean do
50
+ begin
51
+ Dir.chdir(Pathname(__FILE__).dirname + "lib/bson") do
52
+ `rm native.#{extension}`
53
+ `rm native.o`
54
+ `rm NativeService.jar`
55
+ end
56
+ rescue Exception => e
57
+ puts e.message
58
+ end
59
+ end
60
+
61
+ task :release => :build do
62
+ system "git tag -a v#{BSON::VERSION} -m 'Tagging release: #{BSON::VERSION}'"
63
+ system "git push --tags"
64
+ system "gem push bson-#{BSON::VERSION}.gem"
65
+ system "rm bson-#{BSON::VERSION}.gem"
66
+ end
67
+
68
+ namespace :benchmark do
69
+
70
+ task :ruby => :clean_all do
71
+ puts "Benchmarking pure Ruby..."
72
+ require "bson"
73
+ benchmark!
74
+ end
75
+
76
+ task :native => :compile do
77
+ puts "Benchmarking with native extensions..."
78
+ require "bson"
79
+ benchmark!
80
+ end
81
+ end
82
+
83
+ task :default => [ :clean_all, :spec, :compile, :rspec ]
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+ $CFLAGS << " -Wall -g -std=c99"
3
+ create_makefile("native")
@@ -1,113 +1,124 @@
1
- # Copyright (C) 2009-2013 MongoDB, Inc.
1
+ # encoding: utf-8
2
+
3
+ # Determine if we are using JRuby or not.
2
4
  #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
5
+ # @example Are we running with JRuby?
6
+ # jruby?
6
7
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
+ # @return [ true, false ] If JRuby is our vm.
8
9
  #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
10
+ # @since 2.0.0
11
+ def jruby?
12
+ defined?(JRUBY_VERSION)
13
+ end
14
14
 
15
- module BSON
16
- DEFAULT_MAX_BSON_SIZE = 4 * 1024 * 1024
15
+ # Does the Ruby runtime we are using support ordered hashes?
16
+ #
17
+ # @example Does the runtime support ordered hashes?
18
+ # ordered_hash_support?
19
+ #
20
+ # @return [ true, false ] If the runtime has ordered hashes.
21
+ #
22
+ # @since 2.0.0
23
+ def ordered_hash_support?
24
+ jruby? || RUBY_VERSION > "1.9.1"
25
+ end
17
26
 
18
- def self.serialize(obj, check_keys=false, move_id=false)
19
- BSON_CODER.serialize(obj, check_keys, move_id)
20
- end
27
+ # Are we running in a ruby runtime that is version 1.8.x?
28
+ #
29
+ # @since 2.0.0
30
+ def ruby_18?
31
+ RUBY_VERSION < "1.9"
32
+ end
21
33
 
22
- def self.deserialize(buf=nil, opts={})
23
- BSON_CODER.deserialize(buf, opts)
24
- end
34
+ # In the case where we don't have encoding, we need to monkey
35
+ # patch string to ignore the encoding directives.
36
+ #
37
+ # @since 2.0.0
38
+ if ruby_18?
25
39
 
26
- # Reads a single BSON document from an IO object.
27
- # This method is used in the executable b2json, bundled with
28
- # the bson gem, for reading a file of bson documents.
29
- #
30
- # @param [IO] io an io object containing a bson object.
31
- #
32
- # @return [ByteBuffer]
33
- def self.read_bson_document(io)
34
- bytebuf = BSON::ByteBuffer.new
35
- sz = io.read(4).unpack("V")[0]
36
- bytebuf.put_int(sz)
37
- bytebuf.put_array(io.read(sz-4).unpack("C*"))
38
- bytebuf.rewind
39
- return BSON.deserialize(bytebuf)
40
- end
40
+ class String
41
41
 
42
- def self.extension?
43
- !((ENV.key?('BSON_EXT_DISABLED') && RUBY_PLATFORM =~ /java/) ||
44
- (ENV.key?('BSON_EXT_DISABLED') || "\x01\x00\x00\x00".unpack("i")[0] != 1))
42
+ # In versions prior to 1.9 we need to ignore the encoding requests.
43
+ #
44
+ # @since 2.0.0
45
+ def chr; self; end
46
+ def force_encoding(*); self; end
47
+ def encode(*); self; end
48
+ def encode!(*); self; end
45
49
  end
46
- end
47
50
 
48
- begin
49
- # Skips loading extensions if one of the following is true:
50
- # 1) JRuby and BSON_EXT_DISABLED is set.
51
- # -OR-
52
- # 2) Ruby MRI and big endian or BSON_EXT_DISABLED is set.
53
- raise LoadError unless BSON.extension?
51
+ class EncodingError < RuntimeError; end
52
+ end
54
53
 
55
- if RUBY_PLATFORM =~ /java/
56
- require 'bson/bson_java'
57
- module BSON
58
- BSON_CODER = BSON_JAVA
59
- end
60
- else
61
- require 'bson_ext/cbson'
62
- raise LoadError unless defined?(CBson::VERSION)
63
- require 'bson/bson_c'
64
- module BSON
65
- BSON_CODER = BSON_C
66
- end
67
- end
68
- rescue LoadError
69
- require 'bson/bson_ruby'
70
- module BSON
71
- BSON_CODER = BSON_RUBY
72
- end
54
+ #
55
+ # The core namespace for all BSON related behaviour.
56
+ #
57
+ # @since 0.0.0
58
+ module BSON
73
59
 
74
- if RUBY_PLATFORM =~ /java/
75
- unless ENV['TEST_MODE']
76
- warn <<-NOTICE
77
- ** Notice: The BSON extension was not loaded. **
60
+ # Constant for binary string encoding.
61
+ #
62
+ # @since 2.0.0
63
+ BINARY = "BINARY".freeze
78
64
 
79
- For optimal performance, use of the BSON extension is recommended. To
80
- enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set.
81
- NOTICE
82
- end
83
- else
84
- unless ENV['TEST_MODE']
85
- warn <<-NOTICE
86
- ** Notice: The native BSON extension was not loaded. **
65
+ # Constant for bson types that don't actually serialize a value.
66
+ #
67
+ # @since 2.0.0
68
+ NO_VALUE = "".force_encoding(BINARY).freeze
87
69
 
88
- For optimal performance, use of the BSON extension is recommended.
70
+ # Constant for a null byte (0x00).
71
+ #
72
+ # @since 2.0.0
73
+ NULL_BYTE = 0.chr.force_encoding(BINARY).freeze
89
74
 
90
- To enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set
91
- and run the following command:
75
+ # Constant for UTF-8 string encoding.
76
+ #
77
+ # @since 2.0.0
78
+ UTF8 = "UTF-8".freeze
79
+ end
92
80
 
93
- gem install bson_ext
81
+ require "bson/registry"
82
+ require "bson/specialized"
83
+ require "bson/json"
84
+ require "bson/int32"
85
+ require "bson/int64"
86
+ require "bson/integer"
87
+ require "bson/encodable"
88
+ require "bson/array"
89
+ require "bson/binary"
90
+ require "bson/boolean"
91
+ require "bson/code"
92
+ require "bson/code_with_scope"
93
+ require "bson/document"
94
+ require "bson/false_class"
95
+ require "bson/float"
96
+ require "bson/hash"
97
+ require "bson/max_key"
98
+ require "bson/min_key"
99
+ require "bson/nil_class"
100
+ require "bson/object_id"
101
+ require "bson/regexp"
102
+ require "bson/string"
103
+ require "bson/symbol"
104
+ require "bson/time"
105
+ require "bson/timestamp"
106
+ require "bson/true_class"
107
+ require "bson/undefined"
108
+ require "bson/version"
94
109
 
95
- If you continue to receive this message after installing, make sure that
96
- the bson_ext gem is in your load path.
97
- NOTICE
98
- end
110
+ # If we are using JRuby, attempt to load the Java extensions, if we are using
111
+ # MRI or Rubinius, attempt to load the C extenstions. If either of these fail,
112
+ # we revert back to a pure Ruby implementation of the Buffer class.
113
+ #
114
+ # @since 2.0.0
115
+ begin
116
+ if jruby?
117
+ require "bson/NativeService.jar"
118
+ org.bson.NativeService.new.basicLoad(JRuby.runtime)
119
+ else
120
+ require "bson/native"
99
121
  end
122
+ rescue LoadError
123
+ $stderr.puts("BSON is using the pure Ruby implementation.")
100
124
  end
101
-
102
- require 'base64'
103
- require 'bson/bson_ruby'
104
- require 'bson/byte_buffer'
105
- require 'bson/exceptions'
106
- require 'bson/ordered_hash'
107
- require 'bson/types/binary'
108
- require 'bson/types/code'
109
- require 'bson/types/dbref'
110
- require 'bson/types/min_max_keys'
111
- require 'bson/types/regex'
112
- require 'bson/types/object_id'
113
- require 'bson/types/timestamp'