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

Commit 0d94777

Browse files
committed
Review feedback
Signed-off-by: Chris Marchbanks <[email protected]>
1 parent 40434f8 commit 0d94777

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

head_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,7 @@ func TestWalRepair(t *testing.T) {
10241024
}{
10251025
"invalid_record": {
10261026
func(rec []byte) []byte {
1027+
// Do not modify the base record because it is Logged multiple times.
10271028
res := make([]byte, len(rec))
10281029
copy(res, rec)
10291030
res[0] = byte(RecordInvalid)

wal/live_reader.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (r *LiveReader) buildRecord() (bool, error) {
174174
r.snappyBuf = r.snappyBuf[:0]
175175
}
176176

177-
compressed := r.hdr[0]&8 == 8
177+
compressed := r.hdr[0]&snappyMask != 0
178178
if compressed {
179179
r.snappyBuf = append(r.snappyBuf, temp...)
180180
} else {
@@ -188,6 +188,9 @@ func (r *LiveReader) buildRecord() (bool, error) {
188188
if rt == recLast || rt == recFull {
189189
r.index = 0
190190
if compressed && len(r.snappyBuf) > 0 {
191+
// The snappy library uses `len` to calculate if we need a new buffer.
192+
// In order to allocate as few buffers as possible make the length
193+
// equal to the capacity.
191194
r.rec = r.rec[:cap(r.rec)]
192195
r.rec, err = snappy.Decode(r.rec, r.snappyBuf)
193196
if err != nil {

wal/reader.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (r *Reader) next() (err error) {
7272
}
7373
r.total++
7474
r.curRecTyp = recTypeFromHeader(hdr[0])
75-
compressed := hdr[0]&8 == 8
75+
compressed := hdr[0]&snappyMask != 0
7676

7777
// Gobble up zero bytes.
7878
if r.curRecTyp == recPageTerm {
@@ -139,6 +139,9 @@ func (r *Reader) next() (err error) {
139139
}
140140
if r.curRecTyp == recLast || r.curRecTyp == recFull {
141141
if compressed && len(r.snappyBuf) > 0 {
142+
// The snappy library uses `len` to calculate if we need a new buffer.
143+
// In order to allocate as few buffers as possible make the length
144+
// equal to the capacity.
142145
r.rec = r.rec[:cap(r.rec)]
143146
r.rec, err = snappy.Decode(r.rec, r.snappyBuf)
144147
return err

wal/wal.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,14 @@ func (w *WAL) flushPage(clear bool) error {
477477
return nil
478478
}
479479

480+
// First Byte of header format:
481+
// [ 4 bits unallocated] [1 bit snappy compression flag] [ 3 bit record type ]
482+
483+
const (
484+
recTypeMask = 7
485+
snappyMask = 8
486+
)
487+
480488
type recType uint8
481489

482490
const (
@@ -488,7 +496,7 @@ const (
488496
)
489497

490498
func recTypeFromHeader(header byte) recType {
491-
return recType(header & 7)
499+
return recType(header & recTypeMask)
492500
}
493501

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

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

0 commit comments

Comments
 (0)