swh:1:snp:8a1bf80ec89c62a71cdcaaf0c2f9145695a5340a
Tip revision: d01d6119e54f729f54e9776ad5729277fcf38668 authored by Xiang Li on 26 November 2014, 07:32:01 UTC
Merge pull request #1792 from xiang90/fix_raft_l
Merge pull request #1792 from xiang90/fix_raft_l
Tip revision: d01d611
encoder.go
/*
Copyright 2014 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wal
import (
"bufio"
"encoding/binary"
"hash"
"io"
"github.com/coreos/etcd/pkg/crc"
"github.com/coreos/etcd/wal/walpb"
)
type encoder struct {
bw *bufio.Writer
crc hash.Hash32
}
func newEncoder(w io.Writer, prevCrc uint32) *encoder {
return &encoder{
bw: bufio.NewWriter(w),
crc: crc.New(prevCrc, crcTable),
}
}
func (e *encoder) encode(rec *walpb.Record) error {
e.crc.Write(rec.Data)
rec.Crc = e.crc.Sum32()
data, err := rec.Marshal()
if err != nil {
return err
}
if err := writeInt64(e.bw, int64(len(data))); err != nil {
return err
}
_, err = e.bw.Write(data)
return err
}
func (e *encoder) flush() error {
return e.bw.Flush()
}
func (e *encoder) buffered() int {
return e.bw.Buffered()
}
func writeInt64(w io.Writer, n int64) error {
return binary.Write(w, binary.LittleEndian, n)
}