compact.go 1.9 KB

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