Guest User

Untitled

a guest
Jul 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. package persistance
  2.  
  3. import (
  4. "encoding/binary"
  5. "fmt"
  6. "hydra-dht/constants"
  7. pb "hydra-dht/protobuf/node"
  8. "hydra-dht/structures"
  9. "os"
  10.  
  11. "github.com/golang/protobuf/proto"
  12. )
  13.  
  14. var (
  15. logFile *os.File
  16. filePosition int64
  17. )
  18.  
  19.  
  20. /*
  21. ReadLatestObjectFromLog reads and sends the latest / last log Object inside
  22. the log file.
  23.  
  24. Arguments:
  25. None
  26. Returns:
  27. 1. logNode = The Log node object retrieved from the log file. It will be empty
  28. if there is an error
  29. 2. error = Error object. Will be nil if no error.
  30.  
  31. */
  32. func ReadLatestObjectFromLog() (*pb.LogNode, error) {
  33.  
  34. logFile.Seek(filePosition, 0)
  35.  
  36. sizeOfLogObjectBuffer := make([]byte, constants.LOG_OBJECT_BYTE_SIZE)
  37. logFile.Read(sizeOfLogObjectBuffer)
  38. filePosition += constants.LOG_OBJECT_BYTE_SIZE
  39.  
  40. sizeOfLogObject, _ := binary.Uvarint(sizeOfLogObjectBuffer)
  41. filePosition += int64(sizeOfLogObject)
  42.  
  43. logObjectBuffer := make([]byte, sizeOfLogObject)
  44. logFile.Read(logObjectBuffer)
  45.  
  46. logObject := &pb.LogNode{}
  47. err := proto.Unmarshal(logObjectBuffer, logObject)
  48.  
  49. return logObject, err
  50. }
  51.  
  52. /*
  53. addToDHT is a helper function to the LoadDHT function. It adds the value to the
  54. DHT. If required it appends the value to list, else it inserts the value at the
  55. said position.
  56.  
  57. Arguments:
  58. 1. dht - The DHT in which value is added
  59. 2. logObject - The object which contains value to be added, along with dhtIndex
  60. (the bucket in which the value is to be inserted) and bucket list index in
  61. which the value is to inserted.
  62.  
  63. Please check proto Log Object defination to know more about the LogNode variable.
  64. */
  65. func addToDHT(dht *structures.DHT, logObject *pb.LogNode) {
  66. row := logObject.DhtIndex
  67. col := logObject.ListIndex
  68.  
  69. var nodeID structures.NodeID
  70. copy(nodeID[:], logObject.Node.NodeId)
  71.  
  72. n := &structures.Node{
  73. Key: nodeID,
  74. Port: int(logObject.Node.Port),
  75. Domain: logObject.Node.Domain,
  76. }
  77.  
  78. if len(dht.Lists[row]) < int(col+1) {
  79. dht.Lists[row] = append(dht.Lists[row], *n)
  80. fmt.Println(dht.Lists[row])
  81. } else {
  82. dht.Lists[row][col] = *n
  83. }
  84. }
  85.  
  86. /*
  87. LoadDHT loads the DHT by replaying the tra nsaction Log entirely.
  88. It performs all the operations on the DHT that have occurred in the past, hence
  89. getting DHT to the same state as before the crash/shut down
  90.  
  91. Arguments: None
  92. Returns:
  93. 1. DHT - Returns the DHDT loaded from log, empty if error occurs.
  94. 2. error - Non nil value if some error occured in between the program.
  95. */
  96. func LoadDHT() (*structures.DHT, error) {
  97.  
  98. var dht structures.DHT
  99. fi, err := logFile.Stat()
  100. filePosition = 0
  101. for {
  102. if filePosition >= fi.Size() {
  103. break
  104. }
  105. logObject, err := ReadLatestObjectFromLog()
  106. if err != nil {
  107. return &dht, err
  108. }
  109.  
  110. addToDHT(&dht, logObject)
  111. }
  112.  
  113. return &dht, err
  114. }
Add Comment
Please, Sign In to add comment