github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/index/btree_test.go (about) 1 package index 2 3 import ( 4 "bytes" 5 "fmt" 6 "testing" 7 8 "github.com/rosedblabs/wal" 9 ) 10 11 func TestMemoryBTree_Put_Get(t *testing.T) { 12 mt := newBTree() 13 w, _ := wal.Open(wal.DefaultOptions) 14 15 key := []byte("testKey") 16 chunkPosition, _ := w.Write([]byte("some data 1")) 17 18 // Test Put 19 oldPos := mt.Put(key, chunkPosition) 20 if oldPos != nil { 21 t.Fatalf("expected nil, got %+v", oldPos) 22 } 23 24 // Test Get 25 gotPos := mt.Get(key) 26 if chunkPosition.ChunkOffset != gotPos.ChunkOffset { 27 t.Fatalf("expected %+v, got %+v", chunkPosition, gotPos) 28 } 29 } 30 31 func TestMemoryBTree_Delete(t *testing.T) { 32 mt := newBTree() 33 w, _ := wal.Open(wal.DefaultOptions) 34 35 key := []byte("testKey") 36 chunkPosition, _ := w.Write([]byte("some data 2")) 37 38 mt.Put(key, chunkPosition) 39 40 // Test Delete 41 delPos, ok := mt.Delete(key) 42 if !ok { 43 t.Fatal("expected item to be deleted") 44 } 45 if chunkPosition.ChunkOffset != delPos.ChunkOffset { 46 t.Fatalf("expected %+v, got %+v", chunkPosition, delPos) 47 } 48 49 // Ensure the key is deleted 50 if mt.Get(key) != nil { 51 t.Fatal("expected nil, got value") 52 } 53 } 54 55 func TestMemoryBTree_Size(t *testing.T) { 56 mt := newBTree() 57 58 if mt.Size() != 0 { 59 t.Fatalf("expected size to be 0, got %d", mt.Size()) 60 } 61 62 w, _ := wal.Open(wal.DefaultOptions) 63 key := []byte("testKey") 64 chunkPosition, _ := w.Write([]byte("some data 3")) 65 66 mt.Put(key, chunkPosition) 67 68 if mt.Size() != 1 { 69 t.Fatalf("expected size to be 1, got %d", mt.Size()) 70 } 71 } 72 73 func TestMemoryBTree_Ascend_Descend(t *testing.T) { 74 mt := newBTree() 75 w, _ := wal.Open(wal.DefaultOptions) 76 77 data := map[string][]byte{ 78 "apple": []byte("some data 4"), 79 "banana": []byte("some data 5"), 80 "cherry": []byte("some data 6"), 81 } 82 83 positionMap := make(map[string]*wal.ChunkPosition) 84 85 for k, v := range data { 86 chunkPosition, _ := w.Write(v) 87 positionMap[k] = chunkPosition 88 mt.Put([]byte(k), chunkPosition) 89 } 90 91 // Test Ascend 92 prevKey := "" 93 94 // Define the Ascend handler function 95 ascendHandler := func(key []byte, pos *wal.ChunkPosition) (bool, error) { 96 if prevKey != "" && bytes.Compare([]byte(prevKey), key) >= 0 { 97 return false, fmt.Errorf("items are not in ascending order") 98 } 99 expectedPos := positionMap[string(key)] 100 if expectedPos.ChunkOffset != pos.ChunkOffset { 101 return false, fmt.Errorf("expected position %+v, got %+v", expectedPos, pos) 102 } 103 prevKey = string(key) 104 return true, nil 105 } 106 107 mt.Ascend(ascendHandler) 108 109 // Define the Descend handler function 110 descendHandler := func(key []byte, pos *wal.ChunkPosition) (bool, error) { 111 if bytes.Compare([]byte(prevKey), key) <= 0 { 112 return false, fmt.Errorf("items are not in descending order") 113 } 114 expectedPos := positionMap[string(key)] 115 if expectedPos.ChunkOffset != pos.ChunkOffset { 116 return false, fmt.Errorf("expected position %+v, got %+v", expectedPos, pos) 117 } 118 prevKey = string(key) 119 return true, nil 120 } 121 122 // Test Descend 123 prevKey = "zzzzzz" 124 mt.Descend(descendHandler) 125 } 126 127 func TestMemoryBTree_AscendRange_DescendRange(t *testing.T) { 128 mt := newBTree() 129 w, _ := wal.Open(wal.DefaultOptions) 130 131 data := map[string][]byte{ 132 "apple": []byte("some data 1"), 133 "banana": []byte("some data 2"), 134 "cherry": []byte("some data 3"), 135 "date": []byte("some data 4"), 136 "grape": []byte("some data 5"), 137 } 138 139 positionMap := make(map[string]*wal.ChunkPosition) 140 141 for k, v := range data { 142 chunkPosition, _ := w.Write(v) 143 positionMap[k] = chunkPosition 144 mt.Put([]byte(k), chunkPosition) 145 } 146 147 // Test AscendRange 148 fmt.Println("Testing AscendRange:") 149 mt.AscendRange([]byte("banana"), []byte("grape"), func(key []byte, pos *wal.ChunkPosition) (bool, error) { 150 fmt.Printf("Key: %s, Position: %+v\n", key, pos) 151 return true, nil 152 }) 153 154 // Test DescendRange 155 fmt.Println("Testing DescendRange:") 156 mt.DescendRange([]byte("cherry"), []byte("date"), func(key []byte, pos *wal.ChunkPosition) (bool, error) { 157 fmt.Printf("Key: %s, Position: %+v\n", key, pos) 158 return true, nil 159 }) 160 } 161 162 func TestMemoryBTree_AscendGreaterOrEqual_DescendLessOrEqual(t *testing.T) { 163 mt := newBTree() 164 w, _ := wal.Open(wal.DefaultOptions) 165 166 data := map[string][]byte{ 167 "apple": []byte("some data 1"), 168 "banana": []byte("some data 2"), 169 "cherry": []byte("some data 3"), 170 "date": []byte("some data 4"), 171 "grape": []byte("some data 5"), 172 } 173 174 positionMap := make(map[string]*wal.ChunkPosition) 175 176 for k, v := range data { 177 chunkPosition, _ := w.Write(v) 178 positionMap[k] = chunkPosition 179 mt.Put([]byte(k), chunkPosition) 180 } 181 182 // Test AscendGreaterOrEqual 183 fmt.Println("Testing AscendGreaterOrEqual:") 184 mt.AscendGreaterOrEqual([]byte("cherry"), func(key []byte, pos *wal.ChunkPosition) (bool, error) { 185 fmt.Printf("Key: %s, Position: %+v\n", key, pos) 186 return true, nil 187 }) 188 189 // Test DescendLessOrEqual 190 fmt.Println("Testing DescendLessOrEqual:") 191 mt.DescendLessOrEqual([]byte("date"), func(key []byte, pos *wal.ChunkPosition) (bool, error) { 192 fmt.Printf("Key: %s, Position: %+v\n", key, pos) 193 return true, nil 194 }) 195 }