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-22 01:31:32 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
"github.com/pushrax/chihaya/config"
|
2013-06-22 01:31:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var drivers = make(map[string]StorageDriver)
|
|
|
|
|
|
|
|
type StorageDriver interface {
|
2013-06-22 03:43:11 +02:00
|
|
|
New(*config.Storage) (Storage, error)
|
2013-06-22 01:31:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Register(name string, driver StorageDriver) {
|
|
|
|
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-06-22 03:43:11 +02:00
|
|
|
func New(conf *config.Storage) (Storage, error) {
|
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
store, err := driver.New(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Storage interface {
|
2013-06-23 09:56:28 +02:00
|
|
|
Close() error
|
2013-06-22 01:31:32 +02:00
|
|
|
|
2013-06-22 03:43:11 +02:00
|
|
|
FindUser(passkey string) (*User, bool, error)
|
|
|
|
FindTorrent(infohash string) (*Torrent, bool, error)
|
2013-06-22 01:31:32 +02:00
|
|
|
UnpruneTorrent(torrent *Torrent) error
|
|
|
|
|
|
|
|
RecordUser(
|
|
|
|
user *User,
|
|
|
|
rawDeltaUpload int64,
|
|
|
|
rawDeltaDownload int64,
|
|
|
|
deltaUpload int64,
|
|
|
|
deltaDownload int64,
|
|
|
|
) error
|
|
|
|
RecordSnatch(peer *Peer, now int64) error
|
|
|
|
RecordTorrent(torrent *Torrent, deltaSnatch uint64) error
|
|
|
|
RecordTransferIP(peer *Peer) error
|
|
|
|
RecordTransferHistory(
|
|
|
|
peer *Peer,
|
|
|
|
rawDeltaUpload int64,
|
|
|
|
rawDeltaDownload int64,
|
|
|
|
deltaTime int64,
|
|
|
|
deltaSnatch uint64,
|
|
|
|
active bool,
|
|
|
|
) error
|
|
|
|
}
|