Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AssignNodes is used for assigning nodes into different specific shards
- func AssignNodes(nodes []Address, shardSize int, randoms []int) (int, [][]Address) {
- numNodes := len(nodes) //the number of participating nodes
- numShard := numNodes / shardSize //the number of shards
- copies := []Address{} //the copies of nodes awaiting assignment
- copies = append(copies, nodes...) //copy the participting nodes
- shards := AllocateShards(shardSize, numShard) //create a dynamic slice storing different shards
- for i := 0; i < numNodes; i++ { //loop over the random numbers
- shardIndex := i / shardSize //the index of the shard for assignment
- nodeIndex := i % shardSize //index of the node within the assigned shard
- currentNumNodes := len(copies) //the current number of not yet assigned nodes
- selectedIndex := randoms[i] % currentNumNodes //select a node index from the not yet assigned nodes
- selectedNode := copies[selectedIndex] //the address of the node to be assigned
- shards[shardIndex][nodeIndex] = selectedNode //assign to the specified shard
- copies = append(copies[:i], copies[i+1:]...) //update the nodes waiting for assignment
- }
- return numShard, shards
- }
Add Comment
Please, Sign In to add comment