Commit 43f8f601 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

merkledag: add NodeStat object

parent 1a5c8cc4
......@@ -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.
......
......@@ -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) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment