From c89041c70065254ee5dff999714a067da2f8f014 Mon Sep 17 00:00:00 2001 From: Luca Ruggieri Date: Thu, 14 Nov 2019 20:00:52 +0100 Subject: [PATCH] added write-only interface for stdout and beanstalk --- pkg/persistence/interface.go | 47 ++++++++++++++++++++++++++++++++++-- pkg/persistence/stdout.go | 44 ++++----------------------------- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/pkg/persistence/interface.go b/pkg/persistence/interface.go index 1225ce171..a1c531f83 100644 --- a/pkg/persistence/interface.go +++ b/pkg/persistence/interface.go @@ -42,6 +42,35 @@ type Database interface { GetStatistics(from string, n uint) (*Statistics, error) } +//implements only a subset (the read functions) of Database +type WriteOnlyDatabase struct{kind databaseEngine} +func (wod *WriteOnlyDatabase) WriteOnlyError() error{ + return errors.New(`This dummy storage engine ("`+wod.kind.String()+`") is write-only`) +} +func (wod *WriteOnlyDatabase) GetTorrent(infoHash []byte) (*TorrentMetadata, error) { + return nil, wod.WriteOnlyError() +} +func (wod *WriteOnlyDatabase) GetFiles(infoHash []byte) ([]File, error) { + return nil, wod.WriteOnlyError() +} +func (wod *WriteOnlyDatabase) GetStatistics(from string, n uint) (*Statistics, error) { + return nil, wod.WriteOnlyError() +} +func (wod *WriteOnlyDatabase) GetNumberOfTorrents() (uint, error) { + return 0, wod.WriteOnlyError() +} +func (wod *WriteOnlyDatabase) QueryTorrents( + query string, + epoch int64, + orderBy OrderingCriteria, + ascending bool, + limit uint, + lastOrderedValue *float64, + lastID *uint64, +) ([]TorrentMetadata, error) { + return nil, wod.WriteOnlyError() +} + type OrderingCriteria uint8 const ( @@ -57,10 +86,18 @@ const ( // TODO: search `swtich (orderBy)` and see if all cases are covered all the time type databaseEngine uint8 +func( dbe databaseEngine) String() string{ + switch dbe{ + case Sqlite3:{return "Sqlite3"} + case Stdout:{return "Stdout"} + default: + return "unnamed" + } +} const ( - Sqlite3 databaseEngine = 1 - Stdout + Sqlite3 databaseEngine = 1 + Stdout databaseEngine = 3 ) type Statistics struct { @@ -88,6 +125,12 @@ type TorrentMetadata struct { Relevance float64 `json:"relevance"` } +type SimpleTorrentSummary struct { + InfoHash string `json:"infoHash"` + Name string `json:"name"` + Files []File `json:"files"` +} + func (tm *TorrentMetadata) MarshalJSON() ([]byte, error) { type Alias TorrentMetadata return json.Marshal(&struct { diff --git a/pkg/persistence/stdout.go b/pkg/persistence/stdout.go index 814e4ac92..b9922f3d4 100644 --- a/pkg/persistence/stdout.go +++ b/pkg/persistence/stdout.go @@ -9,14 +9,6 @@ import ( "github.com/pkg/errors" ) -type out struct { - InfoHash string `json:"infoHash"` - Name string `json:"name"` - Files []File `json:"files"` -} - -var notSupportedError = errors.New("This dummy database engine (\"stdout\") does not support any sort of queries") - func makeStdoutDatabase(_ *url.URL) (Database, error) { s := new(stdout) s.encoder = json.NewEncoder(os.Stdout) @@ -24,11 +16,13 @@ func makeStdoutDatabase(_ *url.URL) (Database, error) { } type stdout struct { + WriteOnlyDatabase encoder *json.Encoder } func (s *stdout) Engine() databaseEngine { - return Stdout + s.kind = Stdout + return s.kind } func (s *stdout) DoesTorrentExist(infoHash []byte) (bool, error) { @@ -41,7 +35,7 @@ func (s *stdout) DoesTorrentExist(infoHash []byte) (bool, error) { } func (s *stdout) AddNewTorrent(infoHash []byte, name string, files []File) error { - err := s.encoder.Encode(out{ + err := s.encoder.Encode(SimpleTorrentSummary{ InfoHash: hex.EncodeToString(infoHash), Name: name, Files: files, @@ -55,32 +49,4 @@ func (s *stdout) AddNewTorrent(infoHash []byte, name string, files []File) error func (s *stdout) Close() error { return os.Stdout.Sync() -} - -func (s *stdout) GetNumberOfTorrents() (uint, error) { - return 0, notSupportedError -} - -func (s *stdout) QueryTorrents( - query string, - epoch int64, - orderBy OrderingCriteria, - ascending bool, - limit uint, - lastOrderedValue *float64, - lastID *uint64, -) ([]TorrentMetadata, error) { - return nil, notSupportedError -} - -func (s *stdout) GetTorrent(infoHash []byte) (*TorrentMetadata, error) { - return nil, notSupportedError -} - -func (s *stdout) GetFiles(infoHash []byte) ([]File, error) { - return nil, notSupportedError -} - -func (s *stdout) GetStatistics(from string, n uint) (*Statistics, error) { - return nil, notSupportedError -} +} \ No newline at end of file