mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
merkledag: add NodeStat object
This commit is contained in:
@ -51,6 +51,20 @@ type Node struct {
|
|||||||
cached mh.Multihash
|
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.
|
// Link represents an IPFS Merkle DAG Link between Nodes.
|
||||||
type Link struct {
|
type Link struct {
|
||||||
// utf string name. should be unique per object
|
// utf string name. should be unique per object
|
||||||
@ -162,6 +176,27 @@ func (n *Node) Size() (uint64, error) {
|
|||||||
return s, nil
|
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.
|
// Multihash hashes the encoded data of this node.
|
||||||
func (n *Node) Multihash() (mh.Multihash, error) {
|
func (n *Node) Multihash() (mh.Multihash, error) {
|
||||||
// Note: Encoded generates the hash and puts it in n.cached.
|
// Note: Encoded generates the hash and puts it in n.cached.
|
||||||
|
@ -85,6 +85,8 @@ func TestNode(t *testing.T) {
|
|||||||
} else {
|
} else {
|
||||||
fmt.Println("key: ", k)
|
fmt.Println("key: ", k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SubtestNodeStat(t, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
printn("beep", n1)
|
printn("beep", n1)
|
||||||
@ -92,6 +94,40 @@ func TestNode(t *testing.T) {
|
|||||||
printn("beep boop", n3)
|
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{}
|
type devZero struct{}
|
||||||
|
|
||||||
func (_ devZero) Read(b []byte) (int, error) {
|
func (_ devZero) Read(b []byte) (int, error) {
|
||||||
|
Reference in New Issue
Block a user