github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/memorystorage/data.go (about)

     1  // Copyright 2022 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 memorystorage
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  )
    23  
    24  type NamedRow interface {
    25  	AttrByName(handler *MemHandler, tx *Transaction, name string) (Nullable, error)
    26  }
    27  
    28  type DataKey struct {
    29  	tableID    ID
    30  	primaryKey Tuple
    31  }
    32  
    33  func (d DataKey) Less(than DataKey) bool {
    34  	if d.tableID.Less(than.tableID) {
    35  		return true
    36  	}
    37  	if than.tableID.Less(d.tableID) {
    38  		return false
    39  	}
    40  	return d.primaryKey.Less(than.primaryKey)
    41  }
    42  
    43  // use AttributeRow.Order as index
    44  type DataValue = []Nullable
    45  
    46  type DataRow struct {
    47  	key           DataKey
    48  	value         DataValue
    49  	indexes       []Tuple
    50  	uniqueIndexes []Tuple
    51  }
    52  
    53  func (a DataRow) Key() DataKey {
    54  	return a.key
    55  }
    56  
    57  func (a DataRow) Value() DataValue {
    58  	return a.value
    59  }
    60  
    61  func (a DataRow) Indexes() []Tuple {
    62  	return a.indexes
    63  }
    64  
    65  func (a DataRow) UniqueIndexes() []Tuple {
    66  	return a.uniqueIndexes
    67  }
    68  
    69  func (a *DataRow) String() string {
    70  	buf := new(strings.Builder)
    71  	buf.WriteString("DataRow{")
    72  	fmt.Fprintf(buf, "key: %+v", a.key)
    73  	for _, attr := range a.value {
    74  		fmt.Fprintf(buf, ", %+v", attr)
    75  	}
    76  	buf.WriteString("}")
    77  	return buf.String()
    78  }
    79  
    80  func NewDataRow(
    81  	tableID ID,
    82  	indexes []Tuple,
    83  ) *DataRow {
    84  	return &DataRow{
    85  		key: DataKey{
    86  			tableID: tableID,
    87  		},
    88  		indexes: indexes,
    89  	}
    90  }
    91  
    92  type NamedDataRow struct {
    93  	Value    DataValue
    94  	AttrsMap map[string]*AttributeRow
    95  }
    96  
    97  var _ NamedRow = new(NamedDataRow)
    98  
    99  func (n *NamedDataRow) AttrByName(handler *MemHandler, tx *Transaction, name string) (Nullable, error) {
   100  	return n.Value[n.AttrsMap[name].Order], nil
   101  }
   102  
   103  func appendNamedRowToBatch(
   104  	tx *Transaction,
   105  	handler *MemHandler,
   106  	offset int,
   107  	bat *batch.Batch,
   108  	row NamedRow,
   109  ) error {
   110  	for i := offset; i < len(bat.Attrs); i++ {
   111  		name := bat.Attrs[i]
   112  		value, err := row.AttrByName(handler, tx, name)
   113  		if err != nil {
   114  			return err
   115  		}
   116  		value.AppendVector(bat.Vecs[i], handler.mheap)
   117  	}
   118  	return nil
   119  }