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

     1  // Copyright 2021 - 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 mem
    16  
    17  import (
    18  	"bytes"
    19  
    20  	"github.com/fagongzi/goetty/v2/buf"
    21  	"github.com/matrixorigin/matrixone/pkg/pb/timestamp"
    22  )
    23  
    24  // MVCCKV mvcc kv based on KV
    25  type MVCCKV struct {
    26  	kv *KV
    27  }
    28  
    29  // NewMVCCKV create a mvcc based kv
    30  func NewMVCCKV() *MVCCKV {
    31  	return &MVCCKV{kv: NewKV()}
    32  }
    33  
    34  // Set set mvcc key value
    35  func (mkv *MVCCKV) Set(rawKey []byte, ts timestamp.Timestamp, value []byte) {
    36  	mkv.kv.Set(encodeMVCCKey(rawKey, ts), value)
    37  }
    38  
    39  // Get returns the value of a key in the specified version
    40  func (mkv *MVCCKV) Get(rawKey []byte, ts timestamp.Timestamp) ([]byte, bool) {
    41  	return mkv.kv.Get(encodeMVCCKey(rawKey, ts))
    42  }
    43  
    44  // Delete delete the value of a key in the specified version
    45  func (mkv *MVCCKV) Delete(rawKey []byte, ts timestamp.Timestamp) bool {
    46  	ok := mkv.kv.Delete(encodeMVCCKey(rawKey, ts))
    47  	return ok
    48  }
    49  
    50  // AscendRange iter in [rawkey:from, rawkey:to)
    51  func (mkv *MVCCKV) AscendRange(rawKey []byte, from timestamp.Timestamp, to timestamp.Timestamp,
    52  	fn func([]byte, timestamp.Timestamp)) {
    53  	fromMVCC := encodeMVCCKey(rawKey, from)
    54  	toMVCC := encodeMVCCKey(rawKey, to)
    55  
    56  	mkv.kv.AscendRange(fromMVCC, toMVCC, func(key, value []byte) bool {
    57  		k, ts := decodeMVCCKey(key)
    58  		if bytes.Equal(rawKey, k) {
    59  			fn(value, ts)
    60  		}
    61  		return true
    62  	})
    63  }
    64  
    65  func encodeMVCCKey(rawKey []byte, ts timestamp.Timestamp) []byte {
    66  	buffer := buf.NewByteBuf(len(rawKey) + 12)
    67  	buffer.MustWrite(rawKey)
    68  	buffer.WriteInt64(ts.PhysicalTime)
    69  	buffer.WriteUint32(ts.LogicalTime)
    70  	_, v := buffer.ReadAll()
    71  	return v
    72  }
    73  
    74  func decodeMVCCKey(mvccKey []byte) ([]byte, timestamp.Timestamp) {
    75  	n := len(mvccKey)
    76  	rawKey := mvccKey[:n-12]
    77  	return rawKey, timestamp.Timestamp{
    78  		PhysicalTime: buf.Byte2Int64(mvccKey[n-12:]),
    79  		LogicalTime:  buf.Byte2Uint32(mvccKey[n-4:]),
    80  	}
    81  }