forked from anacrolix/torrent
/
boltPieceCompletion.go
73 lines (64 loc) · 1.38 KB
/
boltPieceCompletion.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
package storage
import (
"encoding/binary"
"path/filepath"
"time"
"github.com/boltdb/bolt"
"github.com/anacrolix/torrent/metainfo"
)
var (
value = []byte{}
)
type boltPieceCompletion struct {
db *bolt.DB
}
func newBoltPieceCompletion(dir string) (ret *boltPieceCompletion, err error) {
p := filepath.Join(dir, ".torrent.bolt.db")
db, err := bolt.Open(p, 0660, &bolt.Options{
Timeout: time.Second,
})
if err != nil {
return
}
ret = &boltPieceCompletion{db}
return
}
func (me *boltPieceCompletion) Get(pk metainfo.PieceKey) (ret bool, err error) {
err = me.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(completed)
if c == nil {
return nil
}
ih := c.Bucket(pk.InfoHash[:])
if ih == nil {
return nil
}
var key [4]byte
binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
ret = ih.Get(key[:]) != nil
return nil
})
return
}
func (me *boltPieceCompletion) Set(pk metainfo.PieceKey, b bool) error {
return me.db.Update(func(tx *bolt.Tx) error {
c, err := tx.CreateBucketIfNotExists(completed)
if err != nil {
return err
}
ih, err := c.CreateBucketIfNotExists(pk.InfoHash[:])
if err != nil {
return err
}
var key [4]byte
binary.BigEndian.PutUint32(key[:], uint32(pk.Index))
if b {
return ih.Put(key[:], value)
} else {
return ih.Delete(key[:])
}
})
}
func (me *boltPieceCompletion) Close() error {
return me.db.Close()
}