2017-10-12 13:09:32 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2020-02-27 13:45:57 +08:00
|
|
|
"io"
|
2017-10-12 13:09:32 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo/x"
|
2020-03-24 09:54:57 +08:00
|
|
|
"go.etcd.io/bbolt"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2017-10-12 13:09:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type boltDBPiece struct {
|
2020-03-24 09:54:57 +08:00
|
|
|
db *bbolt.DB
|
2017-10-12 13:09:32 +08:00
|
|
|
p metainfo.Piece
|
|
|
|
ih metainfo.Hash
|
|
|
|
key [24]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ PieceImpl = (*boltDBPiece)(nil)
|
|
|
|
dataBucketKey = []byte("data")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (me *boltDBPiece) pc() PieceCompletionGetSetter {
|
|
|
|
return boltPieceCompletion{me.db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *boltDBPiece) pk() metainfo.PieceKey {
|
|
|
|
return metainfo.PieceKey{me.ih, me.p.Index()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *boltDBPiece) Completion() Completion {
|
|
|
|
c, err := me.pc().Get(me.pk())
|
|
|
|
x.Pie(err)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *boltDBPiece) MarkComplete() error {
|
|
|
|
return me.pc().Set(me.pk(), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *boltDBPiece) MarkNotComplete() error {
|
|
|
|
return me.pc().Set(me.pk(), false)
|
|
|
|
}
|
|
|
|
func (me *boltDBPiece) ReadAt(b []byte, off int64) (n int, err error) {
|
2020-03-24 09:54:57 +08:00
|
|
|
err = me.db.View(func(tx *bbolt.Tx) error {
|
2017-10-12 13:09:32 +08:00
|
|
|
db := tx.Bucket(dataBucketKey)
|
|
|
|
if db == nil {
|
2020-02-27 13:45:57 +08:00
|
|
|
return io.EOF
|
2017-10-12 13:09:32 +08:00
|
|
|
}
|
|
|
|
ci := off / chunkSize
|
|
|
|
off %= chunkSize
|
|
|
|
for len(b) != 0 {
|
|
|
|
ck := me.chunkKey(int(ci))
|
|
|
|
_b := db.Get(ck[:])
|
2020-02-27 13:45:57 +08:00
|
|
|
// If the chunk is the wrong size, assume it's missing as we can't rely on the data.
|
2017-10-12 13:09:32 +08:00
|
|
|
if len(_b) != chunkSize {
|
2020-02-27 13:45:57 +08:00
|
|
|
return io.EOF
|
2017-10-12 13:09:32 +08:00
|
|
|
}
|
|
|
|
n1 := copy(b, _b[off:])
|
|
|
|
off = 0
|
|
|
|
ci++
|
|
|
|
b = b[n1:]
|
|
|
|
n += n1
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *boltDBPiece) chunkKey(index int) (ret [26]byte) {
|
|
|
|
copy(ret[:], me.key[:])
|
|
|
|
binary.BigEndian.PutUint16(ret[24:], uint16(index))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *boltDBPiece) WriteAt(b []byte, off int64) (n int, err error) {
|
2020-03-24 09:54:57 +08:00
|
|
|
err = me.db.Update(func(tx *bbolt.Tx) error {
|
2017-10-12 13:09:32 +08:00
|
|
|
db, err := tx.CreateBucketIfNotExists(dataBucketKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ci := off / chunkSize
|
|
|
|
off %= chunkSize
|
|
|
|
for len(b) != 0 {
|
|
|
|
_b := make([]byte, chunkSize)
|
|
|
|
ck := me.chunkKey(int(ci))
|
|
|
|
copy(_b, db.Get(ck[:]))
|
|
|
|
n1 := copy(_b[off:], b)
|
|
|
|
db.Put(ck[:], _b)
|
|
|
|
if n1 > len(b) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
b = b[n1:]
|
|
|
|
off = 0
|
|
|
|
ci++
|
|
|
|
n += n1
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|