Merge pull request #84 from etcd-team/walpb

wal: move pb files to walpb
This commit is contained in:
Xiang Li 2014-09-03 17:03:16 -07:00
commit 16d337db70
8 changed files with 38 additions and 27 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/coreos/etcd/crc"
"github.com/coreos/etcd/raft/raftpb"
"github.com/coreos/etcd/wal/walpb"
)
type decoder struct {
@ -24,7 +25,7 @@ func newDecoder(rc io.ReadCloser) *decoder {
}
}
func (d *decoder) decode(rec *Record) error {
func (d *decoder) decode(rec *walpb.Record) error {
rec.Reset()
l, err := readInt64(d.br)
if err != nil {
@ -42,7 +43,7 @@ func (d *decoder) decode(rec *Record) error {
return nil
}
d.crc.Write(rec.Data)
return rec.validate(d.crc.Sum32())
return rec.Validate(d.crc.Sum32())
}
func (d *decoder) updateCRC(prevCrc uint32) {

View File

@ -7,6 +7,7 @@ import (
"io"
"github.com/coreos/etcd/crc"
"github.com/coreos/etcd/wal/walpb"
)
type encoder struct {
@ -21,7 +22,7 @@ func newEncoder(w io.Writer, prevCrc uint32) *encoder {
}
}
func (e *encoder) encode(rec *Record) error {
func (e *encoder) encode(rec *walpb.Record) error {
e.crc.Write(rec.Data)
rec.Crc = e.crc.Sum32()
data, err := rec.Marshal()

View File

@ -23,6 +23,8 @@ import (
"io/ioutil"
"reflect"
"testing"
"github.com/coreos/etcd/wal/walpb"
)
func TestReadRecord(t *testing.T) {
@ -32,18 +34,18 @@ func TestReadRecord(t *testing.T) {
tests := []struct {
data []byte
wr *Record
wr *walpb.Record
we error
}{
{infoRecord, &Record{Type: 1, Crc: crc32.Checksum(infoData, crcTable), Data: infoData}, nil},
{[]byte(""), &Record{}, io.EOF},
{infoRecord[:len(infoRecord)-len(infoData)-8], &Record{}, io.ErrUnexpectedEOF},
{infoRecord[:len(infoRecord)-len(infoData)], &Record{}, io.ErrUnexpectedEOF},
{infoRecord[:len(infoRecord)-8], &Record{}, io.ErrUnexpectedEOF},
{badInfoRecord, &Record{}, ErrCRCMismatch},
{infoRecord, &walpb.Record{Type: 1, Crc: crc32.Checksum(infoData, crcTable), Data: infoData}, nil},
{[]byte(""), &walpb.Record{}, io.EOF},
{infoRecord[:len(infoRecord)-len(infoData)-8], &walpb.Record{}, io.ErrUnexpectedEOF},
{infoRecord[:len(infoRecord)-len(infoData)], &walpb.Record{}, io.ErrUnexpectedEOF},
{infoRecord[:len(infoRecord)-8], &walpb.Record{}, io.ErrUnexpectedEOF},
{badInfoRecord, &walpb.Record{}, walpb.ErrCRCMismatch},
}
rec := &Record{}
rec := &walpb.Record{}
for i, tt := range tests {
buf := bytes.NewBuffer(tt.data)
decoder := newDecoder(ioutil.NopCloser(buf))
@ -54,17 +56,17 @@ func TestReadRecord(t *testing.T) {
if !reflect.DeepEqual(e, tt.we) {
t.Errorf("#%d: err = %v, want %v", i, e, tt.we)
}
rec = &Record{}
rec = &walpb.Record{}
}
}
func TestWriteRecord(t *testing.T) {
b := &Record{}
b := &walpb.Record{}
typ := int64(0xABCD)
d := []byte("Hello world!")
buf := new(bytes.Buffer)
e := newEncoder(buf, 0)
e.encode(&Record{Type: typ, Data: d})
e.encode(&walpb.Record{Type: typ, Data: d})
e.flush()
decoder := newDecoder(ioutil.NopCloser(buf))
err := decoder.decode(b)

View File

@ -27,6 +27,7 @@ import (
"sort"
"github.com/coreos/etcd/raft/raftpb"
"github.com/coreos/etcd/wal/walpb"
)
const (
@ -37,8 +38,8 @@ const (
)
var (
ErrIdMismatch = fmt.Errorf("wal: unmatch id")
ErrNotFound = fmt.Errorf("wal: file is not found")
ErrIdMismatch = errors.New("wal: unmatch id")
ErrNotFound = errors.New("wal: file is not found")
ErrCRCMismatch = errors.New("wal: crc mismatch")
crcTable = crc32.MakeTable(crc32.Castagnoli)
)
@ -140,7 +141,7 @@ func (w *WAL) ReadAll() (int64, raftpb.State, []raftpb.Entry, error) {
var state raftpb.State
var entries []raftpb.Entry
rec := &Record{}
rec := &walpb.Record{}
decoder := w.decoder
var err error
for err = decoder.decode(rec); err == nil; err = decoder.decode(rec) {
@ -163,7 +164,7 @@ func (w *WAL) ReadAll() (int64, raftpb.State, []raftpb.Entry, error) {
crc := decoder.crc.Sum32()
// current crc of decoder must match the crc of the record.
// do no need to match 0 crc, since the decoder is a new one at this case.
if crc != 0 && rec.validate(crc) != nil {
if crc != 0 && rec.Validate(crc) != nil {
state.Reset()
return 0, state, nil, ErrCRCMismatch
}
@ -234,7 +235,7 @@ func (w *WAL) SaveInfo(i *raftpb.Info) error {
if err != nil {
panic(err)
}
rec := &Record{Type: infoType, Data: b}
rec := &walpb.Record{Type: infoType, Data: b}
return w.encoder.encode(rec)
}
@ -243,7 +244,7 @@ func (w *WAL) SaveEntry(e *raftpb.Entry) error {
if err != nil {
panic(err)
}
rec := &Record{Type: entryType, Data: b}
rec := &walpb.Record{Type: entryType, Data: b}
return w.encoder.encode(rec)
}
@ -253,10 +254,10 @@ func (w *WAL) SaveState(s *raftpb.State) error {
if err != nil {
panic(err)
}
rec := &Record{Type: stateType, Data: b}
rec := &walpb.Record{Type: stateType, Data: b}
return w.encoder.encode(rec)
}
func (w *WAL) saveCrc(prevCrc uint32) error {
return w.encoder.encode(&Record{Type: crcType, Crc: prevCrc})
return w.encoder.encode(&walpb.Record{Type: crcType, Crc: prevCrc})
}

View File

@ -14,9 +14,15 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package wal
package walpb
func (rec *Record) validate(crc uint32) error {
import "errors"
var (
ErrCRCMismatch = errors.New("walpb: crc mismatch")
)
func (rec *Record) Validate(crc uint32) error {
if rec.Crc == crc {
return nil
}

View File

@ -3,7 +3,7 @@
// DO NOT EDIT!
/*
Package wal is a generated protocol buffer package.
Package walpb is a generated protocol buffer package.
It is generated from these files:
record.proto
@ -11,7 +11,7 @@
It has these top-level messages:
Record
*/
package wal
package walpb
import proto "github.com/coreos/etcd/third_party/code.google.com/p/gogoprotobuf/proto"
import json "encoding/json"

View File

@ -1,4 +1,4 @@
package wal;
package walpb;
import "code.google.com/p/gogoprotobuf/gogoproto/gogo.proto";