Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dbnode] Decode avoid checked bytes in segment reader #2190

Merged
merged 20 commits into from
Mar 7, 2020
Merged
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions src/dbnode/x/xio/segment_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import (
)

type segmentReader struct {
segment ts.Segment
si int
pool SegmentReaderPool
segment ts.Segment
lazyHead []byte
lazyTail []byte
si int
pool SegmentReaderPool
}

// NewSegmentReader creates a new segment reader along with a specified segment.
Expand All @@ -48,28 +50,29 @@ func (sr *segmentReader) Read(b []byte) (int, error) {
if len(b) == 0 {
return 0, nil
}
var head, tail []byte
if b := sr.segment.Head; b != nil {
head = b.Bytes()

rallen090 marked this conversation as resolved.
Show resolved Hide resolved
if b := sr.segment.Head; b != nil && sr.lazyHead == nil {
rallen090 marked this conversation as resolved.
Show resolved Hide resolved
sr.lazyHead = b.Bytes()
}
if b := sr.segment.Tail; b != nil {
tail = b.Bytes()
if b := sr.segment.Tail; b != nil && sr.lazyTail == nil {
sr.lazyTail = b.Bytes()
}
nh, nt := len(head), len(tail)

nh, nt := len(sr.lazyHead), len(sr.lazyTail)
if sr.si >= nh+nt {
return 0, io.EOF
}
n := 0
if sr.si < nh {
nRead := copy(b, head[sr.si:])
nRead := copy(b, sr.lazyHead[sr.si:])
sr.si += nRead
n += nRead
if n == len(b) {
return n, nil
}
}
if sr.si < nh+nt {
nRead := copy(b[n:], tail[sr.si-nh:])
nRead := copy(b[n:], sr.lazyTail[sr.si-nh:])
sr.si += nRead
n += nRead
}
Expand All @@ -85,14 +88,25 @@ func (sr *segmentReader) Segment() (ts.Segment, error) {

func (sr *segmentReader) Reset(segment ts.Segment) {
sr.segment = segment
sr.si = 0
sr.resetBuffers()
}

func (sr *segmentReader) Finalize() {
// Finalize the segment
sr.segment.Finalize()
sr.resetBuffers()
rallen090 marked this conversation as resolved.
Show resolved Hide resolved

if pool := sr.pool; pool != nil {
pool.Put(sr)
}
}

func (sr *segmentReader) resetBuffers() {
sr.si = 0
if sr.lazyHead != nil {
rallen090 marked this conversation as resolved.
Show resolved Hide resolved
sr.lazyHead = sr.lazyHead[0:]
}
if sr.lazyTail != nil {
sr.lazyTail = sr.lazyTail[0:]
}
}