github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/ekv/interface_mock_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package ekv
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/whtcorpsinc/milevadb/causetstore/einsteindb/oracle"
    20  )
    21  
    22  // mockTxn is a txn that returns a retryAble error when called Commit.
    23  type mockTxn struct {
    24  	opts  map[Option]interface{}
    25  	valid bool
    26  }
    27  
    28  // Commit always returns a retryable error.
    29  func (t *mockTxn) Commit(ctx context.Context) error {
    30  	return ErrTxnRetryable
    31  }
    32  
    33  func (t *mockTxn) Rollback() error {
    34  	t.valid = false
    35  	return nil
    36  }
    37  
    38  func (t *mockTxn) String() string {
    39  	return ""
    40  }
    41  
    42  func (t *mockTxn) LockKeys(_ context.Context, _ *LockCtx, _ ...Key) error {
    43  	return nil
    44  }
    45  
    46  func (t *mockTxn) SetOption(opt Option, val interface{}) {
    47  	t.opts[opt] = val
    48  }
    49  
    50  func (t *mockTxn) DelOption(opt Option) {
    51  	delete(t.opts, opt)
    52  }
    53  
    54  func (t *mockTxn) GetOption(opt Option) interface{} {
    55  	return t.opts[opt]
    56  }
    57  
    58  func (t *mockTxn) IsReadOnly() bool {
    59  	return true
    60  }
    61  
    62  func (t *mockTxn) StartTS() uint64 {
    63  	return uint64(0)
    64  }
    65  func (t *mockTxn) Get(ctx context.Context, k Key) ([]byte, error) {
    66  	return nil, nil
    67  }
    68  
    69  func (t *mockTxn) BatchGet(ctx context.Context, keys []Key) (map[string][]byte, error) {
    70  	return nil, nil
    71  }
    72  
    73  func (t *mockTxn) Iter(k Key, upperBound Key) (Iterator, error) {
    74  	return nil, nil
    75  }
    76  
    77  func (t *mockTxn) IterReverse(k Key) (Iterator, error) {
    78  	return nil, nil
    79  }
    80  
    81  func (t *mockTxn) Set(k Key, v []byte) error {
    82  	return nil
    83  }
    84  func (t *mockTxn) Delete(k Key) error {
    85  	return nil
    86  }
    87  
    88  func (t *mockTxn) Valid() bool {
    89  	return t.valid
    90  }
    91  
    92  func (t *mockTxn) Len() int {
    93  	return 0
    94  }
    95  
    96  func (t *mockTxn) Size() int {
    97  	return 0
    98  }
    99  
   100  func (t *mockTxn) GetMemBuffer() MemBuffer {
   101  	return nil
   102  }
   103  
   104  func (t *mockTxn) GetSnapshot() Snapshot {
   105  	return nil
   106  }
   107  
   108  func (t *mockTxn) GetUnionStore() UnionStore {
   109  	return nil
   110  }
   111  
   112  func (t *mockTxn) NewStagingBuffer() MemBuffer {
   113  	return nil
   114  }
   115  
   116  func (t *mockTxn) Flush() (int, error) {
   117  	return 0, nil
   118  }
   119  
   120  func (t *mockTxn) Discard() {
   121  
   122  }
   123  
   124  func (t *mockTxn) Reset() {
   125  	t.valid = false
   126  }
   127  
   128  func (t *mockTxn) SetVars(vars *Variables) {
   129  
   130  }
   131  
   132  func (t *mockTxn) GetVars() *Variables {
   133  	return nil
   134  }
   135  
   136  // newMockTxn new a mockTxn.
   137  func newMockTxn() Transaction {
   138  	return &mockTxn{
   139  		opts:  make(map[Option]interface{}),
   140  		valid: true,
   141  	}
   142  }
   143  
   144  // mockStorage is used to start a must commit-failed txn.
   145  type mockStorage struct {
   146  }
   147  
   148  func (s *mockStorage) Begin() (Transaction, error) {
   149  	return newMockTxn(), nil
   150  }
   151  
   152  func (*mockTxn) IsPessimistic() bool {
   153  	return false
   154  }
   155  
   156  // BeginWithStartTS begins a transaction with startTS.
   157  func (s *mockStorage) BeginWithStartTS(startTS uint64) (Transaction, error) {
   158  	return s.Begin()
   159  }
   160  
   161  func (s *mockStorage) GetSnapshot(ver Version) (Snapshot, error) {
   162  	return &mockSnapshot{
   163  		causetstore: newMemDB(),
   164  	}, nil
   165  }
   166  
   167  func (s *mockStorage) Close() error {
   168  	return nil
   169  }
   170  
   171  func (s *mockStorage) UUID() string {
   172  	return ""
   173  }
   174  
   175  // CurrentVersion returns current max committed version.
   176  func (s *mockStorage) CurrentVersion() (Version, error) {
   177  	return NewVersion(1), nil
   178  }
   179  
   180  func (s *mockStorage) GetClient() Client {
   181  	return nil
   182  }
   183  
   184  func (s *mockStorage) GetOracle() oracle.Oracle {
   185  	return nil
   186  }
   187  
   188  func (s *mockStorage) SupportDeleteRange() (supported bool) {
   189  	return false
   190  }
   191  
   192  func (s *mockStorage) Name() string {
   193  	return "KVMockStorage"
   194  }
   195  
   196  func (s *mockStorage) Describe() string {
   197  	return "KVMockStorage is a mock CausetStore implementation, only for unittests in KV package"
   198  }
   199  
   200  func (s *mockStorage) ShowStatus(ctx context.Context, key string) (interface{}, error) {
   201  	return nil, nil
   202  }
   203  
   204  // newMockStorage creates a new mockStorage.
   205  func newMockStorage() CausetStorage {
   206  	return &mockStorage{}
   207  }
   208  
   209  type mockSnapshot struct {
   210  	causetstore MemBuffer
   211  }
   212  
   213  func (s *mockSnapshot) Get(ctx context.Context, k Key) ([]byte, error) {
   214  	return s.causetstore.Get(ctx, k)
   215  }
   216  
   217  func (s *mockSnapshot) SetPriority(priority int) {
   218  
   219  }
   220  
   221  func (s *mockSnapshot) BatchGet(ctx context.Context, keys []Key) (map[string][]byte, error) {
   222  	m := make(map[string][]byte, len(keys))
   223  	for _, k := range keys {
   224  		v, err := s.causetstore.Get(ctx, k)
   225  		if IsErrNotFound(err) {
   226  			continue
   227  		}
   228  		if err != nil {
   229  			return nil, err
   230  		}
   231  		m[string(k)] = v
   232  	}
   233  	return m, nil
   234  }
   235  
   236  func (s *mockSnapshot) Iter(k Key, upperBound Key) (Iterator, error) {
   237  	return s.causetstore.Iter(k, upperBound)
   238  }
   239  
   240  func (s *mockSnapshot) IterReverse(k Key) (Iterator, error) {
   241  	return s.causetstore.IterReverse(k)
   242  }
   243  
   244  func (s *mockSnapshot) SetOption(opt Option, val interface{}) {}
   245  func (s *mockSnapshot) DelOption(opt Option)                  {}