dumpsize.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "container/heap"
  9. "encoding/binary"
  10. "fmt"
  11. "github.com/syncthing/syncthing/lib/db"
  12. )
  13. type SizedElement struct {
  14. key string
  15. size int
  16. }
  17. type ElementHeap []SizedElement
  18. func (h ElementHeap) Len() int { return len(h) }
  19. func (h ElementHeap) Less(i, j int) bool { return h[i].size > h[j].size }
  20. func (h ElementHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  21. func (h *ElementHeap) Push(x interface{}) {
  22. *h = append(*h, x.(SizedElement))
  23. }
  24. func (h *ElementHeap) Pop() interface{} {
  25. old := *h
  26. n := len(old)
  27. x := old[n-1]
  28. *h = old[0 : n-1]
  29. return x
  30. }
  31. func dumpsize(ldb *db.Instance) {
  32. h := &ElementHeap{}
  33. heap.Init(h)
  34. it := ldb.NewIterator(nil, nil)
  35. var ele SizedElement
  36. for it.Next() {
  37. key := it.Key()
  38. switch key[0] {
  39. case db.KeyTypeDevice:
  40. folder := binary.BigEndian.Uint32(key[1:])
  41. device := binary.BigEndian.Uint32(key[1+4:])
  42. name := nulString(key[1+4+4:])
  43. ele.key = fmt.Sprintf("DEVICE:%d:%d:%s", folder, device, name)
  44. case db.KeyTypeGlobal:
  45. folder := binary.BigEndian.Uint32(key[1:])
  46. name := nulString(key[1+4:])
  47. ele.key = fmt.Sprintf("GLOBAL:%d:%s", folder, name)
  48. case db.KeyTypeBlock:
  49. folder := binary.BigEndian.Uint32(key[1:])
  50. hash := key[1+4 : 1+4+32]
  51. name := nulString(key[1+4+32:])
  52. ele.key = fmt.Sprintf("BLOCK:%d:%x:%s", folder, hash, name)
  53. case db.KeyTypeDeviceStatistic:
  54. ele.key = fmt.Sprintf("DEVICESTATS:%s", key[1:])
  55. case db.KeyTypeFolderStatistic:
  56. ele.key = fmt.Sprintf("FOLDERSTATS:%s", key[1:])
  57. case db.KeyTypeVirtualMtime:
  58. ele.key = fmt.Sprintf("MTIME:%s", key[1:])
  59. case db.KeyTypeFolderIdx:
  60. id := binary.BigEndian.Uint32(key[1:])
  61. ele.key = fmt.Sprintf("FOLDERIDX:%d", id)
  62. case db.KeyTypeDeviceIdx:
  63. id := binary.BigEndian.Uint32(key[1:])
  64. ele.key = fmt.Sprintf("DEVICEIDX:%d", id)
  65. default:
  66. ele.key = fmt.Sprintf("UNKNOWN:%x", key)
  67. }
  68. ele.size = len(it.Value())
  69. heap.Push(h, ele)
  70. }
  71. for h.Len() > 0 {
  72. ele = heap.Pop(h).(SizedElement)
  73. fmt.Println(ele.key, ele.size)
  74. }
  75. }