2013-06-22 03:43:11 +02:00
|
|
|
// Copyright 2013 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
// Package redis implements the storage interface for a BitTorrent tracker.
|
2013-07-24 09:08:38 +02:00
|
|
|
//
|
2013-07-24 06:18:43 +02:00
|
|
|
// The client whitelist is represented as a set with the key name "whitelist"
|
2013-08-24 06:24:42 +02:00
|
|
|
// with an optional prefix. Torrents and users are represented as hashes.
|
2013-07-24 09:08:38 +02:00
|
|
|
// Torrents' keys are named "torrent:<infohash>" with an optional prefix.
|
2013-08-24 06:24:42 +02:00
|
|
|
// Users' keys are named "user:<passkey>" with an optional prefix. The
|
|
|
|
// seeders and leechers attributes of torrent hashes are strings that represent
|
|
|
|
// the key for those hashes within redis. This is done because redis cannot
|
|
|
|
// nest their hash data type.
|
2013-06-22 01:31:32 +02:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2013-09-07 02:59:12 +02:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
2013-06-26 03:58:06 +02:00
|
|
|
"time"
|
|
|
|
|
2013-06-24 20:18:32 +02:00
|
|
|
"github.com/garyburd/redigo/redis"
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
"github.com/pushrax/chihaya/cache"
|
2013-06-24 20:18:32 +02:00
|
|
|
"github.com/pushrax/chihaya/config"
|
2013-08-23 21:39:42 +02:00
|
|
|
"github.com/pushrax/chihaya/models"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
2013-06-24 20:18:32 +02:00
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
var (
|
|
|
|
ErrCreateUser = errors.New("redis: Incorrect reply length for user")
|
|
|
|
ErrCreateTorrent = errors.New("redis: Incorrect reply length for torrent")
|
|
|
|
ErrCreatePeer = errors.New("redis: Incorrect reply length for peer")
|
|
|
|
ErrMarkActive = errors.New("redis: Torrent doesn't exist")
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
SeederPrefix = "seeders:"
|
|
|
|
LeecherPrefix = "leechers:"
|
2013-09-07 02:59:12 +02:00
|
|
|
TorrentPrefix = "torrent:"
|
|
|
|
UserPrefix = "user:"
|
2013-09-08 02:22:30 +02:00
|
|
|
PeerPrefix = "peer:"
|
2013-09-07 02:59:12 +02:00
|
|
|
)
|
|
|
|
|
2013-06-24 20:18:32 +02:00
|
|
|
type driver struct{}
|
|
|
|
|
2013-08-26 20:38:45 +02:00
|
|
|
func (d *driver) New(conf *config.DataStore) cache.Pool {
|
2013-07-25 16:03:04 +02:00
|
|
|
return &Pool{
|
2013-06-26 03:58:06 +02:00
|
|
|
conf: conf,
|
2013-07-25 16:03:04 +02:00
|
|
|
pool: redis.Pool{
|
2013-09-07 00:39:14 +02:00
|
|
|
MaxIdle: conf.MaxIdleConns,
|
2013-07-05 12:50:52 +02:00
|
|
|
IdleTimeout: conf.IdleTimeout.Duration,
|
2013-06-26 05:08:54 +02:00
|
|
|
Dial: makeDialFunc(conf),
|
|
|
|
TestOnBorrow: testOnBorrow,
|
2013-06-26 03:58:06 +02:00
|
|
|
},
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
2013-06-24 20:18:32 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 20:38:45 +02:00
|
|
|
func makeDialFunc(conf *config.DataStore) func() (redis.Conn, error) {
|
2013-08-14 02:21:01 +02:00
|
|
|
return func() (conn redis.Conn, err error) {
|
2013-08-29 05:44:45 +02:00
|
|
|
conn, err = redis.Dial(conf.Network, conf.Host+":"+conf.Port)
|
2013-06-26 05:08:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOnBorrow(c redis.Conn, t time.Time) error {
|
|
|
|
_, err := c.Do("PING")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
type Pool struct {
|
2013-08-26 20:38:45 +02:00
|
|
|
conf *config.DataStore
|
2013-07-25 16:03:04 +02:00
|
|
|
pool redis.Pool
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
|
|
|
|
2013-07-25 16:03:04 +02:00
|
|
|
func (p *Pool) Close() error {
|
|
|
|
return p.pool.Close()
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (p *Pool) Get() (cache.Tx, error) {
|
2013-09-08 02:22:30 +02:00
|
|
|
retTx := &Tx{
|
2013-09-07 02:59:12 +02:00
|
|
|
conf: p.conf,
|
|
|
|
done: false,
|
|
|
|
Conn: p.pool.Get(),
|
2013-09-08 02:22:30 +02:00
|
|
|
}
|
|
|
|
// Test valid connection before returning
|
|
|
|
_, err := retTx.Do("PING")
|
|
|
|
|
|
|
|
return retTx, err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Tx struct {
|
2013-08-26 20:38:45 +02:00
|
|
|
conf *config.DataStore
|
2013-07-25 16:03:04 +02:00
|
|
|
done bool
|
|
|
|
multi bool
|
|
|
|
redis.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) close() {
|
|
|
|
if tx.done {
|
|
|
|
panic("redis: transaction closed twice")
|
|
|
|
}
|
|
|
|
tx.done = true
|
|
|
|
tx.Conn.Close()
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func createUser(userVals []string) (*models.User, error) {
|
|
|
|
if len(userVals) != 7 {
|
|
|
|
return nil, ErrCreateUser
|
|
|
|
}
|
|
|
|
// This could be a loop+switch
|
|
|
|
ID, err := strconv.ParseUint(userVals[0], 10, 64)
|
2013-08-13 22:06:22 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
Passkey := userVals[1]
|
|
|
|
UpMultiplier, err := strconv.ParseFloat(userVals[2], 64)
|
2013-07-25 16:03:04 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
DownMultiplier, err := strconv.ParseFloat(userVals[3], 64)
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
Slots, err := strconv.ParseInt(userVals[4], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
SlotsUsed, err := strconv.ParseInt(userVals[5], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
Snatches, err := strconv.ParseUint(userVals[6], 10, 64)
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
return &models.User{ID, Passkey, UpMultiplier, DownMultiplier, Slots, SlotsUsed, uint(Snatches)}, nil
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
func (tx *Tx) createTorrent(torrentVals []string) (*models.Torrent, error) {
|
2013-09-07 02:59:12 +02:00
|
|
|
if len(torrentVals) != 7 {
|
|
|
|
return nil, ErrCreateTorrent
|
|
|
|
}
|
|
|
|
ID, err := strconv.ParseUint(torrentVals[0], 10, 64)
|
2013-08-13 22:06:22 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
Infohash := torrentVals[1]
|
|
|
|
Active, err := strconv.ParseBool(torrentVals[2])
|
2013-07-25 16:03:04 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
Snatches, err := strconv.ParseUint(torrentVals[3], 10, 32)
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
UpMultiplier, err := strconv.ParseFloat(torrentVals[4], 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
DownMultiplier, err := strconv.ParseFloat(torrentVals[5], 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
LastAction, err := strconv.ParseInt(torrentVals[6], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-09-08 02:22:30 +02:00
|
|
|
seeders, err := tx.getPeers(ID, SeederPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
leechers, err := tx.getPeers(ID, LeecherPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-07-05 12:50:52 +02:00
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
return &models.Torrent{ID, Infohash, Active, seeders, leechers, uint(Snatches), UpMultiplier, DownMultiplier, LastAction}, nil
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
// hashkey relies on combination of peerID, userID, and torrentID being unique
|
|
|
|
func (tx *Tx) setPeer(peer *models.Peer, peerTypePrefix string) error {
|
|
|
|
hashKey := tx.conf.Prefix + peerTypePrefix + getPeerHashKey(peer)
|
|
|
|
_, err := tx.Do("HMSET", hashKey,
|
|
|
|
"id", peer.ID,
|
|
|
|
"user_id", peer.UserID,
|
|
|
|
"torrent_id", peer.TorrentID,
|
|
|
|
"ip", peer.IP,
|
|
|
|
"port", peer.Port,
|
|
|
|
"uploaded", peer.Uploaded,
|
|
|
|
"downloaded", peer.Downloaded,
|
|
|
|
"left", peer.Left,
|
|
|
|
"last_announce", peer.LastAnnounce)
|
|
|
|
|
2013-06-26 03:58:06 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return err
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil
|
2013-06-26 03:58:06 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
// Will not return an error if the peer doesn't exist
|
2013-09-08 02:22:30 +02:00
|
|
|
func (tx *Tx) removePeer(peer *models.Peer, peerTypePrefix string) error {
|
|
|
|
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(peer.TorrentID, 36)
|
2013-09-07 02:59:12 +02:00
|
|
|
_, err := tx.Do("SREM", setKey, *peer)
|
2013-08-13 22:06:22 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return err
|
2013-07-05 12:50:52 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
2013-07-04 00:24:03 +02:00
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
func (tx *Tx) removePeers(torrentID uint64, peers map[string]models.Peer, peerTypePrefix string) error {
|
2013-09-07 02:59:12 +02:00
|
|
|
for _, peer := range peers {
|
2013-09-08 02:22:30 +02:00
|
|
|
hashKey := tx.conf.Prefix + peerTypePrefix + getPeerHashKey(&peer)
|
|
|
|
_, err := tx.Do("DEL", hashKey)
|
2013-09-07 02:59:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-08 02:22:30 +02:00
|
|
|
delete(peers, peer.ID)
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
2013-09-08 02:22:30 +02:00
|
|
|
// Only delete the set if all the peer deletions were successful
|
|
|
|
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
|
|
|
|
_, err := tx.Do("DEL", setKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
func getPeerHashKey(peer *models.Peer) string {
|
|
|
|
return peer.ID + ":" + strconv.FormatUint(peer.UserID, 36) + ":" + strconv.FormatUint(peer.TorrentID, 36)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) addPeers(peers map[string]models.Peer, peerTypePrefix string) error {
|
|
|
|
for _, peer := range peers {
|
|
|
|
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(peer.TorrentID, 36)
|
|
|
|
_, err := tx.Do("SADD", setKey, getPeerHashKey(&peer))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tx.setPeer(&peer, peerTypePrefix)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createPeer(peerVals []string) (*models.Peer, error) {
|
2013-09-07 02:59:12 +02:00
|
|
|
if len(peerVals) != 9 {
|
|
|
|
return nil, ErrCreatePeer
|
|
|
|
}
|
|
|
|
ID := peerVals[0]
|
|
|
|
UserID, err := strconv.ParseUint(peerVals[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
TorrentID, err := strconv.ParseUint(peerVals[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
IP := peerVals[3]
|
|
|
|
|
|
|
|
Port, err := strconv.ParseUint(peerVals[4], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
Uploaded, err := strconv.ParseUint(peerVals[5], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
Downloaded, err := strconv.ParseUint(peerVals[6], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
Left, err := strconv.ParseUint(peerVals[7], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
LastAnnounce, err := strconv.ParseInt(peerVals[8], 10, 64)
|
2013-07-04 00:24:03 +02:00
|
|
|
if err != nil {
|
2013-09-07 02:59:12 +02:00
|
|
|
return nil, err
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
return &models.Peer{ID, UserID, TorrentID, IP, Port, Uploaded, Downloaded, Left, LastAnnounce}, nil
|
2013-07-05 12:50:52 +02:00
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
func (tx *Tx) getPeers(torrentID uint64, peerTypePrefix string) (peers map[string]models.Peer, err error) {
|
2013-09-07 02:59:12 +02:00
|
|
|
peers = make(map[string]models.Peer)
|
2013-09-08 02:22:30 +02:00
|
|
|
setKey := tx.conf.Prefix + peerTypePrefix + strconv.FormatUint(torrentID, 36)
|
2013-09-07 02:59:12 +02:00
|
|
|
peerStrings, err := redis.Strings(tx.Do("SMEMBERS", setKey))
|
2013-09-08 02:22:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return peers, err
|
|
|
|
}
|
|
|
|
// Keys map to peer objects stored in hashes
|
|
|
|
for _, peerHashKey := range peerStrings {
|
|
|
|
hashKey := tx.conf.Prefix + peerTypePrefix + peerHashKey
|
|
|
|
peerVals, err := redis.Strings(tx.Do("HVALS", hashKey))
|
|
|
|
if err != nil {
|
|
|
|
return peers, err
|
|
|
|
}
|
|
|
|
peer, err := createPeer(peerVals)
|
2013-09-07 02:59:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peers[peer.ID] = *peer
|
|
|
|
}
|
2013-07-25 16:03:04 +02:00
|
|
|
return
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) AddTorrent(t *models.Torrent) error {
|
|
|
|
hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash
|
|
|
|
_, err := tx.Do("HMSET", hashkey,
|
|
|
|
"id", t.ID,
|
|
|
|
"infohash", t.Infohash,
|
|
|
|
"active", t.Active,
|
|
|
|
"snatches", t.Snatches,
|
|
|
|
"up_multiplier", t.UpMultiplier,
|
|
|
|
"down_multiplier", t.DownMultiplier,
|
|
|
|
"last_action", t.LastAction)
|
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
2013-07-22 02:15:48 +02:00
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
tx.addPeers(t.Seeders, SeederPrefix)
|
|
|
|
tx.addPeers(t.Leechers, LeecherPrefix)
|
2013-09-07 02:59:12 +02:00
|
|
|
|
2013-07-22 02:15:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) RemoveTorrent(t *models.Torrent) error {
|
|
|
|
hashkey := tx.conf.Prefix + TorrentPrefix + t.Infohash
|
2013-09-08 02:22:30 +02:00
|
|
|
_, err := tx.Do("DEL", hashkey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove seeders and leechers as well
|
|
|
|
err = tx.removePeers(t.ID, t.Seeders, SeederPrefix)
|
2013-09-07 02:59:12 +02:00
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-05 12:50:52 +02:00
|
|
|
}
|
2013-09-08 02:22:30 +02:00
|
|
|
err = tx.removePeers(t.ID, t.Leechers, LeecherPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-22 02:15:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) AddUser(u *models.User) error {
|
|
|
|
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
|
|
|
_, err := tx.Do("HMSET", hashkey,
|
|
|
|
"id", u.ID,
|
|
|
|
"passkey", u.Passkey,
|
|
|
|
"up_multiplier", u.UpMultiplier,
|
|
|
|
"down_multiplier", u.DownMultiplier,
|
|
|
|
"slots", u.Slots,
|
|
|
|
"slots_used", u.SlotsUsed,
|
|
|
|
"snatches", u.Snatches)
|
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-07-05 12:50:52 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) RemoveUser(u *models.User) error {
|
|
|
|
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
2013-09-08 02:22:30 +02:00
|
|
|
_, err := tx.Do("DEL", hashkey)
|
2013-09-07 02:59:12 +02:00
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-07-24 03:23:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) FindUser(passkey string) (*models.User, bool, error) {
|
|
|
|
hashkey := tx.conf.Prefix + UserPrefix + passkey
|
|
|
|
userStrings, err := redis.Strings(tx.Do("HVALS", hashkey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
} else if len(userStrings) == 0 {
|
|
|
|
return nil, false, nil
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
foundUser, err := createUser(userStrings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return foundUser, true, nil
|
2013-07-22 02:15:48 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
// This is a mulple action command, it's not internally atomic
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) FindTorrent(infohash string) (*models.Torrent, bool, error) {
|
|
|
|
hashkey := tx.conf.Prefix + TorrentPrefix + infohash
|
|
|
|
torrentStrings, err := redis.Strings(tx.Do("HVALS", hashkey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
} else if len(torrentStrings) == 0 {
|
|
|
|
return nil, false, nil
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
foundTorrent, err := tx.createTorrent(torrentStrings)
|
2013-09-07 02:59:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return foundTorrent, true, nil
|
2013-07-22 02:15:48 +02:00
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) ClientWhitelisted(peerID string) (exists bool, err error) {
|
|
|
|
key := tx.conf.Prefix + "whitelist"
|
2013-09-08 02:22:30 +02:00
|
|
|
return redis.Bool(tx.Do("SISMEMBER", key, peerID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) WhitelistClient(peerID string) error {
|
|
|
|
key := tx.conf.Prefix + "whitelist"
|
|
|
|
_, err := tx.Do("SADD", key, peerID)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) UnWhitelistClient(peerID string) error {
|
|
|
|
key := tx.conf.Prefix + "whitelist"
|
|
|
|
_, err := tx.Do("SREM", key, peerID)
|
|
|
|
return err
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a mulple action command, it's not internally atomic
|
|
|
|
func (tx *Tx) RecordSnatch(user *models.User, torrent *models.Torrent) error {
|
|
|
|
|
|
|
|
torrentKey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
|
|
|
|
snatchCount, err := redis.Int(tx.Do("HINCRBY", torrentKey, 1))
|
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
torrent.Snatches = uint(snatchCount)
|
2013-07-25 16:03:04 +02:00
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
userKey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
|
|
|
|
snatchCount, err = redis.Int(tx.Do("HINCRBY", userKey, 1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user.Snatches = uint(snatchCount)
|
2013-07-24 03:23:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) MarkActive(torrent *models.Torrent) error {
|
|
|
|
hashkey := tx.conf.Prefix + TorrentPrefix + torrent.Infohash
|
|
|
|
activeExists, err := redis.Int(tx.Do("HSET", hashkey, true))
|
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
// HSET returns 1 if hash didn't exist before
|
|
|
|
if activeExists == 1 {
|
|
|
|
return ErrMarkActive
|
|
|
|
}
|
2013-07-25 16:03:04 +02:00
|
|
|
return nil
|
2013-07-22 02:15:48 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
func (tx *Tx) AddLeecher(torrent *models.Torrent, peer *models.Peer) error {
|
|
|
|
setKey := tx.conf.Prefix + LeecherPrefix + strconv.FormatUint(torrent.ID, 36)
|
|
|
|
_, err := tx.Do("SADD", setKey, getPeerHashKey(peer))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tx.setPeer(peer, LeecherPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if torrent.Leechers == nil {
|
|
|
|
torrent.Leechers = make(map[string]models.Peer)
|
|
|
|
}
|
|
|
|
torrent.Leechers[peer.ID] = *peer
|
|
|
|
return nil
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
// Setting assumes it is already a leecher, and just needs to be updated
|
|
|
|
// Maybe eventually there will be a move from leecher to seeder method
|
2013-09-07 02:59:12 +02:00
|
|
|
func (tx *Tx) SetLeecher(t *models.Torrent, p *models.Peer) error {
|
2013-09-08 02:22:30 +02:00
|
|
|
return tx.setPeer(p, LeecherPrefix)
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) RemoveLeecher(t *models.Torrent, p *models.Peer) error {
|
2013-09-08 02:22:30 +02:00
|
|
|
err := tx.removePeer(p, LeecherPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(t.Leechers, p.ID)
|
|
|
|
return nil
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
2013-09-08 02:22:30 +02:00
|
|
|
func (tx *Tx) AddSeeder(torrent *models.Torrent, peer *models.Peer) error {
|
|
|
|
setKey := tx.conf.Prefix + SeederPrefix + strconv.FormatUint(torrent.ID, 36)
|
|
|
|
_, err := tx.Do("SADD", setKey, getPeerHashKey(peer))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = tx.setPeer(peer, SeederPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if torrent.Seeders == nil {
|
|
|
|
torrent.Seeders = make(map[string]models.Peer)
|
|
|
|
}
|
|
|
|
torrent.Seeders[peer.ID] = *peer
|
|
|
|
return nil
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) SetSeeder(t *models.Torrent, p *models.Peer) error {
|
2013-09-08 02:22:30 +02:00
|
|
|
return tx.setPeer(p, SeederPrefix)
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *Tx) RemoveSeeder(t *models.Torrent, p *models.Peer) error {
|
2013-09-08 02:22:30 +02:00
|
|
|
err := tx.removePeer(p, SeederPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(t.Seeders, p.ID)
|
|
|
|
return nil
|
2013-09-07 02:59:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) IncrementSlots(u *models.User) error {
|
2013-09-07 02:59:12 +02:00
|
|
|
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
|
|
|
slotCount, err := redis.Int(tx.Do("HINCRBY", hashkey, 1))
|
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
u.Slots = int64(slotCount)
|
2013-07-25 16:03:04 +02:00
|
|
|
return nil
|
2013-07-22 02:15:48 +02:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:39:42 +02:00
|
|
|
func (tx *Tx) DecrementSlots(u *models.User) error {
|
2013-09-07 02:59:12 +02:00
|
|
|
hashkey := tx.conf.Prefix + UserPrefix + u.Passkey
|
|
|
|
slotCount, err := redis.Int(tx.Do("HINCRBY", hashkey, -1))
|
|
|
|
if err != nil {
|
2013-08-13 08:20:44 +02:00
|
|
|
return err
|
2013-07-25 16:03:04 +02:00
|
|
|
}
|
2013-09-07 02:59:12 +02:00
|
|
|
u.Slots = int64(slotCount)
|
2013-07-25 16:03:04 +02:00
|
|
|
return nil
|
2013-06-24 20:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2013-08-23 21:39:42 +02:00
|
|
|
cache.Register("redis", &driver{})
|
2013-06-24 20:18:32 +02:00
|
|
|
}
|