Move backend to root level, combine announce tests, rename files

This commit is contained in:
Justin Li
2014-07-17 01:26:34 -04:00
parent 67a8473f6e
commit 199496bfa6
8 changed files with 95 additions and 107 deletions
+71
View File
@@ -0,0 +1,71 @@
// Copyright 2014 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 backend provides a generic interface for manipulating a
// BitTorrent tracker's consistent backend data store (usually for
// a web application).
package backend
import (
"fmt"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker/models"
)
var drivers = make(map[string]Driver)
// Driver represents an interface to a long-running connection with a
// consistent data store.
type Driver interface {
New(*config.DriverConfig) (Conn, error)
}
// 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
}
// Open creates a connection specified by a configuration.
func Open(cfg *config.DriverConfig) (Conn, error) {
driver, ok := drivers[cfg.Name]
if !ok {
return nil, fmt.Errorf(
"backend: unknown driver %q (forgotten import?)",
cfg.Name,
)
}
return driver.New(cfg)
}
// 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.
RecordAnnounce(delta *models.AnnounceDelta) error
// LoadTorrents fetches and returns the specified torrents.
LoadTorrents(ids []uint64) ([]*models.Torrent, error)
// LoadAllTorrents fetches and returns all torrents.
LoadAllTorrents() ([]*models.Torrent, error)
// LoadUsers fetches and returns the specified users.
LoadUsers(ids []uint64) ([]*models.User, error)
// LoadAllUsers fetches and returns all users.
LoadAllUsers(ids []uint64) ([]*models.User, error)
}
+56
View File
@@ -0,0 +1,56 @@
// Copyright 2014 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 noop implements a Chihaya backend storage driver as a no-op. This is
// useful for running Chihaya as a public tracker.
package noop
import (
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/backend"
"github.com/chihaya/chihaya/tracker/models"
)
type driver struct{}
type NoOp struct{}
// New returns a new Chihaya backend driver that does nothing.
func (d *driver) New(cfg *config.DriverConfig) (backend.Conn, error) {
return &NoOp{}, nil
}
// Close returns nil.
func (n *NoOp) Close() error {
return nil
}
// RecordAnnounce returns nil.
func (n *NoOp) RecordAnnounce(delta *models.AnnounceDelta) error {
return nil
}
// LoadTorrents returns (nil, nil).
func (n *NoOp) LoadTorrents(ids []uint64) ([]*models.Torrent, error) {
return nil, nil
}
// LoadAllTorrents returns (nil, nil).
func (n *NoOp) LoadAllTorrents() ([]*models.Torrent, error) {
return nil, nil
}
// LoadUsers returns (nil, nil).
func (n *NoOp) LoadUsers(ids []uint64) ([]*models.User, error) {
return nil, nil
}
// LoadAllUsers returns (nil, nil).
func (n *NoOp) LoadAllUsers(ids []uint64) ([]*models.User, error) {
return nil, nil
}
func init() {
backend.Register("noop", &driver{})
}