Last active
December 12, 2022 02:16
-
-
Save yihuang/7031416e592f0a2f85201a775269f6a3 to your computer and use it in GitHub Desktop.
Delete IAVL orphan records using compaction filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"os" | |
"github.com/cosmos/gorocksdb" | |
) | |
var _ gorocksdb.CompactionFilter = RangeCompactionFilter{} | |
type RangeCompactionFilter struct { | |
startKey []byte | |
endKey []byte | |
} | |
func (rcf RangeCompactionFilter) Filter(level int, key, val []byte) (remove bool, newVal []byte) { | |
if bytes.Compare(key, rcf.startKey) >= 0 && bytes.Compare(key, rcf.endKey) < 0 { | |
// Delete the key. | |
return true, nil | |
} | |
// Keep the key. | |
return false, nil | |
} | |
func (rcf RangeCompactionFilter) Name() string { | |
return "range" | |
} | |
func DeleteOrphanNodes(dbpath string, store string) { | |
prefix := []byte(fmt.Sprintf("s/k:%s/o", store)) | |
endKey := []byte(fmt.Sprintf("s/k:%s/p", store)) | |
filter := RangeCompactionFilter{ | |
startKey: prefix, | |
endKey: endKey, | |
} | |
// Open a RocksDB database. | |
options := gorocksdb.NewDefaultOptions() | |
options.SetCompactionFilter(filter) | |
db, err := gorocksdb.OpenDb(options, dbpath) | |
if err != nil { | |
panic(err) | |
} | |
defer db.Close() | |
// Perform a manual compaction to apply the filter. | |
db.CompactRange(gorocksdb.Range{Start: filter.startKey, Limit: filter.endKey}) | |
} | |
func main() { | |
DeleteOrphanNodes(os.Args[1], os.Args[2]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment