compact.go 1.6 KB

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