2021-07-14 12:35:52 +08:00
|
|
|
//go:build !noboltdb && !wasm
|
2021-06-23 15:24:50 +08:00
|
|
|
// +build !noboltdb,!wasm
|
2021-05-25 16:48:59 +08:00
|
|
|
|
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
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
type boltPiece 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 (
|
2021-05-05 10:05:02 +08:00
|
|
|
_ PieceImpl = (*boltPiece)(nil)
|
2017-10-12 13:09:32 +08:00
|
|
|
dataBucketKey = []byte("data")
|
|
|
|
)
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) pc() PieceCompletionGetSetter {
|
2017-10-12 13:09:32 +08:00
|
|
|
return boltPieceCompletion{me.db}
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) pk() metainfo.PieceKey {
|
2017-10-12 13:09:32 +08:00
|
|
|
return metainfo.PieceKey{me.ih, me.p.Index()}
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) Completion() Completion {
|
2017-10-12 13:09:32 +08:00
|
|
|
c, err := me.pc().Get(me.pk())
|
2020-11-14 19:54:40 +08:00
|
|
|
switch err {
|
|
|
|
case bbolt.ErrDatabaseNotOpen:
|
|
|
|
return Completion{}
|
|
|
|
case nil:
|
|
|
|
default:
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-10-12 13:09:32 +08:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) MarkComplete() error {
|
2017-10-12 13:09:32 +08:00
|
|
|
return me.pc().Set(me.pk(), true)
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) MarkNotComplete() error {
|
2017-10-12 13:09:32 +08:00
|
|
|
return me.pc().Set(me.pk(), false)
|
|
|
|
}
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) 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
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) chunkKey(index int) (ret [26]byte) {
|
2017-10-12 13:09:32 +08:00
|
|
|
copy(ret[:], me.key[:])
|
|
|
|
binary.BigEndian.PutUint16(ret[24:], uint16(index))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:05:02 +08:00
|
|
|
func (me *boltPiece) 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
|
|
|
|
}
|