cht.CHashTable
is a local consistent hash table that stores the hash IDs and corresponding node identifiers for each known node.
- The "node identifier" could be an IP address, a port or anything that uniquely identifies the node.
- Note that this doesn't actually store any data, the
CHashTable
only knows which node ID a given data key belongs to.
The consistent hash table uses a binary search tree bst
in the background, and this is also used to identify the node to which data keys should be alloted to.
Creates a new hash table.
nodeList
: A slice ofcht.NodeId
node identifiers.- Note that
cht.NodeId
is just an alias for a string, an example node ID would becht.NodeId("192.168.0.1")
orcht.NodeId("6969")
.
- Note that
- This returns a pointer to a
CHashTable
instance.
Returns the cht.NodeId
node identifier for a given key
string.
Returns replicaCount
node identifiers responsible for a given key
string.
- This includes the main node and (
replicaCount - 1
) successors of that node.
Adds a node ID to the table. Returns an error if the given nodeId
is already in the table.
Remove nodeId
from the table.
import "cht"
func main() {
// A nodelist for processes running on the following ports: 6969, 8080, 80 and 443.
nodeList := []cht.NodeId{
"6969",
"8080",
"80",
"443",
}
// Initialise the table
table := cht.NewCHashTable(nodeList)
// Data tuple (key, value)
key := "02.069"
value := "Duck-flavoured Potatoes and their Repercussions on World War XI"
// Identify which node is responsible for this data tuple
nodeId := table.GetNode(key)
// Identify which nodes are responsible for this data tuple
nodeIds := table.GetNodes(key, 2) // This returns TWO node IDs -- main node and 1 replica node
}
- Note that the actual node ID returned could vary, based on the
defaultSeed
value set incht.go
.