]> Sergey Matveev's repositories - btrtrc.git/blob - storage/boltdb.go
Get benchmarks working
[btrtrc.git] / storage / boltdb.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,
16         // most chunk writes are to exactly one full item in bbolt DB.
17         chunkSize = 1 << 14
18 )
19
20 type boltDBClient struct {
21         db *bbolt.DB
22 }
23
24 type boltDBTorrent struct {
25         cl *boltDBClient
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 &boltDBClient{db}
36 }
37
38 func (me *boltDBClient) Close() error {
39         return me.db.Close()
40 }
41
42 func (me *boltDBClient) OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error) {
43         return &boltDBTorrent{me, infoHash}, nil
44 }
45
46 func (me *boltDBTorrent) Piece(p metainfo.Piece) PieceImpl {
47         ret := &boltDBPiece{
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 (boltDBTorrent) Close() error { return nil }