dag: Interface updates
This commit was moved from ipfs/go-ipfs-http-client@7bea2efb45cba072b4e24c20b46a4372be3a716d
Łukasz Magiera committed
Feb 4, 2019 at 19:45 UTC
88139ddc50e111e39721990bb86bb1c018c488b5
2 files changed
+34
-7
client/httpapi/api.go
+1
-2
@@ -11,7 +11,6 @@ import (
11
"github.com/ipfs/go-ipfs/core/coreapi/interface"
12
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13
14
- "github.com/ipfs/go-ipld-format"
14
homedir "github.com/mitchellh/go-homedir"
15
ma "github.com/multiformats/go-multiaddr"
16
manet "github.com/multiformats/go-multiaddr-net"
@@ -139,7 +138,7 @@ func (api *HttpApi) Block() iface.BlockAPI {
138
return (*BlockAPI)(api)
139
}
140
142
-func (api *HttpApi) Dag() format.DAGService {
141
+func (api *HttpApi) Dag() iface.APIDagService {
142
return (*HttpDagServ)(api)
143
}
144
client/httpapi/dag.go
+33
-5
@@ -15,7 +15,9 @@ import (
15
"github.com/ipfs/go-ipld-format"
16
)
17
18
-type HttpDagServ HttpApi
18
+type httpNodeAdder HttpApi
19
+type HttpDagServ httpNodeAdder
20
+type pinningHttpNodeAdder httpNodeAdder
21
22
func (api *HttpDagServ) Get(ctx context.Context, c cid.Cid) (format.Node, error) {
23
r, err := api.core().Block().Get(ctx, iface.IpldPath(c))
@@ -56,7 +58,7 @@ func (api *HttpDagServ) GetMany(ctx context.Context, cids []cid.Cid) <-chan *for
58
return out
59
}
60
59
-func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
61
+func (api *httpNodeAdder) add(ctx context.Context, nd format.Node, pin bool) error {
62
c := nd.Cid()
63
prefix := c.Prefix()
64
format := cid.CodecToStr[prefix.Codec]
@@ -65,7 +67,9 @@ func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
67
}
68
69
stat, err := api.core().Block().Put(ctx, bytes.NewReader(nd.RawData()),
68
- options.Block.Hash(prefix.MhType, prefix.MhLength), options.Block.Format(format))
70
+ options.Block.Hash(prefix.MhType, prefix.MhLength),
71
+ options.Block.Format(format),
72
+ options.Block.Pin(pin))
73
if err != nil {
74
return err
75
}
@@ -75,16 +79,36 @@ func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
79
return nil
80
}
81
78
-func (api *HttpDagServ) AddMany(ctx context.Context, nds []format.Node) error {
82
+func (api *httpNodeAdder) addMany(ctx context.Context, nds []format.Node, pin bool) error {
83
for _, nd := range nds {
84
// TODO: optimize
81
- if err := api.Add(ctx, nd); err != nil {
85
+ if err := api.add(ctx, nd, pin); err != nil {
86
return err
87
}
88
}
89
return nil
90
}
91
92
+func (api *HttpDagServ) AddMany(ctx context.Context, nds []format.Node) error {
93
+ return (*httpNodeAdder)(api).addMany(ctx, nds, false)
94
+}
95
+
96
+func (api *HttpDagServ) Add(ctx context.Context, nd format.Node) error {
97
+ return (*httpNodeAdder)(api).add(ctx, nd, false)
98
+}
99
+
100
+func (api *pinningHttpNodeAdder) Add(ctx context.Context, nd format.Node) error {
101
+ return (*httpNodeAdder)(api).add(ctx, nd, true)
102
+}
103
+
104
+func (api *pinningHttpNodeAdder) AddMany(ctx context.Context, nds []format.Node) error {
105
+ return (*httpNodeAdder)(api).addMany(ctx, nds, true)
106
+}
107
+
108
+func (api *HttpDagServ) Pinning() format.NodeAdder {
109
+ return (*pinningHttpNodeAdder)(api)
110
+}
111
+
112
func (api *HttpDagServ) Remove(ctx context.Context, c cid.Cid) error {
113
return api.core().Block().Rm(ctx, iface.IpldPath(c)) //TODO: should we force rm?
114
}
@@ -99,6 +123,10 @@ func (api *HttpDagServ) RemoveMany(ctx context.Context, cids []cid.Cid) error {
123
return nil
124
}
125
126
+func (api *httpNodeAdder) core() *HttpApi {
127
+ return (*HttpApi)(api)
128
+}
129
+
130
func (api *HttpDagServ) core() *HttpApi {
131
return (*HttpApi)(api)
132
}