2
0
mirror of synced 2025-02-24 14:48:27 +00:00
torrent/storage/bolt.go

65 lines
1.2 KiB
Go
Raw Normal View History

2021-07-14 14:35:52 +10:00
//go:build !noboltdb && !wasm
2021-06-23 17:24:50 +10:00
// +build !noboltdb,!wasm
2016-08-31 17:48:50 +10:00
package storage
import (
"encoding/binary"
"path/filepath"
"time"
2016-08-31 17:48:50 +10:00
2018-04-12 11:34:24 +10:00
"github.com/anacrolix/missinggo/expect"
2020-03-24 12:54:57 +11:00
"go.etcd.io/bbolt"
2019-08-21 20:58:40 +10:00
"github.com/anacrolix/torrent/metainfo"
2016-08-31 17:48:50 +10:00
)
const (
// Chosen to match the usual chunk size in a torrent client. This way, most chunk writes are to
// exactly one full item in bbolt DB.
chunkSize = 1 << 14
)
2016-08-31 18:04:11 +10:00
type boltClient struct {
2020-03-24 12:54:57 +11:00
db *bbolt.DB
2016-08-31 17:48:50 +10:00
}
type boltTorrent struct {
cl *boltClient
2016-08-31 17:48:50 +10:00
ih metainfo.Hash
}
func NewBoltDB(filePath string) ClientImplCloser {
2020-03-24 12:54:57 +11:00
db, err := bbolt.Open(filepath.Join(filePath, "bolt.db"), 0600, &bbolt.Options{
Timeout: time.Second,
})
2018-04-12 11:34:24 +10:00
expect.Nil(err)
db.NoSync = true
return &boltClient{db}
2016-08-31 17:48:50 +10:00
}
func (me *boltClient) Close() error {
return me.db.Close()
}
func (me *boltClient) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) {
t := &boltTorrent{me, infoHash}
return TorrentImpl{
Piece: t.Piece,
Close: t.Close,
}, nil
2016-08-31 17:48:50 +10:00
}
func (me *boltTorrent) Piece(p metainfo.Piece) PieceImpl {
ret := &boltPiece{
p: p,
db: me.cl.db,
ih: me.ih,
}
2016-08-31 17:48:50 +10:00
copy(ret.key[:], me.ih[:])
binary.BigEndian.PutUint32(ret.key[20:], uint32(p.Index()))
return ret
}
func (boltTorrent) Close() error { return nil }