github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/unionstore/mock.go (about)

     1  // Copyright 2021 TiKV Authors
     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  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/unionstore/mock.go
    19  //
    20  
    21  // Copyright 2015 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package unionstore
    36  
    37  import (
    38  	"context"
    39  
    40  	tikverr "github.com/KinWaiYuen/client-go/v2/error"
    41  )
    42  
    43  type mockSnapshot struct {
    44  	store *MemDB
    45  }
    46  
    47  func (s *mockSnapshot) Get(_ context.Context, k []byte) ([]byte, error) {
    48  	return s.store.Get(k)
    49  }
    50  
    51  func (s *mockSnapshot) SetPriority(priority int) {
    52  
    53  }
    54  
    55  func (s *mockSnapshot) BatchGet(_ context.Context, keys [][]byte) (map[string][]byte, error) {
    56  	m := make(map[string][]byte, len(keys))
    57  	for _, k := range keys {
    58  		v, err := s.store.Get(k)
    59  		if tikverr.IsErrNotFound(err) {
    60  			continue
    61  		}
    62  		if err != nil {
    63  			return nil, err
    64  		}
    65  		m[string(k)] = v
    66  	}
    67  	return m, nil
    68  }
    69  
    70  func (s *mockSnapshot) Iter(k []byte, upperBound []byte) (Iterator, error) {
    71  	return s.store.Iter(k, upperBound)
    72  }
    73  
    74  func (s *mockSnapshot) IterReverse(k []byte) (Iterator, error) {
    75  	return s.store.IterReverse(k)
    76  }
    77  
    78  func (s *mockSnapshot) SetOption(opt int, val interface{}) {}