dumpsize.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "fmt"
  10. "github.com/syncthing/syncthing/lib/db"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. "github.com/syndtr/goleveldb/leveldb"
  13. )
  14. type SizedElement struct {
  15. key string
  16. size int
  17. }
  18. type ElementHeap []SizedElement
  19. func (h ElementHeap) Len() int { return len(h) }
  20. func (h ElementHeap) Less(i, j int) bool { return h[i].size > h[j].size }
  21. func (h ElementHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  22. func (h *ElementHeap) Push(x interface{}) {
  23. *h = append(*h, x.(SizedElement))
  24. }
  25. func (h *ElementHeap) Pop() interface{} {
  26. old := *h
  27. n := len(old)
  28. x := old[n-1]
  29. *h = old[0 : n-1]
  30. return x
  31. }
  32. func dumpsize(ldb *leveldb.DB) {
  33. h := &ElementHeap{}
  34. heap.Init(h)
  35. it := ldb.NewIterator(nil, nil)
  36. var dev protocol.DeviceID
  37. var ele SizedElement
  38. for it.Next() {
  39. key := it.Key()
  40. switch key[0] {
  41. case db.KeyTypeDevice:
  42. folder := nulString(key[1 : 1+64])
  43. devBytes := key[1+64 : 1+64+32]
  44. name := nulString(key[1+64+32:])
  45. copy(dev[:], devBytes)
  46. ele.key = fmt.Sprintf("DEVICE:%s:%s:%s", dev, folder, name)
  47. case db.KeyTypeGlobal:
  48. folder := nulString(key[1 : 1+64])
  49. name := nulString(key[1+64:])
  50. ele.key = fmt.Sprintf("GLOBAL:%s:%s", folder, name)
  51. case db.KeyTypeBlock:
  52. folder := nulString(key[1 : 1+64])
  53. hash := key[1+64 : 1+64+32]
  54. name := nulString(key[1+64+32:])
  55. ele.key = fmt.Sprintf("BLOCK:%s:%x:%s", folder, hash, name)
  56. case db.KeyTypeDeviceStatistic:
  57. ele.key = fmt.Sprintf("DEVICESTATS:%s", key[1:])
  58. case db.KeyTypeFolderStatistic:
  59. ele.key = fmt.Sprintf("FOLDERSTATS:%s", key[1:])
  60. case db.KeyTypeVirtualMtime:
  61. ele.key = fmt.Sprintf("MTIME:%s", key[1:])
  62. default:
  63. ele.key = fmt.Sprintf("UNKNOWN:%x", key)
  64. }
  65. ele.size = len(it.Value())
  66. heap.Push(h, ele)
  67. }
  68. for h.Len() > 0 {
  69. ele = heap.Pop(h).(SizedElement)
  70. fmt.Println(ele.key, ele.size)
  71. }
  72. }