Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package persistance
- import (
- "encoding/binary"
- "fmt"
- "hydra-dht/constants"
- pb "hydra-dht/protobuf/node"
- "hydra-dht/structures"
- "os"
- "github.com/golang/protobuf/proto"
- )
- var (
- logFile *os.File
- filePosition int64
- )
- /*
- convertNumberToBytes is a helper function to AppendToLog. It converts a uint64 number
- to a byte array . The size of byte array is 10. This is required to keep track of size of
- logObject in the log as that object can be of any size.
- Hence , we first store the size of log Object in bytes, and then store the byte representation
- of the Log Object.
- Arguments:
- 1. i = The uint64 number , the size of logObject
- Returns
- 1. byte[] = The byte array representation of i
- 2. int = The actual number of bytes , the number i took. The max number of bytes it can take is 10
- */
- func convertNumberToBytes(i uint64) ([]byte, int) {
- buf := make([]byte, binary.MaxVarintLen64)
- binary.PutUvarint(buf, i)
- return buf, constants.LOG_OBJECT_BYTE_SIZE
- }
- /*
- AppendToLog appends a DHT entry into the persistent transaction log.
- The function first adds the number of bytes the LogObject is of, then inserts
- the LogObject into the log file.
- This method helps when we're reading the contents of the log back into memory.
- As the Log Object does not have a fixed size.
- Arguments:
- 1. node : Node object inside the DHT
- 2. dhtIndex: Bucket Index of DHT table into which to put the node into
- 3. listIndex: The index in the Bucket List of DHT in which the Node is added to.
- Returns:
- 1. error: Returns error , if no error then error is nil
- */
- func AppendToLog(node structures.Node, dhtIndex int32, listIndex int32) error {
- // the data structure that contains information on our write query
- // we want to store this data structure into our log
- logObject := &pb.LogNode{
- Node: &pb.Node{
- NodeId: node.Key[:],
- Domain: node.Domain,
- Port: int32(node.Port),
- },
- DhtIndex: dhtIndex,
- ListIndex: listIndex,
- }
- // converts out log Object into bytes
- // as data in a file is stored in bytes.
- // we are serialising/ marshalling our object
- out, err := proto.Marshal(logObject)
- if err != nil {
- return err
- }
- // the number of bytes consisting of logObject
- i := uint64(len(out))
- // As the size of byte object can be indeterminate, we store the size of log Object
- // in bytes before appending our logObject. hence while reading,
- // we find out how many bytes to read to get our entire log Object.
- // the size of log Object is aof fixed size. That size is 10 bytes.
- // 10 bytes is the size of an uint64 integer.
- buf, _ := convertNumberToBytes(i)
- // Appending the size and logObject and writing to file
- ob := append(buf, out...)
- n2, err := logFile.Write(ob)
- if err != nil {
- return err
- }
- fmt.Printf("Wrote %d bytes for LogObject\n", n2)
- // Flushing that write to disk.
- err = logFile.Sync()
- return err
- }
Add Comment
Please, Sign In to add comment