-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitumtime_unflush.go
115 lines (103 loc) · 2.39 KB
/
bitumtime_unflush.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
// Copyright (c) 2017 The Bitum developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
// This is a test utility. DO NOT USE!
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/bitum-project/bitumtime/bitumtimed/backend"
"github.com/bitum-project/bitumtime/bitumtimed/backend/filesystem"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
)
const (
flushedKey = "flushed"
)
func _main() error {
reset := flag.Bool("reset", false, "Only reset ChainTimestamp")
flag.Parse()
for _, a := range flag.Args() {
// We do all this stuff because leveldb always creates a
// directory and populates it with LOG and LOCK.
fi, err := os.Stat(a)
if err != nil {
fmt.Printf("%v: %v\n", a, err)
continue
}
if !fi.Mode().IsDir() {
fmt.Printf("%v: not a directory\n", a)
continue
}
_, err = os.Stat(filepath.Join(a, "LOCK"))
if err != nil {
fmt.Printf("%v: no LOCK\n", a)
continue
}
_, err = os.Stat(filepath.Join(a, "LOG"))
if err != nil {
fmt.Printf("%v: no LOG\n", a)
continue
}
// Assume we have a leveldb directory at this point.
db, err := leveldb.OpenFile(a, &opt.Options{
ErrorIfMissing: true,
})
if err != nil {
fmt.Printf("%v: leveldb.OpenFile %v\n", a, err)
continue
}
var action string
if *reset {
// Rewrite flush record.
var fr *backend.FlushRecord
payload, err := db.Get([]byte(flushedKey), nil)
if err != nil {
db.Close()
fmt.Printf("%v: Get %v\n", a, err)
continue
}
fr, err = filesystem.DecodeFlushRecord(payload)
if err != nil {
db.Close()
fmt.Printf("%v: decode %v\n", a, err)
continue
}
fr.ChainTimestamp = 0
payload, err = filesystem.EncodeFlushRecord(*fr)
if err != nil {
db.Close()
fmt.Printf("%v: encode %v\n", a, err)
continue
}
err = db.Put([]byte(flushedKey), payload, nil)
if err != nil {
db.Close()
fmt.Printf("%v: Put %v\n", a, err)
continue
}
action = "reset"
} else {
// Delete record.
err = db.Delete([]byte(flushedKey), nil)
if err != nil {
db.Close()
fmt.Printf("%v: leveldb.Delete %v\n", a, err)
continue
}
action = "unflushed"
}
db.Close()
fmt.Printf("%v: %v\n", action, a)
}
return nil
}
func main() {
err := _main()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}