Guest User

Untitled

a guest
Jun 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. //AssignNodes is used for assigning nodes into different specific shards
  2. func AssignNodes(nodes []Address, shardSize int, randoms []int) (int, [][]Address) {
  3. numNodes := len(nodes) //the number of participating nodes
  4. numShard := numNodes / shardSize //the number of shards
  5. copies := []Address{} //the copies of nodes awaiting assignment
  6. copies = append(copies, nodes...) //copy the participting nodes
  7. shards := AllocateShards(shardSize, numShard) //create a dynamic slice storing different shards
  8. for i := 0; i < numNodes; i++ { //loop over the random numbers
  9. shardIndex := i / shardSize //the index of the shard for assignment
  10. nodeIndex := i % shardSize //index of the node within the assigned shard
  11. currentNumNodes := len(copies) //the current number of not yet assigned nodes
  12. selectedIndex := randoms[i] % currentNumNodes //select a node index from the not yet assigned nodes
  13. selectedNode := copies[selectedIndex] //the address of the node to be assigned
  14. shards[shardIndex][nodeIndex] = selectedNode //assign to the specified shard
  15. copies = append(copies[:i], copies[i+1:]...) //update the nodes waiting for assignment
  16. }
  17. return numShard, shards
  18. }
Add Comment
Please, Sign In to add comment