2015-01-01 18:02:25 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
2013-10-04 10:19:43 +02:00
|
|
|
// 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"
|
2014-07-17 06:09:56 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker/models"
|
2013-10-04 10:19:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var drivers = make(map[string]Driver)
|
|
|
|
|
2014-05-01 06:30:46 +02:00
|
|
|
// Driver represents an interface to a long-running connection with a
|
|
|
|
// consistent data store.
|
2013-10-04 10:19:43 +02:00
|
|
|
type Driver interface {
|
2014-07-03 23:37:13 +02:00
|
|
|
New(*config.DriverConfig) (Conn, error)
|
2013-10-04 10:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
// Open creates a connection specified by a configuration.
|
2014-07-03 23:37:13 +02:00
|
|
|
func Open(cfg *config.DriverConfig) (Conn, error) {
|
|
|
|
driver, ok := drivers[cfg.Name]
|
2013-10-04 10:19:43 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
2013-12-06 01:55:00 +01:00
|
|
|
"backend: unknown driver %q (forgotten import?)",
|
2014-07-03 23:37:13 +02:00
|
|
|
cfg.Name,
|
2013-10-04 10:19:43 +02:00
|
|
|
)
|
|
|
|
}
|
2014-07-03 23:37:13 +02:00
|
|
|
return driver.New(cfg)
|
2013-10-04 10:19:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Conn represents a connection to the data store.
|
|
|
|
type Conn interface {
|
|
|
|
// Close terminates connections to the database(s) and gracefully shuts
|
|
|
|
// down the driver
|
|
|
|
Close() error
|
|
|
|
|
2015-02-08 23:14:48 +01:00
|
|
|
// Ping just checks to see if the database is still alive. This is typically
|
|
|
|
// used for health checks.
|
|
|
|
Ping() error
|
|
|
|
|
2013-10-04 10:19:43 +02:00
|
|
|
// RecordAnnounce is called once per announce, and is passed the delta in
|
|
|
|
// statistics for the client peer since its last announce.
|
2014-06-24 04:47:43 +02:00
|
|
|
RecordAnnounce(delta *models.AnnounceDelta) error
|
2013-10-04 10:19:43 +02:00
|
|
|
|
|
|
|
// LoadTorrents fetches and returns the specified torrents.
|
2014-06-24 04:47:43 +02:00
|
|
|
LoadTorrents(ids []uint64) ([]*models.Torrent, error)
|
2013-10-04 10:19:43 +02:00
|
|
|
|
|
|
|
// LoadAllTorrents fetches and returns all torrents.
|
2014-06-24 04:47:43 +02:00
|
|
|
LoadAllTorrents() ([]*models.Torrent, error)
|
2013-10-04 10:19:43 +02:00
|
|
|
|
|
|
|
// LoadUsers fetches and returns the specified users.
|
2014-06-24 04:47:43 +02:00
|
|
|
LoadUsers(ids []uint64) ([]*models.User, error)
|
2013-10-04 10:19:43 +02:00
|
|
|
|
|
|
|
// LoadAllUsers fetches and returns all users.
|
2015-10-28 19:08:11 +01:00
|
|
|
LoadAllUsers() ([]*models.User, error)
|
2013-10-04 10:19:43 +02:00
|
|
|
}
|