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

Commit c89041c

Browse files
committed
added write-only interface for stdout and beanstalk
1 parent d822314 commit c89041c

File tree

2 files changed

+50
-41
lines changed

2 files changed

+50
-41
lines changed

pkg/persistence/interface.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ type Database interface {
4242
GetStatistics(from string, n uint) (*Statistics, error)
4343
}
4444

45+
//implements only a subset (the read functions) of Database
46+
type WriteOnlyDatabase struct{kind databaseEngine}
47+
func (wod *WriteOnlyDatabase) WriteOnlyError() error{
48+
return errors.New(`This dummy storage engine ("`+wod.kind.String()+`") is write-only`)
49+
}
50+
func (wod *WriteOnlyDatabase) GetTorrent(infoHash []byte) (*TorrentMetadata, error) {
51+
return nil, wod.WriteOnlyError()
52+
}
53+
func (wod *WriteOnlyDatabase) GetFiles(infoHash []byte) ([]File, error) {
54+
return nil, wod.WriteOnlyError()
55+
}
56+
func (wod *WriteOnlyDatabase) GetStatistics(from string, n uint) (*Statistics, error) {
57+
return nil, wod.WriteOnlyError()
58+
}
59+
func (wod *WriteOnlyDatabase) GetNumberOfTorrents() (uint, error) {
60+
return 0, wod.WriteOnlyError()
61+
}
62+
func (wod *WriteOnlyDatabase) QueryTorrents(
63+
query string,
64+
epoch int64,
65+
orderBy OrderingCriteria,
66+
ascending bool,
67+
limit uint,
68+
lastOrderedValue *float64,
69+
lastID *uint64,
70+
) ([]TorrentMetadata, error) {
71+
return nil, wod.WriteOnlyError()
72+
}
73+
4574
type OrderingCriteria uint8
4675

4776
const (
@@ -57,10 +86,18 @@ const (
5786
// TODO: search `swtich (orderBy)` and see if all cases are covered all the time
5887

5988
type databaseEngine uint8
89+
func( dbe databaseEngine) String() string{
90+
switch dbe{
91+
case Sqlite3:{return "Sqlite3"}
92+
case Stdout:{return "Stdout"}
93+
default:
94+
return "unnamed"
95+
}
96+
}
6097

6198
const (
62-
Sqlite3 databaseEngine = 1
63-
Stdout
99+
Sqlite3 databaseEngine = 1
100+
Stdout databaseEngine = 3
64101
)
65102

66103
type Statistics struct {
@@ -88,6 +125,12 @@ type TorrentMetadata struct {
88125
Relevance float64 `json:"relevance"`
89126
}
90127

128+
type SimpleTorrentSummary struct {
129+
InfoHash string `json:"infoHash"`
130+
Name string `json:"name"`
131+
Files []File `json:"files"`
132+
}
133+
91134
func (tm *TorrentMetadata) MarshalJSON() ([]byte, error) {
92135
type Alias TorrentMetadata
93136
return json.Marshal(&struct {

pkg/persistence/stdout.go

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,20 @@ import (
99
"github.com/pkg/errors"
1010
)
1111

12-
type out struct {
13-
InfoHash string `json:"infoHash"`
14-
Name string `json:"name"`
15-
Files []File `json:"files"`
16-
}
17-
18-
var notSupportedError = errors.New("This dummy database engine (\"stdout\") does not support any sort of queries")
19-
2012
func makeStdoutDatabase(_ *url.URL) (Database, error) {
2113
s := new(stdout)
2214
s.encoder = json.NewEncoder(os.Stdout)
2315
return s, nil
2416
}
2517

2618
type stdout struct {
19+
WriteOnlyDatabase
2720
encoder *json.Encoder
2821
}
2922

3023
func (s *stdout) Engine() databaseEngine {
31-
return Stdout
24+
s.kind = Stdout
25+
return s.kind
3226
}
3327

3428
func (s *stdout) DoesTorrentExist(infoHash []byte) (bool, error) {
@@ -41,7 +35,7 @@ func (s *stdout) DoesTorrentExist(infoHash []byte) (bool, error) {
4135
}
4236

4337
func (s *stdout) AddNewTorrent(infoHash []byte, name string, files []File) error {
44-
err := s.encoder.Encode(out{
38+
err := s.encoder.Encode(SimpleTorrentSummary{
4539
InfoHash: hex.EncodeToString(infoHash),
4640
Name: name,
4741
Files: files,
@@ -55,32 +49,4 @@ func (s *stdout) AddNewTorrent(infoHash []byte, name string, files []File) error
5549

5650
func (s *stdout) Close() error {
5751
return os.Stdout.Sync()
58-
}
59-
60-
func (s *stdout) GetNumberOfTorrents() (uint, error) {
61-
return 0, notSupportedError
62-
}
63-
64-
func (s *stdout) QueryTorrents(
65-
query string,
66-
epoch int64,
67-
orderBy OrderingCriteria,
68-
ascending bool,
69-
limit uint,
70-
lastOrderedValue *float64,
71-
lastID *uint64,
72-
) ([]TorrentMetadata, error) {
73-
return nil, notSupportedError
74-
}
75-
76-
func (s *stdout) GetTorrent(infoHash []byte) (*TorrentMetadata, error) {
77-
return nil, notSupportedError
78-
}
79-
80-
func (s *stdout) GetFiles(infoHash []byte) ([]File, error) {
81-
return nil, notSupportedError
82-
}
83-
84-
func (s *stdout) GetStatistics(from string, n uint) (*Statistics, error) {
85-
return nil, notSupportedError
86-
}
52+
}

0 commit comments

Comments
 (0)