-
Notifications
You must be signed in to change notification settings - Fork 127
/
main.go
96 lines (81 loc) · 2.03 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
package main
import (
"flag"
"os"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v3"
)
func main() {
dbDir := flag.String("db", "/tmp/mixin/snapshots", "the mixin badger snapshots directory")
dicDir := flag.String("dic", "/tmp/zstd", "the directory to store zstd dictionary samples")
flag.Parse()
db, err := openDB(*dbDir)
if err != nil {
panic(err)
}
defer db.Close()
loopUTXOs(db, *dicDir)
loopTransactions(db, *dicDir)
loopSnapshots(db, *dicDir)
}
func loopSnapshots(db *badger.DB, dir string) {
txn := db.NewTransaction(false)
defer txn.Discard()
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
it.Seek([]byte("SNAPSHOT"))
for ; it.ValidForPrefix([]byte("SNAPSHOT")); it.Next() {
item := it.Item()
key := item.Key()
val, err := item.ValueCopy(nil)
if err != nil {
panic(err)
}
err = os.WriteFile(dir+"/SNAPSHOT-"+crypto.NewHash(key).String(), val, 0644)
if err != nil {
panic(err)
}
}
}
func loopTransactions(db *badger.DB, dir string) {
txn := db.NewTransaction(false)
defer txn.Discard()
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
it.Seek([]byte("TRANSACTION"))
for ; it.ValidForPrefix([]byte("TRANSACTION")); it.Next() {
item := it.Item()
key := item.Key()
val, err := item.ValueCopy(nil)
if err != nil {
panic(err)
}
err = os.WriteFile(dir+"/TRANSACTION-"+crypto.NewHash(key).String(), val, 0644)
if err != nil {
panic(err)
}
}
}
func loopUTXOs(db *badger.DB, dir string) {
txn := db.NewTransaction(false)
defer txn.Discard()
it := txn.NewIterator(badger.DefaultIteratorOptions)
defer it.Close()
it.Seek([]byte("UTXO"))
for ; it.ValidForPrefix([]byte("UTXO")); it.Next() {
item := it.Item()
key := item.Key()
val, err := item.ValueCopy(nil)
if err != nil {
panic(err)
}
err = os.WriteFile(dir+"/UTXO-"+crypto.NewHash(key).String(), val, 0644)
if err != nil {
panic(err)
}
}
}
func openDB(dir string) (*badger.DB, error) {
opts := badger.DefaultOptions(dir)
return badger.Open(opts)
}