1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:30:39 +08:00

merkledag: add NodeStat object

This commit is contained in:
Juan Batiz-Benet
2015-01-07 02:13:44 -08:00
parent f862933c9d
commit 13e79a05e5
2 changed files with 71 additions and 0 deletions

View File

@ -51,6 +51,20 @@ type Node struct {
cached mh.Multihash
}
// NodeStat is a statistics object for a Node. Mostly sizes.
type NodeStat struct {
NumLinks int // number of links in link table
BlockSize int // size of the raw data
LinksSize int // size of the links segment
DataSize int // size of the data segment
CumulativeSize int // cumulatie size of object + all it references
}
func (ns NodeStat) String() string {
f := "NodeStat{NumLinks: %d, BlockSize: %d, LinksSize: %d, DataSize: %d, CumulativeSize: %d}"
return fmt.Sprintf(f, ns.NumLinks, ns.BlockSize, ns.LinksSize, ns.DataSize, ns.CumulativeSize)
}
// Link represents an IPFS Merkle DAG Link between Nodes.
type Link struct {
// utf string name. should be unique per object
@ -162,6 +176,27 @@ func (n *Node) Size() (uint64, error) {
return s, nil
}
// Stat returns statistics on the node.
func (n *Node) Stat() (NodeStat, error) {
enc, err := n.Encoded(false)
if err != nil {
return NodeStat{}, err
}
cumSize, err := n.Size()
if err != nil {
return NodeStat{}, err
}
return NodeStat{
NumLinks: len(n.Links),
BlockSize: len(enc),
LinksSize: len(enc) - len(n.Data), // includes framing.
DataSize: len(n.Data),
CumulativeSize: int(cumSize),
}, nil
}
// Multihash hashes the encoded data of this node.
func (n *Node) Multihash() (mh.Multihash, error) {
// Note: Encoded generates the hash and puts it in n.cached.

View File

@ -85,6 +85,8 @@ func TestNode(t *testing.T) {
} else {
fmt.Println("key: ", k)
}
SubtestNodeStat(t, n)
}
printn("beep", n1)
@ -92,6 +94,40 @@ func TestNode(t *testing.T) {
printn("beep boop", n3)
}
func SubtestNodeStat(t *testing.T, n *Node) {
enc, err := n.Encoded(true)
if err != nil {
t.Error("n.Encoded(true) failed")
return
}
cumSize, err := n.Size()
if err != nil {
t.Error("n.Size() failed")
return
}
expected := NodeStat{
NumLinks: len(n.Links),
BlockSize: len(enc),
LinksSize: len(enc) - len(n.Data), // includes framing.
DataSize: len(n.Data),
CumulativeSize: int(cumSize),
}
actual, err := n.Stat()
if err != nil {
t.Error("n.Stat() failed")
return
}
if expected != actual {
t.Error("n.Stat incorrect.\nexpect: %s\nactual: %s", expected, actual)
} else {
fmt.Printf("n.Stat correct: %s\n", actual)
}
}
type devZero struct{}
func (_ devZero) Read(b []byte) (int, error) {