提交 cd65ec61 作者: Juan Batiz-Benet

merkledag: keep links sorted by name

May not be necessary to sort when adding each link--
doing so would be unnecessarily expensive O(n^2) when
constructing nodes -- though n wont be big.
上级 e1cde6bc
......@@ -2,6 +2,7 @@ package merkledag
import (
"fmt"
"sort"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
......@@ -30,6 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error {
}
n.Links[i].Hash = h
}
sort.Stable(LinkSlice(n.Links)) // keep links sorted
n.Data = pbn.GetData()
return nil
......@@ -59,6 +61,8 @@ func (n *Node) Marshal() ([]byte, error) {
func (n *Node) getPBNode() *pb.PBNode {
pbn := &pb.PBNode{}
pbn.Links = make([]*pb.PBLink, len(n.Links))
sort.Stable(LinkSlice(n.Links)) // keep links sorted
for i, l := range n.Links {
pbn.Links[i] = &pb.PBLink{}
pbn.Links[i].Name = &l.Name
......@@ -73,6 +77,7 @@ func (n *Node) getPBNode() *pb.PBNode {
// Encoded returns the encoded raw data version of a Node instance.
// It may use a cached encoded version, unless the force flag is given.
func (n *Node) Encoded(force bool) ([]byte, error) {
sort.Stable(LinkSlice(n.Links)) // keep links sorted
if n.encoded == nil || force {
var err error
n.encoded, err = n.Marshal()
......
......@@ -66,6 +66,12 @@ type Link struct {
Node *Node
}
type LinkSlice []*Link
func (ls LinkSlice) Len() int { return len(ls) }
func (ls LinkSlice) Swap(a, b int) { ls[a], ls[b] = ls[b], ls[a] }
func (ls LinkSlice) Less(a, b int) bool { return ls[a].Name < ls[b].Name }
// MakeLink creates a link to the given node
func MakeLink(n *Node) (*Link, error) {
s, err := n.Size()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论