-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
129 lines (102 loc) · 2.61 KB
/
main.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
package main
import (
"encoding/hex"
"fmt"
"github.com/cockroachdb/pebble"
"math"
levelopt "github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
tmdb "github.com/tendermint/tm-db"
"os"
"path/filepath"
"runtime"
)
func main() {
if len(os.Args) != 3 {
panic("Usage: level2pebble <sourcePath> <targetDir>")
}
fmt.Printf("source=%s, target=%s\n", os.Args[1], os.Args[2])
dbName := filepath.Base(os.Args[1])
dbName = dbName[:len(dbName)-len(filepath.Ext(dbName))] // get rid of .db
dbDirSource := filepath.Dir(os.Args[1])
dbDirTarget := os.Args[2]
// options to disable compaction for goleveldb
levelOptions := levelopt.Options{
ReadOnly: true,
//CompactionTableSizeMultiplier: 2.0,
}
dbLev, errLev := tmdb.NewGoLevelDBWithOpts(dbName, dbDirSource, &levelOptions)
if errLev != nil {
panic(errLev)
}
// options to disable compaction for pebbledb
pebbleOptions := &pebble.Options{
L0CompactionFileThreshold: math.MaxInt32,
L0CompactionThreshold: math.MaxInt32,
L0StopWritesThreshold: math.MaxInt32,
MaxConcurrentCompactions: 1,
DisableAutomaticCompactions: true,
MaxOpenFiles: 100,
}
pebbleOptions.Experimental.ReadCompactionRate = math.MaxInt32
pebbleOptions.EnsureDefaults()
dbPeb, errPeb := tmdb.NewPebbleDBWithOpts(dbName, dbDirTarget, pebbleOptions)
if errPeb != nil {
panic(errPeb)
}
defer func() {
dbPeb.Close()
dbLev.Close()
}()
readOptions := levelopt.ReadOptions{
DontFillCache: true,
Strict: levelopt.DefaultStrict,
}
itr := dbLev.DB().NewIterator(&util.Range{Start: nil, Limit: nil}, &readOptions)
defer func() {
// itr.Close()
itr.Release()
}()
offset := 0
rawDBPebble := dbPeb.DB()
bat := rawDBPebble.NewBatch()
for itr.First(); itr.Valid(); itr.Next() {
offset++
key := cp(itr.Key())
value := cp(itr.Value())
errSet := bat.Set(key, value, pebble.Sync)
if errSet != nil {
panic(errSet)
}
if bat.Len() >= 107374182 { // 100 MB
str_hex_key := hex.EncodeToString(key)
fmt.Printf("processing %s: %d, key=%s\n", dbName, offset, str_hex_key)
errComit := bat.Commit(pebble.Sync)
if errComit != nil {
panic(errComit)
}
errFlush := rawDBPebble.Flush()
if errFlush != nil {
panic(errFlush)
}
bat.Reset()
runtime.GC() // Force GC
}
}
// write the last batch
errComit := bat.Commit(pebble.Sync)
if errComit != nil {
panic(errComit)
}
errFlush := rawDBPebble.Flush()
if errFlush != nil {
panic(errFlush)
}
bat.Close()
fmt.Printf("Done!")
}
func cp(bz []byte) (ret []byte) {
ret = make([]byte, len(bz))
copy(ret, bz)
return ret
}