Class: IdPack::UuidPacker

Inherits:
Object
  • Object
show all
Defined in:
lib/id_pack/uuid_packer.rb

Instance Method Summary collapse

Instance Method Details

#alphanum_compress(arr, alphanum_string, order) ⇒ Object

compress UUIDs array



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
97
98
99
100
101
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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/id_pack/uuid_packer.rb', line 56

def alphanum_compress(arr, alphanum_string, order)
  # length of UUID in bits
  uuid_bit_length = 128

  # compress without delta
  nresult        = ''
  alphanum_array = alphanum_to_array alphanum_string, false
  pow            = alphanum_array[0][2]
  lowhi          = alphanum_array[0][0]

  # first bit equal 0 => compress without delta
  achr = 0
  rest = 1

  # loop by UUIDs
  arr.each do |item|

    # remove '-' characters from UUID
    curr = item.delete('-').to_i(16)

    # get base binary code (BBC)
    achr += (curr << rest)

    # look for number of bits in BBC
    rest += uuid_bit_length

    # create symbols to compressed string
    until rest < pow

      power_c  = pow - 1
      code     = (
        (achr & (2**power_c - 1)) + 2**power_c
      ).to_s(2).reverse.to_i(2) >> 1

      power_c += 1 if code >= lowhi

      # decrease number of bits in BBC
      rest -= power_c

      # get reverse bits from the end of BBC to create new symbol
      code = (
        (achr & (2**power_c - 1)) + 2**power_c
      ).to_s(2).reverse.to_i(2) >> 1

      # add new symbol
      nresult += alphanum_array.assoc(code)[1]

      # remove used bits from BBC
      achr >>= power_c
    end
  end

  # check if we have tail of BBC
  if rest.positive?
    code   = ((achr & (2**rest - 1)) + 2**rest).to_s(2).reverse.to_i(2) >> 1
    code <<= pow - rest - 1
    code <<= 1 if code >= lowhi

    # add tail symbol
    nresult += alphanum_array.assoc(code)[1]
  end

  # compress with delta
  arr = arr.sort

  # first character is delimiter => compress with delta : delimiter (last
  # character in alphabet) always has code of all ones
  dresult        = alphanum_array[-1][1]
  alphanum_array = alphanum_to_array alphanum_string, true
  pow            = alphanum_array[0][2]

  if pow > 1
    lowhi = alphanum_array[0][0]
    prev  = 0

    # loop by UUIDs
    arr.each do |item|

      # remove '-' characters from UUID
      curr = item.delete('-').to_i(16)

      # calculate delta
      curr   -= prev
      prev    = item.delete('-').to_i(16)
      binlog  = bin_pow curr
      binlog  = uuid_bit_length if binlog >= uuid_bit_length - pow

      # get BBC for only current UUID
      achr = curr

      # look for number of bits in BBC (also for only current UUID)
      rest = binlog

      # create symbols to compressed string
      until rest < pow
        power_c  = pow - 1
        code     = (
          (achr & (2**power_c - 1)) +
          2**power_c
        ).to_s(2).reverse.to_i(2) >> 1

        power_c += 1 if code >= lowhi

        # decrease number of bits in BBC
        rest -= power_c

        # get reverse bits from the end of BBC to create new symbol
        code = (
          (achr & (2**power_c - 1)) +
          2**power_c
        ).to_s(2).reverse.to_i(2) >> 1

        # add new symbol
        dresult += alphanum_array.assoc(code)[1]

        # remove used bits from BBC
        achr >>= power_c
      end

      # check if we have tail of BBC for current UUID
      if rest.positive?
        code   = (
          (achr & (2**rest - 1)) + 2**rest
        ).to_s(2).reverse.to_i(2) >> 1

        code <<= pow - rest - 1
        code <<= 1 if code >= lowhi

        # add tail symbol for current UUID
        dresult += alphanum_array.assoc(code)[1]
      end

      # add delimiter if we use less symbols than for whole UUID
      dresult += alphanum_array[0][1] if binlog < uuid_bit_length
    end
  else
    order = true
  end

  result = nresult

  # get better result or non delta if we need to keep order
  result = dresult if dresult.length < nresult.length && !order
  result
end

#alphanum_decompress(str, alphanum_string) ⇒ Object

decompress UUIDs array



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/id_pack/uuid_packer.rb', line 203

def alphanum_decompress(str, alphanum_string)
  # length of UUID in bits
  uuid_bit_length = 128

  result = []
  alphanum_array = alphanum_to_array alphanum_string, false

  # check if delta used when compress
  if (
      alphanum_array.rassoc(str[0])[0] &
      (2**(alphanum_array.rassoc(str[0])[2] - 1))
    ) != 0

    # delta used
    alphanum_array = alphanum_to_array alphanum_string, true
    pow            = alphanum_array[0][2]
    lowhi          = alphanum_array[0][0]
    prev           = 0
    item           = 1
    achr           = 0
    rest           = 0

    # loop by symbols of compressed string starting from second (the first is
    # header) to next after last (for BBC length processing after last)
    while item <= str.length

      # we catch delimiter or we get BBC with length equal whole UUID
      if str[item] == alphanum_array[0][1] || rest >= uuid_bit_length

        # if BBC length than we need to look to current symbol one more time
        # if it is delimiter
        item -= 1 if rest >= uuid_bit_length

        # calculate UUID from delta
        achr += prev
        prev = achr

        # transform UUID to hexadecimal
        curr = prev.to_s(16)

        # add first characters if UUID start with 0
        curr = '0' * (uuid_bit_length / 4 - curr.length) + curr

        # add '-' characters from UUID
        curr = [
          curr[0..7],
          curr[8..11],
          curr[12..15],
          curr[16..19],
          curr[20..31],
        ].join('-')

        # add new UUID to array
        result.push curr
        achr = 0
        rest = 0

      # if we become last symbol we need no to symbol processing
      elsif item < str.length

        # reverse symbol code to BBC bits
        code = (
          alphanum_array.rassoc(str[item])[0] + 2**alphanum_array.rassoc(str[item])[2]
        ).to_s(2).reverse.to_i(2) >> 1

        # add bits to BBC
        achr += code << rest

        # look for number of bits in BBC
        rest += pow
        rest -= 1 if code < lowhi

      end

      item += 1

    end

  else
    # delta not used
    achr  = 0
    rest  = 0
    pow   = alphanum_array[0][2]
    lowhi = alphanum_array[0][0]

    # for first bit processing
    frst = true
    item = 0

    # loop by symbols of compressed string
    while item < str.length

      # reverse symbol code to BBC bits
      code =
        (
          alphanum_array.rassoc(str[item])[0] +
          2**alphanum_array.rassoc(str[item])[2]
        )
          .to_s(2).reverse.to_i(2) >> 1

      # add bits to BBC
      achr += code << rest

      # look for number of bits in BBC
      rest += pow
      rest -= 1 if alphanum_array.rassoc(str[item])[0] < lowhi

      # first bit processing
      if frst
        frst = false
        achr >>= 1
        rest -= 1
      end

      # we get BBC with length equal whole UUID
      if rest >= uuid_bit_length

        # calculate number of bits in BBC
        rest -= uuid_bit_length

        # transform UUID to hexadecimal
        curr = (achr & (2**uuid_bit_length - 1)).to_s(16)

        # add first characters if UUID start with 0
        curr = '0' * (uuid_bit_length / 4 - curr.length) + curr

        # add '-' characters from UUID
        curr = [
          curr[0..7],
          curr[8..11],
          curr[12..15],
          curr[16..19],
          curr[20..31],
        ].join('-')

        # add new UUID to array
        result.push curr

        # remove used bits from BBC
        achr >>= uuid_bit_length

      end

      item += 1
    end
  end
  result
end

#alphanum_to_array(alphanum_string, del) ⇒ Object

transform string of valid characters to useful array (del = true if we need delimiter)



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
# File 'lib/id_pack/uuid_packer.rb', line 14

def alphanum_to_array(alphanum_string, del)

  alphanum_array = []
  el = alphanum_string.length

  # max number of bits coding by one character (some characters will be one
  # bit less)
  pow = bin_pow (el - 1)

  # how many characters will be one bit less
  lowhi = 2**pow - el

  # if delimited we can't use last characters
  if del
    el    -= 1
    pow    = bin_pow(el - 1)
    lowhi  = 2**pow - el
    lowhi  = -1 if lowhi.zero?
    # first element include main data about alphabet and delimiter character
    alphanum_array.push [lowhi, alphanum_string[el], pow]
  else
    # first element include main data about alphabet
    lowhi = -1 if lowhi.zero?
    alphanum_array.push [lowhi, '', pow]
  end

  lowhi     = 0 if lowhi == -1
  char_item = 0

  # loop by characters and get code for each one
  until char_item == el
    if char_item < lowhi
      alphanum_array.push [char_item, alphanum_string[char_item], pow - 1]
    else
      alphanum_array.push [lowhi + char_item, alphanum_string[char_item], pow]
    end
    char_item += 1
  end
  alphanum_array
end

#bin_pow(num) ⇒ Object

calculate bits in number



6
7
8
9
10
# File 'lib/id_pack/uuid_packer.rb', line 6

def bin_pow(num)
  pow = 0
  pow += 1 until (num >> pow).zero?
  pow
end