9 "github.com/anacrolix/torrent/metainfo"
10 "github.com/boltdb/bolt"
14 boltDbCompleteValue = "c"
15 boltDbIncompleteValue = "i"
19 completionBucketKey = []byte("completion")
22 type boltPieceCompletion struct {
26 var _ PieceCompletion = (*boltPieceCompletion)(nil)
28 func NewBoltPieceCompletion(dir string) (ret PieceCompletion, err error) {
29 os.MkdirAll(dir, 0770)
30 p := filepath.Join(dir, ".torrent.bolt.db")
31 db, err := bolt.Open(p, 0660, &bolt.Options{
38 ret = &boltPieceCompletion{db}
42 func (me boltPieceCompletion) Get(pk metainfo.PieceKey) (cn Completion, err error) {
43 err = me.db.View(func(tx *bolt.Tx) error {
44 cb := tx.Bucket(completionBucketKey)
48 ih := cb.Bucket(pk.InfoHash[:])
53 binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
55 switch string(ih.Get(key[:])) {
56 case boltDbCompleteValue:
58 case boltDbIncompleteValue:
68 func (me boltPieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
69 return me.db.Update(func(tx *bolt.Tx) error {
70 c, err := tx.CreateBucketIfNotExists(completionBucketKey)
74 ih, err := c.CreateBucketIfNotExists(pk.InfoHash[:])
79 binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
80 return ih.Put(key[:], []byte(func() string {
82 return boltDbCompleteValue
84 return boltDbIncompleteValue
90 func (me *boltPieceCompletion) Close() error {