filechunks2_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package filer
  2. import (
  3. "sort"
  4. "testing"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. )
  8. func TestCompactFileChunksRealCase(t *testing.T) {
  9. chunks := []*filer_pb.FileChunk{
  10. {FileId: "2,512f31f2c0700a", Offset: 0, Size: 25 - 0, Mtime: 5320497},
  11. {FileId: "6,512f2c2e24e9e8", Offset: 868352, Size: 917585 - 868352, Mtime: 5320492},
  12. {FileId: "7,514468dd5954ca", Offset: 884736, Size: 901120 - 884736, Mtime: 5325928},
  13. {FileId: "5,5144463173fe77", Offset: 917504, Size: 2297856 - 917504, Mtime: 5325894},
  14. {FileId: "4,51444c7ab54e2d", Offset: 2301952, Size: 2367488 - 2301952, Mtime: 5325900},
  15. {FileId: "4,514450e643ad22", Offset: 2371584, Size: 2420736 - 2371584, Mtime: 5325904},
  16. {FileId: "6,514456a5e9e4d7", Offset: 2449408, Size: 2490368 - 2449408, Mtime: 5325910},
  17. {FileId: "3,51444f8d53eebe", Offset: 2494464, Size: 2555904 - 2494464, Mtime: 5325903},
  18. {FileId: "4,5144578b097c7e", Offset: 2560000, Size: 2596864 - 2560000, Mtime: 5325911},
  19. {FileId: "3,51445500b6b4ac", Offset: 2637824, Size: 2678784 - 2637824, Mtime: 5325909},
  20. {FileId: "1,51446285e52a61", Offset: 2695168, Size: 2715648 - 2695168, Mtime: 5325922},
  21. }
  22. printChunks("before", chunks)
  23. compacted, garbage := CompactFileChunks(nil, chunks)
  24. printChunks("compacted", compacted)
  25. printChunks("garbage", garbage)
  26. }
  27. func printChunks(name string, chunks []*filer_pb.FileChunk) {
  28. sort.Slice(chunks, func(i, j int) bool {
  29. if chunks[i].Offset == chunks[j].Offset {
  30. return chunks[i].Mtime < chunks[j].Mtime
  31. }
  32. return chunks[i].Offset < chunks[j].Offset
  33. })
  34. for _, chunk := range chunks {
  35. glog.V(0).Infof("%s chunk %s [%10d,%10d)", name, chunk.GetFileIdString(), chunk.Offset, chunk.Offset+int64(chunk.Size))
  36. }
  37. }