Commit 20ee0c20 authored by Jeromy's avatar Jeromy

move util.Key into its own package under blocks

parent 29000371
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
blocks "github.com/ipfs/go-ipfs/blocks" blocks "github.com/ipfs/go-ipfs/blocks"
key "github.com/ipfs/go-ipfs/blocks/key"
bserv "github.com/ipfs/go-ipfs/blockservice" bserv "github.com/ipfs/go-ipfs/blockservice"
u "github.com/ipfs/go-ipfs/util" u "github.com/ipfs/go-ipfs/util"
) )
...@@ -16,15 +17,15 @@ var ErrNotFound = fmt.Errorf("merkledag: not found") ...@@ -16,15 +17,15 @@ var ErrNotFound = fmt.Errorf("merkledag: not found")
// DAGService is an IPFS Merkle DAG service. // DAGService is an IPFS Merkle DAG service.
type DAGService interface { type DAGService interface {
Add(*Node) (u.Key, error) Add(*Node) (key.Key, error)
AddRecursive(*Node) error AddRecursive(*Node) error
Get(context.Context, u.Key) (*Node, error) Get(context.Context, key.Key) (*Node, error)
Remove(*Node) error Remove(*Node) error
// GetDAG returns, in order, all the single leve child // GetDAG returns, in order, all the single leve child
// nodes of the passed in node. // nodes of the passed in node.
GetDAG(context.Context, *Node) []NodeGetter GetDAG(context.Context, *Node) []NodeGetter
GetNodes(context.Context, []u.Key) []NodeGetter GetNodes(context.Context, []key.Key) []NodeGetter
} }
func NewDAGService(bs *bserv.BlockService) DAGService { func NewDAGService(bs *bserv.BlockService) DAGService {
...@@ -41,7 +42,7 @@ type dagService struct { ...@@ -41,7 +42,7 @@ type dagService struct {
} }
// Add adds a node to the dagService, storing the block in the BlockService // Add adds a node to the dagService, storing the block in the BlockService
func (n *dagService) Add(nd *Node) (u.Key, error) { func (n *dagService) Add(nd *Node) (key.Key, error) {
if n == nil { // FIXME remove this assertion. protect with constructor invariant if n == nil { // FIXME remove this assertion. protect with constructor invariant
return "", fmt.Errorf("dagService is nil") return "", fmt.Errorf("dagService is nil")
} }
...@@ -82,7 +83,7 @@ func (n *dagService) AddRecursive(nd *Node) error { ...@@ -82,7 +83,7 @@ func (n *dagService) AddRecursive(nd *Node) error {
} }
// Get retrieves a node from the dagService, fetching the block in the BlockService // Get retrieves a node from the dagService, fetching the block in the BlockService
func (n *dagService) Get(ctx context.Context, k u.Key) (*Node, error) { func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
if n == nil { if n == nil {
return nil, fmt.Errorf("dagService is nil") return nil, fmt.Errorf("dagService is nil")
} }
...@@ -148,7 +149,7 @@ func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{} ...@@ -148,7 +149,7 @@ func FetchGraph(ctx context.Context, root *Node, serv DAGService) chan struct{}
// FindLinks searches this nodes links for the given key, // FindLinks searches this nodes links for the given key,
// returns the indexes of any links pointing to it // returns the indexes of any links pointing to it
func FindLinks(links []u.Key, k u.Key, start int) []int { func FindLinks(links []key.Key, k key.Key, start int) []int {
var out []int var out []int
for i, lnk_k := range links[start:] { for i, lnk_k := range links[start:] {
if k == lnk_k { if k == lnk_k {
...@@ -162,9 +163,9 @@ func FindLinks(links []u.Key, k u.Key, start int) []int { ...@@ -162,9 +163,9 @@ func FindLinks(links []u.Key, k u.Key, start int) []int {
// It returns a channel of nodes, which the caller can receive // It returns a channel of nodes, which the caller can receive
// all the child nodes of 'root' on, in proper order. // all the child nodes of 'root' on, in proper order.
func (ds *dagService) GetDAG(ctx context.Context, root *Node) []NodeGetter { func (ds *dagService) GetDAG(ctx context.Context, root *Node) []NodeGetter {
var keys []u.Key var keys []key.Key
for _, lnk := range root.Links { for _, lnk := range root.Links {
keys = append(keys, u.Key(lnk.Hash)) keys = append(keys, key.Key(lnk.Hash))
} }
return ds.GetNodes(ctx, keys) return ds.GetNodes(ctx, keys)
...@@ -172,7 +173,7 @@ func (ds *dagService) GetDAG(ctx context.Context, root *Node) []NodeGetter { ...@@ -172,7 +173,7 @@ func (ds *dagService) GetDAG(ctx context.Context, root *Node) []NodeGetter {
// GetNodes returns an array of 'NodeGetter' promises, with each corresponding // GetNodes returns an array of 'NodeGetter' promises, with each corresponding
// to the key with the same index as the passed in keys // to the key with the same index as the passed in keys
func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter { func (ds *dagService) GetNodes(ctx context.Context, keys []key.Key) []NodeGetter {
// Early out if no work to do // Early out if no work to do
if len(keys) == 0 { if len(keys) == 0 {
...@@ -219,9 +220,9 @@ func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter { ...@@ -219,9 +220,9 @@ func (ds *dagService) GetNodes(ctx context.Context, keys []u.Key) []NodeGetter {
} }
// Remove duplicates from a list of keys // Remove duplicates from a list of keys
func dedupeKeys(ks []u.Key) []u.Key { func dedupeKeys(ks []key.Key) []key.Key {
kmap := make(map[u.Key]struct{}) kmap := make(map[key.Key]struct{})
var out []u.Key var out []key.Key
for _, k := range ks { for _, k := range ks {
if _, ok := kmap[k]; !ok { if _, ok := kmap[k]; !ok {
kmap[k] = struct{}{} kmap[k] = struct{}{}
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync" dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
bstore "github.com/ipfs/go-ipfs/blocks/blockstore" bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
key "github.com/ipfs/go-ipfs/blocks/key"
blockservice "github.com/ipfs/go-ipfs/blockservice" blockservice "github.com/ipfs/go-ipfs/blockservice"
bserv "github.com/ipfs/go-ipfs/blockservice" bserv "github.com/ipfs/go-ipfs/blockservice"
offline "github.com/ipfs/go-ipfs/exchange/offline" offline "github.com/ipfs/go-ipfs/exchange/offline"
...@@ -81,7 +82,7 @@ func TestNode(t *testing.T) { ...@@ -81,7 +82,7 @@ func TestNode(t *testing.T) {
k, err := n.Key() k, err := n.Key()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else if k != u.Key(h) { } else if k != key.Key(h) {
t.Error("Key is not equivalent to multihash") t.Error("Key is not equivalent to multihash")
} else { } else {
fmt.Println("key: ", k) fmt.Println("key: ", k)
......
...@@ -6,14 +6,9 @@ import ( ...@@ -6,14 +6,9 @@ import (
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash" mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
u "github.com/ipfs/go-ipfs/util" key "github.com/ipfs/go-ipfs/blocks/key"
) )
// NodeMap maps u.Keys to Nodes.
// We cannot use []byte/Multihash for keys :(
// so have to convert Multihash bytes to string (u.Key)
type NodeMap map[u.Key]*Node
// Node represents a node in the IPFS Merkle DAG. // Node represents 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 struct { type Node struct {
...@@ -84,7 +79,7 @@ func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) { ...@@ -84,7 +79,7 @@ func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) {
return l.Node, nil return l.Node, nil
} }
return serv.Get(ctx, u.Key(l.Hash)) return serv.Get(ctx, key.Key(l.Hash))
} }
// AddNodeLink adds a link to another node. // AddNodeLink adds a link to another node.
...@@ -227,7 +222,7 @@ func (n *Node) Multihash() (mh.Multihash, error) { ...@@ -227,7 +222,7 @@ func (n *Node) Multihash() (mh.Multihash, error) {
} }
// Key returns the Multihash as a key, for maps. // Key returns the Multihash as a key, for maps.
func (n *Node) Key() (u.Key, error) { func (n *Node) Key() (key.Key, error) {
h, err := n.Multihash() h, err := n.Multihash()
return u.Key(h), err return key.Key(h), err
} }
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