-
Notifications
You must be signed in to change notification settings - Fork 1
/
ldbdump_base64.go
80 lines (73 loc) · 1.62 KB
/
ldbdump_base64.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
// The ldbdump program dumps the contents of LevelDB tables (.ldb files),
// formerly known as sorted string tables (.sst files).
package main
import (
"bytes"
"flag"
"fmt"
"os"
"encoding/base64"
"github.com/golang/leveldb/db"
"github.com/golang/leveldb/table"
)
var (
verifyChecksums = flag.Bool("c", false, "Verify checksums.")
truncate = flag.Bool("t", false, "Truncate long keys and values.")
kBuf, vBuf bytes.Buffer
)
func main() {
flag.Parse()
bad := false
for i, arg := range flag.Args() {
if i != 0 {
fmt.Println()
}
fmt.Printf("filename: %q\n", arg)
if err := dump(arg); err != nil {
fmt.Printf("error: %q\n", err)
bad = true
}
}
if bad {
os.Exit(1)
}
}
func byteString(p []byte) string {
for i := 0; i < len(p); i++ {
if p[i] == 0 {
return string(p[0:i])
}
}
return string(p)
}
func dump(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
// No need to "defer f.Close()", as closing r will close f.
r := table.NewReader(f, &db.Options{
VerifyChecksums: *verifyChecksums,
})
defer r.Close()
t := r.Find(nil, nil)
for t.Next() {
k, v := t.Key(), t.Value()
if *truncate {
k = trunc(&kBuf, k)
v = trunc(&vBuf, v)
}
enc_k := base64.StdEncoding.EncodeToString([]byte(k))
enc_v := base64.StdEncoding.EncodeToString([]byte(v))
fmt.Printf("%q: %q,\n", enc_k, enc_v)
}
return t.Close()
}
func trunc(dst *bytes.Buffer, b []byte) []byte {
if len(b) < 64 {
return b
}
dst.Reset()
fmt.Fprintf(dst, "%s...(%d bytes)...%s", b[:20], len(b)-40, b[len(b)-20:])
return dst.Bytes()
}