dumpsize.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "container/heap"
  9. "encoding/binary"
  10. "fmt"
  11. "log"
  12. "github.com/syncthing/syncthing/lib/db"
  13. "github.com/syncthing/syncthing/lib/db/backend"
  14. )
  15. type SizedElement struct {
  16. key string
  17. size int
  18. }
  19. type ElementHeap []SizedElement
  20. func (h ElementHeap) Len() int { return len(h) }
  21. func (h ElementHeap) Less(i, j int) bool { return h[i].size > h[j].size }
  22. func (h ElementHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  23. func (h *ElementHeap) Push(x interface{}) {
  24. *h = append(*h, x.(SizedElement))
  25. }
  26. func (h *ElementHeap) Pop() interface{} {
  27. old := *h
  28. n := len(old)
  29. x := old[n-1]
  30. *h = old[0 : n-1]
  31. return x
  32. }
  33. func dumpsize(ldb backend.Backend) {
  34. h := &ElementHeap{}
  35. heap.Init(h)
  36. it, err := ldb.NewPrefixIterator(nil)
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. var ele SizedElement
  41. for it.Next() {
  42. key := it.Key()
  43. switch key[0] {
  44. case db.KeyTypeDevice:
  45. folder := binary.BigEndian.Uint32(key[1:])
  46. device := binary.BigEndian.Uint32(key[1+4:])
  47. name := nulString(key[1+4+4:])
  48. ele.key = fmt.Sprintf("DEVICE:%d:%d:%s", folder, device, name)
  49. case db.KeyTypeGlobal:
  50. folder := binary.BigEndian.Uint32(key[1:])
  51. name := nulString(key[1+4:])
  52. ele.key = fmt.Sprintf("GLOBAL:%d:%s", folder, name)
  53. case db.KeyTypeBlock:
  54. folder := binary.BigEndian.Uint32(key[1:])
  55. hash := key[1+4 : 1+4+32]
  56. name := nulString(key[1+4+32:])
  57. ele.key = fmt.Sprintf("BLOCK:%d:%x:%s", folder, hash, name)
  58. case db.KeyTypeDeviceStatistic:
  59. ele.key = fmt.Sprintf("DEVICESTATS:%s", key[1:])
  60. case db.KeyTypeFolderStatistic:
  61. ele.key = fmt.Sprintf("FOLDERSTATS:%s", key[1:])
  62. case db.KeyTypeVirtualMtime:
  63. ele.key = fmt.Sprintf("MTIME:%s", key[1:])
  64. case db.KeyTypeFolderIdx:
  65. id := binary.BigEndian.Uint32(key[1:])
  66. ele.key = fmt.Sprintf("FOLDERIDX:%d", id)
  67. case db.KeyTypeDeviceIdx:
  68. id := binary.BigEndian.Uint32(key[1:])
  69. ele.key = fmt.Sprintf("DEVICEIDX:%d", id)
  70. default:
  71. ele.key = fmt.Sprintf("UNKNOWN:%x", key)
  72. }
  73. ele.size = len(it.Value())
  74. heap.Push(h, ele)
  75. }
  76. for h.Len() > 0 {
  77. ele = heap.Pop(h).(SizedElement)
  78. fmt.Println(ele.key, ele.size)
  79. }
  80. }