Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created September 11, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arriqaaq/233d580d8a06bd6c5b20f6a37d5cdfa2 to your computer and use it in GitHub Desktop.
Save arriqaaq/233d580d8a06bd6c5b20f6a37d5cdfa2 to your computer and use it in GitHub Desktop.
// fingerEntry represents a single finger table entry
type fingerEntry struct {
Id []byte // ID hash of (n + 2^i) mod (2^m)
Node *internal.Node
}
func newFingerTable(node *internal.Node, m int) fingerTable {
ft := make([]*fingerEntry, m)
for i := range ft {
ft[i] = newFingerEntry(fingerID(node.Id, i, m), node)
}
return ft
}
// newFingerEntry returns an allocated new finger entry with the attributes set
func newFingerEntry(id []byte, node *internal.Node) *fingerEntry {
return &fingerEntry{
Id: id,
Node: node,
}
}
// Computes the offset by (n + 2^i) mod (2^m)
func fingerID(n []byte, i int, m int) []byte {
// Convert the ID to a bigint
idInt := (&big.Int{}).SetBytes(n)
// Get the offset
two := big.NewInt(2)
offset := big.Int{}
offset.Exp(two, big.NewInt(int64(i)), nil)
// Sum
sum := big.Int{}
sum.Add(idInt, &offset)
// Get the ceiling
ceil := big.Int{}
ceil.Exp(two, big.NewInt(int64(m)), nil)
// Apply the mod
idInt.Mod(&sum, &ceil)
// Add together
return idInt.Bytes()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment