compact_leveldb.go 828 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "github.com/syndtr/goleveldb/leveldb"
  6. "github.com/syndtr/goleveldb/leveldb/errors"
  7. "github.com/syndtr/goleveldb/leveldb/opt"
  8. "github.com/syndtr/goleveldb/leveldb/util"
  9. )
  10. var (
  11. dir = flag.String("dir", ".", "data directory to store leveldb files")
  12. )
  13. func main() {
  14. flag.Parse()
  15. opts := &opt.Options{
  16. BlockCacheCapacity: 32 * 1024 * 1024, // default value is 8MiB
  17. WriteBuffer: 16 * 1024 * 1024, // default value is 4MiB
  18. CompactionTableSizeMultiplier: 10,
  19. OpenFilesCacheCapacity: -1,
  20. }
  21. db, err := leveldb.OpenFile(*dir, opts)
  22. if errors.IsCorrupted(err) {
  23. db, err = leveldb.RecoverFile(*dir, opts)
  24. }
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. defer db.Close()
  29. if err := db.CompactRange(util.Range{}); err != nil {
  30. log.Fatal(err)
  31. }
  32. }