bson 1.7.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.

@@ -0,0 +1,280 @@
1
+ # encoding: UTF-8
2
+
3
+ # --
4
+ # Copyright (C) 2008-2011 10gen Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ # ++
18
+
19
+ # A byte buffer.
20
+ module BSON
21
+ class ByteBuffer
22
+
23
+ attr_reader :order, :max_size
24
+
25
+ def initialize(initial_data="", max_size=BSON::DEFAULT_MAX_BSON_SIZE)
26
+ @str = case initial_data
27
+ when String then
28
+ if initial_data.respond_to?(:force_encoding)
29
+ initial_data.force_encoding('binary')
30
+ else
31
+ initial_data
32
+ end
33
+ when BSON::ByteBuffer then
34
+ initial_data.to_a.pack('C*')
35
+ else
36
+ initial_data.pack('C*')
37
+ end
38
+
39
+ @cursor = @str.length
40
+ @order = :little_endian
41
+ @int_pack_order = 'V'
42
+ @double_pack_order = 'E'
43
+ @max_size = max_size
44
+ end
45
+
46
+ if RUBY_VERSION >= '1.9'
47
+ NULL_BYTE = "\0".force_encoding('binary').freeze
48
+ UTF8_ENCODING = Encoding.find('utf-8')
49
+ BINARY_ENCODING = Encoding.find('binary')
50
+
51
+ def self.to_utf8_binary(str)
52
+ str.encode(UTF8_ENCODING).force_encoding(BINARY_ENCODING)
53
+ end
54
+ else
55
+ NULL_BYTE = "\0"
56
+
57
+ def self.to_utf8_binary(str)
58
+ begin
59
+ str.unpack("U*")
60
+ rescue
61
+ raise InvalidStringEncoding, "String not valid utf-8: #{str.inspect}"
62
+ end
63
+ str
64
+ end
65
+ end
66
+
67
+ def self.serialize_cstr(buf, val)
68
+ buf.append!(to_utf8_binary(val.to_s))
69
+ buf.append!(NULL_BYTE)
70
+ end
71
+
72
+ # +endianness+ should be :little_endian or :big_endian. Default is :little_endian
73
+ def order=(endianness)
74
+ @order = endianness
75
+ @int_pack_order = endianness == :little_endian ? 'V' : 'N'
76
+ @double_pack_order = endianness == :little_endian ? 'E' : 'G'
77
+ end
78
+
79
+ def rewind
80
+ @cursor = 0
81
+ end
82
+
83
+ def position
84
+ @cursor
85
+ end
86
+
87
+ def position=(val)
88
+ @cursor = val
89
+ end
90
+
91
+ def clear
92
+ @str = ""
93
+ @str.force_encoding('binary') if @str.respond_to?(:force_encoding)
94
+ rewind
95
+ end
96
+
97
+ def size
98
+ @str.size
99
+ end
100
+ alias_method :length, :size
101
+
102
+ # Appends a second ByteBuffer object, +buffer+, to the current buffer.
103
+ def append!(buffer)
104
+ @str << buffer.to_s
105
+ self
106
+ end
107
+
108
+ # Prepends a second ByteBuffer object, +buffer+, to the current buffer.
109
+ def prepend!(buffer)
110
+ @str = buffer.to_s + @str
111
+ self
112
+ end
113
+
114
+ def put(byte, offset=nil)
115
+ @cursor = offset if offset
116
+ if more?
117
+ @str[@cursor] = chr(byte)
118
+ else
119
+ ensure_length(@cursor)
120
+ @str << chr(byte)
121
+ end
122
+ @cursor += 1
123
+ end
124
+
125
+ def put_binary(data, offset=nil)
126
+ @cursor = offset if offset
127
+ if defined?(BINARY_ENCODING)
128
+ data = data.dup.force_encoding(BINARY_ENCODING)
129
+ end
130
+ if more?
131
+ @str[@cursor, data.length] = data
132
+ else
133
+ ensure_length(@cursor)
134
+ @str << data
135
+ end
136
+ @cursor += data.length
137
+ end
138
+
139
+ def put_array(array, offset=nil)
140
+ @cursor = offset if offset
141
+ if more?
142
+ @str[@cursor, array.length] = array.pack("C*")
143
+ else
144
+ ensure_length(@cursor)
145
+ @str << array.pack("C*")
146
+ end
147
+ @cursor += array.length
148
+ end
149
+
150
+ def put_int(i, offset=nil)
151
+ @cursor = offset if offset
152
+ if more?
153
+ @str[@cursor, 4] = [i].pack(@int_pack_order)
154
+ else
155
+ ensure_length(@cursor)
156
+ @str << [i].pack(@int_pack_order)
157
+ end
158
+ @cursor += 4
159
+ end
160
+
161
+ def put_long(i, offset=nil)
162
+ offset = @cursor unless offset
163
+ if @int_pack_order == 'N'
164
+ put_int(i >> 32, offset)
165
+ put_int(i & 0xffffffff, offset + 4)
166
+ else
167
+ put_int(i & 0xffffffff, offset)
168
+ put_int(i >> 32, offset + 4)
169
+ end
170
+ end
171
+
172
+ def put_double(d, offset=nil)
173
+ a = []
174
+ [d].pack(@double_pack_order).each_byte { |b| a << b }
175
+ put_array(a, offset)
176
+ end
177
+
178
+ # If +size+ == nil, returns one byte. Else returns array of bytes of length
179
+ # # +size+.
180
+ if "x"[0].is_a?(Integer)
181
+ def get(len=nil)
182
+ one_byte = len.nil?
183
+ len ||= 1
184
+ check_read_length(len)
185
+ start = @cursor
186
+ @cursor += len
187
+ if one_byte
188
+ @str[start]
189
+ else
190
+ @str[start, len].unpack("C*")
191
+ end
192
+ end
193
+ else
194
+ def get(len=nil)
195
+ one_byte = len.nil?
196
+ len ||= 1
197
+ check_read_length(len)
198
+ start = @cursor
199
+ @cursor += len
200
+ if one_byte
201
+ @str[start, 1].ord
202
+ else
203
+ @str[start, len].unpack("C*")
204
+ end
205
+ end
206
+ end
207
+
208
+ def get_int
209
+ check_read_length(4)
210
+ vals = @str[@cursor..@cursor+3]
211
+ @cursor += 4
212
+ vals.unpack(@int_pack_order)[0]
213
+ end
214
+
215
+ def get_long
216
+ i1 = get_int
217
+ i2 = get_int
218
+ if @int_pack_order == 'N'
219
+ (i1 << 32) + i2
220
+ else
221
+ (i2 << 32) + i1
222
+ end
223
+ end
224
+
225
+ def get_double
226
+ check_read_length(8)
227
+ vals = @str[@cursor..@cursor+7]
228
+ @cursor += 8
229
+ vals.unpack(@double_pack_order)[0]
230
+ end
231
+
232
+ def more?
233
+ @cursor < @str.size
234
+ end
235
+
236
+ def ==(other)
237
+ other.respond_to?(:to_s) && @str == other.to_s
238
+ end
239
+
240
+ def to_a(format="C*")
241
+ @str.unpack(format)
242
+ end
243
+
244
+ def unpack(format="C*")
245
+ to_a(format)
246
+ end
247
+
248
+ def to_s
249
+ @str
250
+ end
251
+
252
+ def dump
253
+ @str.each_byte do |c, i|
254
+ $stderr.puts "#{'%04d' % i}: #{'%02x' % c} #{'%03o' % c} #{'%s' % c.chr} #{'%3d' % c}"
255
+ i += 1
256
+ end
257
+ end
258
+
259
+ private
260
+
261
+ def ensure_length(length)
262
+ if @str.size < length
263
+ @str << NULL_BYTE * (length - @str.size)
264
+ end
265
+ end
266
+
267
+ def chr(byte)
268
+ if byte < 0
269
+ [byte].pack('c')
270
+ else
271
+ byte.chr
272
+ end
273
+ end
274
+
275
+ def check_read_length(len)
276
+ raise "attempt to read past end of buffer" if @cursor + len > @str.length
277
+ end
278
+
279
+ end
280
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+
3
+ # --
4
+ # Copyright (C) 2008-2011 10gen Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ # ++
18
+
19
+ module BSON
20
+ # Generic Mongo Ruby Driver exception class.
21
+ class MongoRubyError < StandardError; end
22
+
23
+ # Raised when MongoDB itself has returned an error.
24
+ class MongoDBError < RuntimeError; end
25
+
26
+ # This will replace MongoDBError.
27
+ class BSONError < MongoDBError; end
28
+
29
+ # Raised when given a string is not valid utf-8 (Ruby 1.8 only).
30
+ class InvalidStringEncoding < BSONError; end
31
+
32
+ # Raised when attempting to initialize an invalid ObjectId.
33
+ class InvalidObjectId < BSONError; end
34
+
35
+ # Raised when trying to insert a document that exceeds the 4MB limit or
36
+ # when the document contains objects that can't be serialized as BSON.
37
+ class InvalidDocument < BSONError; end
38
+
39
+ # Raised when an invalid name is used.
40
+ class InvalidKeyName < BSONError; end
41
+ end
@@ -0,0 +1,187 @@
1
+ # encoding: UTF-8
2
+
3
+ # --
4
+ # Copyright (C) 2008-2011 10gen Inc.
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ # ++
18
+
19
+ # A hash in which the order of keys are preserved.
20
+ #
21
+ # Under Ruby 1.9 and greater, this class has no added methods because Ruby's
22
+ # Hash already keeps its keys ordered by order of insertion.
23
+
24
+ module BSON
25
+ class OrderedHash < Hash
26
+
27
+ def ==(other)
28
+ begin
29
+ case other
30
+ when BSON::OrderedHash
31
+ keys == other.keys && values == other.values
32
+ else
33
+ super
34
+ end
35
+ rescue
36
+ false
37
+ end
38
+ end
39
+
40
+ # We only need the body of this class if the RUBY_VERSION is before 1.9
41
+ if RUBY_VERSION < '1.9'
42
+ attr_accessor :ordered_keys
43
+
44
+ def self.[] *args
45
+ oh = BSON::OrderedHash.new
46
+ if Hash === args[0]
47
+ oh.merge! args[0]
48
+ elsif (args.size % 2) != 0
49
+ raise ArgumentError, "odd number of elements for Hash"
50
+ else
51
+ 0.step(args.size - 1, 2) do |key|
52
+ value = key + 1
53
+ oh[args[key]] = args[value]
54
+ end
55
+ end
56
+ oh
57
+ end
58
+
59
+ def initialize(*a, &b)
60
+ @ordered_keys = []
61
+ super
62
+ end
63
+
64
+ def yaml_initialize(tag, val)
65
+ @ordered_keys = []
66
+ super
67
+ end
68
+
69
+ def keys
70
+ @ordered_keys.dup
71
+ end
72
+
73
+ def []=(key, value)
74
+ unless has_key?(key)
75
+ @ordered_keys << key
76
+ end
77
+ super(key, value)
78
+ end
79
+
80
+ def each
81
+ @ordered_keys.each { |k| yield k, self[k] }
82
+ self
83
+ end
84
+ alias :each_pair :each
85
+
86
+ def to_a
87
+ @ordered_keys.map { |k| [k, self[k]] }
88
+ end
89
+
90
+ def values
91
+ collect { |k, v| v }
92
+ end
93
+
94
+ def replace(other)
95
+ @ordered_keys.replace(other.keys)
96
+ super
97
+ end
98
+
99
+ def merge(other)
100
+ oh = self.dup
101
+ oh.merge!(other)
102
+ oh
103
+ end
104
+
105
+ def merge!(other)
106
+ @ordered_keys += other.keys # unordered if not an BSON::OrderedHash
107
+ @ordered_keys.uniq!
108
+ super(other)
109
+ end
110
+
111
+ alias :update :merge!
112
+
113
+ def dup
114
+ result = OrderedHash.new
115
+ @ordered_keys.each do |key|
116
+ result[key] = self[key]
117
+ end
118
+ result
119
+ end
120
+
121
+ def inspect
122
+ str = "#<BSON::OrderedHash:0x#{self.object_id.to_s(16)} {"
123
+ str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ")
124
+ str << '}>'
125
+ end
126
+
127
+ def delete(key, &block)
128
+ @ordered_keys.delete(key) if @ordered_keys
129
+ super
130
+ end
131
+
132
+ def delete_if(&block)
133
+ keys.each do |key|
134
+ if yield key, self[key]
135
+ delete(key)
136
+ end
137
+ end
138
+ self
139
+ end
140
+
141
+ def reject(&block)
142
+ clone = self.clone
143
+ return clone unless block_given?
144
+ clone.delete_if(&block)
145
+ end
146
+
147
+ def reject!(&block)
148
+ changed = false
149
+ self.each do |k,v|
150
+ if yield k, v
151
+ changed = true
152
+ delete(k)
153
+ end
154
+ end
155
+ changed ? self : nil
156
+ end
157
+
158
+ def clear
159
+ super
160
+ @ordered_keys = []
161
+ end
162
+
163
+ if RUBY_VERSION =~ /1.8.6/
164
+ def hash
165
+ code = 17
166
+ each_pair do |key, value|
167
+ code = 37 * code + key.hash
168
+ code = 37 * code + value.hash
169
+ end
170
+ code & 0x7fffffff
171
+ end
172
+
173
+ def eql?(o)
174
+ if o.instance_of? BSON::OrderedHash
175
+ self.hash == o.hash
176
+ else
177
+ false
178
+ end
179
+ end
180
+ end
181
+
182
+ def clone
183
+ Marshal::load(Marshal.dump(self))
184
+ end
185
+ end
186
+ end
187
+ end