tracker/cache/cache.go

85 lines
2.4 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-08-26 20:38:45 +02:00
// Package cache provides a generic interface for manipulating a
2013-08-24 06:24:42 +02:00
// BitTorrent tracker's fast moving data.
2013-08-23 21:39:42 +02:00
package cache
2013-06-22 01:31:32 +02:00
import (
"errors"
2013-06-22 01:31:32 +02:00
"fmt"
"github.com/pushrax/chihaya/config"
2013-08-23 21:39:42 +02:00
"github.com/pushrax/chihaya/models"
2013-06-22 01:31:32 +02:00
)
var (
drivers = make(map[string]Driver)
2013-08-26 20:38:45 +02:00
ErrTxDone = errors.New("cache: Transaction has already been committed or rolled back")
)
2013-06-22 01:31:32 +02:00
2013-06-26 03:58:06 +02:00
type Driver interface {
2013-08-26 20:38:45 +02:00
New(*config.DataStore) Pool
2013-06-22 01:31:32 +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.
2013-06-26 03:58:06 +02:00
func Register(name string, driver Driver) {
2013-06-22 01:31:32 +02:00
if driver == nil {
2013-08-26 20:38:45 +02:00
panic("cache: Register driver is nil")
2013-06-22 01:31:32 +02:00
}
if _, dup := drivers[name]; dup {
2013-08-26 20:38:45 +02:00
panic("cache: Register called twice for driver " + name)
2013-06-22 01:31:32 +02:00
}
drivers[name] = driver
}
// Open creates a pool of data store connections specified by a storage configuration.
2013-08-26 20:38:45 +02:00
func Open(conf *config.DataStore) (Pool, error) {
driver, ok := drivers[conf.Driver]
2013-06-22 01:31:32 +02:00
if !ok {
return nil, fmt.Errorf(
2013-08-26 20:38:45 +02:00
"cache: unknown driver %q (forgotten import?)",
conf.Driver,
2013-06-22 01:31:32 +02:00
)
}
2013-07-04 00:24:03 +02:00
pool := driver.New(conf)
return pool, nil
2013-06-22 01:31:32 +02:00
}
// Pool represents a thread-safe pool of connections to the data store
// that can be used to obtain transactions.
type Pool interface {
2013-06-23 09:56:28 +02:00
Close() error
Get() (Tx, error)
2013-07-04 00:24:03 +02:00
}
2013-06-22 01:31:32 +02:00
// Tx represents an in-progress data store transaction.
// A transaction must end with a call to Commit or Rollback.
//
// After a call to Commit or Rollback, all operations on the
// transaction must fail with ErrTxDone.
2013-07-04 00:24:03 +02:00
type Tx interface {
Commit() error
Rollback() error
2013-07-04 00:24:03 +02:00
// Reads
2013-08-23 21:39:42 +02:00
FindUser(passkey string) (*models.User, bool, error)
FindTorrent(infohash string) (*models.Torrent, bool, error)
ClientWhitelisted(peerID string) (bool, error)
2013-07-12 06:36:24 +02:00
// Writes
2013-08-23 21:39:42 +02:00
RecordSnatch(u *models.User, t *models.Torrent) error
MarkActive(t *models.Torrent) error
AddLeecher(t *models.Torrent, p *models.Peer) error
AddSeeder(t *models.Torrent, p *models.Peer) error
RemoveLeecher(t *models.Torrent, p *models.Peer) error
RemoveSeeder(t *models.Torrent, p *models.Peer) error
SetLeecher(t *models.Torrent, p *models.Peer) error
SetSeeder(t *models.Torrent, p *models.Peer) error
IncrementSlots(u *models.User) error
DecrementSlots(u *models.User) error
2013-06-22 01:31:32 +02:00
}