pkg/persistence refactoring

This commit is contained in:
Bora M. Alper 2020-11-27 17:55:57 +00:00
parent 0a497e8fea
commit 8a9b95300a
No known key found for this signature in database
GPG Key ID: 8F1A9504E1BD114D
3 changed files with 11 additions and 15 deletions

View File

@ -10,6 +10,8 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
var NotImplementedError = errors.New("Function not implemented")
type Database interface { type Database interface {
Engine() databaseEngine Engine() databaseEngine
DoesTorrentExist(infoHash []byte) (bool, error) DoesTorrentExist(infoHash []byte) (bool, error)
@ -59,8 +61,8 @@ const (
type databaseEngine uint8 type databaseEngine uint8
const ( const (
Sqlite3 databaseEngine = 1 Sqlite3 databaseEngine = iota + 1
Postgres databaseEngine = 2 Postgres
Stdout Stdout
) )

View File

@ -197,11 +197,7 @@ func (db *postgresDatabase) QueryTorrents(
lastOrderedValue *float64, lastOrderedValue *float64,
lastID *uint64, lastID *uint64,
) ([]TorrentMetadata, error) { ) ([]TorrentMetadata, error) {
err := fmt.Errorf("QueryTorrents is not supported yet by PostgreSQL backend") return nil, NotImplementedError
torrents := make([]TorrentMetadata, 0)
return torrents, err
} }
func (db *postgresDatabase) GetTorrent(infoHash []byte) (*TorrentMetadata, error) { 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) { func (db *postgresDatabase) GetStatistics(from string, n uint) (*Statistics, error) {
panic("Not implemented yet for PostgreSQL") return nil, NotImplementedError
} }
func (db *postgresDatabase) setupDatabase() error { func (db *postgresDatabase) setupDatabase() error {

View File

@ -15,8 +15,6 @@ type out struct {
Files []File `json:"files"` 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) { func makeStdoutDatabase(_ *url.URL) (Database, error) {
s := new(stdout) s := new(stdout)
s.encoder = json.NewEncoder(os.Stdout) s.encoder = json.NewEncoder(os.Stdout)
@ -58,7 +56,7 @@ func (s *stdout) Close() error {
} }
func (s *stdout) GetNumberOfTorrents() (uint, error) { func (s *stdout) GetNumberOfTorrents() (uint, error) {
return 0, notSupportedError return 0, NotImplementedError
} }
func (s *stdout) QueryTorrents( func (s *stdout) QueryTorrents(
@ -70,17 +68,17 @@ func (s *stdout) QueryTorrents(
lastOrderedValue *float64, lastOrderedValue *float64,
lastID *uint64, lastID *uint64,
) ([]TorrentMetadata, error) { ) ([]TorrentMetadata, error) {
return nil, notSupportedError return nil, NotImplementedError
} }
func (s *stdout) GetTorrent(infoHash []byte) (*TorrentMetadata, error) { func (s *stdout) GetTorrent(infoHash []byte) (*TorrentMetadata, error) {
return nil, notSupportedError return nil, NotImplementedError
} }
func (s *stdout) GetFiles(infoHash []byte) ([]File, error) { func (s *stdout) GetFiles(infoHash []byte) ([]File, error) {
return nil, notSupportedError return nil, NotImplementedError
} }
func (s *stdout) GetStatistics(from string, n uint) (*Statistics, error) { func (s *stdout) GetStatistics(from string, n uint) (*Statistics, error) {
return nil, notSupportedError return nil, NotImplementedError
} }