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
@@ -1,241 +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 'bson/grow'
16
-
17
- module BSON
18
- class ByteBuffer
19
- include BSON::Grow
20
-
21
- attr_reader :order, :max_size
22
-
23
- INT32_PACK = 'l<'.freeze
24
- INT64_PACK = 'q<'.freeze
25
- DOUBLE_PACK = 'E'.freeze
26
-
27
- def initialize(initial_data="", max_size=DEFAULT_MAX_BSON_SIZE)
28
- @str = case initial_data
29
- when String then
30
- if initial_data.respond_to?(:force_encoding)
31
- initial_data.force_encoding('binary')
32
- else
33
- initial_data
34
- end
35
- when BSON::ByteBuffer then
36
- initial_data.to_a.pack('C*')
37
- else
38
- initial_data.pack('C*')
39
- end
40
-
41
- @cursor = @str.length
42
- @max_size = max_size
43
- end
44
-
45
- def rewind
46
- @cursor = 0
47
- end
48
-
49
- def position
50
- @cursor
51
- end
52
-
53
- def position=(val)
54
- @cursor = val
55
- end
56
-
57
- def clear
58
- @str = ""
59
- @str.force_encoding('binary') if @str.respond_to?(:force_encoding)
60
- rewind
61
- end
62
-
63
- def size
64
- @str.size
65
- end
66
- alias_method :length, :size
67
-
68
- # Appends a second ByteBuffer object, +buffer+, to the current buffer.
69
- def append!(buffer)
70
- @str << buffer.to_s
71
- self
72
- end
73
-
74
- # Prepends a second ByteBuffer object, +buffer+, to the current buffer.
75
- def prepend!(buffer)
76
- @str = buffer.to_s + @str
77
- self
78
- end
79
-
80
- def put(byte, offset=nil)
81
- @cursor = offset if offset
82
- if more?
83
- @str[@cursor] = chr(byte)
84
- else
85
- ensure_length(@cursor)
86
- @str << chr(byte)
87
- end
88
- @cursor += 1
89
- end
90
-
91
- def put_binary(data, offset=nil)
92
- @cursor = offset if offset
93
- if defined?(BINARY_ENCODING)
94
- data = data.dup.force_encoding(BINARY_ENCODING)
95
- end
96
- if more?
97
- @str[@cursor, data.length] = data
98
- else
99
- ensure_length(@cursor)
100
- @str << data
101
- end
102
- @cursor += data.length
103
- end
104
-
105
- def put_array(array, offset=nil)
106
- @cursor = offset if offset
107
- if more?
108
- @str[@cursor, array.length] = array.pack("C*")
109
- else
110
- ensure_length(@cursor)
111
- @str << array.pack("C*")
112
- end
113
- @cursor += array.length
114
- end
115
-
116
- def put_num(i, offset, bytes)
117
- pack_type = bytes == 4 ? INT32_PACK : INT64_PACK
118
- @cursor = offset if offset
119
- if more?
120
- @str[@cursor, bytes] = [i].pack(pack_type)
121
- else
122
- ensure_length(@cursor)
123
- @str << [i].pack(pack_type)
124
- end
125
- @cursor += bytes
126
- end
127
-
128
- def put_int(i, offset=nil)
129
- put_num(i, offset, 4)
130
- end
131
-
132
- def put_long(i, offset=nil)
133
- put_num(i, offset, 8)
134
- end
135
-
136
- def put_double(d, offset=nil)
137
- a = []
138
- [d].pack(DOUBLE_PACK).each_byte { |b| a << b }
139
- put_array(a, offset)
140
- end
141
-
142
- # If +size+ == nil, returns one byte. Else returns array of bytes of length
143
- # # +size+.
144
- if "x"[0].is_a?(Integer)
145
- def get(len=nil)
146
- one_byte = len.nil?
147
- len ||= 1
148
- check_read_length(len)
149
- start = @cursor
150
- @cursor += len
151
- if one_byte
152
- @str[start]
153
- else
154
- @str[start, len].unpack("C*")
155
- end
156
- end
157
- else
158
- def get(len=nil)
159
- one_byte = len.nil?
160
- len ||= 1
161
- check_read_length(len)
162
- start = @cursor
163
- @cursor += len
164
- if one_byte
165
- @str[start, 1].ord
166
- else
167
- @str[start, len].unpack("C*")
168
- end
169
- end
170
- end
171
-
172
- def get_int
173
- check_read_length(4)
174
- vals = @str[@cursor..@cursor+3]
175
- @cursor += 4
176
- vals.unpack(INT32_PACK)[0]
177
- end
178
-
179
- def get_long
180
- check_read_length(8)
181
- vals = @str[@cursor..@cursor+7]
182
- @cursor += 8
183
- vals.unpack(INT64_PACK)[0]
184
- end
185
-
186
- def get_double
187
- check_read_length(8)
188
- vals = @str[@cursor..@cursor+7]
189
- @cursor += 8
190
- vals.unpack(DOUBLE_PACK)[0]
191
- end
192
-
193
- def more?
194
- @cursor < @str.size
195
- end
196
-
197
- def ==(other)
198
- other.respond_to?(:to_s) && @str == other.to_s
199
- end
200
-
201
- def to_a(format="C*")
202
- @str.unpack(format)
203
- end
204
-
205
- def unpack(format="C*")
206
- to_a(format)
207
- end
208
-
209
- def to_s
210
- @str
211
- end
212
-
213
- def dump
214
- @str.each_byte do |c, i|
215
- $stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}"
216
- i += 1
217
- end
218
- end
219
-
220
- private
221
-
222
- def ensure_length(length)
223
- if @str.size < length
224
- @str << NULL_BYTE * (length - @str.size)
225
- end
226
- end
227
-
228
- def chr(byte)
229
- if byte < 0
230
- [byte].pack('c')
231
- else
232
- byte.chr
233
- end
234
- end
235
-
236
- def check_read_length(len)
237
- raise "attempt to read past end of buffer" if @cursor + len > @str.length
238
- end
239
-
240
- end
241
- end
@@ -1,37 +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
- # Generic Mongo Ruby Driver exception class.
17
- class MongoRubyError < StandardError; end
18
-
19
- # Raised when MongoDB itself has returned an error.
20
- class MongoDBError < RuntimeError; end
21
-
22
- # This will replace MongoDBError.
23
- class BSONError < MongoDBError; end
24
-
25
- # Raised when given a string is not valid utf-8 (Ruby 1.8 only).
26
- class InvalidStringEncoding < BSONError; end
27
-
28
- # Raised when attempting to initialize an invalid ObjectId.
29
- class InvalidObjectId < BSONError; end
30
-
31
- # Raised when trying to insert a document that exceeds the 4MB limit or
32
- # when the document contains objects that can't be serialized as BSON.
33
- class InvalidDocument < BSONError; end
34
-
35
- # Raised when an invalid name is used.
36
- class InvalidKeyName < BSONError; end
37
- end
@@ -1,173 +0,0 @@
1
- # encoding: utf-8
2
-
3
- # Copyright (C) 2009-2013 MongoDB, Inc.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- module BSON
18
- module Grow
19
- # module with methods to grow BSON docs/objects/arrays
20
- # this module is intended for internal use and is subject to change
21
- # proper usage is essential as minimal overhead is preferred over usage checks
22
- # unfinish! returns unfinished BSON for faster growing with bang! methods
23
- # bang! methods work on unfinished BSON with neither terminating nulls nor proper sizes
24
- # finish! must be called to finish BSON after using bang! methods
25
- # corresponding non-bang methods work on finished BSON
26
- # object/array methods should be paired, ex., array!/b_end! and array/b_end
27
- # push!/push and push_doc!/push_doc append to arrays with correct keys
28
- # b_end needs a better name
29
-
30
- def to_e # Extract bytes for elements from BSON
31
- @str[4...-1]
32
- end
33
-
34
- def to_t # Extract type from (single-element) BSON
35
- @str[4,1]
36
- end
37
-
38
- def to_v # Extract value from (single-element) BSON
39
- @str[(@str.index(NULL_BYTE,5)+1)...-1]
40
- end
41
-
42
- def finish_one!(offset = 0) # Appends terminating null byte and sets size
43
- put(0)
44
- put_int(@str.size - offset, offset)
45
- @cursor = @str.size
46
- self
47
- end
48
-
49
- def unfinish! # Backup past terminating null bytes
50
- @b_pos ||= [0]
51
- @cursor = @str.size - @b_pos.size # BSON::BSON_CODER.serialize may not restore @cursor
52
- self
53
- end
54
-
55
- def finish! # Append all terminating null bytes and set all sizes
56
- @b_pos ||= [0]
57
- (@b_pos.size-1).downto(0){|i| finish_one!(@b_pos[i])}
58
- self
59
- end
60
-
61
- def grow!(bson) # Appends BSON elements unfinished
62
- put_binary(bson.to_e)
63
- self
64
- end
65
-
66
- def grow(bson) # Appends BSON elements finished
67
- @b_pos ||= [0]
68
- put_binary(bson.to_e, @str.size - @b_pos.size)
69
- finish!
70
- end
71
-
72
- def push!(bson) # Appends BSON element value with correct key unfinished
73
- @a_index ||= [0]
74
- @b_pos ||= [0]
75
- put_binary(bson.to_t)
76
- put_binary(@a_index[-1].to_s)
77
- put_binary(NULL_BYTE)
78
- @a_index[-1] += 1
79
- put_binary(bson.to_v)
80
- self
81
- end
82
-
83
- def push(bson) # Appends BSON element value with correct key finished
84
- @a_index ||= [0]
85
- @b_pos ||= [0]
86
- put_binary(bson.to_t, @str.size - @b_pos.size)
87
- put_binary(@a_index[-1].to_s)
88
- put_binary(NULL_BYTE)
89
- @a_index[-1] += 1
90
- put_binary(bson.to_v)
91
- finish!
92
- end
93
-
94
- def push_doc!(bson) # Appends BSON doc with correct key unfinished
95
- @a_index ||= [0]
96
- @b_pos ||= [0]
97
- put(BSON::BSON_RUBY::OBJECT)
98
- put_binary(@a_index[-1].to_s)
99
- put(0)
100
- @a_index[-1] += 1
101
- put_binary(bson.to_s)
102
- self
103
- end
104
-
105
- def push_doc(bson) # Appends BSON doc with correct key finished
106
- @a_index ||= [0]
107
- @b_pos ||= [0]
108
- put(BSON::BSON_RUBY::OBJECT, @str.size - @b_pos.size)
109
- put_binary(@a_index[-1].to_s)
110
- put(0)
111
- @a_index[-1] += 1
112
- put_binary(bson.to_s)
113
- finish!
114
- end
115
-
116
- def b_do!(key, type = BSON::BSON_RUBY::OBJECT) # Append object/array element unfinished
117
- put(type)
118
- BSON::BSON_RUBY.serialize_cstr(self, key)
119
- @b_pos ||= [0]
120
- @a_index ||= [0]
121
- @b_pos << @cursor # mark position of size
122
- @a_index << 0
123
- put_int(0)
124
- self
125
- end
126
-
127
- def b_do(key, type = BSON::BSON_RUBY::OBJECT) # Append object/array element finished
128
- @b_pos ||= [0]
129
- @cursor = @str.size - @b_pos.size
130
- b_do!(key, type)
131
- finish!
132
- end
133
-
134
- def doc!(key) # Append object element unfinished
135
- b_do!(key, BSON::BSON_RUBY::OBJECT)
136
- end
137
-
138
- def doc(key) # Append object element finished
139
- b_do(key, BSON::BSON_RUBY::OBJECT)
140
- end
141
-
142
- def array!(key) # Append array element unfinished
143
- b_do!(key, BSON::BSON_RUBY::ARRAY)
144
- end
145
-
146
- def array(key) # Append array element finished
147
- b_do(key, BSON::BSON_RUBY::ARRAY)
148
- end
149
-
150
- def b_end! # End object/array unfinished - next operation will be up one level
151
- @b_pos ||= [0]
152
- finish_one!(@b_pos[-1])
153
- @b_pos.pop
154
- @a_index ||= [0]
155
- @a_index.pop
156
- self
157
- end
158
-
159
- def b_end # End object/array finished - next operation will be up one level
160
- @b_pos ||= [0]
161
- @b_pos.pop
162
- @a_index ||= [0]
163
- @a_index.pop
164
- self
165
- end
166
-
167
- def clear! # Clear internal state for reuse
168
- @b_pos = @a_index = nil
169
- self
170
- end
171
-
172
- end
173
- end