LCOV - code coverage report
Current view: top level - pebble/internal/base - test_utils.go (source / functions) Hit Total Coverage
Test: 2024-06-19 08:16Z 3b3f10c0 - meta test only.lcov Lines: 0 157 0.0 %
Date: 2024-06-19 08:17:02 Functions: 0 0 -

          Line data    Source code
       1             : // Copyright 2024 The LevelDB-Go and Pebble Authors. All rights reserved. Use
       2             : // of this source code is governed by a BSD-style license that can be found in
       3             : // the LICENSE file.
       4             : 
       5             : package base
       6             : 
       7             : import (
       8             :         "context"
       9             :         "fmt"
      10             :         "io"
      11             :         "strconv"
      12             :         "strings"
      13             : )
      14             : 
      15             : // NewDeletableSumValueMerger return a ValueMerger which computes the sum of its
      16             : // arguments, but transforms a zero sum into a non-existent entry.
      17           0 : func NewDeletableSumValueMerger(key, value []byte) (ValueMerger, error) {
      18           0 :         m := &deletableSumValueMerger{}
      19           0 :         return m, m.MergeNewer(value)
      20           0 : }
      21             : 
      22             : type deletableSumValueMerger struct {
      23             :         sum int64
      24             : }
      25             : 
      26           0 : func (m *deletableSumValueMerger) parseAndCalculate(value []byte) error {
      27           0 :         v, err := strconv.ParseInt(string(value), 10, 64)
      28           0 :         if err == nil {
      29           0 :                 m.sum += v
      30           0 :         }
      31           0 :         return err
      32             : }
      33             : 
      34           0 : func (m *deletableSumValueMerger) MergeNewer(value []byte) error {
      35           0 :         return m.parseAndCalculate(value)
      36           0 : }
      37             : 
      38           0 : func (m *deletableSumValueMerger) MergeOlder(value []byte) error {
      39           0 :         return m.parseAndCalculate(value)
      40           0 : }
      41             : 
      42           0 : func (m *deletableSumValueMerger) Finish(includesBase bool) ([]byte, io.Closer, error) {
      43           0 :         if m.sum == 0 {
      44           0 :                 return nil, nil, nil
      45           0 :         }
      46           0 :         return []byte(strconv.FormatInt(m.sum, 10)), nil, nil
      47             : }
      48             : 
      49             : func (m *deletableSumValueMerger) DeletableFinish(
      50             :         includesBase bool,
      51           0 : ) ([]byte, bool, io.Closer, error) {
      52           0 :         value, closer, err := m.Finish(includesBase)
      53           0 :         return value, len(value) == 0, closer, err
      54           0 : }
      55             : 
      56             : // FakeKVs constructs InternalKVs from the given key strings, in the format
      57             : // "key:seq-num". The values are empty.
      58           0 : func FakeKVs(keys ...string) []InternalKV {
      59           0 :         kvs := make([]InternalKV, len(keys))
      60           0 :         for i, k := range keys {
      61           0 :                 kvs[i] = InternalKV{K: fakeIkey(k)}
      62           0 :         }
      63           0 :         return kvs
      64             : }
      65             : 
      66           0 : func fakeIkey(s string) InternalKey {
      67           0 :         j := strings.Index(s, ":")
      68           0 :         seqNum, err := strconv.Atoi(s[j+1:])
      69           0 :         if err != nil {
      70           0 :                 panic(err)
      71             :         }
      72           0 :         return MakeInternalKey([]byte(s[:j]), uint64(seqNum), InternalKeyKindSet)
      73             : }
      74             : 
      75             : // NewFakeIter returns an iterator over the given KVs.
      76           0 : func NewFakeIter(kvs []InternalKV) *FakeIter {
      77           0 :         return &FakeIter{
      78           0 :                 kvs:   kvs,
      79           0 :                 index: 0,
      80           0 :                 valid: len(kvs) > 0,
      81           0 :         }
      82           0 : }
      83             : 
      84             : // FakeIter is an iterator over a fixed set of KVs.
      85             : type FakeIter struct {
      86             :         lower    []byte
      87             :         upper    []byte
      88             :         kvs      []InternalKV
      89             :         index    int
      90             :         valid    bool
      91             :         closeErr error
      92             : }
      93             : 
      94             : // FakeIter implements the InternalIterator interface.
      95             : var _ InternalIterator = (*FakeIter)(nil)
      96             : 
      97             : // SetCloseErr causes future calls to Error() and Close() to return this error.
      98           0 : func (f *FakeIter) SetCloseErr(closeErr error) {
      99           0 :         f.closeErr = closeErr
     100           0 : }
     101             : 
     102           0 : func (f *FakeIter) String() string {
     103           0 :         return "fake"
     104           0 : }
     105             : 
     106             : // SeekGE is part of the InternalIterator interface.
     107           0 : func (f *FakeIter) SeekGE(key []byte, flags SeekGEFlags) *InternalKV {
     108           0 :         f.valid = false
     109           0 :         for f.index = 0; f.index < len(f.kvs); f.index++ {
     110           0 :                 if DefaultComparer.Compare(key, f.key().UserKey) <= 0 {
     111           0 :                         if f.upper != nil && DefaultComparer.Compare(f.upper, f.key().UserKey) <= 0 {
     112           0 :                                 return nil
     113           0 :                         }
     114           0 :                         f.valid = true
     115           0 :                         return f.KV()
     116             :                 }
     117             :         }
     118           0 :         return nil
     119             : }
     120             : 
     121             : // SeekPrefixGE is part of the InternalIterator interface.
     122           0 : func (f *FakeIter) SeekPrefixGE(prefix, key []byte, flags SeekGEFlags) *InternalKV {
     123           0 :         return f.SeekGE(key, flags)
     124           0 : }
     125             : 
     126             : // SeekLT is part of the InternalIterator interface.
     127           0 : func (f *FakeIter) SeekLT(key []byte, flags SeekLTFlags) *InternalKV {
     128           0 :         f.valid = false
     129           0 :         for f.index = len(f.kvs) - 1; f.index >= 0; f.index-- {
     130           0 :                 if DefaultComparer.Compare(key, f.key().UserKey) > 0 {
     131           0 :                         if f.lower != nil && DefaultComparer.Compare(f.lower, f.key().UserKey) > 0 {
     132           0 :                                 return nil
     133           0 :                         }
     134           0 :                         f.valid = true
     135           0 :                         return f.KV()
     136             :                 }
     137             :         }
     138           0 :         return nil
     139             : }
     140             : 
     141             : // First is part of the InternalIterator interface.
     142           0 : func (f *FakeIter) First() *InternalKV {
     143           0 :         f.valid = false
     144           0 :         f.index = -1
     145           0 :         if kv := f.Next(); kv == nil {
     146           0 :                 return nil
     147           0 :         }
     148           0 :         if f.upper != nil && DefaultComparer.Compare(f.upper, f.key().UserKey) <= 0 {
     149           0 :                 return nil
     150           0 :         }
     151           0 :         f.valid = true
     152           0 :         return f.KV()
     153             : }
     154             : 
     155             : // Last is part of the InternalIterator interface.
     156           0 : func (f *FakeIter) Last() *InternalKV {
     157           0 :         f.valid = false
     158           0 :         f.index = len(f.kvs)
     159           0 :         if kv := f.Prev(); kv == nil {
     160           0 :                 return nil
     161           0 :         }
     162           0 :         if f.lower != nil && DefaultComparer.Compare(f.lower, f.key().UserKey) > 0 {
     163           0 :                 return nil
     164           0 :         }
     165           0 :         f.valid = true
     166           0 :         return f.KV()
     167             : }
     168             : 
     169             : // Next is part of the InternalIterator interface.
     170           0 : func (f *FakeIter) Next() *InternalKV {
     171           0 :         f.valid = false
     172           0 :         if f.index == len(f.kvs) {
     173           0 :                 return nil
     174           0 :         }
     175           0 :         f.index++
     176           0 :         if f.index == len(f.kvs) {
     177           0 :                 return nil
     178           0 :         }
     179           0 :         if f.upper != nil && DefaultComparer.Compare(f.upper, f.key().UserKey) <= 0 {
     180           0 :                 return nil
     181           0 :         }
     182           0 :         f.valid = true
     183           0 :         return f.KV()
     184             : }
     185             : 
     186             : // Prev is part of the InternalIterator interface.
     187           0 : func (f *FakeIter) Prev() *InternalKV {
     188           0 :         f.valid = false
     189           0 :         if f.index < 0 {
     190           0 :                 return nil
     191           0 :         }
     192           0 :         f.index--
     193           0 :         if f.index < 0 {
     194           0 :                 return nil
     195           0 :         }
     196           0 :         if f.lower != nil && DefaultComparer.Compare(f.lower, f.key().UserKey) > 0 {
     197           0 :                 return nil
     198           0 :         }
     199           0 :         f.valid = true
     200           0 :         return f.KV()
     201             : }
     202             : 
     203             : // NextPrefix is part of the InternalIterator interface.
     204           0 : func (f *FakeIter) NextPrefix(succKey []byte) *InternalKV {
     205           0 :         return f.SeekGE(succKey, SeekGEFlagsNone)
     206           0 : }
     207             : 
     208             : // key returns the current Key the iterator is positioned at regardless of the
     209             : // value of f.valid.
     210           0 : func (f *FakeIter) key() *InternalKey {
     211           0 :         return &f.kvs[f.index].K
     212           0 : }
     213             : 
     214             : // KV is part of the InternalIterator interface.
     215           0 : func (f *FakeIter) KV() *InternalKV {
     216           0 :         if f.valid {
     217           0 :                 return &f.kvs[f.index]
     218           0 :         }
     219             :         // It is invalid to call Key() when Valid() returns false. Rather than
     220             :         // returning nil here which would technically be more correct, return a
     221             :         // non-nil key which is the behavior of some InternalIterator
     222             :         // implementations. This provides better testing of users of
     223             :         // InternalIterators.
     224           0 :         if f.index < 0 {
     225           0 :                 return &f.kvs[0]
     226           0 :         }
     227           0 :         return &f.kvs[len(f.kvs)-1]
     228             : }
     229             : 
     230             : // Valid is part of the InternalIterator interface.
     231           0 : func (f *FakeIter) Valid() bool {
     232           0 :         return f.index >= 0 && f.index < len(f.kvs) && f.valid
     233           0 : }
     234             : 
     235             : // Error is part of the InternalIterator interface.
     236           0 : func (f *FakeIter) Error() error {
     237           0 :         return f.closeErr
     238           0 : }
     239             : 
     240             : // Close is part of the InternalIterator interface.
     241           0 : func (f *FakeIter) Close() error {
     242           0 :         return f.closeErr
     243           0 : }
     244             : 
     245             : // SetBounds is part of the InternalIterator interface.
     246           0 : func (f *FakeIter) SetBounds(lower, upper []byte) {
     247           0 :         f.lower = lower
     248           0 :         f.upper = upper
     249           0 : }
     250             : 
     251             : // SetContext is part of the InternalIterator interface.
     252           0 : func (f *FakeIter) SetContext(_ context.Context) {}
     253             : 
     254             : // ParseUserKeyBounds parses UserKeyBounds from a string representation of the
     255             : // form "[foo, bar]" or "[foo, bar)".
     256           0 : func ParseUserKeyBounds(s string) UserKeyBounds {
     257           0 :         first, last, s := s[0], s[len(s)-1], s[1:len(s)-1]
     258           0 :         start, end, ok := strings.Cut(s, ", ")
     259           0 :         if !ok || first != '[' || (last != ']' && last != ')') {
     260           0 :                 panic(fmt.Sprintf("invalid bounds %q", s))
     261             :         }
     262           0 :         return UserKeyBoundsEndExclusiveIf([]byte(start), []byte(end), last == ')')
     263             : }

Generated by: LCOV version 1.14