Class: BSON

Inherits:
Object
  • Object
show all
Includes:
XGen::Mongo::Driver
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
TIMESTAMP =
17
MAXKEY =
127

Constants included from XGen::Mongo::Driver

XGen::Mongo::Driver::OP_DELETE, XGen::Mongo::Driver::OP_GET_MORE, XGen::Mongo::Driver::OP_INSERT, XGen::Mongo::Driver::OP_KILL_CURSORS, XGen::Mongo::Driver::OP_MSG, XGen::Mongo::Driver::OP_QUERY, XGen::Mongo::Driver::OP_REPLY, XGen::Mongo::Driver::OP_UPDATE

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBSON

Returns a new instance of BSON.



66
67
68
# File 'lib/mongo/util/bson.rb', line 66

def initialize()
  @buf = ByteBuffer.new
end

Class Method Details

.serialize_cstr(buf, val) ⇒ Object



62
63
64
# File 'lib/mongo/util/bson.rb', line 62

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

.to_utf8(str) ⇒ Object



53
54
55
# File 'lib/mongo/util/bson.rb', line 53

def self.to_utf8(str)
  str.encode("utf-8")
end

Instance Method Details

#bson_type(o) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/mongo/util/bson.rb', line 497

def bson_type(o)
  case o
  when nil
    NULL
  when Integer
    NUMBER_INT
  when Numeric
    NUMBER
  when ByteBuffer
    BINARY
  when Code
    CODE_W_SCOPE
  when String
    STRING
  when Array
    ARRAY
  when Regexp
    REGEX
  when ObjectID
    OID
  when DBRef
    REF
  when true, false
    BOOLEAN
  when Time
    DATE
  when Hash
    OBJECT
  when Symbol
    SYMBOL
  when Undefined
    UNDEFINED
  else
    raise "Unknown type of object: #{o.class.name}"
  end
end

#deserialize_array_data(buf) ⇒ Object



277
278
279
280
281
282
# File 'lib/mongo/util/bson.rb', line 277

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

#deserialize_binary_data(buf) ⇒ Object



336
337
338
339
340
341
# File 'lib/mongo/util/bson.rb', line 336

def deserialize_binary_data(buf)
  len = buf.get_int
  type = buf.get
  len = buf.get_int if type == Binary::SUBTYPE_BYTES
  Binary.new(buf.get(len), type)
end

#deserialize_boolean_data(buf) ⇒ Object



251
252
253
# File 'lib/mongo/util/bson.rb', line 251

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

#deserialize_code_w_scope_data(buf) ⇒ Object



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/mongo/util/bson.rb', line 308

def deserialize_code_w_scope_data(buf)
  buf.get_int
  len = buf.get_int
  code = buf.get(len)[0..-2]
  if code.respond_to? "pack"
    code = code.pack("C*")
  end
  if RUBY_VERSION >= '1.9'
    code.force_encoding("utf-8")
  end

  scope_size = buf.get_int
  buf.position -= 4
  scope = BSON.new().deserialize(buf.get(scope_size))

  Code.new(code, scope)
end

#deserialize_cstr(buf) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/mongo/util/bson.rb', line 484

def deserialize_cstr(buf)
  chars = ""
  while true
    b = buf.get
    break if b == 0
    chars << b.chr
  end
  if RUBY_VERSION >= '1.9'
    chars.force_encoding("utf-8") # Mongo stores UTF-8
  end
  chars
end

#deserialize_date_data(buf) ⇒ Object



246
247
248
249
# File 'lib/mongo/util/bson.rb', line 246

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

#deserialize_dbref_data(buf) ⇒ Object



330
331
332
333
334
# File 'lib/mongo/util/bson.rb', line 330

def deserialize_dbref_data(buf)
  ns = deserialize_string_data(buf)
  oid = deserialize_oid_data(buf)
  DBRef.new(ns, oid)
end

#deserialize_number_data(buf) ⇒ Object



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

def deserialize_number_data(buf)
  buf.get_double
end

#deserialize_number_int_data(buf) ⇒ Object



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

def deserialize_number_int_data(buf)
  # sometimes ruby makes me angry... why would the same code pack as signed
  # but unpack as unsigned
  unsigned = buf.get_int
  unsigned >= 2**32 / 2 ? unsigned - 2**32 : unsigned
end

#deserialize_object_data(buf) ⇒ Object



266
267
268
269
270
271
272
273
274
275
# File 'lib/mongo/util/bson.rb', line 266

def deserialize_object_data(buf)
  size = buf.get_int
  buf.position -= 4
  object = BSON.new().deserialize(buf.get(size))
  if object.has_key? "$ref"
    DBRef.new(object["$ref"], object["$id"])
  else
    object
  end
end

#deserialize_oid_data(buf) ⇒ Object



326
327
328
# File 'lib/mongo/util/bson.rb', line 326

def deserialize_oid_data(buf)
  ObjectID.new(buf.get(12))
end

#deserialize_regex_data(buf) ⇒ Object



284
285
286
287
288
289
290
291
292
293
# File 'lib/mongo/util/bson.rb', line 284

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')
  options_str.gsub!(/[imx]/, '') # Now remove the three we understand
  RegexpOfHolding.new(str, options, options_str)
end

#deserialize_string_data(buf) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/mongo/util/bson.rb', line 295

def deserialize_string_data(buf)
  len = buf.get_int
  bytes = buf.get(len)
  str = bytes[0..-2]
  if str.respond_to? "pack"
    str = str.pack("C*")
  end
  if RUBY_VERSION >= '1.9'
    str.force_encoding("utf-8")
  end
  str
end

#hex_dumpObject

For debugging.



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/mongo/util/bson.rb', line 232

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_array_element(buf, key, val, check_keys) ⇒ Object



415
416
417
418
419
420
421
# File 'lib/mongo/util/bson.rb', line 415

def serialize_array_element(buf, key, val, check_keys)
  # 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, check_keys, ARRAY)
end

#serialize_binary_element(buf, key, val) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/mongo/util/bson.rb', line 359

def serialize_binary_element(buf, key, val)
  buf.put(BINARY)
  self.class.serialize_cstr(buf, key)

  bytes = val.to_a
  num_bytes = bytes.length
  subtype = val.respond_to?(:subtype) ? val.subtype : Binary::SUBTYPE_BYTES
  if subtype == Binary::SUBTYPE_BYTES
    buf.put_int(num_bytes + 4)
    buf.put(subtype)
    buf.put_int(num_bytes)
    buf.put_array(bytes)
  else
    buf.put_int(num_bytes)
    buf.put(subtype)
    buf.put_array(bytes)
  end
end

#serialize_boolean_element(buf, key, val) ⇒ Object



383
384
385
386
387
# File 'lib/mongo/util/bson.rb', line 383

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

#serialize_code_w_scope(buf, key, val) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/mongo/util/bson.rb', line 467

def serialize_code_w_scope(buf, key, val)
  buf.put(CODE_W_SCOPE)
  self.class.serialize_cstr(buf, key)

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

  buf.put_int(val.length + 1)
  self.class.serialize_cstr(buf, val)
  buf.put_array(BSON.new.serialize(val.scope).to_a)

  end_pos = buf.position
  buf.put_int(end_pos - len_pos, len_pos)
  buf.position = end_pos
end

#serialize_date_element(buf, key, val) ⇒ Object



389
390
391
392
393
394
# File 'lib/mongo/util/bson.rb', line 389

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



352
353
354
355
356
357
# File 'lib/mongo/util/bson.rb', line 352

def serialize_dbref_element(buf, key, val)
  oh = OrderedHash.new
  oh['$ref'] = val.namespace
  oh['$id'] = val.object_id
  serialize_object_element(buf, key, oh, false)
end

#serialize_eoo_element(buf) ⇒ Object



343
344
345
# File 'lib/mongo/util/bson.rb', line 343

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

#serialize_key_value(k, v, check_keys) ⇒ Object



102
103
104
105
106
107
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
# File 'lib/mongo/util/bson.rb', line 102

def serialize_key_value(k, v, check_keys)
  k = k.to_s
  if check_keys
    if k[0] == ?$
      raise RuntimeError.new("key #{k} must not start with '$'")
    end
    if k.include? ?.
      raise RuntimeError.new("key #{k} must not contain '.'")
    end
  end
  type = bson_type(v)
  case type
  when STRING, SYMBOL
    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, check_keys)
  when OID
    serialize_oid_element(@buf, k, v)
  when ARRAY
    serialize_array_element(@buf, k, v, check_keys)
  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 BINARY
    serialize_binary_element(@buf, k, v)
  when UNDEFINED
    serialize_undefined_element(@buf, k)
  when CODE_W_SCOPE
    serialize_code_w_scope(@buf, k, v)
  else
    raise "unhandled type #{type}"
  end
end

#serialize_null_element(buf, key) ⇒ Object



347
348
349
350
# File 'lib/mongo/util/bson.rb', line 347

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

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



396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/mongo/util/bson.rb', line 396

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
    if val > 2**32 / 2 - 1 or val < -2**32 / 2
      raise RangeError.new("MongoDB can only handle 4-byte ints - try converting to a double before saving")
    end
    buf.put_int(val)
  end
end

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



409
410
411
412
413
# File 'lib/mongo/util/bson.rb', line 409

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

#serialize_oid_element(buf, key, val) ⇒ Object



440
441
442
443
444
445
# File 'lib/mongo/util/bson.rb', line 440

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



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/mongo/util/bson.rb', line 423

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)
  options_str << val.extra_options_str if val.respond_to?(:extra_options_str)
  # Must store option chars in alphabetical order
  self.class.serialize_cstr(buf, options_str.split(//).sort.uniq.join)
end

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



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/mongo/util/bson.rb', line 447

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_undefined_element(buf, key) ⇒ Object



378
379
380
381
# File 'lib/mongo/util/bson.rb', line 378

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

#to_aObject



70
71
72
# File 'lib/mongo/util/bson.rb', line 70

def to_a
  @buf.to_a
end