]> Sergey Matveev's repositories - btrtrc.git/blob - storage/interface.go
Fix UseSources panicking when sqlite storage is closed
[btrtrc.git] / storage / interface.go
1 package storage
2
3 import (
4         "io"
5
6         "github.com/anacrolix/torrent/metainfo"
7 )
8
9 type ClientImplCloser interface {
10         ClientImpl
11         Close() error
12 }
13
14 // Represents data storage for an unspecified torrent.
15 type ClientImpl interface {
16         OpenTorrent(info *metainfo.Info, infoHash metainfo.Hash) (TorrentImpl, error)
17 }
18
19 type TorrentCapacity *func() (cap int64, capped bool)
20
21 // Data storage bound to a torrent.
22 type TorrentImpl struct {
23         Piece func(p metainfo.Piece) PieceImpl
24         Close func() error
25         Flush func() error
26         // Storages that share the same space, will provide equal pointers. The function is called once
27         // to determine the storage for torrents sharing the same function pointer, and mutated in
28         // place.
29         Capacity TorrentCapacity
30 }
31
32 // Interacts with torrent piece data. Optional interfaces to implement include:
33 //
34 //      io.WriterTo, such as when a piece supports a more efficient way to write out incomplete chunks.
35 //      SelfHashing, such as when a piece supports a more efficient way to hash its contents.
36 type PieceImpl interface {
37         // These interfaces are not as strict as normally required. They can
38         // assume that the parameters are appropriate for the dimensions of the
39         // piece.
40         io.ReaderAt
41         io.WriterAt
42         // Called when the client believes the piece data will pass a hash check.
43         // The storage can move or mark the piece data as read-only as it sees
44         // fit.
45         MarkComplete() error
46         MarkNotComplete() error
47         // Returns true if the piece is complete.
48         Completion() Completion
49 }
50
51 type Completion struct {
52         Complete bool
53         Ok       bool
54         Err      error
55 }
56
57 // Allows a storage backend to override hashing (i.e. if it can do it more efficiently than the torrent client can)
58 type SelfHashing interface {
59         SelfHash() (metainfo.Hash, error)
60 }