github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/moengine/reader.go (about)

     1  // Copyright 2021 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package moengine
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    22  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    27  )
    28  
    29  var (
    30  	_ engine.Reader = (*txnReader)(nil)
    31  )
    32  
    33  func newReader(rel handle.Relation, it handle.BlockIt) *txnReader {
    34  	return &txnReader{
    35  		handle: rel,
    36  		it:     it,
    37  	}
    38  }
    39  
    40  func (r *txnReader) Read(ctx context.Context, attrs []string, _ *plan.Expr, m *mpool.MPool) (*batch.Batch, error) {
    41  	r.it.Lock()
    42  	if !r.it.Valid() {
    43  		r.it.Unlock()
    44  		return nil, nil
    45  	}
    46  	if r.buffer == nil {
    47  		r.buffer = make([]*bytes.Buffer, len(attrs))
    48  		for i := 0; i < len(attrs); i++ {
    49  			r.buffer[i] = new(bytes.Buffer)
    50  		}
    51  	}
    52  	h := r.it.GetBlock()
    53  	r.it.Next()
    54  	r.it.Unlock()
    55  	block := newBlock(h)
    56  	bat, err := block.Read(attrs, nil, r.buffer)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	n := vector.Length(bat.Vecs[0])
    61  	sels := m.GetSels()
    62  	if n > cap(sels) {
    63  		m.PutSels(sels)
    64  		sels = make([]int64, n)
    65  	}
    66  	bat.Zs = sels[:n]
    67  	for i := 0; i < n; i++ {
    68  		bat.Zs[i] = 1
    69  	}
    70  	return bat, nil
    71  }
    72  
    73  func (r *txnReader) Close() error {
    74  	return nil
    75  }