compact.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. `,
  17. }
  18. var (
  19. compactVolumePath = cmdCompact.Flag.String("dir", ".", "data directory to store files")
  20. compactVolumeCollection = cmdCompact.Flag.String("collection", "", "volume collection name")
  21. compactVolumeId = cmdCompact.Flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir.")
  22. compactMethod = cmdCompact.Flag.Int("method", 0, "option to choose which compact method. use 0 or 1.")
  23. compactVolumePreallocate = cmdCompact.Flag.Int64("preallocateMB", 0, "preallocate volume disk space")
  24. )
  25. func runCompact(cmd *Command, args []string) bool {
  26. if *compactVolumeId == -1 {
  27. return false
  28. }
  29. preallocate := *compactVolumePreallocate * (1 << 20)
  30. vid := needle.VolumeId(*compactVolumeId)
  31. v, err := storage.NewVolume(*compactVolumePath, *compactVolumeCollection, vid,
  32. storage.NeedleMapInMemory, nil, nil, preallocate, 0)
  33. if err != nil {
  34. glog.Fatalf("Load Volume [ERROR] %s\n", err)
  35. }
  36. if *compactMethod == 0 {
  37. if err = v.Compact(preallocate, 0); err != nil {
  38. glog.Fatalf("Compact Volume [ERROR] %s\n", err)
  39. }
  40. } else {
  41. if err = v.Compact2(); err != nil {
  42. glog.Fatalf("Compact Volume [ERROR] %s\n", err)
  43. }
  44. }
  45. return true
  46. }