9 "github.com/boltdb/bolt"
11 "github.com/anacrolix/torrent/metainfo"
18 type boltPieceCompletion struct {
22 func NewBoltPieceCompletion(dir string) (ret PieceCompletion, err error) {
23 os.MkdirAll(dir, 0770)
24 p := filepath.Join(dir, ".torrent.bolt.db")
25 db, err := bolt.Open(p, 0660, &bolt.Options{
31 ret = &boltPieceCompletion{db}
35 func (me *boltPieceCompletion) Get(pk metainfo.PieceKey) (ret bool, err error) {
36 err = me.db.View(func(tx *bolt.Tx) error {
37 c := tx.Bucket(completed)
41 ih := c.Bucket(pk.InfoHash[:])
46 binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
47 ret = ih.Get(key[:]) != nil
53 func (me *boltPieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
54 return me.db.Update(func(tx *bolt.Tx) error {
55 c, err := tx.CreateBucketIfNotExists(completed)
59 ih, err := c.CreateBucketIfNotExists(pk.InfoHash[:])
64 binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
66 return ih.Put(key[:], value)
68 return ih.Delete(key[:])
73 func (me *boltPieceCompletion) Close() error {