Skip to content

Commit

Permalink
fix comments. make code more concise.
Browse files Browse the repository at this point in the history
Signed-off-by: guo-shaoge <shaoge1994@163.com>
  • Loading branch information
guo-shaoge committed May 17, 2021
1 parent a8e9931 commit 405eded
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions util/chunk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,27 +406,17 @@ func (r *ReaderWithCache) ReadAt(p []byte, off int64) (readCnt int, err error) {

// When got here, user input is not filled fully, so we need read data from cache.
err = nil
if readCnt == 0 {
// 'readCnt == 0' means all user requested data resides in r.cache.
beg := off - r.cacheOff
if beg < 0 {
return readCnt, errors2.Trace(errors2.New("off must be greater than r.cacheOff when readCnt is 0"))
}
end := int(beg) + len(p)
if end > len(r.cache) {
err = io.EOF
end = len(r.cache)
}
readCnt = copy(p, r.cache[beg:end])
} else {
// readCnt != 0 means only partial data of user requested resides in r.cache.
p = p[readCnt:]
end := len(p)
if end > len(r.cache) {
err = io.EOF
end = len(r.cache)
}
readCnt += copy(p, r.cache[:end])
p = p[readCnt:]
beg := off - r.cacheOff
if beg < 0 {
// This happens when only partial data of user requested resides in r.cache.
beg = 0
}
end := int(beg) + len(p)
if end > len(r.cache) {
err = io.EOF
end = len(r.cache)
}
readCnt += copy(p, r.cache[beg:end])
return readCnt, err
}

0 comments on commit 405eded

Please sign in to comment.