forked from zielmicha/leveldb.nim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleveldbtool.nim
129 lines (116 loc) · 3.08 KB
/
leveldbtool.nim
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
import options, os, strutils
import leveldbstatic as leveldb
proc tool() =
proc usage() =
echo "LevelDB client"
echo ""
echo "Usage:"
echo " leveldb [-d <db_path>] create"
echo " leveldb [-d <db_path>] get <key> [-x | --hex]"
echo " leveldb [-d <db_path>] put <key> <value> [-x | --hex]"
echo " leveldb [-d <db_path>] list [-x | --hex]"
echo " leveldb [-d <db_path>] keys"
echo " leveldb [-d <db_path>] delete <key>"
echo " leveldb [-d <db_path>] repair"
echo " leveldb -h | --help"
echo " leveldb -v | --version"
echo ""
echo "Options:"
echo " -d --database Database path"
echo " -x --hex binary value in uppercase hex"
echo " -h --help Show this screen"
echo " -v --version Show version"
quit()
var args = commandLineParams()
if "-h" in args or "--help" in args or len(args) == 0:
usage()
if "-v" in args or "--version" in args:
echo "leveldb.nim ", version
let (major, minor) = getLibVersion()
echo "leveldb ", major, ".", minor
quit()
proc findArg(s: seq[string], item: string): int =
result = find(s, item)
let stop = find(s, "--")
if stop >= 0 and stop <= result:
result = -1
var dbPath = "./"
var i = findArg(args, "-d")
var j = findArg(args, "--database")
if i >= 0 and j >= 0:
quit("Please specify database path one time only.")
i = max(i, j)
if i >= 0:
if (i + 1) < len(args):
dbPath = args[i+1]
args.delete(i+1)
args.delete(i)
else:
quit("Please specify database path.")
var hex = false
i = findArg(args, "-x")
j = findArg(args, "--hex")
if i >= 0:
hex = true
args.delete(i)
if j >= 0:
hex = true
args.delete(j)
# drop stop word
if "--" in args:
args.delete(args.find("--"))
if len(args) == 0:
usage()
proc checkCommand(args: seq[string], requires: int) =
if len(args) < requires + 1:
quit("Command " & args[0] & " requires at least " & $(requires) & " arguments.")
var db: LevelDb
var key, value: string
if args[0] == "create":
db = leveldb.open(dbPath)
db.close()
elif args[0] == "get":
checkCommand(args, 1)
db = leveldb.open(dbPath)
key = args[1]
let val = db.get(key)
if val.isNone():
quit()
else:
if hex:
echo val.get().toHex()
else:
echo val.get()
db.close()
elif args[0] == "put":
checkCommand(args, 2)
db = leveldb.open(dbPath)
key = args[1]
value = args[2]
if hex:
value = parseHexStr(value)
db.put(key, value)
db.close()
elif args[0] == "list":
db = leveldb.open(dbPath)
for key, value in db.iter():
if hex:
echo key, " ", value.toHex()
else:
echo key, " ", value
db.close()
elif args[0] == "keys":
db = leveldb.open(dbPath)
for key, value in db.iter():
echo key
db.close()
elif args[0] == "delete":
checkCommand(args, 1)
db = leveldb.open(dbPath)
key = args[1]
db.delete(key)
db.close()
elif args[0] == "repair":
repairDb(dbPath)
when isMainModule:
tool()