Refactor per code climate rules
License: MIT Signed-off-by: Michael Avila <davidmichaelavila@gmail.com>
Michael Avila committed
Mar 8, 2019 at 16:58 UTC
dde397ebc1259e7f86ea6a4428314ebb51f58bd7
7 files changed
+42
-37
core/builder.go
+1
-1
@@ -277,7 +277,7 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
277
n.Resolver = resolver.NewBasicResolver(n.DAG)
278
279
// Provider
280
- queue, err := provider.NewQueue("provider-v1", ctx, n.Repo.Datastore())
280
+ queue, err := provider.NewQueue(ctx, "provider-v1", n.Repo.Datastore())
281
if err != nil {
282
return err
283
}
core/core.go
+1
-1
@@ -125,7 +125,7 @@ type IpfsNode struct {
125
Routing routing.IpfsRouting // the routing system. recommend ipfs-dht
126
Exchange exchange.Interface // the block exchange + strategy (bitswap)
127
Namesys namesys.NameSystem // the name system, resolves paths to hashes
128
- Provider provider.Provider // the value provider system
128
+ Provider provider.Provider // the value provider system
129
Reprovider *rp.Reprovider // the value reprovider system
130
IpnsRepub *ipnsrp.Republisher
131
core/coreapi/coreapi.go
+1
-1
@@ -20,8 +20,8 @@ import (
20
21
"github.com/ipfs/go-ipfs/core"
22
"github.com/ipfs/go-ipfs/namesys"
23
- "github.com/ipfs/go-ipfs/provider"
23
"github.com/ipfs/go-ipfs/pin"
24
+ "github.com/ipfs/go-ipfs/provider"
25
"github.com/ipfs/go-ipfs/repo"
26
27
bserv "github.com/ipfs/go-blockservice"
core/coreapi/provider.go
+4
-2
@@ -4,8 +4,10 @@ import (
4
cid "github.com/ipfs/go-cid"
5
)
6
7
+// ProviderAPI brings Provider behavior to CoreAPI
8
type ProviderAPI CoreAPI
9
9
-func (api *ProviderAPI) Provide(root cid.Cid) error {
10
- return api.provider.Provide(root)
10
+// Provide the given cid using the current provider
11
+func (api *ProviderAPI) Provide(cid cid.Cid) error {
12
+ return api.provider.Provide(cid)
13
}
provider/offline.go
+2
-1
@@ -2,8 +2,9 @@ package provider
2
3
import "github.com/ipfs/go-cid"
4
5
-type offlineProvider struct {}
5
+type offlineProvider struct{}
6
7
+// NewOfflineProvider creates a Provider that does nothing
8
func NewOfflineProvider() Provider {
9
return &offlineProvider{}
10
}
provider/provider.go
+2
-2
@@ -18,13 +18,12 @@ const (
18
provideOutgoingWorkerLimit = 8
19
)
20
21
+// Provider announces blocks to the network
22
type Provider interface {
23
Run()
24
Provide(cid.Cid) error
25
}
26
26
-// Provider announces blocks to the network, tracks which blocks are
27
-// being provided, and untracks blocks when they're no longer in the blockstore.
27
type provider struct {
28
ctx context.Context
29
// the CIDs for which provide announcements should be made
@@ -33,6 +32,7 @@ type provider struct {
32
contentRouting routing.ContentRouting
33
}
34
35
+// NewProvider creates a provider that announces blocks to the network using a content router
36
func NewProvider(ctx context.Context, queue *Queue, contentRouting routing.ContentRouting) Provider {
37
return &provider{
38
ctx: ctx,
provider/queue.go
+31
-29
@@ -17,11 +17,12 @@ import (
17
// not removed from the datastore until you call Complete() on the entry you
18
// receive.
19
type Entry struct {
20
- cid cid.Cid
21
- key ds.Key
20
+ cid cid.Cid
21
+ key ds.Key
22
queue *Queue
23
}
24
25
+// Complete the entry by removing it from the queue
26
func (e *Entry) Complete() error {
27
return e.queue.remove(e.key)
28
}
@@ -41,36 +42,37 @@ type Queue struct {
42
tail uint64
43
head uint64
44
44
- lock sync.Mutex
45
+ lock sync.Mutex
46
datastore ds.Datastore
47
47
- dequeue chan *Entry
48
+ dequeue chan *Entry
49
notEmpty chan struct{}
50
51
isRunning bool
52
}
53
53
-func NewQueue(name string, ctx context.Context, datastore ds.Datastore) (*Queue, error) {
54
- namespaced := namespace.Wrap(datastore, ds.NewKey("/" + name + "/queue/"))
55
- head, tail, err := getQueueHeadTail(name, ctx, namespaced)
54
+// NewQueue creates a queue for cids
55
+func NewQueue(ctx context.Context, name string, datastore ds.Datastore) (*Queue, error) {
56
+ namespaced := namespace.Wrap(datastore, ds.NewKey("/"+name+"/queue/"))
57
+ head, tail, err := getQueueHeadTail(ctx, name, namespaced)
58
if err != nil {
59
return nil, err
60
}
61
q := &Queue{
60
- name: name,
61
- ctx: ctx,
62
- head: head,
63
- tail: tail,
64
- lock: sync.Mutex{},
62
+ name: name,
63
+ ctx: ctx,
64
+ head: head,
65
+ tail: tail,
66
+ lock: sync.Mutex{},
67
datastore: namespaced,
66
- dequeue: make(chan *Entry),
67
- notEmpty: make(chan struct{}),
68
+ dequeue: make(chan *Entry),
69
+ notEmpty: make(chan struct{}),
70
isRunning: false,
71
}
72
return q, nil
73
}
74
73
-// Put a cid in the queue
75
+// Enqueue puts a cid in the queue
76
func (q *Queue) Enqueue(cid cid.Cid) error {
77
q.lock.Lock()
78
defer q.lock.Unlock()
@@ -95,21 +97,18 @@ func (q *Queue) Enqueue(cid cid.Cid) error {
97
return nil
98
}
99
98
-// Remove an entry from the queue.
100
+// Dequeue returns a channel that if listened to will remove entries from the queue
101
func (q *Queue) Dequeue() <-chan *Entry {
102
return q.dequeue
103
}
104
105
+// IsEmpty returns whether or not the queue has any items
106
func (q *Queue) IsEmpty() bool {
107
return (q.tail - q.head) == 0
108
}
109
107
-func (q *Queue) remove(key ds.Key) error {
108
- return q.datastore.Delete(key)
109
-}
110
-
111
-// dequeue items when the dequeue channel is available to
112
-// be written to
110
+// Run dequeues items when the dequeue channel is available to
111
+// be written to.
112
func (q *Queue) Run() {
113
q.isRunning = true
114
go func() {
@@ -178,9 +177,9 @@ func (q *Queue) next() (*Entry, error) {
177
return nil, err
178
}
179
181
- entry := &Entry {
182
- cid: id,
183
- key: nextKey,
180
+ entry := &Entry{
181
+ cid: id,
182
+ key: nextKey,
183
queue: q,
184
}
185
@@ -194,14 +193,14 @@ func (q *Queue) queueKey(id uint64) ds.Key {
193
}
194
195
// crawl over the queue entries to find the head and tail
197
-func getQueueHeadTail(name string, ctx context.Context, datastore ds.Datastore) (uint64, uint64, error) {
196
+func getQueueHeadTail(ctx context.Context, name string, datastore ds.Datastore) (uint64, uint64, error) {
197
query := query.Query{}
198
results, err := datastore.Query(query)
199
if err != nil {
200
return 0, 0, err
201
}
202
204
- var tail uint64 = 0
203
+ var tail uint64
204
var head uint64 = math.MaxUint64
205
for entry := range results.Next() {
206
select {
@@ -219,8 +218,8 @@ func getQueueHeadTail(name string, ctx context.Context, datastore ds.Datastore)
218
head = id
219
}
220
222
- if (id+1) > tail {
223
- tail = (id+1)
221
+ if (id + 1) > tail {
222
+ tail = (id + 1)
223
}
224
}
225
if err := results.Close(); err != nil {
@@ -233,3 +232,6 @@ func getQueueHeadTail(name string, ctx context.Context, datastore ds.Datastore)
232
return head, tail, nil
233
}
234
235
+func (q *Queue) remove(key ds.Key) error {
236
+ return q.datastore.Delete(key)
237
+}