Skip to content

Instantly share code, notes, and snippets.

@yihuang
Last active December 12, 2022 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yihuang/7031416e592f0a2f85201a775269f6a3 to your computer and use it in GitHub Desktop.
Save yihuang/7031416e592f0a2f85201a775269f6a3 to your computer and use it in GitHub Desktop.
Delete IAVL orphan records using compaction filter
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