From 3ee3c44d7c999f8f2a626be669ffb2a96c137ecd Mon Sep 17 00:00:00 2001 From: Jeromy Date: Tue, 21 Oct 2014 23:03:10 -0700 Subject: [PATCH] flush! --- core/commands/pin.go | 4 ++-- pin/pin.go | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/core/commands/pin.go b/core/commands/pin.go index 3f72e4d29..cbca3bf92 100644 --- a/core/commands/pin.go +++ b/core/commands/pin.go @@ -34,7 +34,7 @@ func Pin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Wr return fmt.Errorf("pin: %v", err) } } - return nil + return n.Pinning.Flush() } func Unpin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io.Writer) error { @@ -54,5 +54,5 @@ func Unpin(n *core.IpfsNode, args []string, opts map[string]interface{}, out io. return fmt.Errorf("pin: %v", err) } } - return nil + return n.Pinning.Flush() } diff --git a/pin/pin.go b/pin/pin.go index 7e73b33f2..a63dcff57 100644 --- a/pin/pin.go +++ b/pin/pin.go @@ -3,6 +3,8 @@ package pin import ( //ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" + "bytes" + "encoding/json" "sync" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" @@ -175,19 +177,41 @@ func LoadPinner(d ds.Datastore, dserv *mdag.DAGService) (Pinner, error) { func (p *pinner) Flush() error { p.lock.RLock() defer p.lock.RUnlock() + buf := new(bytes.Buffer) + enc := json.NewEncoder(buf) + recurse := p.recursePin.GetKeys() - err := p.dstore.Put(recursePinDatastoreKey, recurse) + err := enc.Encode(recurse) if err != nil { return err } + err = p.dstore.Put(recursePinDatastoreKey, buf.Bytes()) + if err != nil { + return err + } + + buf = new(bytes.Buffer) + enc = json.NewEncoder(buf) direct := p.directPin.GetKeys() - err = p.dstore.Put(directPinDatastoreKey, direct) + err = enc.Encode(direct) if err != nil { return err } - err = p.dstore.Put(indirectPinDatastoreKey, p.indirPin.refCounts) + err = p.dstore.Put(directPinDatastoreKey, buf.Bytes()) + if err != nil { + return err + } + + buf = new(bytes.Buffer) + enc = json.NewEncoder(buf) + err = enc.Encode(p.indirPin.refCounts) + if err != nil { + return err + } + + err = p.dstore.Put(indirectPinDatastoreKey, buf.Bytes()) if err != nil { return err }