Commit 11add1c8 authored by Jeromy's avatar Jeromy

extract node interface

License: MIT
Signed-off-by: default avatarJeromy <why@ipfs.io>
parent c167c8f6
......@@ -3,17 +3,17 @@
package pin
import (
"context"
"fmt"
"os"
"sync"
"time"
mdag "github.com/ipfs/go-ipfs/merkledag"
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
context "context"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
)
......@@ -83,7 +83,7 @@ func StringToPinMode(s string) (PinMode, bool) {
type Pinner interface {
IsPinned(*cid.Cid) (string, bool, error)
IsPinnedWithType(*cid.Cid, PinMode) (string, bool, error)
Pin(context.Context, mdag.Node, bool) error
Pin(context.Context, node.Node, bool) error
Unpin(context.Context, *cid.Cid, bool) error
// Check if a set of keys are pinned, more efficient than
......@@ -162,11 +162,10 @@ func NewPinner(dstore ds.Datastore, serv, internal mdag.DAGService) Pinner {
}
// Pin the given node, optionally recursive
func (p *pinner) Pin(ctx context.Context, node mdag.Node, recurse bool) error {
func (p *pinner) Pin(ctx context.Context, node node.Node, recurse bool) error {
p.lock.Lock()
defer p.lock.Unlock()
c := node.Cid()
k := key.Key(c.Hash())
if recurse {
if p.recursePin.Has(c) {
......@@ -190,7 +189,7 @@ func (p *pinner) Pin(ctx context.Context, node mdag.Node, recurse bool) error {
}
if p.recursePin.Has(c) {
return fmt.Errorf("%s already pinned recursively", k.B58String())
return fmt.Errorf("%s already pinned recursively", c.String())
}
p.directPin.Add(c)
......@@ -248,7 +247,6 @@ func (p *pinner) IsPinnedWithType(c *cid.Cid, mode PinMode) (string, bool, error
// isPinnedWithType is the implementation of IsPinnedWithType that does not lock.
// intended for use by other pinned methods that already take locks
func (p *pinner) isPinnedWithType(c *cid.Cid, mode PinMode) (string, bool, error) {
k := key.Key(c.Hash())
switch mode {
case Any, Direct, Indirect, Recursive, Internal:
default:
......@@ -279,7 +277,7 @@ func (p *pinner) isPinnedWithType(c *cid.Cid, mode PinMode) (string, bool, error
// Default is Indirect
for _, rc := range p.recursePin.Keys() {
has, err := hasChild(p.dserv, rc, k)
has, err := hasChild(p.dserv, rc, c)
if err != nil {
return "", false, err
}
......@@ -521,14 +519,14 @@ func (p *pinner) PinWithMode(c *cid.Cid, mode PinMode) {
}
}
func hasChild(ds mdag.LinkService, root *cid.Cid, child key.Key) (bool, error) {
func hasChild(ds mdag.LinkService, root *cid.Cid, child *cid.Cid) (bool, error) {
links, err := ds.GetLinks(context.Background(), root)
if err != nil {
return false, err
}
for _, lnk := range links {
c := lnk.Cid
if key.Key(c.Hash()) == child {
if lnk.Cid.Equals(child) {
return true, nil
}
......
......@@ -12,9 +12,11 @@ import (
"github.com/ipfs/go-ipfs/merkledag"
"github.com/ipfs/go-ipfs/pin/internal/pb"
cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
"gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
"gx/ipfs/QmZ4Qi3GaRbjcx28Sme5eMH7RQjGkt8wHxt2a65oLaeFEV/gogo-protobuf/proto"
node "gx/ipfs/QmZx42H5khbVQhV5odp66TApShV4XCujYazcvYduZ4TroB/go-ipld-node"
)
const (
......@@ -47,7 +49,7 @@ type itemIterator func() (c *cid.Cid, ok bool)
type keyObserver func(*cid.Cid)
type sortByHash struct {
links []*merkledag.Link
links []*node.Link
}
func (s sortByHash) Len() int {
......@@ -67,9 +69,9 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
if err != nil {
return nil, err
}
links := make([]*merkledag.Link, 0, defaultFanout+maxItems)
links := make([]*node.Link, 0, defaultFanout+maxItems)
for i := 0; i < defaultFanout; i++ {
links = append(links, &merkledag.Link{Cid: emptyKey})
links = append(links, &node.Link{Cid: emptyKey})
}
// add emptyKey to our set of internal pinset objects
......@@ -97,7 +99,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
break
}
links = append(links, &merkledag.Link{Cid: k})
links = append(links, &node.Link{Cid: k})
}
n.SetLinks(links)
......@@ -159,7 +161,7 @@ func storeItems(ctx context.Context, dag merkledag.DAGService, estimatedLen uint
internalKeys(childKey)
// overwrite the 'empty key' in the existing links array
n.Links()[h] = &merkledag.Link{
n.Links()[h] = &node.Link{
Cid: childKey,
Size: size,
}
......@@ -212,7 +214,7 @@ func writeHdr(n *merkledag.ProtoNode, hdr *pb.Set) error {
return nil
}
type walkerFunc func(idx int, link *merkledag.Link) error
type walkerFunc func(idx int, link *node.Link) error
func walkItems(ctx context.Context, dag merkledag.DAGService, n *merkledag.ProtoNode, fn walkerFunc, children keyObserver) error {
hdr, err := readHdr(n)
......@@ -269,7 +271,7 @@ func loadSet(ctx context.Context, dag merkledag.DAGService, root *merkledag.Prot
}
var res []*cid.Cid
walk := func(idx int, link *merkledag.Link) error {
walk := func(idx int, link *node.Link) error {
res = append(res, link.Cid)
return nil
}
......
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