Commit b91db680 authored by Jeromy's avatar Jeromy

update pinning to new semantics, and fix a couple bugs

parent 3601a93d
...@@ -122,6 +122,11 @@ func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) { ...@@ -122,6 +122,11 @@ func add(n *core.IpfsNode, readers []io.Reader) ([]*dag.Node, error) {
dagnodes = append(dagnodes, node) dagnodes = append(dagnodes, node)
} }
err := n.Pinning.Flush()
if err != nil {
return nil, err
}
return dagnodes, nil return dagnodes, nil
} }
......
...@@ -6,8 +6,7 @@ import ( ...@@ -6,8 +6,7 @@ import (
"io" "io"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core" corerepo "github.com/jbenet/go-ipfs/core/repo"
"github.com/jbenet/go-ipfs/merkledag"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
...@@ -58,21 +57,11 @@ on disk. ...@@ -58,21 +57,11 @@ on disk.
recursive = false recursive = false
} }
nodes, err := pin(n, req.Arguments(), recursive) added, err := corerepo.Pin(n, req.Arguments(), recursive)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var added []u.Key
for _, node := range nodes {
k, err := node.Key()
if err != nil {
return nil, err
}
added = append(added, k)
}
// TODO: create some output to show what got pinned
return &PinOutput{added}, nil return &PinOutput{added}, nil
}, },
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
...@@ -122,20 +111,11 @@ collected if needed. ...@@ -122,20 +111,11 @@ collected if needed.
recursive = false // default recursive = false // default
} }
nodes, err := unpin(n, req.Arguments(), recursive) removed, err := corerepo.Unpin(n, req.Arguments(), recursive)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var removed []u.Key
for _, node := range nodes {
k, err := node.Key()
if err != nil {
return nil, err
}
removed = append(removed, k)
}
return &PinOutput{removed}, nil return &PinOutput{removed}, nil
}, },
Marshalers: cmds.MarshalerMap{ Marshalers: cmds.MarshalerMap{
...@@ -215,55 +195,3 @@ Use --type=<type> to specify the type of pinned keys to list. Valid values are: ...@@ -215,55 +195,3 @@ Use --type=<type> to specify the type of pinned keys to list. Valid values are:
cmds.Text: KeyListTextMarshaler, cmds.Text: KeyListTextMarshaler,
}, },
} }
func pin(n *core.IpfsNode, paths []string, recursive bool) ([]*merkledag.Node, error) {
dagnodes := make([]*merkledag.Node, 0)
for _, path := range paths {
dagnode, err := n.Resolver.ResolvePath(path)
if err != nil {
return nil, fmt.Errorf("pin error: %v", err)
}
dagnodes = append(dagnodes, dagnode)
}
for _, dagnode := range dagnodes {
err := n.Pinning.Pin(dagnode, recursive)
if err != nil {
return nil, fmt.Errorf("pin: %v", err)
}
}
err := n.Pinning.Flush()
if err != nil {
return nil, err
}
return dagnodes, nil
}
func unpin(n *core.IpfsNode, paths []string, recursive bool) ([]*merkledag.Node, error) {
dagnodes := make([]*merkledag.Node, 0)
for _, path := range paths {
dagnode, err := n.Resolver.ResolvePath(path)
if err != nil {
return nil, err
}
dagnodes = append(dagnodes, dagnode)
}
for _, dagnode := range dagnodes {
k, _ := dagnode.Key()
err := n.Pinning.Unpin(k, recursive)
if err != nil {
return nil, err
}
}
err := n.Pinning.Flush()
if err != nil {
return nil, err
}
return dagnodes, nil
}
...@@ -3,10 +3,10 @@ package commands ...@@ -3,10 +3,10 @@ package commands
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
cmds "github.com/jbenet/go-ipfs/commands" cmds "github.com/jbenet/go-ipfs/commands"
"github.com/jbenet/go-ipfs/core" "github.com/jbenet/go-ipfs/core"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
"io"
) )
var RepoCmd = &cmds.Command{ var RepoCmd = &cmds.Command{
......
package corerepo
import (
"fmt"
"github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/merkledag"
u "github.com/jbenet/go-ipfs/util"
)
func Pin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
dagnodes := make([]*merkledag.Node, 0)
for _, path := range paths {
dagnode, err := n.Resolver.ResolvePath(path)
if err != nil {
return nil, fmt.Errorf("pin error: %v", err)
}
dagnodes = append(dagnodes, dagnode)
}
var out []u.Key
for _, dagnode := range dagnodes {
k, err := dagnode.Key()
if err != nil {
return nil, err
}
err = n.Pinning.Pin(dagnode, recursive)
if err != nil {
return nil, fmt.Errorf("pin: %v", err)
}
out = append(out, k)
}
err := n.Pinning.Flush()
if err != nil {
return nil, err
}
return out, nil
}
func Unpin(n *core.IpfsNode, paths []string, recursive bool) ([]u.Key, error) {
dagnodes := make([]*merkledag.Node, 0)
for _, path := range paths {
dagnode, err := n.Resolver.ResolvePath(path)
if err != nil {
return nil, err
}
dagnodes = append(dagnodes, dagnode)
}
var unpinned []u.Key
for _, dagnode := range dagnodes {
k, _ := dagnode.Key()
err := n.Pinning.Unpin(k)
if err != nil {
return nil, err
}
unpinned = append(unpinned, k)
}
err := n.Pinning.Flush()
if err != nil {
return nil, err
}
return unpinned, nil
}
...@@ -25,12 +25,13 @@ const ( ...@@ -25,12 +25,13 @@ const (
Recursive PinMode = iota Recursive PinMode = iota
Direct Direct
Indirect Indirect
NotPinned
) )
type Pinner interface { type Pinner interface {
IsPinned(util.Key) bool IsPinned(util.Key) bool
Pin(*mdag.Node, bool) error Pin(*mdag.Node, bool) error
Unpin(util.Key, bool) error Unpin(util.Key) error
Flush() error Flush() error
GetManual() ManualPinner GetManual() ManualPinner
DirectKeys() []util.Key DirectKeys() []util.Key
...@@ -90,6 +91,10 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error { ...@@ -90,6 +91,10 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
return nil return nil
} }
if p.directPin.HasKey(k) {
p.directPin.RemoveBlock(k)
}
p.recursePin.AddBlock(k) p.recursePin.AddBlock(k)
err := p.pinLinks(node) err := p.pinLinks(node)
...@@ -97,16 +102,19 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error { ...@@ -97,16 +102,19 @@ func (p *pinner) Pin(node *mdag.Node, recurse bool) error {
return err return err
} }
} else { } else {
if p.recursePin.HasKey(k) {
return errors.New("Key already pinned recursively.")
}
p.directPin.AddBlock(k) p.directPin.AddBlock(k)
} }
return nil return nil
} }
// Unpin a given key with optional recursive unpinning // Unpin a given key
func (p *pinner) Unpin(k util.Key, recurse bool) error { func (p *pinner) Unpin(k util.Key) error {
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()
if recurse { if p.recursePin.HasKey(k) {
p.recursePin.RemoveBlock(k) p.recursePin.RemoveBlock(k)
node, err := p.dserv.Get(k) node, err := p.dserv.Get(k)
if err != nil { if err != nil {
...@@ -114,9 +122,14 @@ func (p *pinner) Unpin(k util.Key, recurse bool) error { ...@@ -114,9 +122,14 @@ func (p *pinner) Unpin(k util.Key, recurse bool) error {
} }
return p.unpinLinks(node) return p.unpinLinks(node)
} else if p.directPin.HasKey(k) {
p.directPin.RemoveBlock(k)
return nil
} else if p.indirPin.HasKey(k) {
return errors.New("Cannot unpin indirectly pinned block.")
} else {
return errors.New("Given key was not pinned.")
} }
p.directPin.RemoveBlock(k)
return nil
} }
func (p *pinner) unpinLinks(node *mdag.Node) error { func (p *pinner) unpinLinks(node *mdag.Node) error {
......
...@@ -100,8 +100,8 @@ func TestPinnerBasic(t *testing.T) { ...@@ -100,8 +100,8 @@ func TestPinnerBasic(t *testing.T) {
t.Fatal("pinned node not found.") t.Fatal("pinned node not found.")
} }
// Test recursive unpin // Test unpin
err = p.Unpin(dk, true) err = p.Unpin(dk)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(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