merkledag: add NodeStat object
Juan Batiz-Benet committed
Jan 7, 2015 at 02:13 UTC
13e79a05e541ecfa71ff2390c78005076ba50e9a
2 files changed
+71
merkledag/merkledag.go
+35
@@ -51,6 +51,20 @@ type Node struct {
51
cached mh.Multihash
52
}
53
54
+// NodeStat is a statistics object for a Node. Mostly sizes.
55
+type NodeStat struct {
56
+ NumLinks int // number of links in link table
57
+ BlockSize int // size of the raw data
58
+ LinksSize int // size of the links segment
59
+ DataSize int // size of the data segment
60
+ CumulativeSize int // cumulatie size of object + all it references
61
+}
62
+
63
+func (ns NodeStat) String() string {
64
+ f := "NodeStat{NumLinks: %d, BlockSize: %d, LinksSize: %d, DataSize: %d, CumulativeSize: %d}"
65
+ return fmt.Sprintf(f, ns.NumLinks, ns.BlockSize, ns.LinksSize, ns.DataSize, ns.CumulativeSize)
66
+}
67
+
68
// Link represents an IPFS Merkle DAG Link between Nodes.
69
type Link struct {
70
// utf string name. should be unique per object
@@ -162,6 +176,27 @@ func (n *Node) Size() (uint64, error) {
176
return s, nil
177
}
178
179
+// Stat returns statistics on the node.
180
+func (n *Node) Stat() (NodeStat, error) {
181
+ enc, err := n.Encoded(false)
182
+ if err != nil {
183
+ return NodeStat{}, err
184
+ }
185
+
186
+ cumSize, err := n.Size()
187
+ if err != nil {
188
+ return NodeStat{}, err
189
+ }
190
+
191
+ return NodeStat{
192
+ NumLinks: len(n.Links),
193
+ BlockSize: len(enc),
194
+ LinksSize: len(enc) - len(n.Data), // includes framing.
195
+ DataSize: len(n.Data),
196
+ CumulativeSize: int(cumSize),
197
+ }, nil
198
+}
199
+
200
// Multihash hashes the encoded data of this node.
201
func (n *Node) Multihash() (mh.Multihash, error) {
202
// Note: Encoded generates the hash and puts it in n.cached.
merkledag/merkledag_test.go
+36
@@ -85,6 +85,8 @@ func TestNode(t *testing.T) {
85
} else {
86
fmt.Println("key: ", k)
87
}
88
+
89
+ SubtestNodeStat(t, n)
90
}
91
92
printn("beep", n1)
@@ -92,6 +94,40 @@ func TestNode(t *testing.T) {
94
printn("beep boop", n3)
95
}
96
97
+func SubtestNodeStat(t *testing.T, n *Node) {
98
+ enc, err := n.Encoded(true)
99
+ if err != nil {
100
+ t.Error("n.Encoded(true) failed")
101
+ return
102
+ }
103
+
104
+ cumSize, err := n.Size()
105
+ if err != nil {
106
+ t.Error("n.Size() failed")
107
+ return
108
+ }
109
+
110
+ expected := NodeStat{
111
+ NumLinks: len(n.Links),
112
+ BlockSize: len(enc),
113
+ LinksSize: len(enc) - len(n.Data), // includes framing.
114
+ DataSize: len(n.Data),
115
+ CumulativeSize: int(cumSize),
116
+ }
117
+
118
+ actual, err := n.Stat()
119
+ if err != nil {
120
+ t.Error("n.Stat() failed")
121
+ return
122
+ }
123
+
124
+ if expected != actual {
125
+ t.Error("n.Stat incorrect.\nexpect: %s\nactual: %s", expected, actual)
126
+ } else {
127
+ fmt.Printf("n.Stat correct: %s\n", actual)
128
+ }
129
+}
130
+
131
type devZero struct{}
132
133
func (_ devZero) Read(b []byte) (int, error) {