Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Marchbanks <csmarchbanks@gmail.com>
  • Loading branch information
csmarchbanks committed May 29, 2019
1 parent 40434f8 commit 0d94777
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,7 @@ func TestWalRepair(t *testing.T) {
}{
"invalid_record": {
func(rec []byte) []byte {
// Do not modify the base record because it is Logged multiple times.
res := make([]byte, len(rec))
copy(res, rec)
res[0] = byte(RecordInvalid)
Expand Down
5 changes: 4 additions & 1 deletion wal/live_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (r *LiveReader) buildRecord() (bool, error) {
r.snappyBuf = r.snappyBuf[:0]
}

compressed := r.hdr[0]&8 == 8
compressed := r.hdr[0]&snappyMask != 0
if compressed {
r.snappyBuf = append(r.snappyBuf, temp...)
} else {
Expand All @@ -188,6 +188,9 @@ func (r *LiveReader) buildRecord() (bool, error) {
if rt == recLast || rt == recFull {
r.index = 0
if compressed && len(r.snappyBuf) > 0 {
// The snappy library uses `len` to calculate if we need a new buffer.
// In order to allocate as few buffers as possible make the length
// equal to the capacity.
r.rec = r.rec[:cap(r.rec)]
r.rec, err = snappy.Decode(r.rec, r.snappyBuf)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion wal/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (r *Reader) next() (err error) {
}
r.total++
r.curRecTyp = recTypeFromHeader(hdr[0])
compressed := hdr[0]&8 == 8
compressed := hdr[0]&snappyMask != 0

// Gobble up zero bytes.
if r.curRecTyp == recPageTerm {
Expand Down Expand Up @@ -139,6 +139,9 @@ func (r *Reader) next() (err error) {
}
if r.curRecTyp == recLast || r.curRecTyp == recFull {
if compressed && len(r.snappyBuf) > 0 {
// The snappy library uses `len` to calculate if we need a new buffer.
// In order to allocate as few buffers as possible make the length
// equal to the capacity.
r.rec = r.rec[:cap(r.rec)]
r.rec, err = snappy.Decode(r.rec, r.snappyBuf)
return err
Expand Down
14 changes: 12 additions & 2 deletions wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,14 @@ func (w *WAL) flushPage(clear bool) error {
return nil
}

// First Byte of header format:
// [ 4 bits unallocated] [1 bit snappy compression flag] [ 3 bit record type ]

const (
recTypeMask = 7
snappyMask = 8
)

type recType uint8

const (
Expand All @@ -488,7 +496,7 @@ const (
)

func recTypeFromHeader(header byte) recType {
return recType(header & 7)
return recType(header & recTypeMask)
}

func (t recType) String() string {
Expand Down Expand Up @@ -553,7 +561,9 @@ func (w *WAL) log(rec []byte, final bool) error {

compressed := false
if w.compress && len(rec) > 0 {
// Allow Snappy to use the full capacity of the buffer.
// The snappy library uses `len` to calculate if we need a new buffer.
// In order to allocate as few buffers as possible make the length
// equal to the capacity.
w.snappyBuf = w.snappyBuf[:cap(w.snappyBuf)]
w.snappyBuf = snappy.Encode(w.snappyBuf, rec)
if len(w.snappyBuf) < len(rec) {
Expand Down

0 comments on commit 0d94777

Please sign in to comment.