Skip to content

Commit

Permalink
Merge pull request #84 from etcd-team/walpb
Browse files Browse the repository at this point in the history
wal: move pb files to walpb
  • Loading branch information
xiang90 committed Sep 4, 2014
2 parents 3ee83bc + b98cf17 commit 16d337d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 27 deletions.
5 changes: 3 additions & 2 deletions wal/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion wal/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"

"github.com/coreos/etcd/crc"
"github.com/coreos/etcd/wal/walpb"
)

type encoder struct {
Expand All @@ -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()
Expand Down
24 changes: 13 additions & 11 deletions wal/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"io/ioutil"
"reflect"
"testing"

"github.com/coreos/etcd/wal/walpb"
)

func TestReadRecord(t *testing.T) {
Expand All @@ -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))
Expand All @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sort"

"github.com/coreos/etcd/raft/raftpb"
"github.com/coreos/etcd/wal/walpb"
)

const (
Expand All @@ -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)
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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})
}
File renamed without changes.
10 changes: 8 additions & 2 deletions wal/record.go → wal/walpb/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions wal/record.pb.go → wal/walpb/record.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wal/record.proto → wal/walpb/record.proto
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package wal;
package walpb;

import "code.google.com/p/gogoprotobuf/gogoproto/gogo.proto";

Expand Down

0 comments on commit 16d337d

Please sign in to comment.