You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.4 KiB

  1. package main
  2. import (
  3. "flag"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/storage"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. "os"
  8. "path/filepath"
  9. "fmt"
  10. )
  11. var (
  12. volumePath = flag.String("dir", "/tmp", "data directory to store files")
  13. volumeCollection = flag.String("collection", "", "the volume collection name")
  14. volumeId = flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
  15. )
  16. func Checksum(n* needle.Needle) string {
  17. return fmt.Sprintf("%s%x", n.Id, n.Cookie)
  18. }
  19. type VolumeFileScanner4SeeDat struct {
  20. version needle.Version
  21. block storage.SuperBlock
  22. dir string
  23. hashes map[string]bool
  24. dat * os.File
  25. }
  26. func (scanner *VolumeFileScanner4SeeDat) VisitSuperBlock(superBlock storage.SuperBlock) error {
  27. scanner.version = superBlock.Version()
  28. scanner.block = superBlock
  29. return nil
  30. }
  31. func (scanner *VolumeFileScanner4SeeDat) ReadNeedleBody() bool {
  32. return true
  33. }
  34. func (scanner *VolumeFileScanner4SeeDat) VisitNeedle(n *needle.Needle, offset int64) error {
  35. if scanner.dat == nil {
  36. newDatFile, err := os.Create(filepath.Join(*volumePath, "dat_fixed"))
  37. if err != nil {
  38. glog.Fatalf("Write New Volume Data %v", err)
  39. }
  40. scanner.dat = newDatFile
  41. scanner.dat.Write(scanner.block.Bytes())
  42. }
  43. checksum := Checksum(n)
  44. if scanner.hashes[checksum] {
  45. glog.V(0).Infof("duplicate checksum:%s fid:%d,%s%x @ offset:%d", checksum, *volumeId, n.Id, n.Cookie, offset)
  46. return nil
  47. }
  48. scanner.hashes[checksum] = true
  49. _, s, _, e := n.Append(scanner.dat, scanner.version)
  50. fmt.Printf("size %d error %v\n", s, e)
  51. return nil
  52. }
  53. func main() {
  54. flag.Parse()
  55. vid := needle.VolumeId(*volumeId)
  56. outpath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  57. scanner := &VolumeFileScanner4SeeDat{
  58. dir: filepath.Join(outpath, "out"),
  59. hashes: map[string]bool{},
  60. }
  61. if _, err := os.Stat(scanner.dir); err != nil {
  62. if err := os.MkdirAll(scanner.dir, os.ModePerm); err != nil {
  63. glog.Fatalf("could not create output dir : %s", err)
  64. }
  65. }
  66. err := storage.ScanVolumeFile(*volumePath, *volumeCollection, vid, storage.NeedleMapInMemory, scanner)
  67. if err != nil {
  68. glog.Fatalf("Reading Volume File [ERROR] %s\n", err)
  69. }
  70. }