This repository was archived by the owner on Aug 13, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +21
-4
lines changed Expand file tree Collapse file tree 4 files changed +21
-4
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
480488type recType uint8
481489
482490const (
@@ -488,7 +496,7 @@ const (
488496)
489497
490498func recTypeFromHeader (header byte ) recType {
491- return recType (header & 7 )
499+ return recType (header & recTypeMask )
492500}
493501
494502func (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 ) {
You can’t perform that action at this time.
0 commit comments