Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-merkledag
Commits
43f8f601
Commit
43f8f601
authored
Jan 07, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
merkledag: add NodeStat object
parent
1a5c8cc4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
0 deletions
+71
-0
merkledag.go
merkledag.go
+35
-0
merkledag_test.go
merkledag_test.go
+36
-0
No files found.
merkledag.go
View file @
43f8f601
...
...
@@ -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.
...
...
merkledag_test.go
View file @
43f8f601
...
...
@@ -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.
\n
expect: %s
\n
actual: %s"
,
expected
,
actual
)
}
else
{
fmt
.
Printf
(
"n.Stat correct: %s
\n
"
,
actual
)
}
}
type
devZero
struct
{}
func
(
_
devZero
)
Read
(
b
[]
byte
)
(
int
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment