2015-10-23 19:02:38 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-10-23 19:02:38 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2016-07-04 10:40:29 +00:00
|
|
|
"encoding/binary"
|
2015-10-23 19:02:38 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SizedElement struct {
|
|
|
|
key string
|
|
|
|
size int
|
|
|
|
}
|
|
|
|
|
|
|
|
type ElementHeap []SizedElement
|
|
|
|
|
|
|
|
func (h ElementHeap) Len() int { return len(h) }
|
|
|
|
func (h ElementHeap) Less(i, j int) bool { return h[i].size > h[j].size }
|
|
|
|
func (h ElementHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
|
|
|
|
|
|
|
func (h *ElementHeap) Push(x interface{}) {
|
|
|
|
*h = append(*h, x.(SizedElement))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ElementHeap) Pop() interface{} {
|
|
|
|
old := *h
|
|
|
|
n := len(old)
|
|
|
|
x := old[n-1]
|
|
|
|
*h = old[0 : n-1]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func dumpsize(ldb *db.Lowlevel) {
|
2015-10-23 19:02:38 +00:00
|
|
|
h := &ElementHeap{}
|
|
|
|
heap.Init(h)
|
|
|
|
|
|
|
|
it := ldb.NewIterator(nil, nil)
|
|
|
|
var ele SizedElement
|
|
|
|
for it.Next() {
|
|
|
|
key := it.Key()
|
|
|
|
switch key[0] {
|
|
|
|
case db.KeyTypeDevice:
|
2016-07-04 10:40:29 +00:00
|
|
|
folder := binary.BigEndian.Uint32(key[1:])
|
|
|
|
device := binary.BigEndian.Uint32(key[1+4:])
|
|
|
|
name := nulString(key[1+4+4:])
|
|
|
|
ele.key = fmt.Sprintf("DEVICE:%d:%d:%s", folder, device, name)
|
2015-10-23 19:02:38 +00:00
|
|
|
|
|
|
|
case db.KeyTypeGlobal:
|
2016-07-04 10:40:29 +00:00
|
|
|
folder := binary.BigEndian.Uint32(key[1:])
|
|
|
|
name := nulString(key[1+4:])
|
|
|
|
ele.key = fmt.Sprintf("GLOBAL:%d:%s", folder, name)
|
2015-10-23 19:02:38 +00:00
|
|
|
|
|
|
|
case db.KeyTypeBlock:
|
2016-07-04 10:40:29 +00:00
|
|
|
folder := binary.BigEndian.Uint32(key[1:])
|
|
|
|
hash := key[1+4 : 1+4+32]
|
|
|
|
name := nulString(key[1+4+32:])
|
|
|
|
ele.key = fmt.Sprintf("BLOCK:%d:%x:%s", folder, hash, name)
|
2015-10-23 19:02:38 +00:00
|
|
|
|
|
|
|
case db.KeyTypeDeviceStatistic:
|
|
|
|
ele.key = fmt.Sprintf("DEVICESTATS:%s", key[1:])
|
|
|
|
|
|
|
|
case db.KeyTypeFolderStatistic:
|
|
|
|
ele.key = fmt.Sprintf("FOLDERSTATS:%s", key[1:])
|
|
|
|
|
|
|
|
case db.KeyTypeVirtualMtime:
|
|
|
|
ele.key = fmt.Sprintf("MTIME:%s", key[1:])
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case db.KeyTypeFolderIdx:
|
|
|
|
id := binary.BigEndian.Uint32(key[1:])
|
|
|
|
ele.key = fmt.Sprintf("FOLDERIDX:%d", id)
|
|
|
|
|
|
|
|
case db.KeyTypeDeviceIdx:
|
|
|
|
id := binary.BigEndian.Uint32(key[1:])
|
|
|
|
ele.key = fmt.Sprintf("DEVICEIDX:%d", id)
|
|
|
|
|
2015-10-23 19:02:38 +00:00
|
|
|
default:
|
|
|
|
ele.key = fmt.Sprintf("UNKNOWN:%x", key)
|
|
|
|
}
|
|
|
|
ele.size = len(it.Value())
|
|
|
|
heap.Push(h, ele)
|
|
|
|
}
|
|
|
|
|
|
|
|
for h.Len() > 0 {
|
|
|
|
ele = heap.Pop(h).(SizedElement)
|
|
|
|
fmt.Println(ele.key, ele.size)
|
|
|
|
}
|
|
|
|
}
|