Class: BSON

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/util/bson.rb

Overview

A BSON seralizer/deserializer.

Constant Summary collapse

MINKEY =
-1
EOO =
0
NUMBER =
1
STRING =
2
OBJECT =
3
ARRAY =
4
BINARY =
5
UNDEFINED =
6
OID =
7
BOOLEAN =
8
DATE =
9
NULL =
10
REGEX =
11
REF =
12
CODE =
13
SYMBOL =
14
CODE_W_SCOPE =
15
NUMBER_INT =
16
MAXKEY =
127

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db = nil) ⇒ BSON

Returns a new instance of BSON.



50
51
52
53
54
# File 'lib/mongo/util/bson.rb', line 50

def initialize(db=nil)
  # db is only needed during deserialization when the data contains a DBRef
  @db = db
  @buf = ByteBuffer.new
end

Class Method Details

.serialize_cstr(buf, val) ⇒ Object



46
47
48
# File 'lib/mongo/util/bson.rb', line 46

def self.serialize_cstr(buf, val)
  buf.put_array(val.to_s.unpack("C*") + [0])
end

Instance Method Details

#bson_type(o, key) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/mongo/util/bson.rb', line 385

def bson_type(o, key)
  case o
  when nil
    NULL
  when Integer
    NUMBER_INT
  when Numeric
    NUMBER
  when XGen::Mongo::Driver::Binary # must be before String
    BINARY
  when String
    # magic awful stuff - the DB requires that a where clause is sent as CODE
    key == "$where" ? CODE : STRING
  when Array
    ARRAY
  when Regexp
    REGEX
  when XGen::Mongo::Driver::ObjectID
    OID
  when XGen::Mongo::Driver::DBRef
    REF
  when true, false
    BOOLEAN
  when Time
    DATE
  when Hash
    OBJECT
  when Symbol
    SYMBOL
  when XGen::Mongo::Driver::Undefined
    UNDEFINED
  else
    raise "Unknown type of object: #{o.class.name}"
  end
end

#deserialize(buf = nil, parent = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/mongo/util/bson.rb', line 108

def deserialize(buf=nil, parent=nil)
  # If buf is nil, use @buf, assumed to contain already-serialized BSON.
  # This is only true during testing.
  @buf = ByteBuffer.new(buf.to_a) if buf
  @buf.rewind
  @buf.get_int                # eat message size
  doc = OrderedHash.new
  while @buf.more?
    type = @buf.get
    case type
    when STRING, CODE
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_string_data(@buf)
    when NUMBER
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_number_data(@buf)
    when NUMBER_INT
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_number_int_data(@buf)
    when OID
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_oid_data(@buf)
    when ARRAY
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_array_data(@buf, doc)
    when REGEX
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_regex_data(@buf)
    when OBJECT
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_object_data(@buf, doc)
    when BOOLEAN
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_boolean_data(@buf)
    when DATE
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_date_data(@buf)
    when NULL
      key = deserialize_cstr(@buf)
      doc[key] = nil
    when UNDEFINED
      key = deserialize_cstr(@buf)
      doc[key] = XGen::Mongo::Driver::Undefined.new
    when REF
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_dbref_data(@buf, key, parent)
    when SYMBOL
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_symbol_data(@buf)
    when BINARY
      key = deserialize_cstr(@buf)
      doc[key] = deserialize_binary_data(@buf)
    when CODE_W_SCOPE
      # TODO
      raise "unimplemented type #{type}"
    when EOO
      break
    else
      raise "Unknown type #{type}, key = #{key}"
    end
  end
  @buf.rewind
  doc
end

#deserialize_array_data(buf, parent) ⇒ Object



210
211
212
213
214
215
# File 'lib/mongo/util/bson.rb', line 210

def deserialize_array_data(buf, parent)
  h = deserialize_object_data(buf, parent)
  a = []
  h.each { |k, v| a[k.to_i] = v }
  a
end

#deserialize_binary_data(buf) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/mongo/util/bson.rb', line 247

def deserialize_binary_data(buf)
  len = buf.get_int
  bytes = buf.get(len)
  str = ''
  bytes.each { |c| str << c.chr }
  str.to_mongo_binary
end

#deserialize_boolean_data(buf) ⇒ Object



192
193
194
# File 'lib/mongo/util/bson.rb', line 192

def deserialize_boolean_data(buf)
  buf.get == 1
end

#deserialize_cstr(buf) ⇒ Object



375
376
377
378
379
380
381
382
383
# File 'lib/mongo/util/bson.rb', line 375

def deserialize_cstr(buf)
  chars = ""
  while 1
    b = buf.get
    break if b == 0
    chars << b.chr
  end
  chars
end

#deserialize_date_data(buf) ⇒ Object



187
188
189
190
# File 'lib/mongo/util/bson.rb', line 187

def deserialize_date_data(buf)
  millisecs = buf.get_long()
  Time.at(millisecs.to_f / 1000.0) # at() takes fractional seconds
end

#deserialize_dbref_data(buf, key, parent) ⇒ Object



237
238
239
240
241
# File 'lib/mongo/util/bson.rb', line 237

def deserialize_dbref_data(buf, key, parent)
  ns = deserialize_cstr(buf)
  oid = deserialize_oid_data(buf)
  XGen::Mongo::Driver::DBRef.new(parent, key, @db, ns, oid)
end

#deserialize_number_data(buf) ⇒ Object



196
197
198
# File 'lib/mongo/util/bson.rb', line 196

def deserialize_number_data(buf)
  buf.get_double
end

#deserialize_number_int_data(buf) ⇒ Object



200
201
202
# File 'lib/mongo/util/bson.rb', line 200

def deserialize_number_int_data(buf)
  buf.get_int
end

#deserialize_object_data(buf, parent) ⇒ Object



204
205
206
207
208
# File 'lib/mongo/util/bson.rb', line 204

def deserialize_object_data(buf, parent)
  size = buf.get_int
  buf.position -= 4
  BSON.new(@db).deserialize(buf.get(size), parent)
end

#deserialize_oid_data(buf) ⇒ Object



233
234
235
# File 'lib/mongo/util/bson.rb', line 233

def deserialize_oid_data(buf)
  XGen::Mongo::Driver::ObjectID.new(buf.get(12))
end

#deserialize_regex_data(buf) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/mongo/util/bson.rb', line 217

def deserialize_regex_data(buf)
  str = deserialize_cstr(buf)
  options_str = deserialize_cstr(buf)
  options = 0
  options |= Regexp::IGNORECASE if options_str.include?('i')
  options |= Regexp::MULTILINE if options_str.include?('m')
  options |= Regexp::EXTENDED if options_str.include?('x')
  Regexp.new(str, options)
end

#deserialize_string_data(buf) ⇒ Object



227
228
229
230
231
# File 'lib/mongo/util/bson.rb', line 227

def deserialize_string_data(buf)
  len = buf.get_int
  bytes = buf.get(len)
  bytes[0..-2].pack("C*")
end

#deserialize_symbol_data(buf) ⇒ Object



243
244
245
# File 'lib/mongo/util/bson.rb', line 243

def deserialize_symbol_data(buf)
  deserialize_cstr(buf).intern
end

#hex_dumpObject



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mongo/util/bson.rb', line 173

def hex_dump
  str = ''
  @buf.to_a.each_with_index { |b,i|
    if (i % 8) == 0
      str << "\n" if i > 0
      str << '%4d:  ' % i
    else
      str << ' '
    end
    str << '%02X' % b
  }
  str
end

#serialize(obj) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mongo/util/bson.rb', line 60

def serialize(obj)
  raise "Document is null" unless obj

  @buf.rewind
  # put in a placeholder for the total size
  @buf.put_int(0)

  obj.each {|k, v|
    type = bson_type(v, k)
    case type
    when STRING, CODE
      serialize_string_element(@buf, k, v, type)
    when NUMBER, NUMBER_INT
      serialize_number_element(@buf, k, v, type)
    when OBJECT
      serialize_object_element(@buf, k, v)
    when OID
      serialize_oid_element(@buf, k, v)
    when ARRAY
      serialize_array_element(@buf, k, v)
    when REGEX
      serialize_regex_element(@buf, k, v)
    when BOOLEAN
      serialize_boolean_element(@buf, k, v)
    when DATE
      serialize_date_element(@buf, k, v)
    when NULL
      serialize_null_element(@buf, k)
    when REF
      serialize_dbref_element(@buf, k, v)
    when SYMBOL
      serialize_symbol_element(@buf, k, v)
    when BINARY
      serialize_binary_element(@buf, k, v)
    when UNDEFINED
      serialize_undefined_element(@buf, k)
    when CODE_W_SCOPE
      # TODO
      raise "unimplemented type #{type}"
    else
      raise "unhandled type #{type}"
    end
  }
  serialize_eoo_element(@buf)
  @buf.put_int(@buf.size, 0)
  self
end

#serialize_array_element(buf, key, val) ⇒ Object



325
326
327
328
329
330
331
# File 'lib/mongo/util/bson.rb', line 325

def serialize_array_element(buf, key, val)
  # Turn array into hash with integer indices as keys
  h = OrderedHash.new
  i = 0
  val.each { |v| h[i] = v; i += 1 }
  serialize_object_element(buf, key, h, ARRAY)
end

#serialize_binary_element(buf, key, val) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/mongo/util/bson.rb', line 277

def serialize_binary_element(buf, key, val)
  buf.put(BINARY)
  self.class.serialize_cstr(buf, key)
  buf.put_int(val.length)
  bytes = if RUBY_VERSION >= '1.9'
            val.bytes.to_a
          else
            a = []
            val.each_byte { |byte| a << byte }
            a
          end
  buf.put_array(bytes)
end

#serialize_boolean_element(buf, key, val) ⇒ Object



296
297
298
299
300
# File 'lib/mongo/util/bson.rb', line 296

def serialize_boolean_element(buf, key, val)
  buf.put(BOOLEAN)
  self.class.serialize_cstr(buf, key)
  buf.put(val ? 1 : 0)
end

#serialize_date_element(buf, key, val) ⇒ Object



302
303
304
305
306
307
# File 'lib/mongo/util/bson.rb', line 302

def serialize_date_element(buf, key, val)
  buf.put(DATE)
  self.class.serialize_cstr(buf, key)
  millisecs = (val.to_f * 1000).to_i
  buf.put_long(millisecs)
end

#serialize_dbref_element(buf, key, val) ⇒ Object



264
265
266
267
268
269
# File 'lib/mongo/util/bson.rb', line 264

def serialize_dbref_element(buf, key, val)
  buf.put(REF)
  self.class.serialize_cstr(buf, key)
  self.class.serialize_cstr(buf, val.namespace)
  buf.put_array(val.object_id.to_a)
end

#serialize_eoo_element(buf) ⇒ Object



255
256
257
# File 'lib/mongo/util/bson.rb', line 255

def serialize_eoo_element(buf)
  buf.put(EOO)
end

#serialize_null_element(buf, key) ⇒ Object



259
260
261
262
# File 'lib/mongo/util/bson.rb', line 259

def serialize_null_element(buf, key)
  buf.put(NULL)
  self.class.serialize_cstr(buf, key)
end

#serialize_number_element(buf, key, val, type) ⇒ Object



309
310
311
312
313
314
315
316
317
# File 'lib/mongo/util/bson.rb', line 309

def serialize_number_element(buf, key, val, type)
  buf.put(type)
  self.class.serialize_cstr(buf, key)
  if type == NUMBER
    buf.put_double(val)
  else
    buf.put_int(val)
  end
end

#serialize_object_element(buf, key, val, opcode = OBJECT) ⇒ Object



319
320
321
322
323
# File 'lib/mongo/util/bson.rb', line 319

def serialize_object_element(buf, key, val, opcode=OBJECT)
  buf.put(opcode)
  self.class.serialize_cstr(buf, key)
  buf.put_array(BSON.new.serialize(val).to_a)
end

#serialize_oid_element(buf, key, val) ⇒ Object



348
349
350
351
352
353
# File 'lib/mongo/util/bson.rb', line 348

def serialize_oid_element(buf, key, val)
  buf.put(OID)
  self.class.serialize_cstr(buf, key)

  buf.put_array(val.to_a)
end

#serialize_regex_element(buf, key, val) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/mongo/util/bson.rb', line 333

def serialize_regex_element(buf, key, val)
  buf.put(REGEX)
  self.class.serialize_cstr(buf, key)

  str = val.to_s.sub(/.*?:/, '')[0..-2] # Turn "(?xxx:yyy)" into "yyy"
  self.class.serialize_cstr(buf, str)

  options = val.options
  options_str = ''
  options_str << 'i' if ((options & Regexp::IGNORECASE) != 0)
  options_str << 'm' if ((options & Regexp::MULTILINE) != 0)
  options_str << 'x' if ((options & Regexp::EXTENDED) != 0)
  self.class.serialize_cstr(buf, options_str)
end

#serialize_string_element(buf, key, val, type) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/mongo/util/bson.rb', line 355

def serialize_string_element(buf, key, val, type)
  buf.put(type)
  self.class.serialize_cstr(buf, key)

  # Make a hole for the length
  len_pos = buf.position
  buf.put_int(0)

  # Save the string
  start_pos = buf.position
  self.class.serialize_cstr(buf, val)
  end_pos = buf.position

  # Put the string size in front
  buf.put_int(end_pos - start_pos, len_pos)

  # Go back to where we were
  buf.position = end_pos
end

#serialize_symbol_element(buf, key, val) ⇒ Object



271
272
273
274
275
# File 'lib/mongo/util/bson.rb', line 271

def serialize_symbol_element(buf, key, val)
  buf.put(SYMBOL)
  self.class.serialize_cstr(buf, key)
  self.class.serialize_cstr(buf, val)
end

#serialize_undefined_element(buf, key) ⇒ Object



291
292
293
294
# File 'lib/mongo/util/bson.rb', line 291

def serialize_undefined_element(buf, key)
  buf.put(UNDEFINED)
  self.class.serialize_cstr(buf, key)
end

#to_aObject



56
57
58
# File 'lib/mongo/util/bson.rb', line 56

def to_a
  @buf.to_a
end