tracker/drivers/backend/backend.go

73 lines
2.1 KiB
Go
Raw Normal View History

// 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
// BitTorrent tracker's consistent backend data store (usually for
// a web application).
2013-10-21 09:29:35 +02:00
package backend
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"
)
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.
type Driver interface {
New(*config.DriverConfig) 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("backend: Register driver is nil")
}
if _, dup := drivers[name]; dup {
panic("backend: Register called twice for driver " + name)
}
drivers[name] = driver
}
2014-06-24 04:47:43 +02:00
// Open creates a connection specified by a models configuration.
func Open(conf *config.DriverConfig) (Conn, error) {
driver, ok := drivers[conf.Driver]
if !ok {
return nil, fmt.Errorf(
"backend: 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 {
// 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
// LoadTorrents fetches and returns the specified torrents.
2014-06-24 04:47:43 +02:00
LoadTorrents(ids []uint64) ([]*models.Torrent, error)
// LoadAllTorrents fetches and returns all torrents.
2014-06-24 04:47:43 +02:00
LoadAllTorrents() ([]*models.Torrent, error)
// LoadUsers fetches and returns the specified users.
2014-06-24 04:47:43 +02:00
LoadUsers(ids []uint64) ([]*models.User, error)
// LoadAllUsers fetches and returns all users.
2014-06-24 04:47:43 +02:00
LoadAllUsers(ids []uint64) ([]*models.User, error)
}