2013-06-21 21:43:11 -04: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-23 22:34:13 -04:00
|
|
|
// Package storage provides a generic interface for manipulating a
|
|
|
|
// BitTorrent tracker's data store.
|
2013-06-21 19:31:32 -04:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2013-06-21 21:43:11 -04:00
|
|
|
"github.com/pushrax/chihaya/config"
|
2013-06-21 19:31:32 -04:00
|
|
|
)
|
|
|
|
|
2013-06-25 21:58:06 -04:00
|
|
|
var drivers = make(map[string]Driver)
|
2013-06-21 19:31:32 -04:00
|
|
|
|
2013-06-25 21:58:06 -04:00
|
|
|
type Driver interface {
|
2013-07-03 18:24:03 -04:00
|
|
|
New(*config.Storage) Pool
|
2013-06-21 19:31:32 -04:00
|
|
|
}
|
|
|
|
|
2013-06-25 21:58:06 -04:00
|
|
|
func Register(name string, driver Driver) {
|
2013-06-21 19:31:32 -04: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-03 18:24:03 -04:00
|
|
|
func Open(conf *config.Storage) (Pool, error) {
|
2013-06-21 21:43:11 -04:00
|
|
|
driver, ok := drivers[conf.Driver]
|
2013-06-21 19:31:32 -04:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"storage: unknown driver %q (forgotten import?)",
|
2013-06-21 21:43:11 -04:00
|
|
|
conf.Driver,
|
2013-06-21 19:31:32 -04:00
|
|
|
)
|
|
|
|
}
|
2013-07-03 18:24:03 -04:00
|
|
|
pool := driver.New(conf)
|
|
|
|
return pool, nil
|
2013-06-21 19:31:32 -04:00
|
|
|
}
|
|
|
|
|
2013-07-03 18:24:03 -04:00
|
|
|
// ConnPool represents a pool of connections to the data store.
|
|
|
|
type Pool interface {
|
|
|
|
Close() error
|
|
|
|
Get() Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conn represents a single connection to the data store.
|
2013-06-24 14:18:32 -04:00
|
|
|
type Conn interface {
|
2013-06-23 03:56:28 -04:00
|
|
|
Close() error
|
2013-06-21 19:31:32 -04:00
|
|
|
|
2013-07-03 18:24:03 -04:00
|
|
|
NewTx() (Tx, error)
|
|
|
|
|
2013-06-21 21:43:11 -04:00
|
|
|
FindUser(passkey string) (*User, bool, error)
|
|
|
|
FindTorrent(infohash string) (*Torrent, bool, error)
|
2013-07-03 18:24:03 -04:00
|
|
|
}
|
2013-06-21 19:31:32 -04:00
|
|
|
|
2013-07-03 18:24:03 -04:00
|
|
|
// Tx represents a data store transaction.
|
|
|
|
type Tx interface {
|
|
|
|
Commit() error
|
|
|
|
|
|
|
|
UnpruneTorrent(torrent *Torrent) error
|
2013-06-21 19:31:32 -04:00
|
|
|
}
|