Change up the data storage interfaces a bit

This commit is contained in:
Matt Joiner 2015-03-07 17:11:45 +11:00
parent cad611bbcf
commit 3e753bb8ad
6 changed files with 47 additions and 24 deletions

View File

@ -36,6 +36,7 @@ import (
"syscall"
"time"
"bitbucket.org/anacrolix/go.torrent/data"
filePkg "bitbucket.org/anacrolix/go.torrent/data/file"
"bitbucket.org/anacrolix/go.torrent/dht"
"bitbucket.org/anacrolix/go.torrent/internal/pieceordering"
@ -455,8 +456,8 @@ func NewClient(cfg *Config) (cl *Client, err error) {
disableTCP: cfg.DisableTCP,
_configDir: cfg.ConfigDir,
config: *cfg,
torrentDataOpener: func(md *metainfo.Info) (StatelessData, error) {
return filePkg.TorrentData(md, cfg.DataDir), nil
torrentDataOpener: func(md *metainfo.Info) data.Data {
return filePkg.TorrentData(md, cfg.DataDir)
},
quit: make(chan struct{}),
@ -1601,7 +1602,7 @@ func (cl *Client) setStorage(t *torrent, td Data) (err error) {
return
}
type TorrentDataOpener func(*metainfo.Info) (StatelessData, error)
type TorrentDataOpener func(*metainfo.Info) data.Data
func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err error) {
err = t.setMetadata(md, bytes, &cl.mu)
@ -1619,10 +1620,7 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
return
}
close(t.gotMetainfo)
td, err := cl.torrentDataOpener(&md)
if err != nil {
return
}
td := cl.torrentDataOpener(&md)
err = cl.setStorage(t, td)
return
}

View File

@ -12,7 +12,6 @@ import (
"time"
"bitbucket.org/anacrolix/go.torrent/data/blob"
"github.com/anacrolix/libtorgo/metainfo"
"github.com/bradfitz/iter"
@ -254,9 +253,10 @@ func TestClientTransfer(t *testing.T) {
t.Fatal(err)
}
defer os.RemoveAll(leecherDataDir)
cfg.TorrentDataOpener = func(info *metainfo.Info) (StatelessData, error) {
return blob.TorrentData(info, leecherDataDir), nil
}
// cfg.TorrentDataOpener = func(info *metainfo.Info) (data.Data, error) {
// return blob.TorrentData(info, leecherDataDir), nil
// }
cfg.TorrentDataOpener = blob.NewStore(leecherDataDir).OpenTorrent
leecher, _ := NewClient(&cfg)
defer leecher.Stop()
leecherGreeting, _ := leecher.AddTorrent(mi)

View File

@ -9,6 +9,8 @@ import (
"os"
"path/filepath"
dataPkg "bitbucket.org/anacrolix/go.torrent/data"
"github.com/anacrolix/libtorgo/metainfo"
)
@ -22,8 +24,16 @@ type data struct {
baseDir string
}
func TorrentData(info *metainfo.Info, baseDir string) *data {
return &data{info, baseDir}
type store struct {
baseDir string
}
func (me store) OpenTorrent(info *metainfo.Info) dataPkg.Data {
return &data{info, me.baseDir}
}
func NewStore(baseDir string) dataPkg.Store {
return store{baseDir}
}
func (me *data) pieceHashHex(i int) string {

19
data/data.go Normal file
View File

@ -0,0 +1,19 @@
package data
import (
"io"
"github.com/anacrolix/libtorgo/metainfo"
)
type Store interface {
OpenTorrent(*metainfo.Info) Data
}
type Data interface {
// OpenSection(off, n int64) (io.ReadCloser, error)
// ReadAt(p []byte, off int64) (n int, err error)
// Close()
WriteAt(p []byte, off int64) (n int, err error)
WriteSectionTo(w io.Writer, off, n int64) (written int64, err error)
}

View File

@ -14,6 +14,8 @@ import (
"testing"
"time"
"bitbucket.org/anacrolix/go.torrent/data"
"golang.org/x/net/context"
"bitbucket.org/anacrolix/go.torrent"
@ -191,8 +193,9 @@ func TestDownloadOnDemand(t *testing.T) {
NoDefaultBlocklist: true,
TorrentDataOpener: func(info *metainfo.Info) (torrent.StatelessData, error) {
return mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download"))
TorrentDataOpener: func(info *metainfo.Info) data.Data {
ret, _ := mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download"))
return ret
},
// This can be used to check if clients can connect to other clients

View File

@ -12,6 +12,7 @@ import (
"sync"
"time"
"bitbucket.org/anacrolix/go.torrent/data"
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
"bitbucket.org/anacrolix/go.torrent/tracker"
"bitbucket.org/anacrolix/go.torrent/util"
@ -43,18 +44,10 @@ type peersKey struct {
Port int
}
type StatelessData interface {
// OpenSection(off, n int64) (io.ReadCloser, error)
// ReadAt(p []byte, off int64) (n int, err error)
// Close()
WriteAt(p []byte, off int64) (n int, err error)
WriteSectionTo(w io.Writer, off, n int64) (written int64, err error)
}
// Represents data storage for a Torrent. Additional optional interfaces to
// implement are io.Closer, io.ReaderAt, StatefulData, and SectionOpener.
type Data interface {
StatelessData
data.Data
}
// Data maintains per-piece persistent state.