github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitpage/skl_test.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 bitpage
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"os"
    21  	"strconv"
    22  	"sync"
    23  	"sync/atomic"
    24  	"testing"
    25  
    26  	"github.com/zuoyebang/bitalosdb/internal/base"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  const arenaSize = 1 << 20
    32  
    33  type iterAdapter struct {
    34  	*sklIterator
    35  	key *internalKey
    36  	val []byte
    37  }
    38  
    39  func newIterAdapter(iter *sklIterator) *iterAdapter {
    40  	return &iterAdapter{
    41  		sklIterator: iter,
    42  	}
    43  }
    44  
    45  func (i *iterAdapter) update(key *internalKey, val []byte) bool {
    46  	i.key = key
    47  	i.val = val
    48  	return i.key != nil
    49  }
    50  
    51  func (i *iterAdapter) String() string {
    52  	return "iter-adapter"
    53  }
    54  
    55  func (i *iterAdapter) SeekGE(key []byte) bool {
    56  	return i.update(i.sklIterator.SeekGE(key))
    57  }
    58  
    59  func (i *iterAdapter) SeekPrefixGE(prefix, key []byte, trySeekUsingNext bool) bool {
    60  	return i.update(i.sklIterator.SeekPrefixGE(prefix, key, trySeekUsingNext))
    61  }
    62  
    63  func (i *iterAdapter) SeekLT(key []byte) bool {
    64  	return i.update(i.sklIterator.SeekLT(key))
    65  }
    66  
    67  func (i *iterAdapter) First() bool {
    68  	return i.update(i.sklIterator.First())
    69  }
    70  
    71  func (i *iterAdapter) Last() bool {
    72  	return i.update(i.sklIterator.Last())
    73  }
    74  
    75  func (i *iterAdapter) Next() bool {
    76  	return i.update(i.sklIterator.Next())
    77  }
    78  
    79  func (i *iterAdapter) Prev() bool {
    80  	return i.update(i.sklIterator.Prev())
    81  }
    82  
    83  func (i *iterAdapter) Key() internalKey {
    84  	return *i.key
    85  }
    86  
    87  func (i *iterAdapter) Value() []byte {
    88  	return i.val
    89  }
    90  
    91  func (i *iterAdapter) Valid() bool {
    92  	return i.key != nil
    93  }
    94  
    95  func makeIntKey(i int) internalKey {
    96  	return internalKey{UserKey: []byte(fmt.Sprintf("%05d", i))}
    97  }
    98  
    99  func makeKey(s string) []byte {
   100  	return []byte(s)
   101  }
   102  
   103  func makeIkey(s string) internalKey {
   104  	return internalKey{UserKey: []byte(s)}
   105  }
   106  
   107  func makeInternalKey(key []byte, seqNum uint64, kind internalKeyKind) internalKey {
   108  	return base.MakeInternalKey(key, seqNum, kind)
   109  }
   110  
   111  func makeValue(i int) []byte {
   112  	return []byte(fmt.Sprintf("v%05d", i))
   113  }
   114  
   115  func makeinserterAdd(s *skl) func(key internalKey, value []byte) error {
   116  	ins := &Inserter{}
   117  	return func(key internalKey, value []byte) error {
   118  		return ins.Add(s, key, value)
   119  	}
   120  }
   121  
   122  func length(s *skl) int {
   123  	count := 0
   124  
   125  	it := newIterAdapter(s.NewIter(nil, nil))
   126  	for valid := it.First(); valid; valid = it.Next() {
   127  		count++
   128  	}
   129  
   130  	return count
   131  }
   132  
   133  func lengthRev(s *skl) int {
   134  	count := 0
   135  
   136  	it := newIterAdapter(s.NewIter(nil, nil))
   137  	for valid := it.Last(); valid; valid = it.Prev() {
   138  		count++
   139  	}
   140  
   141  	return count
   142  }
   143  
   144  func TestSkl_Empty(t *testing.T) {
   145  	os.Remove(testPath)
   146  	tbl := testNewTable()
   147  	defer func() {
   148  		require.NoError(t, testCloseTable(tbl))
   149  	}()
   150  	l, _ := newSkl(tbl, nil, true)
   151  	it := newIterAdapter(l.NewIter(nil, nil))
   152  
   153  	require.False(t, it.Valid())
   154  
   155  	it.First()
   156  	require.False(t, it.Valid())
   157  
   158  	it.Last()
   159  	require.False(t, it.Valid())
   160  
   161  	key := makeKey("aaa")
   162  	require.False(t, it.SeekGE(key))
   163  	require.False(t, it.Valid())
   164  }
   165  
   166  func TestSkl_IsEmpty(t *testing.T) {
   167  	os.Remove(testPath)
   168  	tbl := testNewTable()
   169  	defer func() {
   170  		require.NoError(t, testCloseTable(tbl))
   171  	}()
   172  	l, _ := newSkl(tbl, nil, true)
   173  	require.Equal(t, true, l.isEmpty())
   174  	add := makeinserterAdd(l)
   175  	add(base.MakeInternalKey([]byte("key1"), 1, internalKeyKindSet), makeValue(1))
   176  	require.Equal(t, false, l.isEmpty())
   177  }
   178  
   179  func TestSkl_Seqnum(t *testing.T) {
   180  	os.Remove(testPath)
   181  	tbl := testNewTable()
   182  	defer func() {
   183  		require.NoError(t, testCloseTable(tbl))
   184  	}()
   185  	l, _ := newSkl(tbl, nil, true)
   186  	it := newIterAdapter(l.NewIter(nil, nil))
   187  
   188  	add := makeinserterAdd(l)
   189  
   190  	// Try adding values.
   191  	add(base.MakeInternalKey([]byte("key1"), 1, internalKeyKindSet), makeValue(1))
   192  	add(base.MakeInternalKey([]byte("key12"), 3, internalKeyKindSet), makeValue(123))
   193  	add(base.MakeInternalKey([]byte("key1"), 2, internalKeyKindSet), makeValue(2))
   194  	add(base.MakeInternalKey([]byte("key12"), 2, internalKeyKindSet), makeValue(122))
   195  	add(base.MakeInternalKey([]byte("key1"), 3, internalKeyKindSet), makeValue(3))
   196  	add(base.MakeInternalKey([]byte("key12"), 1, internalKeyKindSet), makeValue(121))
   197  
   198  	for it.First(); it.Valid(); it.Next() {
   199  		fmt.Println(it.Key().String(), string(it.Value()))
   200  	}
   201  }
   202  
   203  func TestSkl_Basic(t *testing.T) {
   204  	for _, inserter := range []bool{false, true} {
   205  		t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) {
   206  			os.Remove(testPath)
   207  			tbl := testNewTable()
   208  			defer func() {
   209  				require.NoError(t, testCloseTable(tbl))
   210  			}()
   211  			l, _ := newSkl(tbl, nil, true)
   212  			it := newIterAdapter(l.NewIter(nil, nil))
   213  
   214  			add := l.Add
   215  			if inserter {
   216  				add = makeinserterAdd(l)
   217  			}
   218  
   219  			add(makeIkey("key1"), makeValue(1))
   220  			add(makeIkey("key3"), makeValue(3))
   221  			add(makeIkey("key2"), makeValue(2))
   222  
   223  			require.True(t, it.SeekGE(makeKey("key")))
   224  			require.True(t, it.Valid())
   225  			require.NotEqual(t, "key", it.Key().UserKey)
   226  
   227  			require.True(t, it.SeekGE(makeKey("key")))
   228  			require.True(t, it.Valid())
   229  			require.NotEqual(t, "key", it.Key().UserKey)
   230  
   231  			require.True(t, it.SeekGE(makeKey("key1")))
   232  			require.EqualValues(t, "key1", it.Key().UserKey)
   233  			require.EqualValues(t, makeValue(1), it.Value())
   234  
   235  			require.True(t, it.SeekGE(makeKey("key1")))
   236  			require.EqualValues(t, "key1", it.Key().UserKey)
   237  			require.EqualValues(t, makeValue(1), it.Value())
   238  
   239  			require.True(t, it.SeekGE(makeKey("key2")))
   240  			require.EqualValues(t, "key2", it.Key().UserKey)
   241  			require.EqualValues(t, makeValue(2), it.Value())
   242  
   243  			require.True(t, it.SeekGE(makeKey("key2")))
   244  			require.EqualValues(t, "key2", it.Key().UserKey)
   245  			require.EqualValues(t, makeValue(2), it.Value())
   246  
   247  			require.True(t, it.SeekGE(makeKey("key3")))
   248  			require.EqualValues(t, "key3", it.Key().UserKey)
   249  			require.EqualValues(t, makeValue(3), it.Value())
   250  
   251  			require.True(t, it.SeekGE(makeKey("key3")))
   252  			require.EqualValues(t, "key3", it.Key().UserKey)
   253  			require.EqualValues(t, makeValue(3), it.Value())
   254  
   255  			key := makeIkey("a")
   256  			key.SetSeqNum(1)
   257  			add(key, nil)
   258  			key.SetSeqNum(2)
   259  			add(key, nil)
   260  
   261  			require.True(t, it.SeekGE(makeKey("a")))
   262  			require.True(t, it.Valid())
   263  			require.EqualValues(t, "a", it.Key().UserKey)
   264  			require.EqualValues(t, 2, it.Key().SeqNum())
   265  
   266  			require.True(t, it.Next())
   267  			require.True(t, it.Valid())
   268  			require.EqualValues(t, "a", it.Key().UserKey)
   269  			require.EqualValues(t, 1, it.Key().SeqNum())
   270  
   271  			key = makeIkey("b")
   272  			key.SetSeqNum(2)
   273  			add(key, nil)
   274  			//key.SetSeqNum(1)
   275  			//add(key, nil)
   276  			//in single refresh process, flushit is not feasible for a single kv to correspond to multiple seqnum
   277  
   278  			require.True(t, it.SeekGE(makeKey("b")))
   279  			require.True(t, it.Valid())
   280  			require.EqualValues(t, "b", it.Key().UserKey)
   281  			require.EqualValues(t, 2, it.Key().SeqNum())
   282  		})
   283  	}
   284  }
   285  
   286  func TestSkl_ConcurrentBasic(t *testing.T) {
   287  	const n = 1000
   288  
   289  	for _, inse := range []bool{false, true} {
   290  		t.Run(fmt.Sprintf("inserter=%t", inse), func(t *testing.T) {
   291  			os.Remove(testPath)
   292  			tbl := testNewTable()
   293  			defer func() {
   294  				require.NoError(t, testCloseTable(tbl))
   295  			}()
   296  
   297  			l, _ := newSkl(tbl, nil, true)
   298  			l.testing = true
   299  
   300  			var wg sync.WaitGroup
   301  			for i := 0; i < n; i++ {
   302  				wg.Add(1)
   303  				go func(i int) {
   304  					defer wg.Done()
   305  
   306  					if inse {
   307  						var ins Inserter
   308  						ins.Add(l, makeIntKey(i), makeValue(i))
   309  					} else {
   310  						l.Add(makeIntKey(i), makeValue(i))
   311  					}
   312  				}(i)
   313  			}
   314  			wg.Wait()
   315  
   316  			for i := 0; i < n; i++ {
   317  				wg.Add(1)
   318  				go func(i int) {
   319  					defer wg.Done()
   320  
   321  					it := newIterAdapter(l.NewIter(nil, nil))
   322  					require.True(t, it.SeekGE(makeKey(fmt.Sprintf("%05d", i))))
   323  					require.EqualValues(t, fmt.Sprintf("%05d", i), it.Key().UserKey)
   324  				}(i)
   325  			}
   326  			wg.Wait()
   327  			require.Equal(t, n, length(l))
   328  			require.Equal(t, n, lengthRev(l))
   329  		})
   330  	}
   331  }
   332  
   333  func TestSkl_ConcurrentOneKey(t *testing.T) {
   334  	const n = 100
   335  	key := makeKey("thekey")
   336  	ikey := makeIkey("thekey")
   337  
   338  	for _, inse := range []bool{false, true} {
   339  		t.Run(fmt.Sprintf("inserter=%t", inse), func(t *testing.T) {
   340  			os.Remove(testPath)
   341  			tbl := testNewTable()
   342  			defer func() {
   343  				require.NoError(t, testCloseTable(tbl))
   344  			}()
   345  			l, _ := newSkl(tbl, nil, true)
   346  			l.testing = true
   347  
   348  			var wg sync.WaitGroup
   349  			writeDone := make(chan struct{}, 1)
   350  			for i := 0; i < n; i++ {
   351  				wg.Add(1)
   352  				go func(i int) {
   353  					defer func() {
   354  						wg.Done()
   355  						select {
   356  						case writeDone <- struct{}{}:
   357  						default:
   358  						}
   359  					}()
   360  
   361  					if inse {
   362  						var ins Inserter
   363  						ins.Add(l, ikey, makeValue(i))
   364  					} else {
   365  						l.Add(ikey, makeValue(i))
   366  					}
   367  				}(i)
   368  			}
   369  
   370  			<-writeDone
   371  			var sawValue atomic.Int32
   372  			for i := 0; i < n; i++ {
   373  				wg.Add(1)
   374  				go func() {
   375  					defer wg.Done()
   376  
   377  					it := newIterAdapter(l.NewIter(nil, nil))
   378  					it.SeekGE(key)
   379  					require.True(t, it.Valid())
   380  					require.True(t, bytes.Equal(key, it.Key().UserKey))
   381  
   382  					sawValue.Add(1)
   383  					v, err := strconv.Atoi(string(it.Value()[1:]))
   384  					require.NoError(t, err)
   385  					require.True(t, 0 <= v && v < n)
   386  				}()
   387  			}
   388  			wg.Wait()
   389  			require.Equal(t, int32(n), sawValue.Load())
   390  			require.Equal(t, 1, length(l))
   391  			require.Equal(t, 1, lengthRev(l))
   392  		})
   393  	}
   394  }
   395  
   396  func TestSkl_Add(t *testing.T) {
   397  	for _, inserter := range []bool{false, true} {
   398  		t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) {
   399  			os.Remove(testPath)
   400  			tbl := testNewTable()
   401  			l, _ := newSkl(tbl, nil, true)
   402  			it := newIterAdapter(l.NewIter(nil, nil))
   403  
   404  			add := l.Add
   405  			if inserter {
   406  				add = makeinserterAdd(l)
   407  			}
   408  
   409  			err := add(internalKey{}, nil)
   410  			require.Nil(t, err)
   411  			require.True(t, it.SeekGE([]byte{}))
   412  			require.EqualValues(t, []byte{}, it.Key().UserKey)
   413  			require.EqualValues(t, []byte{}, it.Value())
   414  			require.NoError(t, testCloseTable(tbl))
   415  
   416  			tbl1 := testNewTable()
   417  			l, _ = newSkl(tbl1, nil, true)
   418  			it = newIterAdapter(l.NewIter(nil, nil))
   419  
   420  			add = l.Add
   421  			if inserter {
   422  				add = makeinserterAdd(l)
   423  			}
   424  
   425  			err = add(makeIkey(""), []byte{})
   426  			require.Nil(t, err)
   427  			require.True(t, it.SeekGE([]byte{}))
   428  			require.EqualValues(t, []byte{}, it.Key().UserKey)
   429  			require.EqualValues(t, []byte{}, it.Value())
   430  
   431  			err = add(makeIntKey(2), makeValue(2))
   432  			require.Nil(t, err)
   433  			require.True(t, it.SeekGE(makeKey("00002")))
   434  			require.EqualValues(t, "00002", it.Key().UserKey)
   435  			require.EqualValues(t, makeValue(2), it.Value())
   436  
   437  			err = add(makeIntKey(1), makeValue(1))
   438  			require.Nil(t, err)
   439  			require.True(t, it.SeekGE(makeKey("00001")))
   440  			require.EqualValues(t, "00001", it.Key().UserKey)
   441  			require.EqualValues(t, makeValue(1), it.Value())
   442  
   443  			err = add(makeIntKey(4), makeValue(4))
   444  			require.Nil(t, err)
   445  			require.True(t, it.SeekGE(makeKey("00004")))
   446  			require.EqualValues(t, "00004", it.Key().UserKey)
   447  			require.EqualValues(t, makeValue(4), it.Value())
   448  
   449  			err = add(makeIntKey(3), makeValue(3))
   450  			require.Nil(t, err)
   451  			require.True(t, it.SeekGE(makeKey("00003")))
   452  			require.EqualValues(t, "00003", it.Key().UserKey)
   453  			require.EqualValues(t, makeValue(3), it.Value())
   454  
   455  			err = add(makeIntKey(2), nil)
   456  			require.Equal(t, ErrRecordExists, err)
   457  			require.EqualValues(t, "00003", it.Key().UserKey)
   458  			require.EqualValues(t, makeValue(3), it.Value())
   459  
   460  			require.Equal(t, 5, length(l))
   461  			require.Equal(t, 5, lengthRev(l))
   462  
   463  			require.NoError(t, testCloseTable(tbl))
   464  		})
   465  	}
   466  }
   467  
   468  func TestSkl_ConcurrentAdd(t *testing.T) {
   469  	for _, inserter := range []bool{false, true} {
   470  		t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) {
   471  			const n = 100
   472  
   473  			tbl := testNewTable()
   474  			defer func() {
   475  				require.NoError(t, testCloseTable(tbl))
   476  			}()
   477  
   478  			l, _ := newSkl(tbl, nil, true)
   479  			l.testing = true
   480  
   481  			start := make([]sync.WaitGroup, n)
   482  			end := make([]sync.WaitGroup, n)
   483  
   484  			for i := 0; i < n; i++ {
   485  				start[i].Add(1)
   486  				end[i].Add(2)
   487  			}
   488  
   489  			for f := 0; f < 2; f++ {
   490  				go func(f int) {
   491  					it := newIterAdapter(l.NewIter(nil, nil))
   492  					add := l.Add
   493  					if inserter {
   494  						add = makeinserterAdd(l)
   495  					}
   496  
   497  					for i := 0; i < n; i++ {
   498  						start[i].Wait()
   499  
   500  						key := makeIntKey(i)
   501  						if add(key, nil) == nil {
   502  							require.True(t, it.SeekGE(key.UserKey))
   503  							require.EqualValues(t, key, it.Key())
   504  						}
   505  
   506  						end[i].Done()
   507  					}
   508  				}(f)
   509  			}
   510  
   511  			for i := 0; i < n; i++ {
   512  				start[i].Done()
   513  				end[i].Wait()
   514  			}
   515  
   516  			require.Equal(t, n, length(l))
   517  			require.Equal(t, n, lengthRev(l))
   518  		})
   519  	}
   520  }
   521  
   522  func TestSkl_IteratorNext(t *testing.T) {
   523  	const n = 100
   524  	tbl := testNewTable()
   525  	defer func() {
   526  		require.NoError(t, testCloseTable(tbl))
   527  	}()
   528  	l, _ := newSkl(tbl, nil, true)
   529  	it := newIterAdapter(l.NewIter(nil, nil))
   530  
   531  	require.False(t, it.Valid())
   532  
   533  	it.First()
   534  	require.False(t, it.Valid())
   535  
   536  	for i := n - 1; i >= 0; i-- {
   537  		l.Add(makeIntKey(i), makeValue(i))
   538  	}
   539  
   540  	it.First()
   541  	for i := 0; i < n; i++ {
   542  		require.True(t, it.Valid())
   543  		require.EqualValues(t, makeIntKey(i), it.Key())
   544  		require.EqualValues(t, makeValue(i), it.Value())
   545  		it.Next()
   546  	}
   547  	require.False(t, it.Valid())
   548  }
   549  
   550  func TestSkl_IteratorPrev(t *testing.T) {
   551  	const n = 100
   552  	tbl := testNewTable()
   553  	defer func() {
   554  		require.NoError(t, testCloseTable(tbl))
   555  	}()
   556  	l, _ := newSkl(tbl, nil, true)
   557  	it := newIterAdapter(l.NewIter(nil, nil))
   558  
   559  	require.False(t, it.Valid())
   560  
   561  	it.Last()
   562  	require.False(t, it.Valid())
   563  
   564  	var ins Inserter
   565  	for i := 0; i < n; i++ {
   566  		ins.Add(l, makeIntKey(i), makeValue(i))
   567  	}
   568  
   569  	it.Last()
   570  	for i := n - 1; i >= 0; i-- {
   571  		require.True(t, it.Valid())
   572  		require.EqualValues(t, makeIntKey(i), it.Key())
   573  		require.EqualValues(t, makeValue(i), it.Value())
   574  		it.Prev()
   575  	}
   576  	require.False(t, it.Valid())
   577  }
   578  
   579  func TestSkl_IteratorSeekGEAndSeekPrefixGE(t *testing.T) {
   580  	const n = 100
   581  	tbl := testNewTable()
   582  	defer func() {
   583  		require.NoError(t, testCloseTable(tbl))
   584  	}()
   585  	l, _ := newSkl(tbl, nil, true)
   586  	it := newIterAdapter(l.NewIter(nil, nil))
   587  
   588  	require.False(t, it.Valid())
   589  	it.First()
   590  	require.False(t, it.Valid())
   591  
   592  	var ins Inserter
   593  	for i := n - 1; i >= 0; i-- {
   594  		v := i*10 + 1000
   595  		ins.Add(l, makeIntKey(v), makeValue(v))
   596  	}
   597  
   598  	require.True(t, it.SeekGE(makeKey("")))
   599  	require.True(t, it.Valid())
   600  	require.EqualValues(t, "01000", it.Key().UserKey)
   601  	require.EqualValues(t, "v01000", it.Value())
   602  
   603  	require.True(t, it.SeekGE(makeKey("01000")))
   604  	require.True(t, it.Valid())
   605  	require.EqualValues(t, "01000", it.Key().UserKey)
   606  	require.EqualValues(t, "v01000", it.Value())
   607  
   608  	require.True(t, it.SeekGE(makeKey("01005")))
   609  	require.True(t, it.Valid())
   610  	require.EqualValues(t, "01010", it.Key().UserKey)
   611  	require.EqualValues(t, "v01010", it.Value())
   612  
   613  	require.True(t, it.SeekGE(makeKey("01010")))
   614  	require.True(t, it.Valid())
   615  	require.EqualValues(t, "01010", it.Key().UserKey)
   616  	require.EqualValues(t, "v01010", it.Value())
   617  
   618  	require.False(t, it.SeekGE(makeKey("99999")))
   619  	require.False(t, it.Valid())
   620  
   621  	{
   622  		require.True(t, it.SeekPrefixGE(makeKey("01000"), makeKey("01000"), false))
   623  		require.True(t, it.Valid())
   624  		require.EqualValues(t, "01000", it.Key().UserKey)
   625  		require.EqualValues(t, "v01000", it.Value())
   626  
   627  		require.True(t, it.SeekPrefixGE(makeKey("01000"), makeKey("01000"), true))
   628  		require.True(t, it.Valid())
   629  		require.EqualValues(t, "01000", it.Key().UserKey)
   630  		require.EqualValues(t, "v01000", it.Value())
   631  
   632  		require.True(t, it.SeekPrefixGE(makeKey("01020"), makeKey("01020"), true))
   633  		require.True(t, it.Valid())
   634  		require.EqualValues(t, "01020", it.Key().UserKey)
   635  		require.EqualValues(t, "v01020", it.Value())
   636  
   637  		require.True(t, it.SeekPrefixGE(makeKey("01200"), makeKey("01200"), true))
   638  		require.True(t, it.Valid())
   639  		require.EqualValues(t, "01200", it.Key().UserKey)
   640  		require.EqualValues(t, "v01200", it.Value())
   641  
   642  		require.True(t, it.SeekPrefixGE(makeKey("01100"), makeKey("01100"), true))
   643  		require.True(t, it.Valid())
   644  		require.EqualValues(t, "01200", it.Key().UserKey)
   645  		require.EqualValues(t, "v01200", it.Value())
   646  
   647  		require.True(t, it.SeekPrefixGE(makeKey("01100"), makeKey("01100"), false))
   648  		require.True(t, it.Valid())
   649  		require.EqualValues(t, "01100", it.Key().UserKey)
   650  		require.EqualValues(t, "v01100", it.Value())
   651  	}
   652  
   653  	ins.Add(l, internalKey{}, nil)
   654  	require.True(t, it.SeekGE([]byte{}))
   655  	require.True(t, it.Valid())
   656  	require.EqualValues(t, "", it.Key().UserKey)
   657  
   658  	require.True(t, it.SeekGE(makeKey("")))
   659  	require.True(t, it.Valid())
   660  	require.EqualValues(t, "", it.Key().UserKey)
   661  }
   662  
   663  func TestIteratorSeekLT(t *testing.T) {
   664  	const n = 100
   665  	tbl := testNewTable()
   666  	defer func() {
   667  		require.NoError(t, testCloseTable(tbl))
   668  	}()
   669  	l, _ := newSkl(tbl, nil, true)
   670  	it := newIterAdapter(l.NewIter(nil, nil))
   671  
   672  	require.False(t, it.Valid())
   673  	it.First()
   674  	require.False(t, it.Valid())
   675  
   676  	var ins Inserter
   677  	for i := n - 1; i >= 0; i-- {
   678  		v := i*10 + 1000
   679  		ins.Add(l, makeIntKey(v), makeValue(v))
   680  	}
   681  
   682  	require.False(t, it.SeekLT(makeKey("")))
   683  	require.False(t, it.Valid())
   684  
   685  	require.False(t, it.SeekLT(makeKey("01000")))
   686  	require.False(t, it.Valid())
   687  
   688  	require.True(t, it.SeekLT(makeKey("01001")))
   689  	require.True(t, it.Valid())
   690  	require.EqualValues(t, "01000", it.Key().UserKey)
   691  	require.EqualValues(t, "v01000", it.Value())
   692  
   693  	require.True(t, it.SeekLT(makeKey("01005")))
   694  	require.True(t, it.Valid())
   695  	require.EqualValues(t, "01000", it.Key().UserKey)
   696  	require.EqualValues(t, "v01000", it.Value())
   697  
   698  	require.True(t, it.SeekLT(makeKey("01991")))
   699  	require.True(t, it.Valid())
   700  	require.EqualValues(t, "01990", it.Key().UserKey)
   701  	require.EqualValues(t, "v01990", it.Value())
   702  
   703  	require.True(t, it.SeekLT(makeKey("99999")))
   704  	require.True(t, it.Valid())
   705  	require.EqualValues(t, "01990", it.Key().UserKey)
   706  	require.EqualValues(t, "v01990", it.Value())
   707  
   708  	ins.Add(l, internalKey{}, nil)
   709  	require.False(t, it.SeekLT([]byte{}))
   710  	require.False(t, it.Valid())
   711  
   712  	require.True(t, it.SeekLT(makeKey("\x01")))
   713  	require.True(t, it.Valid())
   714  	require.EqualValues(t, "", it.Key().UserKey)
   715  }
   716  
   717  func TestSkl_InvalidInternalKeyDecoding(t *testing.T) {
   718  	tbl := testNewTable()
   719  	defer func() {
   720  		require.NoError(t, testCloseTable(tbl))
   721  	}()
   722  
   723  	l, _ := newSkl(tbl, nil, true)
   724  	it := sklIterator{
   725  		list: l,
   726  	}
   727  
   728  	nd, err := newRawNode(tbl, 1, 1, 1)
   729  	require.Nil(t, err)
   730  
   731  	it.nd = nd
   732  	it.decodeKey()
   733  	require.Nil(t, it.key.UserKey)
   734  	require.Equal(t, uint64(internalKeyKindInvalid), it.key.Trailer)
   735  }