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"
|
2014-06-24 04:47:43 +02:00
|
|
|
"github.com/chihaya/chihaya/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-06-24 09:59:30 +02:00
|
|
|
New(*config.DriverConfig) Conn
|
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-06-24 04:47:43 +02:00
|
|
|
// Open creates a connection specified by a models configuration.
|
2014-06-24 09:59:30 +02:00
|
|
|
func Open(conf *config.DriverConfig) (Conn, error) {
|
2013-10-04 10:19:43 +02:00
|
|
|
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 {
|
|
|
|
// 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.
|
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.
|
2014-06-24 04:47:43 +02:00
|
|
|
LoadAllUsers(ids []uint64) ([]*models.User, error)
|
2013-10-04 10:19:43 +02:00
|
|
|
}
|