-
Notifications
You must be signed in to change notification settings - Fork 40
/
entity.rb
96 lines (78 loc) · 2.24 KB
/
entity.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
require 'json'
require 'zlib'
require 'readthis/passthrough'
module Readthis
class Entity
DEFAULT_OPTIONS = {
compress: false,
marshal: Marshal,
threshold: 8 * 1024
}.freeze
SERIALIZER_FLAGS = {
Marshal => 0x1,
JSON => 0x2,
Passthrough => 0x3
}.freeze
DESERIALIZER_FLAGS = SERIALIZER_FLAGS.invert.freeze
COMPRESSED_FLAG = 0x8
MARSHAL_FLAG = 0x3
def initialize(options = {})
@options = DEFAULT_OPTIONS.merge(options)
end
def dump(value, options = {})
marshal = with_fallback(options, :marshal)
threshold = with_fallback(options, :threshold)
compress = with_fallback(options, :compress)
dumped = deflate(marshal.dump(value), compress, threshold)
compose(dumped, marshal, compress)
end
def load(string)
marshal, compress, value = decompose(string)
marshal.load(inflate(value, compress))
rescue TypeError, NoMethodError
string
end
# Composes a single byte comprised of the chosen serializer and compression
# options. The byte is formatted as:
#
# | 0000 | 0 | 000 |
#
# Where there are four unused bits, 1 compression bit, and 3 bits for the
# serializer. This allows up to 8 different serializers for marshaling.
def compose(value, marshal, compress)
flags = SERIALIZER_FLAGS[marshal]
flags |= COMPRESSED_FLAG if compress
value.prepend([flags].pack('C'))
end
def decompose(string)
flags = string[0].unpack('C').first
if flags < 16
marshal = DESERIALIZER_FLAGS[flags & MARSHAL_FLAG]
compress = (flags & COMPRESSED_FLAG) != 0
[marshal, compress, string[1..-1]]
else
[@options[:marshal], @options[:compress], string]
end
end
private
def deflate(value, compress, threshold)
if compress && value.bytesize >= threshold
Zlib::Deflate.deflate(value)
else
value
end
end
def inflate(value, decompress)
if decompress
Zlib::Inflate.inflate(value)
else
value
end
rescue Zlib::Error
value
end
def with_fallback(options, key)
options.key?(key) ? options[key] : @options[key]
end
end
end