influxdb/log_entry.go

135 lines
3.4 KiB
Go
Raw Normal View History

2013-04-14 21:37:33 +00:00
package raft
import (
"bufio"
"bytes"
2013-04-16 02:47:59 +00:00
"errors"
2013-04-14 21:37:33 +00:00
"hash/crc32"
"fmt"
"io"
"encoding/json"
)
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A log entry stores a single item in the log.
type LogEntry struct {
log *Log
index uint64
2013-04-16 02:47:59 +00:00
term uint64
2013-04-14 21:37:33 +00:00
command Command
}
//------------------------------------------------------------------------------
//
// Constructor
//
//------------------------------------------------------------------------------
// Creates a new log entry associated with a log.
2013-04-16 02:47:59 +00:00
func NewLogEntry(log *Log, index uint64, term uint64, command Command) *LogEntry {
2013-04-14 21:37:33 +00:00
return &LogEntry{
log: log,
index: index,
2013-04-16 02:47:59 +00:00
term: term,
2013-04-14 21:37:33 +00:00
command: command,
}
}
//------------------------------------------------------------------------------
//
// Methods
//
//------------------------------------------------------------------------------
//--------------------------------------
// Encoding
//--------------------------------------
// Encodes the log entry to a buffer.
func (e *LogEntry) Encode(w io.Writer) error {
2013-04-16 02:47:59 +00:00
if w == nil {
return errors.New("raft.LogEntry: Writer required to encode")
}
2013-04-14 21:37:33 +00:00
encodedCommand, err := json.Marshal(e.command)
if err != nil {
return err
}
// Write log line to temporary buffer.
var b bytes.Buffer
2013-04-16 02:47:59 +00:00
if _, err = fmt.Fprintf(&b, "%016x %016x %s %s\n", e.index, e.term, e.command.Name(), encodedCommand); err != nil {
2013-04-14 21:37:33 +00:00
return err
}
// Generate checksum.
checksum := crc32.ChecksumIEEE(b.Bytes())
// Write log entry with checksum.
2013-04-16 02:47:59 +00:00
_, err = fmt.Fprintf(w, "%08x %s", checksum, b.String())
2013-04-14 21:37:33 +00:00
return err
}
// Decodes the log entry from a buffer.
func (e *LogEntry) Decode(r io.Reader) error {
2013-04-16 02:47:59 +00:00
if r == nil {
return errors.New("raft.LogEntry: Reader required to decode")
}
2013-04-14 21:37:33 +00:00
// Read the expected checksum first.
var checksum uint32
2013-04-16 02:47:59 +00:00
if _, err := fmt.Fscanf(r, "%08x", &checksum); err != nil {
return fmt.Errorf("raft.LogEntry: Unable to read checksum: %v", err)
}
2013-04-14 21:37:33 +00:00
// Read the rest of the line.
2013-04-16 02:47:59 +00:00
bufr := bufio.NewReader(r)
if c, _ := bufr.ReadByte(); c != ' ' {
return fmt.Errorf("raft.LogEntry: Expected space, received %02x", c)
}
line, err := bufr.ReadString('\n')
2013-04-14 21:37:33 +00:00
if err == io.EOF {
return fmt.Errorf("raft.LogEntry: Unexpected EOF")
} else if err != nil {
2013-04-16 02:47:59 +00:00
return fmt.Errorf("raft.LogEntry: Unable to read line: %v", err)
2013-04-14 21:37:33 +00:00
}
b := bytes.NewBufferString(line)
// Verify checksum.
bchecksum := crc32.ChecksumIEEE(b.Bytes())
if checksum != bchecksum {
2013-04-16 02:47:59 +00:00
return fmt.Errorf("raft.LogEntry: Invalid checksum: Expected %08x, calculated %08x", checksum, bchecksum)
2013-04-14 21:37:33 +00:00
}
// Read term, index and command name.
var commandName string
2013-04-16 02:47:59 +00:00
if _, err := fmt.Fscanf(b, "%016x %016x %s ", &e.index, &e.term, &commandName); err != nil {
return fmt.Errorf("raft.LogEntry: Unable to scan: %v", err)
2013-04-14 21:37:33 +00:00
}
// Instantiate command by name.
command, err := e.log.NewCommand(commandName)
if err != nil {
2013-04-16 02:47:59 +00:00
return fmt.Errorf("raft.LogEntry: Unable to instantiate command (%s): %v", commandName, err)
2013-04-14 21:37:33 +00:00
}
// Deserialize command.
if err = json.NewDecoder(b).Decode(&command); err != nil {
2013-04-16 02:47:59 +00:00
return fmt.Errorf("raft.LogEntry: Unable to decode: %v", err)
2013-04-14 21:37:33 +00:00
}
2013-04-16 02:47:59 +00:00
e.command = command
// Make sure there's only an EOF remaining.
2013-04-14 21:37:33 +00:00
if c, err := b.ReadByte(); err != io.EOF {
return fmt.Errorf("raft.LogEntry: Expected EOL, received %02x", c)
}
return nil
}