hanahmily commented on code in PR #745: URL: https://github.com/apache/skywalking-banyandb/pull/745#discussion_r2312923230
########## banyand/internal/sidx/block_reader.go: ########## @@ -0,0 +1,235 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package sidx + +import ( + "container/heap" + "errors" + "fmt" + "io" + + "github.com/apache/skywalking-banyandb/pkg/encoding" + "github.com/apache/skywalking-banyandb/pkg/fs" + "github.com/apache/skywalking-banyandb/pkg/logger" + "github.com/apache/skywalking-banyandb/pkg/pool" +) + +type seqReader struct { + sr fs.SeqReader + r fs.Reader + bytesRead uint64 +} + +func (sr *seqReader) reset() { + sr.r = nil + if sr.sr != nil { + fs.MustClose(sr.sr) + } + sr.sr = nil + sr.bytesRead = 0 +} + +func (sr *seqReader) Path() string { + return sr.r.Path() +} + +func (sr *seqReader) init(r fs.Reader) { + sr.reset() + sr.sr = r.SequentialRead() + sr.r = r +} + +func (sr *seqReader) mustReadFull(data []byte) { + n, err := io.ReadFull(sr.sr, data) + if err != nil { + if errors.Is(err, io.EOF) { + return + } + logger.Panicf("cannot read data: %v", err) + } + if n != len(data) { + logger.Panicf("cannot read full data: %d/%d", n, len(data)) + } + sr.bytesRead += uint64(n) +} + +func generateSeqReader() *seqReader { + if v := seqReaderPool.Get(); v != nil { + return v + } + return &seqReader{} +} + +func releaseSeqReader(sr *seqReader) { + sr.reset() + seqReaderPool.Put(sr) +} + +var seqReaderPool = pool.Register[*seqReader]("sidx-seqReader") + +type seqReaders struct { + tagMetadata map[string]*seqReader + tagData map[string]*seqReader + tagFilters map[string]*seqReader + primary seqReader + keys seqReader + data seqReader +} + +func (sr *seqReaders) reset() { + sr.primary.reset() + sr.keys.reset() + sr.data.reset() + if sr.tagMetadata != nil { + for k, r := range sr.tagMetadata { + releaseSeqReader(r) + delete(sr.tagMetadata, k) + releaseSeqReader(r) + delete(sr.tagData, k) + releaseSeqReader(r) + delete(sr.tagFilters, k) Review Comment: This recommendation should be followed because it is more reliable. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@skywalking.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org