2019-09-18 02:51:58 +02:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
2019-09-18 03:13:55 +02:00
|
|
|
"encoding/hex"
|
2019-09-18 02:51:58 +02:00
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type out struct {
|
2019-09-18 03:13:55 +02:00
|
|
|
InfoHash string `json:"infoHash"`
|
2019-09-18 03:05:29 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
Files []File `json:"files"`
|
2019-09-18 02:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-09-18 03:05:29 +02:00
|
|
|
err := s.encoder.Encode(out{
|
2019-09-18 03:13:55 +02:00
|
|
|
InfoHash: hex.EncodeToString(infoHash),
|
2019-09-18 03:05:29 +02:00
|
|
|
Name: name,
|
|
|
|
Files: files,
|
2019-09-18 02:51:58 +02:00
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|