Add interface for loading data from storage

This commit is contained in:
Justin Li 2013-09-06 21:38:56 -04:00
parent eff8e70cde
commit 7e402b162f
6 changed files with 119 additions and 35 deletions

View file

@ -8,42 +8,36 @@ type Peer struct {
ID string `json:"id"` ID string `json:"id"`
UserID uint64 `json:"user_id"` UserID uint64 `json:"user_id"`
TorrentID uint64 `json:"torrent_id"` TorrentID uint64 `json:"torrent_id"`
IP string `json:"ip"`
Port uint64 `json:"port"` IP string `json:"ip"`
Uploaded uint64 `json:"uploaded"` Port uint64 `json:"port"`
Downloaded uint64 `json:"downloaded`
Left uint64 `json:"left"` Uploaded uint64 `json:"uploaded"`
Downloaded uint64 `json:"downloaded`
Left uint64 `json:"left"`
LastAnnounce int64 `json:"last_announce"` LastAnnounce int64 `json:"last_announce"`
} }
type Torrent struct { type Torrent struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
Infohash string `json:"infohash"` Infohash string `json:"infohash"`
Active bool `json:"active"` Active bool `json:"active"`
Seeders map[string]Peer `json:"seeders"`
Leechers map[string]Peer `json:"leechers"` Seeders map[string]Peer `json:"seeders"`
Snatches uint `json:"snatches"` Leechers map[string]Peer `json:"leechers"`
UpMultiplier float64 `json:"up_multiplier"`
DownMultiplier float64 `json:"down_multiplier"` Snatches uint `json:"snatches"`
LastAction int64 `json:"last_action"` UpMultiplier float64 `json:"up_multiplier"`
DownMultiplier float64 `json:"down_multiplier"`
LastAction int64 `json:"last_action"`
} }
type User struct { type User struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
Passkey string `json:"passkey"` Passkey string `json:"passkey"`
UpMultiplier float64 `json:"up_multiplier"` UpMultiplier float64 `json:"up_multiplier"`
DownMultiplier float64 `json:"down_multiplier"` DownMultiplier float64 `json:"down_multiplier"`
Slots int64 `json:"slots"` Slots int64 `json:"slots"`
SlotsUsed int64 `json:"slots_used"` SlotsUsed int64 `json:"slots_used"`
} }
type AnnounceDelta struct {
Peer *Peer
Torrent *Torrent
User *User
Uploaded uint64
Downloaded uint64
Timestamp float64
Snatched bool
}

View file

@ -11,7 +11,6 @@ import (
"fmt" "fmt"
"github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/models"
"github.com/pushrax/chihaya/storage" "github.com/pushrax/chihaya/storage"
_ "github.com/bmizerany/pq" _ "github.com/bmizerany/pq"
@ -48,7 +47,7 @@ func (c *Conn) Start() error {
return nil return nil
} }
func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error {
return nil return nil
} }

25
storage/batter/load.go Normal file
View file

@ -0,0 +1,25 @@
// 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.
package batter
import (
"github.com/pushrax/chihaya/models"
)
func (c *Conn) LoadTorrents(ids []uint64) ([]*models.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadAllTorrents() ([]*models.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadUsers(ids []uint64) ([]*models.User, error) {
return nil, nil
}
func (c *Conn) LoadAllUsers(ids []uint64) ([]*models.User, error) {
return nil, nil
}

View file

@ -12,7 +12,6 @@ import (
"sync" "sync"
"github.com/pushrax/chihaya/config" "github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/models"
"github.com/pushrax/chihaya/storage" "github.com/pushrax/chihaya/storage"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -78,7 +77,7 @@ func (c *Conn) Close() error {
return c.DB.Close() return c.DB.Close()
} }
func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error {
snatchCount := 0 snatchCount := 0
if delta.Snatched { if delta.Snatched {
snatchCount = 1 snatchCount = 1

25
storage/gazelle/load.go Normal file
View file

@ -0,0 +1,25 @@
// 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.
package gazelle
import (
"github.com/pushrax/chihaya/models"
)
func (c *Conn) LoadTorrents(ids []uint64) ([]*models.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadAllTorrents() ([]*models.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadUsers(ids []uint64) ([]*models.User, error) {
return nil, nil
}
func (c *Conn) LoadAllUsers(ids []uint64) ([]*models.User, error) {
return nil, nil
}

View file

@ -13,9 +13,7 @@ import (
"github.com/pushrax/chihaya/models" "github.com/pushrax/chihaya/models"
) )
var ( var drivers = make(map[string]Driver)
drivers = make(map[string]Driver)
)
type Driver interface { type Driver interface {
New(*config.DataStore) Conn New(*config.DataStore) Conn
@ -49,7 +47,51 @@ func Open(conf *config.DataStore) (Conn, error) {
// Conn represents a connection to the data store. // Conn represents a connection to the data store.
type Conn interface { type Conn interface {
// Start is called once when the server starts.
// It starts any necessary goroutines a given driver requires, and sets
// up the driver's initial state
Start() error Start() error
// Close terminates connections to the database(s) and gracefully shuts
// down the driver
Close() error Close() error
RecordAnnounce(delta *models.AnnounceDelta) error
// RecordAnnounce is called once per announce, and is passed the delta in
// statistics for the client peer since its last announce.
RecordAnnounce(delta *AnnounceDelta) error
// LoadTorrents fetches and returns the specified torrents.
LoadTorrents(ids []uint64) ([]*models.Torrent, error)
// LoadAllTorrents fetches and returns all torrents.
LoadAllTorrents() ([]*models.Torrent, error)
// LoadUsers fetches and returns the specified users.
LoadUsers(ids []uint64) ([]*models.User, error)
// LoadAllUsers fetches and returns all users.
LoadAllUsers(ids []uint64) ([]*models.User, error)
}
// AnnounceDelta contains a difference in statistics for a peer.
// It is used for communicating changes to be recorded by the storage driver.
type AnnounceDelta struct {
Peer *models.Peer
Torrent *models.Torrent
User *models.User
// Created is true if this announce created a new peer or changed an existing peer's address
Created bool
// Uploaded contains the raw upload delta for this announce, in bytes
Uploaded uint64
// Downloaded contains the raw download delta for this announce, in bytes
Downloaded uint64
// Timestamp is the unix timestamp this announce occurred at
Timestamp float64
// Snatched is true if this announce completed the download
Snatched bool
} }