bson 1.12.5-java → 2.0.0-java

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 (89) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +80 -0
  5. data/CONTRIBUTING.md +42 -0
  6. data/NOTICE +2 -0
  7. data/README.md +190 -0
  8. data/Rakefile +109 -0
  9. data/lib/bson-ruby.jar +0 -0
  10. data/lib/bson.rb +60 -87
  11. data/lib/bson/array.rb +104 -0
  12. data/lib/bson/binary.rb +193 -0
  13. data/lib/bson/boolean.rb +48 -0
  14. data/lib/bson/code.rb +109 -0
  15. data/lib/bson/code_with_scope.rb +120 -0
  16. data/lib/bson/document.rb +549 -0
  17. data/lib/bson/encodable.rb +86 -0
  18. data/lib/bson/environment.rb +98 -0
  19. data/lib/bson/false_class.rb +61 -0
  20. data/lib/bson/float.rb +82 -0
  21. data/lib/bson/hash.rb +84 -0
  22. data/lib/bson/int32.rb +59 -0
  23. data/lib/bson/int64.rb +59 -0
  24. data/lib/bson/integer.rb +185 -0
  25. data/lib/bson/json.rb +37 -0
  26. data/lib/bson/max_key.rb +70 -0
  27. data/lib/bson/min_key.rb +70 -0
  28. data/lib/bson/nil_class.rb +70 -0
  29. data/lib/bson/object_id.rb +395 -0
  30. data/lib/bson/regexp.rb +124 -0
  31. data/lib/bson/registry.rb +70 -0
  32. data/lib/bson/specialized.rb +74 -0
  33. data/lib/bson/string.rb +203 -0
  34. data/lib/bson/symbol.rb +87 -0
  35. data/lib/bson/time.rb +72 -0
  36. data/lib/bson/timestamp.rb +113 -0
  37. data/lib/bson/true_class.rb +61 -0
  38. data/lib/bson/undefined.rb +74 -0
  39. data/lib/bson/version.rb +17 -0
  40. data/spec/bson/array_spec.rb +58 -0
  41. data/spec/bson/binary_spec.rb +115 -0
  42. data/spec/bson/boolean_spec.rb +48 -0
  43. data/spec/bson/code_spec.rb +42 -0
  44. data/spec/bson/code_with_scope_spec.rb +74 -0
  45. data/spec/bson/document_spec.rb +778 -0
  46. data/spec/bson/false_class_spec.rb +28 -0
  47. data/spec/bson/float_spec.rb +29 -0
  48. data/spec/bson/hash_spec.rb +56 -0
  49. data/spec/bson/int32_spec.rb +28 -0
  50. data/spec/bson/int64_spec.rb +28 -0
  51. data/spec/bson/integer_spec.rb +76 -0
  52. data/spec/bson/json_spec.rb +53 -0
  53. data/spec/bson/max_key_spec.rb +75 -0
  54. data/spec/bson/min_key_spec.rb +75 -0
  55. data/spec/bson/nil_class_spec.rb +29 -0
  56. data/spec/bson/object_id_spec.rb +527 -0
  57. data/spec/bson/regexp_spec.rb +89 -0
  58. data/spec/bson/registry_spec.rb +55 -0
  59. data/spec/bson/string_spec.rb +298 -0
  60. data/spec/bson/symbol_spec.rb +55 -0
  61. data/spec/bson/time_spec.rb +43 -0
  62. data/spec/bson/timestamp_spec.rb +74 -0
  63. data/spec/bson/true_class_spec.rb +28 -0
  64. data/spec/bson/undefined_spec.rb +29 -0
  65. data/{lib/bson/types/dbref.rb → spec/bson_spec.rb} +22 -16
  66. data/spec/spec_helper.rb +32 -0
  67. data/spec/support/shared_examples.rb +95 -0
  68. metadata +116 -48
  69. metadata.gz.sig +1 -1
  70. data/VERSION +0 -1
  71. data/bin/b2json +0 -63
  72. data/bin/j2bson +0 -64
  73. data/bson.gemspec +0 -34
  74. data/ext/jbson/lib/java-bson.jar +0 -0
  75. data/ext/jbson/target/jbson.jar +0 -0
  76. data/lib/bson/bson_c.rb +0 -37
  77. data/lib/bson/bson_java.rb +0 -49
  78. data/lib/bson/bson_ruby.rb +0 -645
  79. data/lib/bson/byte_buffer.rb +0 -241
  80. data/lib/bson/exceptions.rb +0 -37
  81. data/lib/bson/grow.rb +0 -173
  82. data/lib/bson/ordered_hash.rb +0 -197
  83. data/lib/bson/support/hash_with_indifferent_access.rb +0 -174
  84. data/lib/bson/types/binary.rb +0 -52
  85. data/lib/bson/types/code.rb +0 -55
  86. data/lib/bson/types/min_max_keys.rb +0 -56
  87. data/lib/bson/types/object_id.rb +0 -226
  88. data/lib/bson/types/regex.rb +0 -116
  89. data/lib/bson/types/timestamp.rb +0 -72
@@ -1,56 +0,0 @@
1
- # Copyright (C) 2009-2013 MongoDB, 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
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.
14
-
15
- module BSON
16
-
17
- # A class representing the BSON MaxKey type. MaxKey will always compare greater than
18
- # all other BSON types and values.
19
- #
20
- # @example Sorting (assume @numbers is a collection):
21
- #
22
- # >> @numbers.save({"n" => Mongo::MaxKey.new})
23
- # >> @numbers.save({"n" => 0})
24
- # >> @numbers.save({"n" => 5_000_000})
25
- # >> @numbers.find.sort("n").to_a
26
- # => [{"_id"=>4b5a050c238d3bace2000004, "n"=>0},
27
- # {"_id"=>4b5a04e6238d3bace2000002, "n"=>5_000_000},
28
- # {"_id"=>4b5a04ea238d3bace2000003, "n"=>#<Mongo::MaxKey:0x1014ef410>},
29
- # ]
30
- class MaxKey
31
-
32
- def ==(obj)
33
- obj.class == MaxKey
34
- end
35
- end
36
-
37
- # A class representing the BSON MinKey type. MinKey will always compare less than
38
- # all other BSON types and values.
39
- #
40
- # @example Sorting (assume @numbers is a collection):
41
- #
42
- # >> @numbers.save({"n" => Mongo::MinKey.new})
43
- # >> @numbers.save({"n" => -1_000_000})
44
- # >> @numbers.save({"n" => 1_000_000})
45
- # >> @numbers.find.sort("n").to_a
46
- # => [{"_id"=>4b5a050c238d3bace2000004, "n"=>#<Mongo::MinKey:0x1014ef410>},
47
- # {"_id"=>4b5a04e6238d3bace2000002, "n"=>-1_000_000},
48
- # {"_id"=>4b5a04ea238d3bace2000003, "n"=>1_000_000},
49
- # ]
50
- class MinKey
51
-
52
- def ==(obj)
53
- obj.class == MinKey
54
- end
55
- end
56
- end
@@ -1,226 +0,0 @@
1
- # Copyright (C) 2009-2013 MongoDB, 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
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.
14
-
15
- require 'digest/md5'
16
- require 'socket'
17
-
18
- module BSON
19
-
20
- def BSON::ObjectId(s)
21
- ObjectId.from_string(s)
22
- end
23
-
24
- # Generates MongoDB object ids.
25
- class ObjectId
26
- attr_accessor :data
27
-
28
- # Create a new object id. If no parameter is given, an id corresponding
29
- # to the ObjectId BSON data type will be created. This is a 12-byte value
30
- # consisting of a 4-byte timestamp, a 3-byte machine id, a 2-byte process id,
31
- # and a 3-byte counter.
32
- #
33
- # @param [Array] data should be an array of bytes. If you want
34
- # to generate a standard MongoDB object id, leave this argument blank.
35
- #
36
- # @option opts :data (nil) An array of bytes to use as the object id.
37
- # @option opts :time (nil) The value of this object ids timestamp. Note that
38
- # the remaining bytes will consist of the standard machine id, pid, and counter. If
39
- # you need a zeroed timestamp, used ObjectId.from_time.
40
- def initialize(data=nil, time=nil)
41
- if data && (!data.is_a?(Array) || data.size != 12)
42
- raise InvalidObjectId, 'ObjectId requires 12 byte array'
43
- end
44
- @data = data || generate(time)
45
- end
46
-
47
- # Determine if the supplied string is legal. Legal strings will
48
- # consist of 24 hexadecimal characters.
49
- #
50
- # @param [String] str
51
- #
52
- # @return [Boolean]
53
- def self.legal?(str)
54
- str =~ /\A[0-9a-f]{24}\z/i ? true : false
55
- end
56
-
57
- # Create an object id from the given time. This is useful for doing range
58
- # queries; it works because MongoDB's object ids begin
59
- # with a timestamp.
60
- #
61
- # @param [Time] time a utc time to encode as an object id.
62
- #
63
- # @option opts [:unique] (false) If false, the object id's bytes
64
- # succeeding the timestamp will be zeroed; if true, they'll
65
- # consist of the standard machine id, pid, and counter.
66
- #
67
- # @return [BSON::ObjectId]
68
- #
69
- # @example Return all document created before Jan 1, 2010.
70
- # time = Time.utc(2010, 1, 1)
71
- # time_id = ObjectId.from_time(time)
72
- # collection.find({'_id' => {'$lt' => time_id}})
73
- def self.from_time(time, opts={})
74
- unique = opts.fetch(:unique, false)
75
- if unique
76
- self.new(nil, time)
77
- else
78
- self.new([time.to_i,0,0].pack("NNN").unpack("C12"))
79
- end
80
- end
81
-
82
- # Adds a primary key to the given document if needed.
83
- #
84
- # @param [Hash] doc a document requiring an _id.
85
- #
86
- # @return [BSON::ObjectId, Object] returns a newly-created or
87
- # current _id for the given document.
88
- def self.create_pk(doc)
89
- doc.has_key?(:_id) || doc.has_key?('_id') ? doc : doc.merge!(:_id => self.new)
90
- end
91
-
92
- # Check equality of this object id with another.
93
- #
94
- # @param [BSON::ObjectId] object_id
95
- def eql?(object_id)
96
- object_id.kind_of?(BSON::ObjectId) and self.data == object_id.data
97
- end
98
- alias_method :==, :eql?
99
-
100
- # Get a unique hashcode for this object.
101
- # This is required since we've defined an #eql? method.
102
- #
103
- # @return [Integer]
104
- def hash
105
- @data.hash
106
- end
107
-
108
- # Get an array representation of the object id.
109
- #
110
- # @return [Array]
111
- def to_a
112
- @data.dup
113
- end
114
-
115
- # Given a string representation of an ObjectId, return a new ObjectId
116
- # with that value.
117
- #
118
- # @param [String] str
119
- #
120
- # @return [BSON::ObjectId]
121
- def self.from_string(str)
122
- raise InvalidObjectId, "illegal ObjectId format: #{str}" unless legal?(str)
123
- data = []
124
- 12.times do |i|
125
- data[i] = str[i * 2, 2].to_i(16)
126
- end
127
- self.new(data)
128
- end
129
-
130
- # Get a string representation of this object id.
131
- #
132
- # @return [String]
133
- def to_s
134
- @data.map {|e| v=e.to_s(16); v.size == 1 ? "0#{v}" : v }.join
135
- end
136
-
137
- def inspect
138
- "BSON::ObjectId('#{to_s}')"
139
- end
140
-
141
- # Convert to MongoDB extended JSON format. Since JSON includes type information,
142
- # but lacks an ObjectId type, this JSON format encodes the type using an $oid key.
143
- #
144
- # @return [String] the object id represented as MongoDB extended JSON.
145
- def to_json(*a)
146
- "{\"$oid\": \"#{to_s}\"}"
147
- end
148
-
149
- # Create the JSON hash structure convert to MongoDB extended format. Rails 2.3.3
150
- # introduced as_json to create the needed hash structure to encode objects into JSON.
151
- #
152
- # @return [Hash] the hash representation as MongoDB extended JSON
153
- def as_json(options ={})
154
- {"$oid" => to_s}
155
- end
156
-
157
- # Return the UTC time at which this ObjectId was generated. This may
158
- # be used in lieu of a created_at timestamp since this information
159
- # is always encoded in the object id.
160
- #
161
- # @return [Time] the time at which this object was created.
162
- def generation_time
163
- Time.at(@data.pack("C4").unpack("N")[0]).utc
164
- end
165
-
166
- def self.machine_id
167
- @@machine_id
168
- end
169
-
170
- private
171
-
172
- if RUBY_PLATFORM =~ /java/ && BSON.extension?
173
- @@generator = Java::OrgBsonTypes::ObjectId
174
- @@machine_id = [@@generator.genMachineId].pack("N")[0,3]
175
-
176
- def generate(oid_time=nil)
177
- data = (oid_time ? @@generator.new(oid_time) : @@generator.new)
178
-
179
- oid = ''
180
- oid += [data.timeSecond].pack("N")
181
- oid += [data._machine].pack("N")
182
- oid += [data._inc].pack("N")
183
- oid.unpack("C12")
184
- end
185
-
186
- else
187
- @@lock = Mutex.new
188
- @@index = 0
189
- @@machine_id = Digest::MD5.digest(Socket.gethostname)[0, 3]
190
-
191
- # We need to check whether BSON_CODER is defined because it's required by
192
- # the BSON C extensions.
193
- if defined?(BSON::BSON_CODER) && BSON::BSON_CODER == BSON::BSON_RUBY
194
- # This gets overwritten by the C extension if it loads.
195
- def generate(oid_time=nil)
196
- oid = ''
197
-
198
- # 4 bytes current time
199
- if oid_time
200
- t = oid_time.to_i
201
- else
202
- t = Time.new.to_i
203
- end
204
- oid += [t].pack("N")
205
-
206
- # 3 bytes machine
207
- oid += @@machine_id
208
-
209
- # 2 bytes pid
210
- oid += [Process.pid % 0xFFFF].pack("n")
211
-
212
- # 3 bytes inc
213
- oid += [get_inc].pack("N")[1, 3]
214
-
215
- oid.unpack("C12")
216
- end
217
-
218
- def get_inc
219
- @@lock.synchronize do
220
- @@index = (@@index + 1) % 0xFFFFFF
221
- end
222
- end
223
- end
224
- end
225
- end
226
- end
@@ -1,116 +0,0 @@
1
- # Copyright (C) 2009-2013 MongoDB, 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
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.
14
-
15
- module BSON
16
-
17
- # generates a wrapped Regexp with lazy compilation.
18
- # can represent flags not supported in Ruby's core Regexp class before compilation.
19
- class Regex
20
-
21
- IGNORECASE = 0x01
22
- LOCALE_DEPENDENT = 0x02
23
- MULTILINE = 0x04
24
- DOTALL = 0x08
25
- UNICODE = 0x10
26
- EXTENDED = 0x20
27
-
28
- attr_accessor :pattern
29
- alias_method :source, :pattern
30
- attr_accessor :options
31
-
32
- # Create a new regexp.
33
- #
34
- # @param pattern [String]
35
- # @param opts [Array, String]
36
- def initialize(pattern, *opts)
37
- @pattern = pattern
38
- @options = opts.first.is_a?(Fixnum) ? opts.first : str_opts_to_int(opts.join)
39
- end
40
-
41
- # Check equality of this wrapped Regexp with another.
42
- #
43
- # @param [BSON::Regex] regexp
44
- def eql?(regexp)
45
- regexp.kind_of?(BSON::Regex) &&
46
- self.pattern == regexp.pattern &&
47
- self.options == regexp.options
48
- end
49
- alias_method :==, :eql?
50
-
51
- # Get a human-readable representation of this BSON Regex.
52
- def inspect
53
- "#<BSON::Regex:0x#{self.object_id} " <<
54
- "@pattern=#{@pattern}>, @options=#{@options}>"
55
- end
56
-
57
- # Clone or dup the current BSON::Regex.
58
- def initialize_copy
59
- a_copy = self.dup
60
- a_copy.pattern = self.pattern.dup
61
- a_copy.options = self.options.dup
62
- a_copy
63
- end
64
-
65
- # Attempt to convert a native Ruby Regexp to a BSON::Regex.
66
- #
67
- # @note Warning: Ruby regular expressions use a different syntax and different
68
- # set of flags than BSON regular expressions. A regular expression matches different
69
- # strings when executed in Ruby than it matches when used in a MongoDB query,
70
- # if it can be used in a query at all.
71
- #
72
- # @param regexp [Regexp] The native Ruby regexp object to convert to BSON::Regex.
73
- #
74
- # @return [BSON::Regex]
75
- def self.from_native(regexp)
76
- pattern = regexp.source
77
- opts = 0
78
- opts |= MULTILINE # multiline mode is always on for Ruby regular expressions
79
- opts |= IGNORECASE if (Regexp::IGNORECASE & regexp.options != 0)
80
- opts |= DOTALL if (Regexp::MULTILINE & regexp.options != 0)
81
- opts |= EXTENDED if (Regexp::EXTENDED & regexp.options != 0)
82
- self.new(pattern, opts)
83
- end
84
-
85
- # Compile the BSON::Regex.
86
- #
87
- # @note Warning: regular expressions retrieved from the server may include a pattern
88
- # that cannot be compiled into a Ruby regular expression, or which matches a
89
- # different set of strings in Ruby than it does when used in a MongoDB query,
90
- # or it may have flags that are not supported by Ruby regular expressions.
91
- #
92
- # @return [Regexp] A ruby core Regexp object.
93
- def try_compile
94
- regexp_opts = 0
95
- regexp_opts |= Regexp::IGNORECASE if (options & IGNORECASE != 0)
96
- regexp_opts |= Regexp::MULTILINE if (options & DOTALL != 0)
97
- regexp_opts |= Regexp::EXTENDED if (options & EXTENDED != 0)
98
- Regexp.new(pattern, regexp_opts)
99
- end
100
-
101
- private
102
- # Convert the string options to an integer.
103
- #
104
- # @return [Fixnum] The Integer representation of the options.
105
- def str_opts_to_int(str_opts="")
106
- opts = 0
107
- opts |= IGNORECASE if str_opts.include?('i')
108
- opts |= LOCALE_DEPENDENT if str_opts.include?('l')
109
- opts |= MULTILINE if str_opts.include?('m')
110
- opts |= DOTALL if str_opts.include?('s')
111
- opts |= UNICODE if str_opts.include?('u')
112
- opts |= EXTENDED if str_opts.include?('x')
113
- opts
114
- end
115
- end
116
- end
@@ -1,72 +0,0 @@
1
- # Copyright (C) 2009-2013 MongoDB, 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
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.
14
-
15
- module BSON
16
-
17
- # A class for representing BSON Timestamps. The Timestamp type is used
18
- # by MongoDB internally; thus, it should be used by application developers
19
- # for diagnostic purposes only.
20
- class Timestamp
21
- include Enumerable
22
-
23
- attr_reader :seconds, :increment
24
-
25
- # Create a new BSON Timestamp.
26
- #
27
- # @param [Integer] seconds The number of seconds
28
- # @param increment
29
- def initialize(seconds, increment)
30
- @seconds = seconds
31
- @increment = increment
32
- end
33
-
34
- def to_s
35
- "seconds: #{seconds}, increment: #{increment}"
36
- end
37
-
38
- def ==(other)
39
- self.seconds == other.seconds &&
40
- self.increment == other.increment
41
- end
42
-
43
- # This is for backward-compatibility. Timestamps in the Ruby
44
- # driver used to deserialize as arrays. So we provide
45
- # an equivalent interface.
46
- #
47
- # @deprecated
48
- def [](index)
49
- warn "Timestamps are no longer deserialized as arrays. If you're working " +
50
- "with BSON Timestamp objects, see the BSON::Timestamp class. This usage will " +
51
- "be deprecated in Ruby Driver v2.0."
52
- if index == 0
53
- self.increment
54
- elsif index == 1
55
- self.seconds
56
- else
57
- nil
58
- end
59
- end
60
-
61
- # This method exists only for backward-compatibility.
62
- #
63
- # @deprecated
64
- def each
65
- i = 0
66
- while i < 2
67
- yield self[i]
68
- i += 1
69
- end
70
- end
71
- end
72
- end