2013-10-04 10:19:43 +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-10-21 09:29:35 +02:00
|
|
|
// Package backend provides a generic interface for manipulating a
|
2013-12-06 01:55:00 +01:00
|
|
|
// BitTorrent tracker's consistent backend data store (usually for
|
|
|
|
// a web application).
|
2013-10-21 09:29:35 +02:00
|
|
|
package backend
|
2013-10-04 10:19:43 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2013-12-01 04:39:02 +01:00
|
|
|
"github.com/chihaya/chihaya/config"
|
|
|
|
"github.com/chihaya/chihaya/storage"
|
2013-10-04 10:19:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2013-12-06 01:55:00 +01:00
|
|
|
panic("backend: Register driver is nil")
|
2013-10-04 10:19:43 +02:00
|
|
|
}
|
|
|
|
if _, dup := drivers[name]; dup {
|
2013-12-06 01:55:00 +01:00
|
|
|
panic("backend: Register called twice for driver " + name)
|
2013-10-04 10:19:43 +02:00
|
|
|
}
|
|
|
|
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(
|
2013-12-06 01:55:00 +01:00
|
|
|
"backend: unknown driver %q (forgotten import?)",
|
2013-10-04 10:19:43 +02:00
|
|
|
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
|
2014-02-23 05:47:11 +01:00
|
|
|
Timestamp int64
|
2013-10-04 10:19:43 +02:00
|
|
|
|
|
|
|
// Snatched is true if this announce completed the download
|
|
|
|
Snatched bool
|
|
|
|
}
|