github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bithash/block_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 bithash
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  	"github.com/zuoyebang/bitalosdb/internal/base"
    26  )
    27  
    28  func TestBlockIter(t *testing.T) {
    29  	w := blockWriter{restartInterval: 16}
    30  
    31  	makeIkey := func(s string) InternalKey {
    32  		j := strings.Index(s, ":")
    33  		seqNum, err := strconv.Atoi(s[j+1:])
    34  		if err != nil {
    35  			panic(err)
    36  		}
    37  		return base.MakeInternalKey([]byte(s[:j]), uint64(seqNum), InternalKeyKindSet)
    38  	}
    39  
    40  	w.add(makeIkey("testkey:1"), nil)
    41  	w.add(makeIkey("testkey:2"), nil)
    42  	w.add(makeIkey("testkey:3"), nil)
    43  	block := w.finish()
    44  
    45  	iter, err := newBlockIter(bytes.Compare, block)
    46  	require.NoError(t, err)
    47  	defer iter.Close()
    48  	key := []byte("testkey")
    49  	var value []byte
    50  	var fk *InternalKey
    51  	for k, v := iter.SeekGE(key); iter.Valid(); k, v = iter.Next() {
    52  		if bytes.Equal(k.UserKey, key) {
    53  			fmt.Println("find Equal value", string(value))
    54  			for ; iter.Valid() && bytes.Equal(k.UserKey, key); k, v = iter.Next() {
    55  				value = v
    56  				fk = k
    57  			}
    58  			break
    59  		}
    60  	}
    61  	fmt.Println("find value", string(value), fk.String())
    62  }
    63  
    64  func TestBlockSameKey(t *testing.T) {
    65  	w := blockWriter{restartInterval: 16}
    66  	seqNum := uint64(1)
    67  	skey := []byte("ip_b_39.144.177.163")
    68  	w.add(base.MakeInternalKey(skey, seqNum, InternalKeyKindSet), skey)
    69  	seqNum++
    70  	for i := 0; i < 10; i++ {
    71  		key := fmt.Sprintf("key_%d", i)
    72  		val := fmt.Sprintf("value_%d", i)
    73  		w.add(base.MakeInternalKey([]byte(key), seqNum, InternalKeyKindSet), []byte(val))
    74  		seqNum++
    75  		if i == 5 {
    76  			w.add(base.MakeInternalKey(skey, seqNum, InternalKeyKindSet), skey)
    77  			seqNum++
    78  		}
    79  	}
    80  	w.add(base.MakeInternalKey(skey, seqNum, InternalKeyKindSet), skey)
    81  	seqNum++
    82  
    83  	iter, err := newBlockIter(bytes.Compare, w.finish())
    84  	require.NoError(t, err)
    85  	defer iter.Close()
    86  
    87  	var v []byte
    88  	var k *InternalKey
    89  	for k, v = iter.First(); iter.Valid(); k, v = iter.Next() {
    90  		fmt.Println("find1 Equal value", k.String(), string(v))
    91  	}
    92  	fmt.Println("find end", k, v)
    93  }