-
Notifications
You must be signed in to change notification settings - Fork 0
/
indirect.go
78 lines (65 loc) · 1.61 KB
/
indirect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package pin
import (
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
"github.com/ipfs/go-ipfs/blocks/set"
"github.com/ipfs/go-ipfs/util"
)
type indirectPin struct {
blockset set.BlockSet
refCounts map[util.Key]int
}
func NewIndirectPin(dstore ds.Datastore) *indirectPin {
return &indirectPin{
blockset: set.NewDBWrapperSet(dstore, set.NewSimpleBlockSet()),
refCounts: make(map[util.Key]int),
}
}
func loadIndirPin(d ds.Datastore, k ds.Key) (*indirectPin, error) {
var rcStore map[string]int
err := loadSet(d, k, &rcStore)
if err != nil {
return nil, err
}
refcnt := make(map[util.Key]int)
var keys []util.Key
for encK, v := range rcStore {
if v > 0 {
k := util.B58KeyDecode(encK)
keys = append(keys, k)
refcnt[k] = v
}
}
// log.Debugf("indirPin keys: %#v", keys)
return &indirectPin{blockset: set.SimpleSetFromKeys(keys), refCounts: refcnt}, nil
}
func storeIndirPin(d ds.Datastore, k ds.Key, p *indirectPin) error {
rcStore := map[string]int{}
for k, v := range p.refCounts {
rcStore[util.B58KeyEncode(k)] = v
}
return storeSet(d, k, rcStore)
}
func (i *indirectPin) Increment(k util.Key) {
c := i.refCounts[k]
i.refCounts[k] = c + 1
if c <= 0 {
i.blockset.AddBlock(k)
}
}
func (i *indirectPin) Decrement(k util.Key) {
c := i.refCounts[k] - 1
i.refCounts[k] = c
if c <= 0 {
i.blockset.RemoveBlock(k)
delete(i.refCounts, k)
}
}
func (i *indirectPin) HasKey(k util.Key) bool {
return i.blockset.HasKey(k)
}
func (i *indirectPin) Set() set.BlockSet {
return i.blockset
}
func (i *indirectPin) GetRefs() map[util.Key]int {
return i.refCounts
}