-
Notifications
You must be signed in to change notification settings - Fork 3
/
dbfactory.go
69 lines (56 loc) · 2.04 KB
/
dbfactory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package shardchain
import (
"fmt"
"path"
"path/filepath"
"time"
"github.com/PositionExchange/posichain/internal/shardchain/leveldb_shard"
"github.com/PositionExchange/posichain/internal/shardchain/local_cache"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/ethdb"
)
const (
LDBDirPrefix = "posichain_db"
LDBShardDirPrefix = "posichain_sharddb"
)
// DBFactory is a blockchain database factory.
type DBFactory interface {
// NewChainDB returns a new database for the blockchain for
// given shard.
NewChainDB(shardID uint32) (ethdb.Database, error)
}
// LDBFactory is a LDB-backed blockchain database factory.
type LDBFactory struct {
RootDir string // directory in which to put shard databases in.
}
// NewChainDB returns a new LDB for the blockchain for given shard.
func (f *LDBFactory) NewChainDB(shardID uint32) (ethdb.Database, error) {
dir := path.Join(f.RootDir, fmt.Sprintf("%s_%d", LDBDirPrefix, shardID))
return rawdb.NewLevelDBDatabase(dir, 256, 1024, "")
}
// MemDBFactory is a memory-backed blockchain database factory.
type MemDBFactory struct{}
// NewChainDB returns a new memDB for the blockchain for given shard.
func (f *MemDBFactory) NewChainDB(shardID uint32) (ethdb.Database, error) {
return rawdb.NewMemoryDatabase(), nil
}
// LDBShardFactory is a merged Multi-LDB-backed blockchain database factory.
type LDBShardFactory struct {
RootDir string // directory in which to put shard databases in.
DiskCount int
ShardCount int
CacheTime int
CacheSize int
}
// NewChainDB returns a new memDB for the blockchain for given shard.
func (f *LDBShardFactory) NewChainDB(shardID uint32) (ethdb.Database, error) {
dir := filepath.Join(f.RootDir, fmt.Sprintf("%s_%d", LDBShardDirPrefix, shardID))
shard, err := leveldb_shard.NewLeveldbShard(dir, f.DiskCount, f.ShardCount)
if err != nil {
return nil, err
}
return rawdb.NewDatabase(local_cache.NewLocalCacheDatabase(shard, local_cache.CacheConfig{
CacheTime: time.Duration(f.CacheTime) * time.Minute,
CacheSize: f.CacheSize,
})), nil
}