Class: VoldemortJsonBinarySerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/voldemort-serializer.rb

Constant Summary collapse

BYTE_MIN_VAL =
-128
SHORT_MIN_VAL =
-32768
SHORT_MAX_VAL =
2 ** 15 - 1
INT_MIN_VAL =
-2147483648
LONG_MIN_VAL =
-9223372036854775808
FLOAT_MIN_VAL =
2 ** -149
DOUBLE_MIN_VAL =
2 ** -1074

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_def_versions) ⇒ VoldemortJsonBinarySerializer

Returns a new instance of VoldemortJsonBinarySerializer.



16
17
18
19
20
21
22
23
24
# File 'lib/voldemort-serializer.rb', line 16

def initialize(type_def_versions)
  @has_version = true
  @type_def_versions = {}
  
  # convert versioned json strings to ruby objects
  type_def_versions.each_pair do |version, json_type_def_version|
    @type_def_versions[version.to_i] = get_type_def(json_type_def_version)
  end
end

Instance Attribute Details

#has_versionObject

Returns the value of attribute has_version.



5
6
7
# File 'lib/voldemort-serializer.rb', line 5

def has_version
  @has_version
end

#type_def_versionsObject

Returns the value of attribute type_def_versions.



6
7
8
# File 'lib/voldemort-serializer.rb', line 6

def type_def_versions
  @type_def_versions
end

Instance Method Details

#get_type_def(json_type_def_version) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/voldemort-serializer.rb', line 33

def get_type_def(json_type_def_version)
  # replace all single quotes with " since the JSON parser wants it this way
  json_type_def_version = json_type_def_version.gsub(/\'/, '"')
  
  if((json_type_def_version =~ /[\{\[]/) == 0)
    # check if the json is a list or string, since these are 
    # the only ones that JSON.parse() will work with
    return JSON.parse(json_type_def_version)
  else
    # otherwise it's a primitive, so just strip the quotes
    return json_type_def_version.gsub(/\"/, '')
  end
end

#read(bytes, type) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/voldemort-serializer.rb', line 335

def read(bytes, type)
  if(type.kind_of? Hash)
    return read_map(bytes, type)
  elsif(type.kind_of? Array)
    return read_list(bytes, type)
  else
    case(type)
      when 'string'
        return read_bytes(bytes)
      when 'int8'
        return read_int8(bytes)
      when 'int16'
        return read_int16(bytes)
      when 'int32'
        return read_int32(bytes)
      when 'int64'
        return read_int64(bytes)
      when 'float32'
        return read_float32(bytes)
      when 'float64'
        return read_float64(bytes)
      when 'date'
        return read_date(bytes)
      when 'bytes'
        return read_bytes(bytes)
      when 'boolean'
        return read_boolean(bytes)
      # TODO default throw unknown type exception
    end
  end
end

#read_boolean(bytes) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
# File 'lib/voldemort-serializer.rb', line 397

def read_boolean(bytes)
  b = read_slice(1, bytes).unpack('c').first
  
  if(b < 0)
    return nil
  elsif(b == 0)
    return false
  else
    return true
  end
end

#read_bytes(bytes) ⇒ Object



483
484
485
486
487
488
489
490
491
# File 'lib/voldemort-serializer.rb', line 483

def read_bytes(bytes)
  size = read_int16(bytes)
  
  if(size < 0)
    return nil
  else
    return read_slice(size, bytes)
  end
end

#read_date(bytes) ⇒ Object



473
474
475
476
477
478
479
480
481
# File 'lib/voldemort-serializer.rb', line 473

def read_date(bytes)
  d = read_int64(bytes)
  
  if(d != nil)
    d = Time.at((d / 1000).to_i, d % 1000)
  end
  
  return d
end

#read_float32(bytes) ⇒ Object



453
454
455
456
457
458
459
460
461
# File 'lib/voldemort-serializer.rb', line 453

def read_float32(bytes)
  f = read_slice(4, bytes).unpack("g").first.to_f
  
  if(f == FLOAT_MIN_VAL)
    return nil
  end
  
  return f
end

#read_float64(bytes) ⇒ Object



463
464
465
466
467
468
469
470
471
# File 'lib/voldemort-serializer.rb', line 463

def read_float64(bytes)
  d = read_slice(8, bytes).unpack("G").first.to_f
  
  if(d == DOUBLE_MIN_VAL)
    return nil
  end
  
  return d
end

#read_int16(bytes) ⇒ Object



419
420
421
422
423
424
425
426
427
# File 'lib/voldemort-serializer.rb', line 419

def read_int16(bytes)
  s = to_signed(read_slice(2, bytes).unpack("n").first, 16)

  if(s == SHORT_MIN_VAL)
    return nil
  end
  
  return s
end

#read_int32(bytes) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
# File 'lib/voldemort-serializer.rb', line 429

def read_int32(bytes)
  # reverse here to switch little endian to big endian
  # this is because pack('N') is choking on 'bigint', wtf?
  i = read_slice(4, bytes).reverse.unpack("i").first.to_i
  
  if(i == INT_MIN_VAL)
    return nil
  end
  
  return i
end

#read_int64(bytes) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
# File 'lib/voldemort-serializer.rb', line 441

def read_int64(bytes)
  # reverse here to switch little endian to big endian
  # this is because pack('N') is choking on 'bigint', wtf?
  l = read_slice(8, bytes).reverse.unpack("q").first.to_i
  
  if(l == LONG_MIN_VAL)
    return nil
  end
  
  return l
end

#read_int8(bytes) ⇒ Object



409
410
411
412
413
414
415
416
417
# File 'lib/voldemort-serializer.rb', line 409

def read_int8(bytes)
  b = read_slice(1, bytes).unpack("c").first.to_i
  
  if(b == BYTE_MIN_VAL)
    return nil
  end
  
  return b
end

#read_list(bytes, type) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/voldemort-serializer.rb', line 384

def read_list(bytes, type)
  size = read_int16(bytes)
  if(size < 0)
    return nil
  else
    object = []
    
    size.times { object << read(bytes, type.first) }
    
    return object
  end
end

#read_map(bytes, type) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/voldemort-serializer.rb', line 367

def read_map(bytes, type)
  # convert to char to string, and string to int
  if(read_slice(1, bytes).unpack('c').to_s.to_i == -1)
    return nil
  else
    object = {}
    
    type.sort.each do |type_pair|
      name = type_pair.first
      sub_type = type_pair.last
      object[name] = read(bytes, sub_type)
    end
    
    return object
  end
end

#read_slice(length, bytes) ⇒ Object



47
48
49
50
51
# File 'lib/voldemort-serializer.rb', line 47

def read_slice(length, bytes)
  substr = bytes[0, length]
  bytes.slice!(0..length - 1)
  return substr
end

#to_bytes(object) ⇒ Object

handle serialization



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/voldemort-serializer.rb', line 55

def to_bytes(object)
  bytes = ''
  newest_version = 0 # TODO get highest number from map
  type_def = @type_def_versions[newest_version]
  
  if(@has_version)
    bytes << newest_version.chr
  end
  
  bytes << write(object, type_def)
  
  return bytes
end

#to_object(bytes) ⇒ Object

handle deserialization



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/voldemort-serializer.rb', line 319

def to_object(bytes)
  version = 0
  
  if(@has_version)
    version = read_slice(1, bytes).to_i
  end
  
  type = @type_def_versions[version]
  
  if(type == nil)
    # TODO throw exception here
  end
  
  return read(bytes, type)
end

#to_signed(unsigned, bits) ⇒ Object



26
27
28
29
30
31
# File 'lib/voldemort-serializer.rb', line 26

def to_signed(unsigned, bits)
  max_unsigned = 2 ** bits
  max_signed = 2 ** (bits - 1)
  to_signed = proc { |n| (n >= max_signed) ? n - max_unsigned : n }
  return to_signed[unsigned]
end

#write(object, type) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
# File 'lib/voldemort-serializer.rb', line 69

def write(object, type)
  bytes = ''
  
  if(type.kind_of? Hash)
  	if(object != nil && !object.kind_of?(Hash))
  	  # TODO throw exception
  	else
  	  bytes << write_map(object, type)
  	end
  elsif(type.kind_of? Array)
  	if(object != nil && !object.kind_of?(Array))
  	  # TODO throw exception
  	else
  	  bytes << write_list(object, type)
  	end
  else
    case(type)
      when 'string'
        bytes << write_string(object)
      when 'int8'
        bytes << write_int8(object)
      when 'int16'
        bytes << write_int16(object)
      when 'int32'
        bytes << write_int32(object)
      when 'int64'
        bytes << write_int64(object)
      when 'float32'
        bytes << write_float32(object)
      when 'float64'
        bytes << write_float64(object)
      when 'date'
        bytes << write_date(object)
      when 'bytes'
        bytes << write_bytes(object)
      when 'boolean'
        bytes << write_boolean(object)
      else
        # TODO throw unsupported type exception
    end
  end
  
  if(bytes == '')
    return nil
  end
  
  return bytes
end

#write_boolean(object) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/voldemort-serializer.rb', line 118

def write_boolean(object)
  bytes = ''
  
  if(object == nil)
    bytes << [BYTE_MIN_VAL].pack('c')
  elsif(object)
    bytes << [0x1].pack('c')
  else
    bytes << [0x0].pack('c')
  end
  
  return bytes
end

#write_bytes(object) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/voldemort-serializer.rb', line 252

def write_bytes(object)
  bytes = ''
  
  if(object == nil)
    bytes << write_int16(-1)
  elsif(object.length < SHORT_MAX_VAL)
    bytes << write_int16(object.length)
    bytes << object
  else
    # TODO throw "length too long to serialize" exception
  end
  
  return bytes
end

#write_date(object) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/voldemort-serializer.rb', line 236

def write_date(object)
  bytes = ''
  
  if(object == LONG_MIN_VAL)
    # TODO throw underflow exception
  else
    if(object == nil)
      bytes << write_int64(nil)
    else
      bytes << write_int64((object.to_f * 1000).to_i)
    end
  end
  
  return bytes
end

#write_float32(object) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/voldemort-serializer.rb', line 204

def write_float32(object)
  bytes = ''
  
  if(object == FLOAT_MIN_VAL)
    # TODO throw underflow exception
  else
    if(object == nil)
      object = FLOAT_MIN_VAL
    end
    
    bytes << [object].pack('g')
  end
  
  return bytes
end

#write_float64(object) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/voldemort-serializer.rb', line 220

def write_float64(object)
  bytes = ''
  
  if(object == DOUBLE_MIN_VAL)
    # TODO throw underflow exception
  else
    if(object == nil)
      object = DOUBLE_MIN_VAL
    end
    
    bytes << [object].pack('G')
  end
  
  return bytes
end

#write_int16(object) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/voldemort-serializer.rb', line 152

def write_int16(object)
  bytes = ''
  
  if(object == SHORT_MIN_VAL)
    # TODO throw underflow exception
  else
    if(object == nil)
      object = SHORT_MIN_VAL
    end
    
    bytes << [object].pack('n')
  end
  
  return bytes
end

#write_int32(object) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/voldemort-serializer.rb', line 168

def write_int32(object)
  bytes = ''
  
  if(object == INT_MIN_VAL)
    # TODO throw underflow exception
  else
    if(object == nil)
      object = INT_MIN_VAL
    end
    
    # reverse here to switch little endian to big endian
    # this is because pack('N') is choking on 'bigint', wtf?
    bytes << [object].pack('i').reverse
  end
  
  return bytes
end

#write_int64(object) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/voldemort-serializer.rb', line 186

def write_int64(object)
  bytes = ''
  
  if(object == LONG_MIN_VAL)
    # TODO throw underflow exception
  else
    if(object == nil)
      object = LONG_MIN_VAL
    end
    
    # reverse here to switch little endian to big endian
    # this is because pack('N') is choking on 'bigint', wtf?
    bytes << [object].pack('q').reverse
  end
  
  return bytes
end

#write_int8(object) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/voldemort-serializer.rb', line 136

def write_int8(object)
  bytes = ''
  
  if(object == BYTE_MIN_VAL)
    # TODO throw underflow exception
  else
    if(object == nil)
      object = BYTE_MIN_VAL
    end
    
    bytes << [object].pack('c')
  end
  
  return bytes
end

#write_list(object, type) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/voldemort-serializer.rb', line 294

def write_list(object, type)
  bytes = ''
  
  if(type.length != 1)
    # TODO throw new exception (expected single type in list)
  else
    entry_type = type.first
    
    if(object == nil)
      bytes << write_int16(-1)
    elsif(object.length < SHORT_MAX_VAL)
      bytes << write_int16(object.length)
      object.each do |o|
        bytes << write(o, entry_type)
      end
    else
      # TODO throw serialization exception
    end
  end
  
  return bytes
end

#write_map(object, type) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/voldemort-serializer.rb', line 267

def write_map(object, type)
  bytes = ''
  
  if(object == nil)
    bytes << [-1].pack('c')
  else
    bytes << [1].pack('c')
    
    if(object.length != type.length)
      # TODO throw exception here.. invalid map serialization, expected: but got 
    else
      type.sort.each do |type_pair|
        key = type_pair.first
        subtype = type_pair.last
        
        if(!object.has_key? key)
          # TODO throw "missing property exception"
        else
          bytes << write(object[key], subtype)
        end
      end
    end
  end
  
  return bytes
end

#write_string(object) ⇒ Object



132
133
134
# File 'lib/voldemort-serializer.rb', line 132

def write_string(object)
  return write_bytes(object)
end