class Innodb::Checksum

Constants

MASK1
MASK2
MAX

Public Class Methods

fold_enumerator(enumerator) click to toggle source

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.

# 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) click to toggle source

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.

# 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) click to toggle source

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

# File lib/innodb/checksum.rb, line 29
def self.fold_string(string)
  fold_enumerator(string.bytes)
end