Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
added write-only interface for stdout and beanstalk
Browse files Browse the repository at this point in the history
  • Loading branch information
lruggieri committed Nov 14, 2019
1 parent d822314 commit c89041c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 41 deletions.
47 changes: 45 additions & 2 deletions pkg/persistence/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
44 changes: 5 additions & 39 deletions pkg/persistence/stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ 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)
return s, nil
}

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) {
Expand All @@ -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,
Expand All @@ -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
}
}

0 comments on commit c89041c

Please sign in to comment.