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
dcdb1e32
Commit
dcdb1e32
authored
Jul 04, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
coding with protobuf
parent
75719112
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
15 deletions
+22
-15
coding.go
coding.go
+11
-10
merkledag.go
merkledag.go
+10
-4
node.proto
node.proto
+1
-1
No files found.
coding.go
View file @
dcdb1e32
...
@@ -10,18 +10,19 @@ import (
...
@@ -10,18 +10,19 @@ import (
func
(
n
*
Node
)
Unmarshal
(
encoded
[]
byte
)
error
{
func
(
n
*
Node
)
Unmarshal
(
encoded
[]
byte
)
error
{
var
pbn
PBNode
var
pbn
PBNode
if
err
:=
pbn
.
Unmarshal
(
encoded
)
,
err
!=
nil
{
if
err
:=
pbn
.
Unmarshal
(
encoded
)
;
err
!=
nil
{
return
fmt
.
Errorf
(
"Unmarshal failed. %v"
,
err
)
return
fmt
.
Errorf
(
"Unmarshal failed. %v"
,
err
)
}
}
pbnl
:=
pbn
.
Links
()
pbnl
:=
pbn
.
Get
Links
()
n
.
Links
=
make
([]
*
Link
,
len
(
pbnl
))
n
.
Links
=
make
([]
*
Link
,
len
(
pbnl
))
for
i
,
l
:=
range
(
pbnl
)
{
for
i
,
l
:=
range
(
pbnl
)
{
n
.
Links
[
i
]
=
&
Link
{
Name
:
l
.
GetName
(),
Size
:
l
.
Get
S
ize
()}
n
.
Links
[
i
]
=
&
Link
{
Name
:
l
.
GetName
(),
Size
:
l
.
Get
Ts
ize
()}
n
.
Links
[
i
]
.
Has
h
,
err
:=
mh
.
Cast
(
l
.
GetHash
())
h
,
err
:=
mh
.
Cast
(
l
.
GetHash
())
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Link hash is not valid multihash. %v"
,
err
)
return
fmt
.
Errorf
(
"Link hash is not valid multihash. %v"
,
err
)
}
}
n
.
Links
[
i
]
.
Hash
=
h
}
}
n
.
Data
=
pbn
.
GetData
()
n
.
Data
=
pbn
.
GetData
()
...
@@ -30,7 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error {
...
@@ -30,7 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error {
func
(
n
*
Node
)
MarshalTo
(
encoded
[]
byte
)
error
{
func
(
n
*
Node
)
MarshalTo
(
encoded
[]
byte
)
error
{
pbn
:=
n
.
getPBNode
()
pbn
:=
n
.
getPBNode
()
if
err
:=
pbn
.
MarshalTo
(
encoded
)
,
err
!=
nil
{
if
_
,
err
:=
pbn
.
MarshalTo
(
encoded
)
;
err
!=
nil
{
return
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
return
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
}
}
return
nil
return
nil
...
@@ -50,12 +51,12 @@ func (n *Node) getPBNode() *PBNode {
...
@@ -50,12 +51,12 @@ func (n *Node) getPBNode() *PBNode {
pbn
.
Links
=
make
([]
*
PBLink
,
len
(
n
.
Links
))
pbn
.
Links
=
make
([]
*
PBLink
,
len
(
n
.
Links
))
for
i
,
l
:=
range
(
n
.
Links
)
{
for
i
,
l
:=
range
(
n
.
Links
)
{
pbn
.
Links
[
i
]
=
&
PBLink
{}
pbn
.
Links
[
i
]
=
&
PBLink
{}
n
.
Links
[
i
]
.
Name
=
&
l
.
Name
pb
n
.
Links
[
i
]
.
Name
=
&
l
.
Name
n
.
Links
[
i
]
.
S
ize
=
l
.
Size
pb
n
.
Links
[
i
]
.
Ts
ize
=
&
l
.
Size
n
.
Links
[
i
]
.
Hash
=
&
[]
byte
(
l
.
Hash
)
pb
n
.
Links
[
i
]
.
Hash
=
[]
byte
(
l
.
Hash
)
}
}
pbn
.
Data
=
&
n
.
Data
pbn
.
Data
=
n
.
Data
return
pbn
,
nil
return
pbn
}
}
merkledag.go
View file @
dcdb1e32
...
@@ -6,14 +6,14 @@ import (
...
@@ -6,14 +6,14 @@ import (
// A node in the IPFS Merkle DAG.
// A node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
// nodes have opaque data and a set of navigable links.
type
Node
{
type
Node
struct
{
Links
[]
*
Link
Links
[]
*
Link
Data
[]
byte
Data
[]
byte
}
}
// An IPFS Merkle DAG Link
// An IPFS Merkle DAG Link
type
Link
{
type
Link
struct
{
// utf string name. should be unique per object
// utf string name. should be unique per object
Name
string
// utf8
Name
string
// utf8
...
@@ -28,9 +28,15 @@ type Link {
...
@@ -28,9 +28,15 @@ type Link {
type
EncodedNode
[]
byte
type
EncodedNode
[]
byte
func
(
n
*
Node
)
Size
()
uint64
{
func
(
n
*
Node
)
Size
()
(
uint64
,
error
)
{
uint64
s
=
len
(
n
.
Encode
())
d
,
err
:=
n
.
Marshal
()
if
err
!=
nil
{
return
0
,
err
}
s
:=
uint64
(
len
(
d
))
for
_
,
l
:=
range
(
n
.
Links
)
{
for
_
,
l
:=
range
(
n
.
Links
)
{
s
+=
l
.
Size
s
+=
l
.
Size
}
}
return
s
,
nil
}
}
node.proto
View file @
dcdb1e32
...
@@ -25,7 +25,7 @@ message PBLink {
...
@@ -25,7 +25,7 @@ message PBLink {
optional
string
Name
=
2
;
optional
string
Name
=
2
;
// cumulative size of target object
// cumulative size of target object
optional
uint64
S
ize
=
3
;
optional
uint64
Ts
ize
=
3
;
}
}
// An IPFS MerkleDAG Node
// An IPFS MerkleDAG Node
...
...
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