Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
dag: Interface updates
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Feb 4, 2019
1 parent 83dfd84 commit 7bea2ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
3 changes: 1 addition & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

"github.com/ipfs/go-ipld-format"
homedir "github.com/mitchellh/go-homedir"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
Expand Down Expand Up @@ -139,7 +138,7 @@ func (api *HttpApi) Block() iface.BlockAPI {
return (*BlockAPI)(api)
}

func (api *HttpApi) Dag() format.DAGService {
func (api *HttpApi) Dag() iface.APIDagService {
return (*HttpDagServ)(api)
}

Expand Down
38 changes: 33 additions & 5 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
"github.com/ipfs/go-ipld-format"
)

type HttpDagServ HttpApi
type httpNodeAdder HttpApi
type HttpDagServ httpNodeAdder
type pinningHttpNodeAdder httpNodeAdder

func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error) {
r, err := api.core().Block().Get(ctx, iface.IpldPath(c))
Expand Down Expand Up @@ -56,7 +58,7 @@ func (api *HttpDagServ) GetMany(ctx context.Context, cids []cid.Cid) <-chan *for
return out
}

func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
func (api *httpNodeAdder) add(ctx context.Context, nd format.Node, pin bool) error {
c := nd.Cid()
prefix := c.Prefix()
format := cid.CodecToStr[prefix.Codec]
Expand All @@ -65,7 +67,9 @@ func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
}

stat, err := api.core().Block().Put(ctx, bytes.NewReader(nd.RawData()),
options.Block.Hash(prefix.MhType, prefix.MhLength), options.Block.Format(format))
options.Block.Hash(prefix.MhType, prefix.MhLength),
options.Block.Format(format),
options.Block.Pin(pin))
if err != nil {
return err
}
Expand All @@ -75,16 +79,36 @@ func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
return nil
}

func (api *HttpDagServ) AddMany(ctx context.Context, nds []format.Node) error {
func (api *httpNodeAdder) addMany(ctx context.Context, nds []format.Node, pin bool) error {
for _, nd := range nds {
// TODO: optimize
if err := api.Add(ctx, nd); err != nil {
if err := api.add(ctx, nd, pin); err != nil {
return err
}
}
return nil
}

func (api *HttpDagServ) AddMany(ctx context.Context, nds []format.Node) error {
return (*httpNodeAdder)(api).addMany(ctx, nds, false)
}

func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
return (*httpNodeAdder)(api).add(ctx, nd, false)
}

func (api *pinningHttpNodeAdder) Add(ctx context.Context, nd format.Node) error {
return (*httpNodeAdder)(api).add(ctx, nd, true)
}

func (api *pinningHttpNodeAdder) AddMany(ctx context.Context, nds []format.Node) error {
return (*httpNodeAdder)(api).addMany(ctx, nds, true)
}

func (api *HttpDagServ) Pinning() format.NodeAdder {
return (*pinningHttpNodeAdder)(api)
}

func (api *HttpDagServ) Remove(ctx context.Context, c cid.Cid) error {
return api.core().Block().Rm(ctx, iface.IpldPath(c)) //TODO: should we force rm?
}
Expand All @@ -99,6 +123,10 @@ func (api *HttpDagServ) RemoveMany(ctx context.Context, cids []cid.Cid) error {
return nil
}

func (api *httpNodeAdder) core() *HttpApi {
return (*HttpApi)(api)
}

func (api *HttpDagServ) core() *HttpApi {
return (*HttpApi)(api)
}
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"gxDependencies": [
{
"author": "why",
"hash": "QmNYPETsdAu2uQ1k9q9S1jYEGURaLHV6cbYRSVFVRftpF8",
"hash": "QmWqh9oob7ZHQRwU5CdTqpnC8ip8BEkFNrwXRxeNo5Y7vA",
"name": "go-path",
"version": "1.1.27"
"version": "1.1.30"
},
{
"author": "mitchellh",
Expand Down Expand Up @@ -39,9 +39,9 @@
},
{
"author": "magik6k",
"hash": "QmXWZCd8jfaHmt4UDSnjKmGcrQMw95bDGWqEeVLVJjoANX",
"hash": "QmaXvvAVAQ5ABqM5xtjYmV85xmN5MkWAZsX9H9Fwo4FVXp",
"name": "go-ipfs-files",
"version": "2.0.3"
"version": "2.0.4"
},
{
"author": "whyrusleeping",
Expand All @@ -57,9 +57,21 @@
},
{
"author": "why",
"hash": "QmQXze9tG878pa4Euya4rrDpyTNX3kQe4dhCaBzBozGgpe",
"hash": "QmQ1JnYpnzkaurjW1yxkQxC2w3K1PorNE1nv1vaP5Le7sq",
"name": "go-unixfs",
"version": "1.2.12"
"version": "1.3.1"
},
{
"author": "whyrusleeping",
"hash": "QmRL22E4paat7ky7vx9MLpR97JHHbFPrg3ytFQw6qp1y1s",
"name": "go-ipld-format",
"version": "0.8.0"
},
{
"author": "why",
"hash": "Qmb2UEG2TAeVrEJSjqsZF7Y2he7wRDkrdt6c3bECxwZf4k",
"name": "go-merkledag",
"version": "1.1.28"
}
],
"gxVersion": "0.12.1",
Expand Down

0 comments on commit 7bea2ef

Please sign in to comment.