Class: Innodb::Checksum

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/checksum.rb

Constant Summary collapse

MAX =
0xFFFFFFFF
MASK1 =
1_463_735_687
MASK2 =
1_653_893_711

Class Method Summary collapse

Class Method Details

.fold_enumerator(enumerator) ⇒ Object

Iterate through the provided enumerator, which is expected to return a Integer (or something coercible to it), and “fold” them together to produce a single value.



20
21
22
23
24
25
26
# File 'lib/innodb/checksum.rb', line 20

def self.fold_enumerator(enumerator)
  fold = 0
  enumerator.each do |byte|
    fold = fold_pair(fold, byte)
  end
  fold
end

.fold_pair(num1, num2) ⇒ Object

This is derived from ut_fold_ulint_pair in include/ut0rnd.ic in the InnoDB source code. Since Ruby’s Bignum class is much slower than its Integer class, we mask back to 32 bits to keep things from overflowing and being promoted to Bignum.



13
14
15
# File 'lib/innodb/checksum.rb', line 13

def self.fold_pair(num1, num2)
  (((((((num1 ^ num2 ^ MASK2) << 8) & MAX) + num1) & MAX) ^ MASK1) + num2) & MAX
end

.fold_string(string) ⇒ Object

A simple helper (and example) to fold a provided string.



29
30
31
# File 'lib/innodb/checksum.rb', line 29

def self.fold_string(string)
  fold_enumerator(string.bytes)
end