Class: Innodb::Checksum

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

Constant Summary collapse

MAX =
0xFFFFFFFF.freeze
MASK1 =
1463735687.freeze
MASK2 =
1653893711.freeze

Class Method Summary collapse

Class Method Details

.fold_enumerator(enumerator) ⇒ Object

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



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

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

.fold_pair(n1, n2) ⇒ 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 Fixnum class, we mask back to 32 bits to keep things from overflowing and being promoted to Bignum.



11
12
13
# File 'lib/innodb/checksum.rb', line 11

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

.fold_string(string) ⇒ Object

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



27
28
29
# File 'lib/innodb/checksum.rb', line 27

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