diff --git a/models/models.go b/models/models.go index 2ea8527..15b3a9a 100644 --- a/models/models.go +++ b/models/models.go @@ -8,42 +8,36 @@ type Peer struct { ID string `json:"id"` UserID uint64 `json:"user_id"` TorrentID uint64 `json:"torrent_id"` - IP string `json:"ip"` - Port uint64 `json:"port"` - Uploaded uint64 `json:"uploaded"` - Downloaded uint64 `json:"downloaded` - Left uint64 `json:"left"` + + IP string `json:"ip"` + Port uint64 `json:"port"` + + Uploaded uint64 `json:"uploaded"` + Downloaded uint64 `json:"downloaded` + Left uint64 `json:"left"` LastAnnounce int64 `json:"last_announce"` } type Torrent struct { - ID uint64 `json:"id"` - Infohash string `json:"infohash"` - Active bool `json:"active"` - Seeders map[string]Peer `json:"seeders"` - Leechers map[string]Peer `json:"leechers"` - Snatches uint `json:"snatches"` - UpMultiplier float64 `json:"up_multiplier"` - DownMultiplier float64 `json:"down_multiplier"` - LastAction int64 `json:"last_action"` + ID uint64 `json:"id"` + Infohash string `json:"infohash"` + Active bool `json:"active"` + + Seeders map[string]Peer `json:"seeders"` + Leechers map[string]Peer `json:"leechers"` + + Snatches uint `json:"snatches"` + UpMultiplier float64 `json:"up_multiplier"` + DownMultiplier float64 `json:"down_multiplier"` + LastAction int64 `json:"last_action"` } type User struct { - ID uint64 `json:"id"` - Passkey string `json:"passkey"` + ID uint64 `json:"id"` + Passkey string `json:"passkey"` + UpMultiplier float64 `json:"up_multiplier"` DownMultiplier float64 `json:"down_multiplier"` Slots int64 `json:"slots"` SlotsUsed int64 `json:"slots_used"` } - -type AnnounceDelta struct { - Peer *Peer - Torrent *Torrent - User *User - - Uploaded uint64 - Downloaded uint64 - Timestamp float64 - Snatched bool -} diff --git a/storage/batter/batter.go b/storage/batter/batter.go index 63e9114..311436e 100644 --- a/storage/batter/batter.go +++ b/storage/batter/batter.go @@ -11,7 +11,6 @@ import ( "fmt" "github.com/pushrax/chihaya/config" - "github.com/pushrax/chihaya/models" "github.com/pushrax/chihaya/storage" _ "github.com/bmizerany/pq" @@ -48,7 +47,7 @@ func (c *Conn) Start() error { return nil } -func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { +func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error { return nil } diff --git a/storage/batter/load.go b/storage/batter/load.go new file mode 100644 index 0000000..9e35bd2 --- /dev/null +++ b/storage/batter/load.go @@ -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 +} diff --git a/storage/gazelle/gazelle.go b/storage/gazelle/gazelle.go index 237c916..484409b 100644 --- a/storage/gazelle/gazelle.go +++ b/storage/gazelle/gazelle.go @@ -12,7 +12,6 @@ import ( "sync" "github.com/pushrax/chihaya/config" - "github.com/pushrax/chihaya/models" "github.com/pushrax/chihaya/storage" _ "github.com/go-sql-driver/mysql" @@ -78,7 +77,7 @@ func (c *Conn) Close() error { return c.DB.Close() } -func (c *Conn) RecordAnnounce(delta *models.AnnounceDelta) error { +func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error { snatchCount := 0 if delta.Snatched { snatchCount = 1 diff --git a/storage/gazelle/load.go b/storage/gazelle/load.go new file mode 100644 index 0000000..6b5320e --- /dev/null +++ b/storage/gazelle/load.go @@ -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 +} diff --git a/storage/storage.go b/storage/storage.go index b452e76..9949fe2 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -13,9 +13,7 @@ import ( "github.com/pushrax/chihaya/models" ) -var ( - drivers = make(map[string]Driver) -) +var drivers = make(map[string]Driver) type Driver interface { New(*config.DataStore) Conn @@ -49,7 +47,51 @@ func Open(conf *config.DataStore) (Conn, error) { // Conn represents a connection to the data store. 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 + + // Close terminates connections to the database(s) and gracefully shuts + // down the driver 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 }