2013-06-22 03:43:11 +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-06-24 04:34:13 +02:00
|
|
|
// Package storage provides a generic interface for manipulating a
|
|
|
|
// BitTorrent tracker's data store.
|
2013-06-22 01:31:32 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-07-05 12:50:52 +02:00
|
|
|
"errors"
|
2013-06-22 01:31:32 +02:00
|
|
|
"fmt"
|
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
"github.com/pushrax/chihaya/config"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
var (
|
|
|
|
drivers = make(map[string]Driver)
|
|
|
|
ErrTxDone = errors.New("storage: 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-07-05 12:50:52 +02:00
|
|
|
New(*config.Storage) DS
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 12:50:52 +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 {
|
|
|
|
panic("storage: Register driver is nil")
|
|
|
|
}
|
|
|
|
if _, dup := drivers[name]; dup {
|
|
|
|
panic("storage: Register called twice for driver " + name)
|
|
|
|
}
|
|
|
|
drivers[name] = driver
|
|
|
|
}
|
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
// Open opens a data store specified by a storage configuration.
|
|
|
|
func Open(conf *config.Storage) (DS, error) {
|
2013-06-22 03:43:11 +02:00
|
|
|
driver, ok := drivers[conf.Driver]
|
2013-06-22 01:31:32 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"storage: unknown driver %q (forgotten import?)",
|
2013-06-22 03:43:11 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
// DS represents a data store handle. It's expected to be safe for concurrent
|
|
|
|
// use by multiple goroutines.
|
|
|
|
//
|
|
|
|
// A pool of connections or a database/sql.DB is a great concrete type to
|
|
|
|
// implement the DS interface.
|
|
|
|
type DS interface {
|
2013-06-23 09:56:28 +02:00
|
|
|
Close() error
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-07-05 12:50:52 +02:00
|
|
|
Begin() (Tx, error)
|
2013-07-04 00:24:03 +02:00
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
FindUser(passkey string) (*User, bool, error)
|
|
|
|
FindTorrent(infohash string) (*Torrent, bool, error)
|
2013-07-05 12:50:52 +02:00
|
|
|
ClientWhitelisted(peerID string) (bool, error)
|
2013-07-04 00:24:03 +02:00
|
|
|
}
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-07-05 12:50:52 +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
|
2013-07-05 12:50:52 +02:00
|
|
|
Rollback() error
|
2013-07-04 00:24:03 +02:00
|
|
|
|
2013-07-12 06:36:24 +02:00
|
|
|
// Torrents
|
|
|
|
Snatch(userID, torrentID uint64) error
|
|
|
|
Unprune(torrentID uint64) error
|
|
|
|
|
|
|
|
// Peers
|
|
|
|
NewLeecher(torrent *Torrent, p *Peer) error
|
|
|
|
RmLeecher(torrentID uint64, peerID string) error
|
|
|
|
|
|
|
|
NewSeeder(torrent *Torrent, p *Peer) error
|
|
|
|
RmSeeder(torrentID uint64, peerID string) error
|
|
|
|
|
|
|
|
// Users
|
|
|
|
DecrementSlots(userID uint64) error
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|