-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FNV hashing in the project (#14)
Implement FNV (Fowler–Noll–Vo) hashing in the project instead of pulling in a separate gem. This normally wouldn't really be worth it, but the algorithm is extremely simple to implement, and it is somewhat nice to keep the gem so that it has no dependencies.
- Loading branch information
Showing
9 changed files
with
710 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module River | ||
# FNV is the Fowler–Noll–Vo hash function, a simple hash that's very easy to | ||
# implement, and hash the perfect characteristics for use with the 64 bits of | ||
# available space in a PG advisory lock. | ||
# | ||
# I'm implemented it myself so that the River gem can stay dependency free | ||
# (and because it's quite easy to do). | ||
module FNV | ||
def self.fnv1_hash(str, size:) | ||
hash = OFFSET_BASIS.fetch(size) | ||
mask = MASK.fetch(size) | ||
prime = PRIME.fetch(size) | ||
|
||
str.each_byte do |byte| | ||
hash *= prime | ||
hash &= mask # take lower N bits of multiplication product | ||
hash ^= byte | ||
end | ||
|
||
hash | ||
end | ||
|
||
MASK = { | ||
32 => 0xffffffff, # mask 32 bits long | ||
64 => 0xffffffffffffffff # mask 64 bits long | ||
}.freeze | ||
private_constant :MASK | ||
|
||
OFFSET_BASIS = { | ||
32 => 0x811c9dc5, | ||
64 => 0xcbf29ce484222325 | ||
}.freeze | ||
private_constant :OFFSET_BASIS | ||
|
||
PRIME = { | ||
32 => 0x01000193, | ||
64 => 0x00000100000001B3 | ||
}.freeze | ||
private_constant :PRIME | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require "json" | ||
|
||
require_relative "fnv" | ||
require_relative "insert_opts" | ||
require_relative "job" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module River | ||
module FNV | ||
def self.fnv1_hash: (String, size: 32 | 64) -> Integer | ||
|
||
MASK: Hash[Integer, Integer] | ||
OFFSET_BASIS: Hash[Integer, Integer] | ||
PRIME: Hash[Integer, Integer] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.