1 //go:build !noboltdb && !wasm
2 // +build !noboltdb,!wasm
14 "github.com/anacrolix/torrent/metainfo"
18 boltDbCompleteValue = "c"
19 boltDbIncompleteValue = "i"
23 completionBucketKey = []byte("completion")
26 type boltPieceCompletion struct {
30 var _ PieceCompletion = (*boltPieceCompletion)(nil)
32 func NewBoltPieceCompletion(dir string) (ret PieceCompletion, err error) {
33 os.MkdirAll(dir, 0770)
34 p := filepath.Join(dir, ".torrent.bolt.db")
35 db, err := bbolt.Open(p, 0660, &bbolt.Options{
42 ret = &boltPieceCompletion{db}
46 func (me boltPieceCompletion) Get(pk metainfo.PieceKey) (cn Completion, err error) {
47 err = me.db.View(func(tx *bbolt.Tx) error {
48 cb := tx.Bucket(completionBucketKey)
52 ih := cb.Bucket(pk.InfoHash[:])
57 binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
59 switch string(ih.Get(key[:])) {
60 case boltDbCompleteValue:
62 case boltDbIncompleteValue:
72 func (me boltPieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
73 if c, err := me.Get(pk); err == nil && c.Ok && c.Complete == b {
76 return me.db.Update(func(tx *bbolt.Tx) error {
77 c, err := tx.CreateBucketIfNotExists(completionBucketKey)
81 ih, err := c.CreateBucketIfNotExists(pk.InfoHash[:])
86 binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
87 return ih.Put(key[:], []byte(func() string {
89 return boltDbCompleteValue
91 return boltDbIncompleteValue
97 func (me *boltPieceCompletion) Close() error {