forked from gochain/gochain
/
db.go
304 lines (253 loc) · 7.64 KB
/
db.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package ethdb
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/gochain-io/gochain/common"
"github.com/gochain-io/gochain/log"
"github.com/syndtr/goleveldb/leveldb"
)
// Database errors.
var (
ErrInvalidSegmentType = errors.New("ethdb: Invalid segment type")
)
// Prefix used for each table type.
const (
HeaderPrefix = "h"
BlockHashPrefix = "H"
BodyPrefix = "b"
BlockReceiptsPrefix = "r"
LookupPrefix = "l"
BloomBitsPrefix = "B"
)
const (
// DefaultPartitionSize is the default number of blocks per partition.
DefaultPartitionSize = 1024
// DefaultMinMutableSegmentCount is the minimum number of mutable segments on a given table.
DefaultMinMutableSegmentCount = 40
// DefaultMinCompactionAge is the minimum age after creation before an LDB
// segment can be compacted into a file segment.
DefaultMinCompactionAge = 1 * time.Minute
)
// DB is the top-level database and contains a mixture of LevelDB & File storage layers.
type DB struct {
mu sync.RWMutex
global *Table
body *Table
header *Table
receipt *Table
// Filename of the root of the database.
Path string
// Number of blocks grouped together.
PartitionSize uint64
// Maximum number of segments that can be opened at once.
MaxOpenSegmentCount int
// Age before LDB segment can be compacted to a file segment.
MinCompactionAge time.Duration
SegmentOpener SegmentOpener
SegmentCompactor SegmentCompactor
}
// NewDB returns a new instance of DB.
func NewDB(path string) *DB {
return &DB{
Path: path,
PartitionSize: DefaultPartitionSize,
MaxOpenSegmentCount: DefaultMaxOpenSegmentCount,
MinCompactionAge: DefaultMinCompactionAge,
SegmentOpener: NewFileSegmentOpener(),
SegmentCompactor: NewFileSegmentCompactor(),
}
}
// Open initializes and opens the database.
func (db *DB) Open() error {
// Migrate database first, if necessary.
if err := db.migrate(); err != nil {
return err
}
// Ensure directory exists.
if err := os.MkdirAll(db.Path, 0777); err != nil {
return err
}
db.global = NewTable("global", db.TablePath("global"), &StaticPartitioner{Name: "data"})
db.body = NewTable("body", db.TablePath("body"), NewBlockNumberPartitioner(db.PartitionSize))
db.header = NewTable("header", db.TablePath("header"), NewBlockNumberPartitioner(db.PartitionSize))
db.receipt = NewTable("receipt", db.TablePath("receipt"), NewBlockNumberPartitioner(db.PartitionSize))
for _, tbl := range db.Tables() {
// Allow 100x header files since they are small.
tbl.MaxOpenSegmentCount = db.MaxOpenSegmentCount
if tbl.Name == "header" {
tbl.MaxOpenSegmentCount *= 100
}
tbl.MinCompactionAge = db.MinCompactionAge
tbl.SegmentOpener = db.SegmentOpener
tbl.SegmentCompactor = db.SegmentCompactor
if err := tbl.Open(); err != nil {
log.Error("Cannot open table", "name", tbl.Name, "err", err)
db.Close()
return err
}
}
return nil
}
// Close closes all underlying tables.
func (db *DB) Close() error {
for _, tbl := range db.Tables() {
if tbl != nil {
tbl.Close()
}
}
return nil
}
// TablePath returns the filename for the given table.
func (db *DB) TablePath(name string) string {
return filepath.Join(db.Path, name)
}
// GlobalTable returns the global, statically partitioned table.
func (db *DB) GlobalTable() common.Table { return db.global }
// BodyTable returns the table which holds body data.
func (db *DB) BodyTable() common.Table { return db.body }
// HeaderTable returns the table which holds header data.
func (db *DB) HeaderTable() common.Table { return db.header }
// ReceiptTable returns the table which holds receipt data.
func (db *DB) ReceiptTable() common.Table { return db.receipt }
// Tables returns a sorted list of all tables.
func (db *DB) Tables() []*Table {
return []*Table{db.global, db.body, db.header, db.receipt}
}
// Table returns a table by name.
func (db *DB) Table(name string) *Table {
switch name {
case "global":
return db.global
case "body":
return db.body
case "header":
return db.header
case "receipt":
return db.receipt
default:
return nil
}
}
// migrate converts a source LevelDB database to the new ethdb formatted database.
func (db *DB) migrate() error {
const suffix = ".migrating"
if strings.HasSuffix(db.Path, suffix) {
return nil
}
// Ignore if there is no directory.
if _, err := os.Stat(db.Path); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("ethdb.DB.migrate: %s", err)
}
// Ignore if path has already been migrated.
if ok, err := IsDBDir(db.Path); err != nil {
return fmt.Errorf("ethdb.DB.migrate: cannot check ethdb directory: %s", err)
} else if ok {
return nil // already migrated
}
// Skip if not an LevelDB directory. Probably empty.
if ok, err := IsLevelDBDir(db.Path); err != nil {
return fmt.Errorf("ethdb.DB.migrate: cannot check leveldb directory: %s", err)
} else if !ok {
return nil
}
log.Info("begin ethdb migration", "path", db.Path)
// Remove partial migration, if exists.
tmpPath := db.Path + suffix
if err := os.RemoveAll(tmpPath); err != nil {
return fmt.Errorf("ethdb.DB.migrate: cannot remove partial migration: %s", err)
}
// Open source database.
src, err := leveldb.OpenFile(db.Path, nil)
if err != nil {
return err
}
defer src.Close()
// Clone to temporary destination database.
dst := *db
dst.Path = tmpPath
dst.MinCompactionAge = 0
if err := dst.Open(); err != nil {
return fmt.Errorf("cannot open dst database: %s", err)
}
defer dst.Close()
// Iterate over every key in the source database.
itr := src.NewIterator(nil, nil)
defer itr.Release()
// Write all key/values to new database.
for itr.Next() {
var tbl *Table
switch {
case isBodyKey(itr.Key()):
tbl = dst.BodyTable().(*Table)
case isHeaderKey(itr.Key()):
tbl = dst.HeaderTable().(*Table)
case isReceiptKey(itr.Key()):
tbl = dst.ReceiptTable().(*Table)
default:
tbl = dst.GlobalTable().(*Table)
}
if err := tbl.Put(itr.Key(), itr.Value()); err != nil {
return fmt.Errorf("cannot insert item: tbl=%s key=%x err=%q", tbl.Name, itr.Key(), err)
}
}
if err := itr.Error(); err != nil {
return err
}
// Close both databases.
if err := src.Close(); err != nil {
return err
} else if err := dst.Close(); err != nil {
return err
}
// Rename databases.
if err := os.Rename(db.Path, db.Path+".old"); err != nil {
return err
} else if err := os.Rename(tmpPath, db.Path); err != nil {
return err
}
log.Info("ethdb migration complete", "path", db.Path)
return nil
}
func isBodyKey(key []byte) bool {
return bytes.HasPrefix(key, []byte("b")) && len(key) == 41
}
func isHeaderKey(key []byte) bool {
return bytes.HasPrefix(key, []byte("h")) && (len(key) == 41 || (len(key) == 10 && key[9] == 'n'))
}
func isReceiptKey(key []byte) bool {
return bytes.HasPrefix(key, []byte("r")) && len(key) == 41
}
// IsDBDir returns true if path contains an ethdb.DB.
// Checks if the "global" table exists.
func IsDBDir(path string) (bool, error) {
fi, err := os.Stat(filepath.Join(path, "global"))
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
return fi.IsDir(), nil
}
// IsLevelDBDir returns true if path contains a goleveldb database.
// Verifies that path contains a CURRENT file that starts with MANIFEST.
func IsLevelDBDir(path string) (bool, error) {
f, err := os.Open(filepath.Join(path, "CURRENT"))
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
defer f.Close()
buf := make([]byte, 8)
if _, err := io.ReadFull(f, buf); err != nil {
return false, fmt.Errorf("ethdb.DB.IsLevelDBDir: cannot read CURRENT: %s", err)
}
return string(buf) == "MANIFEST", nil
}