-
Notifications
You must be signed in to change notification settings - Fork 458
/
mem.go
150 lines (123 loc) · 3.5 KB
/
mem.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package repo
import (
"io/ioutil"
"os"
"sync"
"github.com/ipfs/go-datastore"
dss "github.com/ipfs/go-datastore/sync"
"github.com/ipfs/go-ipfs-keystore"
"github.com/filecoin-project/go-filecoin/config"
)
// MemRepo is a mostly (see `stagingDir` and `sealedDir`) in-memory
// implementation of the Repo interface.
type MemRepo struct {
// lk guards the config
lk sync.RWMutex
C *config.Config
D Datastore
Ks keystore.Keystore
W Datastore
Chain Datastore
DealsDs Datastore
version uint
apiAddress string
stagingDir string
sealedDir string
}
var _ Repo = (*MemRepo)(nil)
// NewInMemoryRepo makes a new instance of MemRepo
func NewInMemoryRepo() *MemRepo {
stagingDir, err := ioutil.TempDir("", "staging")
if err != nil {
panic(err)
}
sealedDir, err := ioutil.TempDir("", "staging")
if err != nil {
panic(err)
}
return NewInMemoryRepoWithSectorDirectories(stagingDir, sealedDir)
}
// NewInMemoryRepoWithSectorDirectories makes a new instance of MemRepo
// configured to use the provided directories as sealed and staged
// sector-storage.
func NewInMemoryRepoWithSectorDirectories(staging, sealedDir string) *MemRepo {
return &MemRepo{
C: config.NewDefaultConfig(),
D: dss.MutexWrap(datastore.NewMapDatastore()),
Ks: keystore.MutexWrap(keystore.NewMemKeystore()),
W: dss.MutexWrap(datastore.NewMapDatastore()),
Chain: dss.MutexWrap(datastore.NewMapDatastore()),
DealsDs: dss.MutexWrap(datastore.NewMapDatastore()),
version: Version,
stagingDir: staging,
sealedDir: sealedDir,
}
}
// Config returns the configuration object.
func (mr *MemRepo) Config() *config.Config {
mr.lk.RLock()
defer mr.lk.RUnlock()
return mr.C
}
// ReplaceConfig replaces the current config with the newly passed in one.
func (mr *MemRepo) ReplaceConfig(cfg *config.Config) error {
mr.lk.Lock()
defer mr.lk.Unlock()
mr.C = cfg
return nil
}
func (mr *MemRepo) NewFunc() string {
return "6 x 7"
}
// Datastore returns the datastore.
func (mr *MemRepo) Datastore() Datastore {
return mr.D
}
// Keystore returns the keystore.
func (mr *MemRepo) Keystore() keystore.Keystore {
return mr.Ks
}
// WalletDatastore returns the wallet datastore.
func (mr *MemRepo) WalletDatastore() Datastore {
return mr.W
}
// ChainDatastore returns the chain datastore.
func (mr *MemRepo) ChainDatastore() Datastore {
return mr.Chain
}
// DealsDatastore returns the deals datastore for miners.
func (mr *MemRepo) DealsDatastore() Datastore {
return mr.DealsDs
}
// Version returns the version of the repo.
func (mr *MemRepo) Version() uint {
return mr.version
}
// Close deletes the temporary directories which hold staged piece data and
// sealed sectors.
func (mr *MemRepo) Close() error {
mr.CleanupSectorDirs()
return nil
}
// StagingDir implements node.StagingDir.
func (mr *MemRepo) StagingDir() string {
return mr.stagingDir
}
// SealedDir implements node.SectorDirs.
func (mr *MemRepo) SealedDir() string {
return mr.sealedDir
}
// CleanupSectorDirs removes all sector directories and their contents.
func (mr *MemRepo) CleanupSectorDirs() {
os.RemoveAll(mr.StagingDir()) // nolint: errcheck
os.RemoveAll(mr.SealedDir()) // nolint:errcheck
}
// SetAPIAddr writes the address of the running API to memory.
func (mr *MemRepo) SetAPIAddr(addr string) error {
mr.apiAddress = addr
return nil
}
// APIAddr reads the address of the running API from memory.
func (mr *MemRepo) APIAddr() (string, error) {
return mr.apiAddress, nil
}