Commit 1a5c8cc4 authored by Juan Batiz-Benet's avatar 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.
parent a32fc7ff
...@@ -2,6 +2,7 @@ package merkledag ...@@ -2,6 +2,7 @@ package merkledag
import ( import (
"fmt" "fmt"
"sort"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" 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 { ...@@ -30,6 +31,7 @@ func (n *Node) Unmarshal(encoded []byte) error {
} }
n.Links[i].Hash = h n.Links[i].Hash = h
} }
sort.Stable(LinkSlice(n.Links)) // keep links sorted
n.Data = pbn.GetData() n.Data = pbn.GetData()
return nil return nil
...@@ -59,6 +61,8 @@ func (n *Node) Marshal() ([]byte, error) { ...@@ -59,6 +61,8 @@ func (n *Node) Marshal() ([]byte, error) {
func (n *Node) getPBNode() *pb.PBNode { func (n *Node) getPBNode() *pb.PBNode {
pbn := &pb.PBNode{} pbn := &pb.PBNode{}
pbn.Links = make([]*pb.PBLink, len(n.Links)) pbn.Links = make([]*pb.PBLink, len(n.Links))
sort.Stable(LinkSlice(n.Links)) // keep links sorted
for i, l := range n.Links { for i, l := range n.Links {
pbn.Links[i] = &pb.PBLink{} pbn.Links[i] = &pb.PBLink{}
pbn.Links[i].Name = &l.Name pbn.Links[i].Name = &l.Name
...@@ -73,6 +77,7 @@ func (n *Node) getPBNode() *pb.PBNode { ...@@ -73,6 +77,7 @@ func (n *Node) getPBNode() *pb.PBNode {
// Encoded returns the encoded raw data version of a Node instance. // Encoded returns the encoded raw data version of a Node instance.
// It may use a cached encoded version, unless the force flag is given. // It may use a cached encoded version, unless the force flag is given.
func (n *Node) Encoded(force bool) ([]byte, error) { func (n *Node) Encoded(force bool) ([]byte, error) {
sort.Stable(LinkSlice(n.Links)) // keep links sorted
if n.encoded == nil || force { if n.encoded == nil || force {
var err error var err error
n.encoded, err = n.Marshal() n.encoded, err = n.Marshal()
......
...@@ -66,6 +66,12 @@ type Link struct { ...@@ -66,6 +66,12 @@ type Link struct {
Node *Node 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 // MakeLink creates a link to the given node
func MakeLink(n *Node) (*Link, error) { func MakeLink(n *Node) (*Link, error) {
s, err := n.Size() s, err := n.Size()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment