influxdb/log_entry.go

107 lines
2.2 KiB
Go
Raw Normal View History

2013-04-14 21:37:33 +00:00
package raft
import (
"bytes"
2013-07-18 16:26:31 +00:00
"code.google.com/p/goprotobuf/proto"
2013-04-28 21:23:21 +00:00
"encoding/json"
2013-07-18 16:26:31 +00:00
"fmt"
2013-07-18 16:41:33 +00:00
"github.com/benbjohnson/go-raft/protobuf"
2013-04-14 21:37:33 +00:00
"io"
)
// A log entry stores a single item in the log.
type LogEntry struct {
2013-07-18 16:26:31 +00:00
log *Log
Index uint64
Term uint64
CommandName string
2013-07-18 16:26:31 +00:00
Command []byte
Position int64 // position in the log file
2013-07-18 16:26:31 +00:00
commit chan bool
2013-05-10 03:50:57 +00:00
}
2013-04-14 21:37:33 +00:00
// Creates a new log entry associated with a log.
func newLogEntry(log *Log, index uint64, term uint64, command Command) (*LogEntry, error) {
var buf bytes.Buffer
var commandName string
if command != nil {
commandName = command.CommandName()
if encoder, ok := command.(CommandEncoder); ok {
if err := encoder.Encode(&buf); err != nil {
return nil, err
}
} else {
json.NewEncoder(&buf).Encode(command)
}
}
e := &LogEntry{
2013-07-18 16:26:31 +00:00
log: log,
Index: index,
Term: term,
CommandName: commandName,
2013-07-18 16:26:31 +00:00
Command: buf.Bytes(),
commit: make(chan bool, 5),
2013-04-14 21:37:33 +00:00
}
return e, nil
2013-04-14 21:37:33 +00:00
}
// Encodes the log entry to a buffer. Returns the number of bytes
// written and any error that may have occurred.
func (e *LogEntry) encode(w io.Writer) (int, error) {
2013-08-02 00:58:03 +00:00
defer e.log.pBuffer.Reset()
2013-07-18 16:26:31 +00:00
2013-08-02 00:58:03 +00:00
e.log.pLogEntry.Index = proto.Uint64(e.Index)
e.log.pLogEntry.Term = proto.Uint64(e.Term)
e.log.pLogEntry.CommandName = proto.String(e.CommandName)
e.log.pLogEntry.Command = e.Command
2013-07-18 16:26:31 +00:00
2013-08-02 00:58:03 +00:00
err := e.log.pBuffer.Marshal(e.log.pLogEntry)
2013-07-18 16:26:31 +00:00
if err != nil {
return -1, err
}
2013-08-02 00:58:03 +00:00
_, err = fmt.Fprintf(w, "%8x\n", len(e.log.pBuffer.Bytes()))
2013-07-18 16:26:31 +00:00
if err != nil {
return -1, err
}
2013-08-02 00:58:03 +00:00
return w.Write(e.log.pBuffer.Bytes())
}
2013-04-28 21:23:21 +00:00
// Decodes the log entry from a buffer. Returns the number of bytes read and
// any error that occurs.
func (e *LogEntry) decode(r io.Reader) (int, error) {
2013-04-28 21:23:21 +00:00
2013-07-18 16:26:31 +00:00
var length int
_, err := fmt.Fscanf(r, "%8x\n", &length)
2013-07-18 16:26:31 +00:00
if err != nil {
return -1, err
2013-04-16 02:47:59 +00:00
}
2013-07-18 16:26:31 +00:00
data := make([]byte, length)
2013-07-25 00:21:30 +00:00
_, err = r.Read(data)
2013-07-18 16:26:31 +00:00
if err != nil {
return -1, err
}
2013-07-18 16:26:31 +00:00
pb := &protobuf.ProtoLogEntry{}
p := proto.NewBuffer(data)
err = p.Unmarshal(pb)
if err != nil {
return -1, err
}
2013-04-28 21:23:21 +00:00
2013-07-18 16:26:31 +00:00
e.Term = pb.GetTerm()
e.Index = pb.GetIndex()
e.CommandName = pb.GetCommandName()
e.Command = pb.Command
return length, nil
2013-05-10 03:50:57 +00:00
}