]> Sergey Matveev's repositories - btrtrc.git/blob - storage/bolt.go
Progress testing without cgo a bit
[btrtrc.git] / storage / bolt.go
1 package storage
2
3 import (
4         "encoding/binary"
5         "path/filepath"
6         "time"
7
8         "github.com/anacrolix/missinggo/expect"
9         "go.etcd.io/bbolt"
10
11         "github.com/anacrolix/torrent/metainfo"
12 )
13
14 const (
15         // Chosen to match the usual chunk size in a torrent client. This way, most chunk writes are to
16         // exactly one full item in bbolt DB.
17         chunkSize = 1 << 14
18 )
19
20 type boltClient struct {
21         db *bbolt.DB
22 }
23
24 type boltTorrent struct {
25         cl *boltClient
26         ih metainfo.Hash
27 }
28
29 func NewBoltDB(filePath string) ClientImplCloser {
30         db, err := bbolt.Open(filepath.Join(filePath, "bolt.db"), 0600, &bbolt.Options{
31                 Timeout: time.Second,
32         })
33         expect.Nil(err)
34         db.NoSync = true
35         return &boltClient{db}
36 }
37
38 func (me *boltClient) Close() error {
39         return me.db.Close()
40 }
41
42 func (me *boltClient) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) {
43         return &boltTorrent{me, infoHash}, nil
44 }
45
46 func (me *boltTorrent) Piece(p metainfo.Piece) PieceImpl {
47         ret := &boltPiece{
48                 p:  p,
49                 db: me.cl.db,
50                 ih: me.ih,
51         }
52         copy(ret.key[:], me.ih[:])
53         binary.BigEndian.PutUint32(ret.key[20:], uint32(p.Index()))
54         return ret
55 }
56
57 func (boltTorrent) Close() error { return nil }