github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/mem/log.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  	"context"
    19  	"encoding/json"
    20  	"sync"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/logservice"
    23  	logpb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    25  )
    26  
    27  // KVLog kv log
    28  type KVLog struct {
    29  	Txn    txn.TxnMeta `json:"txn"`
    30  	Keys   [][]byte    `json:"key,omitempty"`
    31  	Values [][]byte    `json:"value,omitempty"`
    32  }
    33  
    34  // MustUnmarshal must unmarshal
    35  func (l *KVLog) MustUnmarshal(data []byte) {
    36  	if err := json.Unmarshal(data, l); err != nil {
    37  		panic(l)
    38  	}
    39  }
    40  
    41  // MustMarshal must marshal
    42  func (l *KVLog) MustMarshal() []byte {
    43  	data, err := json.Marshal(l)
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	return data
    48  }
    49  
    50  // NewMemLog new log use memory as backend. Log index is start from 1.
    51  func NewMemLog() logservice.Client {
    52  	return &memLogClient{}
    53  }
    54  
    55  type memLogClient struct {
    56  	sync.RWMutex
    57  	logs []logpb.LogRecord
    58  }
    59  
    60  func (mc *memLogClient) Close() error { return nil }
    61  
    62  func (mc *memLogClient) Config() logservice.ClientConfig { return logservice.ClientConfig{} }
    63  
    64  func (mc *memLogClient) GetLogRecord(payloadLength int) logpb.LogRecord {
    65  	return logpb.LogRecord{}
    66  }
    67  
    68  func (mc *memLogClient) Append(ctx context.Context, log logpb.LogRecord) (logservice.Lsn, error) {
    69  	mc.Lock()
    70  	defer mc.Unlock()
    71  
    72  	log.Lsn = logservice.Lsn(len(mc.logs) + 1)
    73  	mc.logs = append(mc.logs, log)
    74  	return logservice.Lsn(len(mc.logs)), nil
    75  }
    76  
    77  func (mc *memLogClient) Read(ctx context.Context, firstIndex logservice.Lsn, maxSize uint64) ([]logpb.LogRecord, logservice.Lsn, error) {
    78  	mc.RLock()
    79  	defer mc.RUnlock()
    80  
    81  	if firstIndex > logservice.Lsn(len(mc.logs)) {
    82  		return nil, firstIndex, nil
    83  	}
    84  
    85  	values := make([]logpb.LogRecord, logservice.Lsn(len(mc.logs))-firstIndex+1)
    86  	copy(values, mc.logs[firstIndex-1:])
    87  	return values, firstIndex, nil
    88  }
    89  
    90  func (mc *memLogClient) Truncate(ctx context.Context, index logservice.Lsn) error {
    91  	mc.Lock()
    92  	defer mc.Unlock()
    93  
    94  	if index > logservice.Lsn(len(mc.logs)) {
    95  		panic("invalid truncate index")
    96  	}
    97  
    98  	mc.logs = mc.logs[index:]
    99  	return nil
   100  }
   101  
   102  func (mc *memLogClient) GetTruncatedLsn(ctx context.Context) (logservice.Lsn, error) {
   103  	return 0, nil
   104  }
   105  
   106  func (mc *memLogClient) GetTSOTimestamp(ctx context.Context, count uint64) (uint64, error) {
   107  	return 0, nil
   108  }