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

View File

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

View File

@ -9,6 +9,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
dataPkg "bitbucket.org/anacrolix/go.torrent/data"
"github.com/anacrolix/libtorgo/metainfo" "github.com/anacrolix/libtorgo/metainfo"
) )
@ -22,8 +24,16 @@ type data struct {
baseDir string baseDir string
} }
func TorrentData(info *metainfo.Info, baseDir string) *data { type store struct {
return &data{info, baseDir} 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 { 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" "testing"
"time" "time"
"bitbucket.org/anacrolix/go.torrent/data"
"golang.org/x/net/context" "golang.org/x/net/context"
"bitbucket.org/anacrolix/go.torrent" "bitbucket.org/anacrolix/go.torrent"
@ -191,8 +193,9 @@ func TestDownloadOnDemand(t *testing.T) {
NoDefaultBlocklist: true, NoDefaultBlocklist: true,
TorrentDataOpener: func(info *metainfo.Info) (torrent.StatelessData, error) { TorrentDataOpener: func(info *metainfo.Info) data.Data {
return mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download")) ret, _ := mmap.TorrentData(info, filepath.Join(layout.BaseDir, "download"))
return ret
}, },
// This can be used to check if clients can connect to other clients // This can be used to check if clients can connect to other clients

View File

@ -12,6 +12,7 @@ import (
"sync" "sync"
"time" "time"
"bitbucket.org/anacrolix/go.torrent/data"
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol" pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
"bitbucket.org/anacrolix/go.torrent/tracker" "bitbucket.org/anacrolix/go.torrent/tracker"
"bitbucket.org/anacrolix/go.torrent/util" "bitbucket.org/anacrolix/go.torrent/util"
@ -43,18 +44,10 @@ type peersKey struct {
Port int 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 // Represents data storage for a Torrent. Additional optional interfaces to
// implement are io.Closer, io.ReaderAt, StatefulData, and SectionOpener. // implement are io.Closer, io.ReaderAt, StatefulData, and SectionOpener.
type Data interface { type Data interface {
StatelessData data.Data
} }
// Data maintains per-piece persistent state. // Data maintains per-piece persistent state.