2019-01-10 13:13:27 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
pstore "storj.io/storj/pkg/piecestore"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/pkg/piecestore/psserver/psdb"
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/teststore"
|
|
|
|
"storj.io/storj/storagenode"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ storagenode.DB = (*DB)(nil)
|
|
|
|
|
|
|
|
// DB contains access to different database tables
|
|
|
|
type DB struct {
|
2019-01-11 11:26:39 +00:00
|
|
|
storage *pstore.Storage
|
2019-01-10 13:13:27 +00:00
|
|
|
psdb *psdb.DB
|
|
|
|
kdb, ndb storage.KeyValueStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInMemory creates new inmemory database for storagenode
|
|
|
|
// TODO: still stores data on disk
|
|
|
|
func NewInMemory(storageDir string) (*DB, error) {
|
2019-01-11 11:26:39 +00:00
|
|
|
storage := pstore.NewStorage(storageDir)
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// TODO: OpenInMemory shouldn't need context argument
|
2019-01-11 11:26:39 +00:00
|
|
|
psdb, err := psdb.OpenInMemory(context.TODO(), storage)
|
2019-01-10 13:13:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DB{
|
2019-01-11 11:26:39 +00:00
|
|
|
storage: storage,
|
|
|
|
psdb: psdb,
|
|
|
|
kdb: teststore.New(),
|
|
|
|
ndb: teststore.New(),
|
2019-01-10 13:13:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes any resources.
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return errs.Combine(
|
|
|
|
db.psdb.Close(),
|
|
|
|
db.kdb.Close(),
|
|
|
|
db.ndb.Close(),
|
2019-01-11 11:26:39 +00:00
|
|
|
db.storage.Close(),
|
2019-01-10 13:13:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:26:39 +00:00
|
|
|
// Storage returns piecestore location
|
|
|
|
func (db *DB) Storage() *pstore.Storage {
|
|
|
|
return db.storage
|
2019-01-10 13:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PSDB returns piecestore database
|
|
|
|
func (db *DB) PSDB() *psdb.DB {
|
|
|
|
return db.psdb
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoutingTable returns kademlia routing table
|
|
|
|
func (db *DB) RoutingTable() (kdb, ndb storage.KeyValueStore) {
|
|
|
|
return db.kdb, db.ndb
|
|
|
|
}
|