From 8a9b95300af2e86e650c6c8d622a3cca728af5b4 Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Fri, 27 Nov 2020 17:55:57 +0000 Subject: [PATCH] pkg/persistence refactoring --- pkg/persistence/interface.go | 6 ++++-- pkg/persistence/postgres.go | 8 ++------ pkg/persistence/stdout.go | 12 +++++------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/pkg/persistence/interface.go b/pkg/persistence/interface.go index 26fd623..330c9ff 100644 --- a/pkg/persistence/interface.go +++ b/pkg/persistence/interface.go @@ -10,6 +10,8 @@ import ( "go.uber.org/zap" ) +var NotImplementedError = errors.New("Function not implemented") + type Database interface { Engine() databaseEngine DoesTorrentExist(infoHash []byte) (bool, error) @@ -59,8 +61,8 @@ const ( type databaseEngine uint8 const ( - Sqlite3 databaseEngine = 1 - Postgres databaseEngine = 2 + Sqlite3 databaseEngine = iota + 1 + Postgres Stdout ) diff --git a/pkg/persistence/postgres.go b/pkg/persistence/postgres.go index edfdd71..7a98278 100644 --- a/pkg/persistence/postgres.go +++ b/pkg/persistence/postgres.go @@ -197,11 +197,7 @@ func (db *postgresDatabase) QueryTorrents( lastOrderedValue *float64, lastID *uint64, ) ([]TorrentMetadata, error) { - err := fmt.Errorf("QueryTorrents is not supported yet by PostgreSQL backend") - - torrents := make([]TorrentMetadata, 0) - - return torrents, err + return nil, NotImplementedError } func (db *postgresDatabase) GetTorrent(infoHash []byte) (*TorrentMetadata, error) { @@ -259,7 +255,7 @@ func (db *postgresDatabase) GetFiles(infoHash []byte) ([]File, error) { } func (db *postgresDatabase) GetStatistics(from string, n uint) (*Statistics, error) { - panic("Not implemented yet for PostgreSQL") + return nil, NotImplementedError } func (db *postgresDatabase) setupDatabase() error { diff --git a/pkg/persistence/stdout.go b/pkg/persistence/stdout.go index 814e4ac..a6228c8 100644 --- a/pkg/persistence/stdout.go +++ b/pkg/persistence/stdout.go @@ -15,8 +15,6 @@ type out struct { 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) @@ -58,7 +56,7 @@ func (s *stdout) Close() error { } func (s *stdout) GetNumberOfTorrents() (uint, error) { - return 0, notSupportedError + return 0, NotImplementedError } func (s *stdout) QueryTorrents( @@ -70,17 +68,17 @@ func (s *stdout) QueryTorrents( lastOrderedValue *float64, lastID *uint64, ) ([]TorrentMetadata, error) { - return nil, notSupportedError + return nil, NotImplementedError } func (s *stdout) GetTorrent(infoHash []byte) (*TorrentMetadata, error) { - return nil, notSupportedError + return nil, NotImplementedError } func (s *stdout) GetFiles(infoHash []byte) ([]File, error) { - return nil, notSupportedError + return nil, NotImplementedError } func (s *stdout) GetStatistics(from string, n uint) (*Statistics, error) { - return nil, notSupportedError + return nil, NotImplementedError }