commit
964d1b5069
|
@ -1,2 +0,0 @@
|
|||
*
|
||||
!.gitignore
|
|
@ -6,8 +6,7 @@ description = "LevelDB wrapper for Nim"
|
|||
license = "MIT"
|
||||
srcDir = "src"
|
||||
installExt = @["nim"]
|
||||
binDir = "bin"
|
||||
bin = @["leveldb"]
|
||||
bin = @["leveldbtool"]
|
||||
|
||||
# Dependencies
|
||||
|
||||
|
|
130
src/leveldb.nim
130
src/leveldb.nim
|
@ -1,4 +1,5 @@
|
|||
import options, os, strutils, leveldbpkg/raw
|
||||
import options, os, strutils
|
||||
import leveldb/raw
|
||||
|
||||
type
|
||||
LevelDb* = ref object
|
||||
|
@ -239,130 +240,3 @@ proc repairDb*(name: string) =
|
|||
var errPtr: cstring = nil
|
||||
leveldb_repair_db(options, name, addr errPtr)
|
||||
checkError(errPtr)
|
||||
|
||||
when isMainModule:
|
||||
import os
|
||||
|
||||
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.")
|
||||
|
||||
import strutils
|
||||
|
||||
var db: LevelDb
|
||||
var key, value: string
|
||||
if args[0] == "create":
|
||||
db = open(dbPath)
|
||||
db.close()
|
||||
elif args[0] == "get":
|
||||
checkCommand(args, 1)
|
||||
db = 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 = open(dbPath)
|
||||
key = args[1]
|
||||
value = args[2]
|
||||
if hex:
|
||||
value = parseHexStr(value)
|
||||
db.put(key, value)
|
||||
db.close()
|
||||
elif args[0] == "list":
|
||||
db = 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 = open(dbPath)
|
||||
for key, value in db.iter():
|
||||
echo key
|
||||
db.close()
|
||||
elif args[0] == "delete":
|
||||
checkCommand(args, 1)
|
||||
db = open(dbPath)
|
||||
key = args[1]
|
||||
db.delete(key)
|
||||
db.close()
|
||||
elif args[0] == "repair":
|
||||
repairDb(dbPath)
|
||||
|
|
|
@ -0,0 +1,129 @@
|
|||
import options, os, strutils
|
||||
import 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()
|
|
@ -1,5 +1,5 @@
|
|||
import unittest, options, os, osproc, sequtils, strutils
|
||||
import leveldb, leveldbpkg/raw
|
||||
import leveldb, leveldb/raw
|
||||
|
||||
const
|
||||
tmpDir = "/tmp/testleveldb"
|
||||
|
@ -31,7 +31,7 @@ proc execTool(args: varargs[string]): tuple[output: string, exitCode: int] =
|
|||
var quotedArgs = @args
|
||||
quotedArgs.insert(tmpDbDir)
|
||||
quotedArgs.insert("--database")
|
||||
quotedArgs.insert(tmpNimbleDir / "bin" / "leveldb")
|
||||
quotedArgs.insert(tmpNimbleDir / "bin" / "leveldbtool")
|
||||
quotedArgs = quotedArgs.map(proc (x: string): string = "\"" & x & "\"")
|
||||
|
||||
if not existsDir(tmpDbDir):
|
||||
|
@ -203,6 +203,9 @@ suite "leveldb":
|
|||
|
||||
suite "package":
|
||||
|
||||
setup:
|
||||
removeDir(tmpDir)
|
||||
|
||||
test "import as package":
|
||||
let (output, exitCode) = execNimble("install")
|
||||
check exitCode == QuitSuccess
|
||||
|
@ -220,6 +223,9 @@ suite "package":
|
|||
|
||||
suite "tool":
|
||||
|
||||
setup:
|
||||
removeDir(tmpDir)
|
||||
|
||||
test "leveldb tool":
|
||||
var (output, exitCode) = execNimble("install")
|
||||
check exitCode == QuitSuccess
|
||||
|
|
Loading…
Reference in New Issue