rework exclude repository

This commit is contained in:
royalcat 2023-12-26 22:30:19 +03:00
parent 0332206560
commit cd6cf8dd74
4 changed files with 22 additions and 29 deletions

View file

@ -73,10 +73,7 @@ func run(configPath string) error {
log.Err(err).Msg("set priority failed")
}
if err := os.MkdirAll(filepath.Join(conf.TorrentClient.MetadataFolder, "meta"), 0744); err != nil {
return fmt.Errorf("error creating metadata folder: %w", err)
}
rep, err := repository.NewTorrentMetaRepository(filepath.Join(conf.TorrentClient.MetadataFolder, "meta"))
rep, err := repository.NewTorrentMetaRepository(conf.TorrentClient.MetadataFolder)
if err != nil {
return err
}

View file

@ -2,6 +2,7 @@ package repository
import (
"errors"
"path/filepath"
"sync"
"github.com/anacrolix/torrent/metainfo"
@ -10,34 +11,31 @@ import (
"github.com/philippgille/gokv/encoding"
)
type TorrentMetaRepository interface {
type TorrentsRepository interface {
ExcludeFile(hash metainfo.Hash, file ...string) error
ExcludedFiles(hash metainfo.Hash) ([]string, error)
}
func NewTorrentMetaRepository(dir string) (TorrentMetaRepository, error) {
store, err := badgerdb.NewStore(badgerdb.Options{
Dir: dir,
func NewTorrentMetaRepository(dir string) (TorrentsRepository, error) {
excludedFilesStore, err := badgerdb.NewStore(badgerdb.Options{
Dir: filepath.Join(dir, "excluded-files"),
Codec: encoding.JSON,
})
if err != nil {
return nil, err
}
r := &torrentRepositoryImpl{
store: store,
excludedFiles: excludedFilesStore,
}
return r, nil
}
type torrentRepositoryImpl struct {
m sync.RWMutex
store gokv.Store
}
type torrentMeta struct {
ExludedFiles []string
m sync.RWMutex
excludedFiles gokv.Store
}
var ErrNotFound = errors.New("not found")
@ -46,27 +44,25 @@ func (r *torrentRepositoryImpl) ExcludeFile(hash metainfo.Hash, file ...string)
r.m.Lock()
defer r.m.Unlock()
var meta torrentMeta
found, err := r.store.Get(hash.AsString(), &meta)
var excludedFiles []string
found, err := r.excludedFiles.Get(hash.AsString(), &excludedFiles)
if err != nil {
return err
}
if !found {
meta = torrentMeta{
ExludedFiles: file,
}
excludedFiles = []string{}
}
meta.ExludedFiles = unique(append(meta.ExludedFiles, file...))
excludedFiles = unique(append(excludedFiles, file...))
return r.store.Set(hash.AsString(), meta)
return r.excludedFiles.Set(hash.AsString(), excludedFiles)
}
func (r *torrentRepositoryImpl) ExcludedFiles(hash metainfo.Hash) ([]string, error) {
r.m.Lock()
defer r.m.Unlock()
var meta torrentMeta
found, err := r.store.Get(hash.AsString(), &meta)
var excludedFiles []string
found, err := r.excludedFiles.Get(hash.AsString(), &excludedFiles)
if err != nil {
return nil, err
}
@ -74,7 +70,7 @@ func (r *torrentRepositoryImpl) ExcludedFiles(hash metainfo.Hash) ([]string, err
return nil, nil
}
return meta.ExludedFiles, nil
return excludedFiles, nil
}
func unique[C comparable](intSlice []C) []C {

View file

@ -15,7 +15,7 @@ import (
type Service struct {
c *torrent.Client
rep repository.TorrentMetaRepository
rep repository.TorrentsRepository
// stats *Stats
DefaultPriority types.PiecePriority
@ -24,7 +24,7 @@ type Service struct {
addTimeout, readTimeout int
}
func NewService(c *torrent.Client, rep repository.TorrentMetaRepository, addTimeout, readTimeout int) *Service {
func NewService(c *torrent.Client, rep repository.TorrentsRepository, addTimeout, readTimeout int) *Service {
l := slog.With("component", "torrent-service")
return &Service{
log: l,

View file

@ -21,7 +21,7 @@ var _ Filesystem = &TorrentFs{}
type TorrentFs struct {
mu sync.Mutex
t *torrent.Torrent
rep repository.TorrentMetaRepository
rep repository.TorrentsRepository
readTimeout int
@ -31,7 +31,7 @@ type TorrentFs struct {
resolver *resolver
}
func NewTorrentFs(t *torrent.Torrent, rep repository.TorrentMetaRepository, readTimeout int) *TorrentFs {
func NewTorrentFs(t *torrent.Torrent, rep repository.TorrentsRepository, readTimeout int) *TorrentFs {
return &TorrentFs{
t: t,
rep: rep,