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