From 7c881cae653291df358df82add3b77d8623a8feb Mon Sep 17 00:00:00 2001 From: "Bora M. Alper" Date: Wed, 18 Sep 2019 01:51:58 +0100 Subject: [PATCH] add Stdout dummy database engine --- pkg/persistence/interface.go | 4 ++ pkg/persistence/stdout.go | 85 ++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 pkg/persistence/stdout.go diff --git a/pkg/persistence/interface.go b/pkg/persistence/interface.go index 61986c3..1225ce1 100644 --- a/pkg/persistence/interface.go +++ b/pkg/persistence/interface.go @@ -60,6 +60,7 @@ type databaseEngine uint8 const ( Sqlite3 databaseEngine = 1 + Stdout ) type Statistics struct { @@ -112,6 +113,9 @@ func MakeDatabase(rawURL string, logger *zap.Logger) (Database, error) { case "sqlite3": return makeSqlite3Database(url_) + case "stdout": + return makeStdoutDatabase(url_) + case "postgresql": return nil, fmt.Errorf("postgresql is not yet supported") diff --git a/pkg/persistence/stdout.go b/pkg/persistence/stdout.go new file mode 100644 index 0000000..501681f --- /dev/null +++ b/pkg/persistence/stdout.go @@ -0,0 +1,85 @@ +package persistence + +import ( + "encoding/json" + "net/url" + "os" + + "github.com/pkg/errors" +) + +type out struct { + InfoHash []byte `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) + return s, nil +} + +type stdout struct { + encoder *json.Encoder +} + +func (s *stdout) Engine() databaseEngine { + return Stdout +} + +func (s *stdout) DoesTorrentExist(infoHash []byte) (bool, error) { + // Always say that "No the torrent does not exist" because we do not have + // a way to know if we have seen it before or not. + // TODO: + // A possible improvement would be using bloom filters (with low false positive + // probabilities) to apply some reasonable filtering. + return false, nil +} + +func (s *stdout) AddNewTorrent(infoHash []byte, name string, files []File) error { + err := s.encoder.Encode(out { + InfoHash: infoHash, + Name: name, + Files: files, + }) + if err != nil { + return errors.Wrap(err, "DB engine stdout encode error") + } + + return nil +} + +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 +}