move indirect pinning to its own structure
Jeromy committed
Oct 17, 2014 at 13:53 UTC
4d63820bbe93df7ffa9b06a5a78692249532bc9f
3 files changed
+50
-43
blocks/set/refset.go
deleted
-39
@@ -1,39 +0,0 @@
1
-package set
2
-
3
-import (
4
- "github.com/jbenet/go-ipfs/blocks/bloom"
5
- "github.com/jbenet/go-ipfs/util"
6
-)
7
-
8
-type refCntBlockSet struct {
9
- blocks map[util.Key]int
10
-}
11
-
12
-func NewRefCountBlockSet() BlockSet {
13
- return &refCntBlockSet{blocks: make(map[util.Key]int)}
14
-}
15
-
16
-func (r *refCntBlockSet) AddBlock(k util.Key) {
17
- r.blocks[k]++
18
-}
19
-
20
-func (r *refCntBlockSet) RemoveBlock(k util.Key) {
21
- v, ok := r.blocks[k]
22
- if !ok {
23
- return
24
- }
25
- if v <= 1 {
26
- delete(r.blocks, k)
27
- } else {
28
- r.blocks[k] = v - 1
29
- }
30
-}
31
-
32
-func (r *refCntBlockSet) HasKey(k util.Key) bool {
33
- _, ok := r.blocks[k]
34
- return ok
35
-}
36
-
37
-func (r *refCntBlockSet) GetBloomFilter() bloom.Filter {
38
- return nil
39
-}
pin/indirect.go
new
+43
@@ -0,0 +1,43 @@
1
+package pin
2
+
3
+import (
4
+ ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
5
+ bc "github.com/jbenet/go-ipfs/blocks/set"
6
+ "github.com/jbenet/go-ipfs/util"
7
+)
8
+
9
+type indirectPin struct {
10
+ blockset bc.BlockSet
11
+ refCounts map[util.Key]int
12
+}
13
+
14
+func loadBlockSet(d ds.Datastore) (bc.BlockSet, map[util.Key]int) {
15
+ panic("Not yet implemented!")
16
+ return nil, nil
17
+}
18
+
19
+func newIndirectPin(d ds.Datastore) indirectPin {
20
+ // suppose the blockset actually takes blocks, not just keys
21
+ bs, rc := loadBlockSet(d)
22
+ return indirectPin{bs, rc}
23
+}
24
+
25
+func (i *indirectPin) Increment(k util.Key) {
26
+ c := i.refCounts[k]
27
+ i.refCounts[k] = c + 1
28
+ if c <= 0 {
29
+ i.blockset.AddBlock(k)
30
+ }
31
+}
32
+
33
+func (i *indirectPin) Decrement(k util.Key) {
34
+ c := i.refCounts[k] - 1
35
+ i.refCounts[k] = c
36
+ if c <= 0 {
37
+ i.blockset.RemoveBlock(k)
38
+ }
39
+}
40
+
41
+func (i *indirectPin) HasKey(k util.Key) bool {
42
+ return i.blockset.HasKey(k)
43
+}
pin/pin.go
+7
-4
@@ -15,19 +15,22 @@ type Pinner interface {
15
type pinner struct {
16
recursePin set.BlockSet
17
directPin set.BlockSet
18
- indirPin set.BlockSet
18
+ indirPin indirectPin
19
dserv *mdag.DAGService
20
dstore ds.Datastore
21
}
22
23
func NewPinner(dstore ds.Datastore, serv *mdag.DAGService) Pinner {
24
+
25
+ // Load set from given datastore...
26
rcset := set.NewDBWrapperSet(dstore, "/pinned/recurse/", set.NewSimpleBlockSet())
27
dirset := set.NewDBWrapperSet(dstore, "/pinned/direct/", set.NewSimpleBlockSet())
26
- indset := set.NewDBWrapperSet(dstore, "/pinned/indirect/", set.NewRefCountBlockSet())
28
+
29
+ nsdstore := dstore // WRAP IN NAMESPACE
30
return &pinner{
31
recursePin: rcset,
32
directPin: dirset,
30
- indirPin: indset,
33
+ indirPin: newIndirectPin(nsdstore),
34
dserv: serv,
35
dstore: dstore,
36
}
@@ -99,7 +102,7 @@ func (p *pinner) pinIndirectRecurse(node *mdag.Node) error {
102
return err
103
}
104
102
- p.indirPin.AddBlock(k)
105
+ p.indirPin.Increment(k)
106
return p.pinLinks(node)
107
}
108