github.com/rosedblabs/rosedb/v2@v2.3.7-0.20240423093736-a89ea823e5b9/index/index.go (about) 1 package index 2 3 import "github.com/rosedblabs/wal" 4 5 // Indexer is an interface for indexing key and position. 6 // It is used to store the key and the position of the data in the WAL. 7 // The index will be rebuilt when the database is opened. 8 // You can implement your own indexer by implementing this interface. 9 type Indexer interface { 10 // Put key and position into the index. 11 Put(key []byte, position *wal.ChunkPosition) *wal.ChunkPosition 12 13 // Get the position of the key in the index. 14 Get(key []byte) *wal.ChunkPosition 15 16 // Delete the index of the key. 17 Delete(key []byte) (*wal.ChunkPosition, bool) 18 19 // Size represents the number of keys in the index. 20 Size() int 21 22 // Ascend iterates over items in ascending order and invokes the handler function for each item. 23 // If the handler function returns false, iteration stops. 24 Ascend(handleFn func(key []byte, position *wal.ChunkPosition) (bool, error)) 25 26 // AscendRange iterates in ascending order within [startKey, endKey], invoking handleFn. 27 // Stops if handleFn returns false. 28 AscendRange(startKey, endKey []byte, handleFn func(key []byte, position *wal.ChunkPosition) (bool, error)) 29 30 // AscendGreaterOrEqual iterates in ascending order, starting from key >= given key, 31 // invoking handleFn. Stops if handleFn returns false. 32 AscendGreaterOrEqual(key []byte, handleFn func(key []byte, position *wal.ChunkPosition) (bool, error)) 33 34 // Descend iterates over items in descending order and invokes the handler function for each item. 35 // If the handler function returns false, iteration stops. 36 Descend(handleFn func(key []byte, pos *wal.ChunkPosition) (bool, error)) 37 38 // DescendRange iterates in descending order within [startKey, endKey], invoking handleFn. 39 // Stops if handleFn returns false. 40 DescendRange(startKey, endKey []byte, handleFn func(key []byte, position *wal.ChunkPosition) (bool, error)) 41 42 // DescendLessOrEqual iterates in descending order, starting from key <= given key, 43 // invoking handleFn. Stops if handleFn returns false. 44 DescendLessOrEqual(key []byte, handleFn func(key []byte, position *wal.ChunkPosition) (bool, error)) 45 } 46 47 type IndexerType = byte 48 49 const ( 50 BTree IndexerType = iota 51 ) 52 53 // Change the index type as you implement. 54 var indexType = BTree 55 56 func NewIndexer() Indexer { 57 switch indexType { 58 case BTree: 59 return newBTree() 60 default: 61 panic("unexpected index type") 62 } 63 }