package shuffling: models -> storage, old storage -> web

This commit is contained in:
Jimmy Zelinskie
2013-10-04 04:19:43 -04:00
parent cf53f9554c
commit 2cd7f0d22f
14 changed files with 264 additions and 262 deletions
+4 -4
View File
@@ -11,14 +11,14 @@ import (
"fmt"
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/storage"
"github.com/pushrax/chihaya/storage/web"
_ "github.com/bmizerany/pq"
)
type driver struct{}
func (d *driver) New(conf *config.DataStore) storage.Conn {
func (d *driver) New(conf *config.DataStore) web.Conn {
dsn := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s",
conf.Host,
@@ -47,10 +47,10 @@ func (c *Conn) Start() error {
return nil
}
func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error {
func (c *Conn) RecordAnnounce(delta *web.AnnounceDelta) error {
return nil
}
func init() {
storage.Register("batter", &driver{})
web.Register("batter", &driver{})
}
+5 -5
View File
@@ -5,21 +5,21 @@
package batter
import (
"github.com/pushrax/chihaya/models"
"github.com/pushrax/chihaya/storage"
)
func (c *Conn) LoadTorrents(ids []uint64) ([]*models.Torrent, error) {
func (c *Conn) LoadTorrents(ids []uint64) ([]*storage.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadAllTorrents() ([]*models.Torrent, error) {
func (c *Conn) LoadAllTorrents() ([]*storage.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadUsers(ids []uint64) ([]*models.User, error) {
func (c *Conn) LoadUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil
}
func (c *Conn) LoadAllUsers(ids []uint64) ([]*models.User, error) {
func (c *Conn) LoadAllUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil
}
+4 -4
View File
@@ -12,14 +12,14 @@ import (
"sync"
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/storage"
"github.com/pushrax/chihaya/storage/web"
_ "github.com/go-sql-driver/mysql"
)
type driver struct{}
func (d *driver) New(conf *config.DataStore) storage.Conn {
func (d *driver) New(conf *config.DataStore) web.Conn {
dsn := fmt.Sprintf(
"%s:%s@%s:%s/%s?charset=utf8mb4,utf8",
conf.Username,
@@ -77,7 +77,7 @@ func (c *Conn) Close() error {
return c.DB.Close()
}
func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error {
func (c *Conn) RecordAnnounce(delta *web.AnnounceDelta) error {
snatchCount := 0
if delta.Snatched {
snatchCount = 1
@@ -95,5 +95,5 @@ func (c *Conn) RecordAnnounce(delta *storage.AnnounceDelta) error {
}
func init() {
storage.Register("gazelle", &driver{})
web.Register("gazelle", &driver{})
}
+5 -5
View File
@@ -5,21 +5,21 @@
package gazelle
import (
"github.com/pushrax/chihaya/models"
"github.com/pushrax/chihaya/storage"
)
func (c *Conn) LoadTorrents(ids []uint64) ([]*models.Torrent, error) {
func (c *Conn) LoadTorrents(ids []uint64) ([]*storage.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadAllTorrents() ([]*models.Torrent, error) {
func (c *Conn) LoadAllTorrents() ([]*storage.Torrent, error) {
return nil, nil
}
func (c *Conn) LoadUsers(ids []uint64) ([]*models.User, error) {
func (c *Conn) LoadUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil
}
func (c *Conn) LoadAllUsers(ids []uint64) ([]*models.User, error) {
func (c *Conn) LoadAllUsers(ids []uint64) ([]*storage.User, error) {
return nil, nil
}
+97
View File
@@ -0,0 +1,97 @@
// 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 web provides a generic interface for manipulating a
// BitTorrent tracker's web application data.
package web
import (
"fmt"
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/storage"
)
var drivers = make(map[string]Driver)
type Driver interface {
New(*config.DataStore) Conn
}
// Register makes a database driver available by the provided name.
// If Register is called twice with the same name or if driver is nil,
// it panics.
func Register(name string, driver Driver) {
if driver == nil {
panic("web: Register driver is nil")
}
if _, dup := drivers[name]; dup {
panic("web: Register called twice for driver " + name)
}
drivers[name] = driver
}
// Open creates a connection specified by a storage configuration.
func Open(conf *config.DataStore) (Conn, error) {
driver, ok := drivers[conf.Driver]
if !ok {
return nil, fmt.Errorf(
"web: unknown driver %q (forgotten import?)",
conf.Driver,
)
}
pool := driver.New(conf)
return pool, nil
}
// 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 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) ([]*storage.Torrent, error)
// LoadAllTorrents fetches and returns all torrents.
LoadAllTorrents() ([]*storage.Torrent, error)
// LoadUsers fetches and returns the specified users.
LoadUsers(ids []uint64) ([]*storage.User, error)
// LoadAllUsers fetches and returns all users.
LoadAllUsers(ids []uint64) ([]*storage.User, error)
}
// AnnounceDelta contains a difference in statistics for a peer.
// It is used for communicating changes to be recorded by the driver.
type AnnounceDelta struct {
Peer *storage.Peer
Torrent *storage.Torrent
User *storage.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
}