indirect.go 908 Bytes
Newer Older
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
package pin

import (
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
	bc "github.com/jbenet/go-ipfs/blocks/set"
	"github.com/jbenet/go-ipfs/util"
)

type indirectPin struct {
	blockset  bc.BlockSet
	refCounts map[util.Key]int
}

func loadBlockSet(d ds.Datastore) (bc.BlockSet, map[util.Key]int) {
	panic("Not yet implemented!")
	return nil, nil
}

func newIndirectPin(d ds.Datastore) indirectPin {
	// suppose the blockset actually takes blocks, not just keys
	bs, rc := loadBlockSet(d)
	return indirectPin{bs, rc}
}

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)
	}
}

func (i *indirectPin) HasKey(k util.Key) bool {
	return i.blockset.HasKey(k)
}