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
63e44775
Commit
63e44775
authored
Jul 04, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
merkledag
parents
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
137 additions
and
0 deletions
+137
-0
coding.go
coding.go
+61
-0
merkledag.go
merkledag.go
+36
-0
merkledag.proto
merkledag.proto
+40
-0
No files found.
coding.go
0 → 100644
View file @
63e44775
package
merkledag
import
(
"fmt"
mh
"github.com/jbenet/go-multihash"
)
// for now, we use a PBNode intermediate thing.
// because native go objects are nice.
func
(
n
*
Node
)
Unmarshal
(
encoded
[]
byte
)
error
{
var
pbn
PBNode
if
err
:=
pbn
.
Unmarshal
(
encoded
),
err
!=
nil
{
return
fmt
.
Errorf
(
"Unmarshal failed. %v"
,
err
)
}
pbnl
:=
pbn
.
Links
()
n
.
Links
=
make
([]
*
Link
,
len
(
pbnl
))
for
i
,
l
:=
range
(
pbnl
)
{
n
.
Links
[
i
]
=
&
Link
{
Name
:
l
.
GetName
(),
Size
:
l
.
GetSize
()}
n
.
Links
[
i
]
.
Hash
,
err
:=
mh
.
Cast
(
l
.
GetHash
())
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Link hash is not valid multihash. %v"
,
err
)
}
}
n
.
Data
=
pbn
.
GetData
()
return
nil
}
func
(
n
*
Node
)
MarshalTo
(
encoded
[]
byte
)
error
{
pbn
:=
n
.
getPBNode
()
if
err
:=
pbn
.
MarshalTo
(
encoded
),
err
!=
nil
{
return
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
}
return
nil
}
func
(
n
*
Node
)
Marshal
()
([]
byte
,
error
)
{
pbn
:=
n
.
getPBNode
()
data
,
err
:=
pbn
.
Marshal
()
if
err
!=
nil
{
return
data
,
fmt
.
Errorf
(
"Marshal failed. %v"
,
err
)
}
return
data
,
nil
}
func
(
n
*
Node
)
getPBNode
()
*
PBNode
{
pbn
:=
&
PBNode
{}
pbn
.
Links
=
make
([]
*
PBLink
,
len
(
n
.
Links
))
for
i
,
l
:=
range
(
n
.
Links
)
{
pbn
.
Links
[
i
]
=
&
PBLink
{}
n
.
Links
[
i
]
.
Name
=
&
l
.
Name
n
.
Links
[
i
]
.
Size
=
l
.
Size
n
.
Links
[
i
]
.
Hash
=
&
[]
byte
(
l
.
Hash
)
}
pbn
.
Data
=
&
n
.
Data
return
pbn
,
nil
}
merkledag.go
0 → 100644
View file @
63e44775
package
merkledag
import
(
mh
"github.com/jbenet/go-multihash"
)
// A node in the IPFS Merkle DAG.
// nodes have opaque data and a set of navigable links.
type
Node
{
Links
[]
*
Link
Data
[]
byte
}
// An IPFS Merkle DAG Link
type
Link
{
// utf string name. should be unique per object
Name
string
// utf8
// cumulative size of target object
Size
uint64
// multihash of the target object
Hash
mh
.
Multihash
}
type
EncodedNode
[]
byte
func
(
n
*
Node
)
Size
()
uint64
{
uint64
s
=
len
(
n
.
Encode
())
for
_
,
l
:=
range
(
n
.
Links
)
{
s
+=
l
.
Size
}
}
merkledag.proto
0 → 100644
View file @
63e44775
package
merkledag
import
"code.google.com/p/gogoprotobuf/gogoproto/gogo.proto"
;
option
(
gogoproto.gostring_all
)
=
true
;
option
(
gogoproto.equal_all
)
=
true
;
option
(
gogoproto.verbose_equal_all
)
=
true
;
option
(
gogoproto.goproto_stringer_all
)
=
false
;
option
(
gogoproto.stringer_all
)
=
true
;
option
(
gogoproto.populate_all
)
=
true
;
option
(
gogoproto.testgen_all
)
=
true
;
option
(
gogoproto.benchgen_all
)
=
true
;
option
(
gogoproto.marshaler_all
)
=
true
;
option
(
gogoproto.sizer_all
)
=
true
;
option
(
gogoproto.unmarshaler_all
)
=
true
;
// An IPFS MerkleDAG Node
message
PBNode
{
// refs to other objects
repeated
Link
Links
=
2
;
// opaque user data
optional
bytes
Data
=
1
;
}
// An IPFS MerkleDAG Link
message
PBLink
{
// multihash of the target object
optional
bytes
Hash
=
1
;
// utf string name. should be unique per object
optional
string
Name
=
2
;
// cumulative size of target object
optional
uint64
Size
=
3
;
}
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