compact.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package command
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/seaweedfs/weed/storage"
  5. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. )
  8. func init() {
  9. cmdCompact.Run = runCompact // break init cycle
  10. }
  11. var cmdCompact = &Command{
  12. UsageLine: "compact -dir=/tmp -volumeId=234",
  13. Short: "run weed tool compact on volume file",
  14. Long: `Force an compaction to remove deleted files from volume files.
  15. The compacted .dat file is stored as .cpd file.
  16. The compacted .idx file is stored as .cpx file.
  17. For method=0, it compacts based on the .dat file, works if .idx file is corrupted.
  18. For method=1, it compacts based on the .idx file, works if deletion happened but not written to .dat files.
  19. `,
  20. }
  21. var (
  22. compactVolumePath = cmdCompact.Flag.String("dir", ".", "data directory to store files")
  23. compactVolumeCollection = cmdCompact.Flag.String("collection", "", "volume collection name")
  24. compactVolumeId = cmdCompact.Flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir.")
  25. compactMethod = cmdCompact.Flag.Int("method", 0, "option to choose which compact method. use 0 or 1.")
  26. compactVolumePreallocate = cmdCompact.Flag.Int64("preallocateMB", 0, "preallocate volume disk space")
  27. )
  28. func runCompact(cmd *Command, args []string) bool {
  29. if *compactVolumeId == -1 {
  30. return false
  31. }
  32. preallocate := *compactVolumePreallocate * (1 << 20)
  33. vid := needle.VolumeId(*compactVolumeId)
  34. v, err := storage.NewVolume(util.ResolvePath(*compactVolumePath), *compactVolumeCollection, vid,
  35. storage.NeedleMapInMemory, nil, nil, preallocate, 0)
  36. if err != nil {
  37. glog.Fatalf("Load Volume [ERROR] %s\n", err)
  38. }
  39. if *compactMethod == 0 {
  40. if err = v.Compact(preallocate, 0); err != nil {
  41. glog.Fatalf("Compact Volume [ERROR] %s\n", err)
  42. }
  43. } else {
  44. if err = v.Compact2(preallocate, 0); err != nil {
  45. glog.Fatalf("Compact Volume [ERROR] %s\n", err)
  46. }
  47. }
  48. return true
  49. }