github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/base/internal_test.go (about) 1 // Copyright 2012 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 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func (k InternalKey) encodedString() string { 14 buf := make([]byte, k.Size()) 15 k.Encode(buf) 16 return string(buf) 17 } 18 19 func TestInternalKey(t *testing.T) { 20 k := MakeInternalKey([]byte("foo"), 0x08070605040302, 1) 21 if got, want := k.encodedString(), "foo\x01\x02\x03\x04\x05\x06\x07\x08"; got != want { 22 t.Fatalf("k = %q want %q", got, want) 23 } 24 if !k.Valid() { 25 t.Fatalf("invalid key") 26 } 27 if got, want := string(k.UserKey), "foo"; got != want { 28 t.Errorf("ukey = %q want %q", got, want) 29 } 30 if got, want := k.Kind(), InternalKeyKind(1); got != want { 31 t.Errorf("kind = %d want %d", got, want) 32 } 33 if got, want := k.SeqNum(), uint64(0x08070605040302); got != want { 34 t.Errorf("seqNum = %d want %d", got, want) 35 } 36 } 37 38 func TestInvalidInternalKey(t *testing.T) { 39 testCases := []string{ 40 "", 41 "\x01\x02\x03\x04\x05\x06\x07", 42 "foo", 43 "foo\x08\x07\x06\x05\x04\x03\x02", 44 "foo\x16\x07\x06\x05\x04\x03\x02\x01", 45 } 46 for _, tc := range testCases { 47 k := DecodeInternalKey([]byte(tc)) 48 if k.Valid() { 49 t.Errorf("%q is a valid key, want invalid", tc) 50 } 51 // Invalid key kind because the key doesn't have an 8 byte trailer. 52 if k.Kind() == InternalKeyKindInvalid && k.UserKey != nil { 53 t.Errorf("expected nil UserKey after decoding encodedKey=%q", tc) 54 } 55 } 56 } 57 58 func TestInternalKeyComparer(t *testing.T) { 59 // keys are some internal keys, in sorted order. 60 keys := []string{ 61 // The remaining test keys are all valid. 62 "" + "\x01\xff\xff\xff\xff\xff\xff\xff", 63 "" + "\x00\xff\xff\xff\xff\xff\xff\xff", 64 "" + "\x01\x01\x00\x00\x00\x00\x00\x00", 65 "" + "\x00\x01\x00\x00\x00\x00\x00\x00", 66 // Invalid internal keys have no user key, but have trailer "\xff \x00 \x00 \x00 \x00 \x00 \x00 \x00" 67 // i.e. seqNum 0 and kind 255 (InternalKeyKindInvalid). 68 "", 69 "" + "\x01\x00\x00\x00\x00\x00\x00\x00", 70 "" + "\x00\x00\x00\x00\x00\x00\x00\x00", 71 "\x00" + "\x00\x00\x00\x00\x00\x00\x00\x00", 72 "\x00blue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 73 "bl\x00ue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 74 "blue" + "\x01\x11\x00\x00\x00\x00\x00\x00", 75 "blue\x00" + "\x01\x11\x00\x00\x00\x00\x00\x00", 76 "green" + "\xff\x11\x00\x00\x00\x00\x00\x00", 77 "green" + "\x01\x11\x00\x00\x00\x00\x00\x00", 78 "green" + "\x01\x00\x00\x00\x00\x00\x00\x00", 79 "red" + "\x01\xff\xff\xff\xff\xff\xff\xff", 80 "red" + "\x01\x72\x73\x74\x75\x76\x77\x78", 81 "red" + "\x01\x00\x00\x00\x00\x00\x00\x11", 82 "red" + "\x01\x00\x00\x00\x00\x00\x11\x00", 83 "red" + "\x01\x00\x00\x00\x00\x11\x00\x00", 84 "red" + "\x01\x00\x00\x00\x11\x00\x00\x00", 85 "red" + "\x01\x00\x00\x11\x00\x00\x00\x00", 86 "red" + "\x01\x00\x11\x00\x00\x00\x00\x00", 87 "red" + "\x01\x11\x00\x00\x00\x00\x00\x00", 88 "red" + "\x00\x11\x00\x00\x00\x00\x00\x00", 89 "red" + "\x00\x00\x00\x00\x00\x00\x00\x00", 90 "\xfe" + "\x01\xff\xff\xff\xff\xff\xff\xff", 91 "\xfe" + "\x00\x00\x00\x00\x00\x00\x00\x00", 92 "\xff" + "\x01\xff\xff\xff\xff\xff\xff\xff", 93 "\xff" + "\x00\x00\x00\x00\x00\x00\x00\x00", 94 "\xff\x40" + "\x01\xff\xff\xff\xff\xff\xff\xff", 95 "\xff\x40" + "\x00\x00\x00\x00\x00\x00\x00\x00", 96 "\xff\xff" + "\x01\xff\xff\xff\xff\xff\xff\xff", 97 "\xff\xff" + "\x00\x00\x00\x00\x00\x00\x00\x00", 98 } 99 c := DefaultComparer.Compare 100 for i := range keys { 101 for j := range keys { 102 ik := DecodeInternalKey([]byte(keys[i])) 103 jk := DecodeInternalKey([]byte(keys[j])) 104 got := InternalCompare(c, ik, jk) 105 want := 0 106 if i < j { 107 want = -1 108 } else if i > j { 109 want = +1 110 } 111 if got != want { 112 t.Errorf("i=%d, j=%d, keys[i]=%q, keys[j]=%q: got %d, want %d", 113 i, j, keys[i], keys[j], got, want) 114 } 115 } 116 } 117 } 118 119 func TestInternalKeySeparator(t *testing.T) { 120 testCases := []struct { 121 a string 122 b string 123 expected string 124 }{ 125 {"foo.SET.100", "foo.SET.99", "foo.SET.100"}, 126 {"foo.SET.100", "foo.SET.100", "foo.SET.100"}, 127 {"foo.SET.100", "foo.DEL.100", "foo.SET.100"}, 128 {"foo.SET.100", "foo.SET.101", "foo.SET.100"}, 129 {"foo.SET.100", "bar.SET.99", "foo.SET.100"}, 130 {"foo.SET.100", "hello.SET.200", "g.SEPARATOR.72057594037927935"}, 131 {"ABC1AAAAA.SET.100", "ABC2ABB.SET.200", "ABC2.SEPARATOR.72057594037927935"}, 132 {"AAA1AAA.SET.100", "AAA2AA.SET.200", "AAA2.SEPARATOR.72057594037927935"}, 133 {"AAA1AAA.SET.100", "AAA4.SET.200", "AAA2.SEPARATOR.72057594037927935"}, 134 {"AAA1AAA.SET.100", "AAA2.SET.200", "AAA1B.SEPARATOR.72057594037927935"}, 135 {"AAA1AAA.SET.100", "AAA2A.SET.200", "AAA2.SEPARATOR.72057594037927935"}, 136 {"AAA1.SET.100", "AAA2.SET.200", "AAA1.SET.100"}, 137 {"foo.SET.100", "foobar.SET.200", "foo.SET.100"}, 138 {"foobar.SET.100", "foo.SET.200", "foobar.SET.100"}, 139 } 140 d := DefaultComparer 141 for _, c := range testCases { 142 t.Run("", func(t *testing.T) { 143 a := ParseInternalKey(c.a) 144 b := ParseInternalKey(c.b) 145 expected := ParseInternalKey(c.expected) 146 result := a.Separator(d.Compare, d.Separator, nil, b) 147 if cmp := InternalCompare(d.Compare, expected, result); cmp != 0 { 148 t.Fatalf("expected %s, but found %s", expected, result) 149 } 150 }) 151 } 152 } 153 154 func TestIsExclusiveSentinel(t *testing.T) { 155 userKey := []byte("foo") 156 testCases := []struct { 157 name string 158 key InternalKey 159 want bool 160 }{ 161 { 162 name: "rangedel; max seqnum", 163 key: MakeInternalKey(userKey, InternalKeySeqNumMax, InternalKeyKindRangeKeyDelete), 164 want: true, 165 }, 166 { 167 name: "rangedel; non-max seqnum", 168 key: MakeInternalKey(userKey, 42, InternalKeyKindRangeKeyDelete), 169 want: false, 170 }, 171 { 172 name: "rangekeyset; max seqnum", 173 key: MakeInternalKey(userKey, InternalKeySeqNumMax, InternalKeyKindRangeKeySet), 174 want: true, 175 }, 176 { 177 name: "rangekeyset; non-max seqnum", 178 key: MakeInternalKey(userKey, 42, InternalKeyKindRangeKeySet), 179 want: false, 180 }, 181 { 182 name: "rangekeyunset; max seqnum", 183 key: MakeInternalKey(userKey, InternalKeySeqNumMax, InternalKeyKindRangeKeyUnset), 184 want: true, 185 }, 186 { 187 name: "rangekeyunset; non-max seqnum", 188 key: MakeInternalKey(userKey, 42, InternalKeyKindRangeKeyUnset), 189 want: false, 190 }, 191 { 192 name: "rangekeydel; max seqnum", 193 key: MakeInternalKey(userKey, InternalKeySeqNumMax, InternalKeyKindRangeKeyDelete), 194 want: true, 195 }, 196 { 197 name: "rangekeydel; non-max seqnum", 198 key: MakeInternalKey(userKey, 42, InternalKeyKindRangeKeyDelete), 199 want: false, 200 }, 201 { 202 name: "neither rangedel nor rangekey", 203 key: MakeInternalKey(userKey, InternalKeySeqNumMax, InternalKeyKindSet), 204 want: false, 205 }, 206 } 207 208 for _, tc := range testCases { 209 t.Run(tc.name, func(t *testing.T) { 210 got := tc.key.IsExclusiveSentinel() 211 require.Equal(t, tc.want, got) 212 }) 213 } 214 }