Guest User

Untitled

a guest
Jul 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 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. convertNumberToBytes is a helper function to AppendToLog. It converts a uint64 number
  21. to a byte array . The size of byte array is 10. This is required to keep track of size of
  22. logObject in the log as that object can be of any size.
  23.  
  24. Hence , we first store the size of log Object in bytes, and then store the byte representation
  25. of the Log Object.
  26.  
  27. Arguments:
  28. 1. i = The uint64 number , the size of logObject
  29. Returns
  30. 1. byte[] = The byte array representation of i
  31. 2. int = The actual number of bytes , the number i took. The max number of bytes it can take is 10
  32. */
  33. func convertNumberToBytes(i uint64) ([]byte, int) {
  34. buf := make([]byte, binary.MaxVarintLen64)
  35. binary.PutUvarint(buf, i)
  36.  
  37. return buf, constants.LOG_OBJECT_BYTE_SIZE
  38. }
  39.  
  40. /*
  41. AppendToLog appends a DHT entry into the persistent transaction log.
  42.  
  43. The function first adds the number of bytes the LogObject is of, then inserts
  44. the LogObject into the log file.
  45.  
  46. This method helps when we're reading the contents of the log back into memory.
  47. As the Log Object does not have a fixed size.
  48.  
  49. Arguments:
  50. 1. node : Node object inside the DHT
  51. 2. dhtIndex: Bucket Index of DHT table into which to put the node into
  52. 3. listIndex: The index in the Bucket List of DHT in which the Node is added to.
  53.  
  54. Returns:
  55. 1. error: Returns error , if no error then error is nil
  56.  
  57. */
  58. func AppendToLog(node structures.Node, dhtIndex int32, listIndex int32) error {
  59. // the data structure that contains information on our write query
  60. // we want to store this data structure into our log
  61. logObject := &pb.LogNode{
  62. Node: &pb.Node{
  63. NodeId: node.Key[:],
  64. Domain: node.Domain,
  65. Port: int32(node.Port),
  66. },
  67. DhtIndex: dhtIndex,
  68. ListIndex: listIndex,
  69. }
  70. // converts out log Object into bytes
  71. // as data in a file is stored in bytes.
  72. // we are serialising/ marshalling our object
  73. out, err := proto.Marshal(logObject)
  74.  
  75. if err != nil {
  76. return err
  77. }
  78.  
  79. // the number of bytes consisting of logObject
  80. i := uint64(len(out))
  81.  
  82.  
  83. // As the size of byte object can be indeterminate, we store the size of log Object
  84. // in bytes before appending our logObject. hence while reading,
  85. // we find out how many bytes to read to get our entire log Object.
  86. // the size of log Object is aof fixed size. That size is 10 bytes.
  87. // 10 bytes is the size of an uint64 integer.
  88. buf, _ := convertNumberToBytes(i)
  89.  
  90. // Appending the size and logObject and writing to file
  91. ob := append(buf, out...)
  92. n2, err := logFile.Write(ob)
  93. if err != nil {
  94. return err
  95. }
  96. fmt.Printf("Wrote %d bytes for LogObject\n", n2)
  97. // Flushing that write to disk.
  98. err = logFile.Sync()
  99.  
  100. return err
  101. }
Add Comment
Please, Sign In to add comment