github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/bitpage/merging_iter_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  	"fmt"
    19  	"os"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  	"github.com/zuoyebang/bitalosdb/internal/base"
    24  	"github.com/zuoyebang/bitalosdb/internal/hash"
    25  )
    26  
    27  func TestSkl_Arrat_Merging(t *testing.T) {
    28  	defer os.RemoveAll(testDir)
    29  	os.RemoveAll(testDir)
    30  
    31  	var ikey internalKey
    32  
    33  	valbuf := []byte("val4")
    34  	addval, closer := base.EncodeInternalValue(valbuf, 1, internalKeyKindSetBithash)
    35  	defer func() {
    36  		if closer != nil {
    37  			closer()
    38  		}
    39  	}()
    40  
    41  	type wtest struct {
    42  		key  int
    43  		seq  uint64
    44  		kind internalKeyKind
    45  	}
    46  
    47  	type rtest struct {
    48  		key   int
    49  		seq   uint64
    50  		exist bool
    51  	}
    52  
    53  	writeData := func(bp *Bitpage, pn PageNum, cases []wtest) {
    54  		wr := bp.GetPageWriter(pn, nil)
    55  		if wr == nil {
    56  			t.Fatal("NewWriter fail")
    57  		}
    58  
    59  		for i := 0; i < len(cases); i++ {
    60  			ikey = makeInternalKey(makeTestKey(cases[i].key), cases[i].seq, cases[i].kind)
    61  			if cases[i].kind == internalKeyKindSet {
    62  				if err := wr.Set(ikey, addval); err != nil {
    63  					t.Fatal("set fail", ikey.String(), err)
    64  				}
    65  			} else {
    66  				if err := wr.Set(ikey, nil); err != nil {
    67  					t.Fatal("set fail", ikey.String(), err)
    68  				}
    69  			}
    70  		}
    71  		require.NoError(t, wr.FlushFinish())
    72  	}
    73  
    74  	readData := func(pg *page, cases []rtest) {
    75  		for i := 0; i < len(cases); i++ {
    76  			key := makeTestKey(cases[i].key)
    77  			v, vexist, vcloser, _ := pg.get(key, hash.Crc32(key))
    78  			if cases[i].exist {
    79  				require.Equal(t, true, vexist)
    80  				require.Equal(t, addval, v)
    81  			} else {
    82  				require.Equal(t, false, vexist)
    83  			}
    84  			if vcloser != nil {
    85  				vcloser()
    86  			}
    87  		}
    88  	}
    89  
    90  	bp, err := testOpenBitpage(true)
    91  	require.NoError(t, err)
    92  	pn, err1 := bp.NewPage()
    93  	require.NoError(t, err1)
    94  
    95  	fmt.Printf("#####################writeData#####################\n")
    96  	cases := make([]wtest, 0, 10)
    97  	cases = append(cases, wtest{key: 0, seq: 1, kind: internalKeyKindSet})
    98  	cases = append(cases, wtest{key: 0, seq: 2, kind: internalKeyKindSet})
    99  	cases = append(cases, wtest{key: 0, seq: 3, kind: internalKeyKindSet})
   100  	cases = append(cases, wtest{key: 0, seq: 4, kind: internalKeyKindSet})
   101  	cases = append(cases, wtest{key: 1, seq: 5, kind: internalKeyKindSet})
   102  	cases = append(cases, wtest{key: 1, seq: 6, kind: internalKeyKindSet})
   103  	cases = append(cases, wtest{key: 2, seq: 7, kind: internalKeyKindDelete})
   104  	cases = append(cases, wtest{key: 2, seq: 8, kind: internalKeyKindSet})
   105  	cases = append(cases, wtest{key: 2, seq: 9, kind: internalKeyKindDelete})
   106  	cases = append(cases, wtest{key: 2, seq: 10, kind: internalKeyKindDelete})
   107  	cases = append(cases, wtest{key: 3, seq: 11, kind: internalKeyKindSet})
   108  	cases = append(cases, wtest{key: 4, seq: 12, kind: internalKeyKindSet})
   109  	cases = append(cases, wtest{key: 5, seq: 13, kind: internalKeyKindDelete})
   110  	writeData(bp, pn, cases)
   111  
   112  	rcases := make([]rtest, 0, 10)
   113  	rcases = append(rcases, rtest{key: 0, seq: 4, exist: true})
   114  	rcases = append(rcases, rtest{key: 1, seq: 6, exist: true})
   115  	rcases = append(rcases, rtest{key: 2, seq: 10, exist: false})
   116  	rcases = append(rcases, rtest{key: 3, seq: 11, exist: true})
   117  	rcases = append(rcases, rtest{key: 4, seq: 12, exist: true})
   118  	rcases = append(rcases, rtest{key: 5, seq: 13, exist: false})
   119  	p := bp.GetPage(pn)
   120  	readData(p, rcases)
   121  
   122  	fmt.Printf("#####################flush#####################\n")
   123  	require.NoError(t, p.flush(nil, ""))
   124  
   125  	rcases2 := make([]rtest, 0, 10)
   126  	rcases2 = append(rcases2, rtest{key: 0, seq: 1, exist: true})
   127  	rcases2 = append(rcases2, rtest{key: 1, seq: 1, exist: true})
   128  	rcases2 = append(rcases2, rtest{key: 2, seq: 1, exist: false})
   129  	rcases2 = append(rcases2, rtest{key: 3, seq: 1, exist: true})
   130  	rcases2 = append(rcases2, rtest{key: 4, seq: 1, exist: true})
   131  	rcases2 = append(rcases2, rtest{key: 5, seq: 1, exist: false})
   132  	p = bp.GetPage(pn)
   133  	readData(p, rcases2)
   134  
   135  	fmt.Printf("#####################writeData2#####################\n")
   136  	cases2 := make([]wtest, 0, 10)
   137  	cases2 = append(cases2, wtest{key: 0, seq: 14, kind: internalKeyKindSet})
   138  	cases2 = append(cases2, wtest{key: 1, seq: 15, kind: internalKeyKindSet})
   139  	cases2 = append(cases2, wtest{key: 2, seq: 16, kind: internalKeyKindSet})
   140  	cases2 = append(cases2, wtest{key: 3, seq: 17, kind: internalKeyKindSet})
   141  	cases2 = append(cases2, wtest{key: 4, seq: 18, kind: internalKeyKindSet})
   142  	cases2 = append(cases2, wtest{key: 4, seq: 19, kind: internalKeyKindSet})
   143  	writeData(bp, pn, cases2)
   144  
   145  	rcases3 := make([]rtest, 0, 10)
   146  	rcases3 = append(rcases3, rtest{key: 0, seq: 14, exist: true})
   147  	rcases3 = append(rcases3, rtest{key: 1, seq: 15, exist: true})
   148  	rcases3 = append(rcases3, rtest{key: 2, seq: 16, exist: true})
   149  	rcases3 = append(rcases3, rtest{key: 3, seq: 17, exist: true})
   150  	rcases3 = append(rcases3, rtest{key: 4, seq: 19, exist: true})
   151  	rcases3 = append(rcases3, rtest{key: 5, seq: 1, exist: false})
   152  	p = bp.GetPage(pn)
   153  	readData(p, rcases3)
   154  
   155  	fmt.Printf("#####################flush2#####################\n")
   156  	require.NoError(t, p.flush(nil, ""))
   157  
   158  	rcases4 := make([]rtest, 0, 10)
   159  	rcases4 = append(rcases4, rtest{key: 0, seq: 1, exist: true})
   160  	rcases4 = append(rcases4, rtest{key: 1, seq: 1, exist: true})
   161  	rcases4 = append(rcases4, rtest{key: 2, seq: 1, exist: true})
   162  	rcases4 = append(rcases4, rtest{key: 3, seq: 1, exist: true})
   163  	rcases4 = append(rcases4, rtest{key: 4, seq: 1, exist: true})
   164  	rcases4 = append(rcases4, rtest{key: 5, seq: 1, exist: false})
   165  	p = bp.GetPage(pn)
   166  	readData(p, rcases4)
   167  
   168  	testCloseBitpage(t, bp)
   169  }