Implemented CLI.
h3rald h3rald@h3rald.com
Sun, 18 Jan 2015 18:02:22 +0100
5 files changed,
128 insertions(+),
31 deletions(-)
A
cli.nim
@@ -0,0 +1,66 @@
+import + parseopt2, + strutils +import + types + + +const + version = "1.0" + usage* = " LiteStore v"& version & " - Lightweight REST Document Store" & """ + (c) 2015 Fabio Cevasco + + Usage: + litestore [-p:<port> -a:<address>] [<file>] [--pack:<directory> | --unpack:<directory>] + + Options: + -a, --address Specify address (default: 0.0.0.0). + -h, --help Display this message. + -p, --port Specify port number (default: 70700). + --pack Pack the specified directory (Store all its contents). + --unpack Unpack the previously-packed specified directory to the current directory. + -v, --version Display the program version. +""" + +var + file = "data.ls" + port = 70700 + address = "0.0.0.0" + operation = opRun + directory = "" + + +for kind, key, val in getOpt(): + case kind: + of cmdLongOption, cmdShortOption: + case key: + of "address", "a": + address = val + of "port", "p": + port = val.parseInt + of "pack": + operation = opPack + directory = val + of "unpack": + operation = opUnpack + directory = val + of "version", "v": + echo version + quit(0) + of "help", "h": + echo usage + quit(0) + else: + discard + of cmdArgument: + file = key + else: + discard + +var settings*: Settings + +settings.port = port +settings.address = address +settings.operation = operation +settings.file = file +settings.directory = directory
M
litestore.nim
→
litestore.nim
@@ -13,7 +13,8 @@ import
types, contenttypes, queries, - utils + utils, + cli {.compile: "vendor/sqlite/libsqlite3.c".} {.passC: "-DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS".}@@ -35,7 +36,7 @@ file.removeFile
except: raise newException(EDatastoreUnavailable, "Datastore '$1' cannot deleted." % file) -proc openDatastore(file:string): Datastore = +proc openDatastore*(file:string): Datastore = if not file.fileExists: raise newException(EDatastoreDoesNotExist, "Datastore '$1' does not exists." % file) try:@@ -44,7 +45,7 @@ result.path = file
except: raise newException(EDatastoreUnavailable, "Datastore '$1' cannot be opened." % file) -proc closeDatastore(store:Datastore) = +proc closeDatastore*(store:Datastore) = try: db.close(store.db) except:@@ -156,34 +157,56 @@ data = doc[1]
file.parentDir.createDir file.writeFile(data) -proc deleteDocumentsByTag(store: Datastore, tag: string): int64 = +proc deleteDocumentsByTag*(store: Datastore, tag: string): int64 = result = 0 var ids = store.db.getAllRows(SQL_SELECT_DOCUMENT_IDS_BY_TAG, tag) for id in ids: result.inc(store.deleteDocument(id[0]).int) -# Test -var file = "test.ls" -if file.fileExists: - file.removeFile -createDatastore(file) -var store = file.openDatastore -var id1 = store.createDocument "This is a test document" -var id2 = store.createDocument "This is another test document" -var id3 = store.createDocument "This is yet another test document" -store.createTag "test1", id1 -store.createTag "test2", id2 -store.createTag "test3", id2 -store.createTag "test", id1 -store.createTag "test", id2 -store.createTag "test", id3 -var opts = newQueryOptions() -#opts.tags = "test,test2" -#opts.search = "another yet" -store.packDir("nimcache") -"test".createDir -"test".setCurrentDir -store.unpackDir("nimcache") -echo store.deleteDocumentsByTag("$dir:nimcache") + +# Test +when false: + var file = "test.ls" + if file.fileExists: + file.removeFile + createDatastore(file) + var store = file.openDatastore + var id1 = store.createDocument "This is a test document" + var id2 = store.createDocument "This is another test document" + var id3 = store.createDocument "This is yet another test document" + store.createTag "test1", id1 + store.createTag "test2", id2 + store.createTag "test3", id2 + store.createTag "test", id1 + store.createTag "test", id2 + store.createTag "test", id3 + var opts = newQueryOptions() + #opts.tags = "test,test2" + #opts.search = "another yet" + store.packDir("nimcache") + "test".createDir + "test".setCurrentDir + store.unpackDir("nimcache") + echo store.deleteDocumentsByTag("$dir:nimcache") + +when isMainModule: + # Initialize Datastore + if not settings.file.fileExists: + try: + settings.file.createDatastore() + except: + error(1, "Unable to create datastore '$1'" % [settings.file]) + try: + settings.store = settings.file.openDatastore() + except: + error(2, "Unable to open datastore '$1'" % [settings.file]) + case settings.operation: + of opPack: + settings.store.packDir(settings.directory) + of opUnpack: + settings.store.unpackDir(settings.directory) + of opRun: + #TODO + discard
M
queries.nim
→
queries.nim
@@ -3,10 +3,6 @@
# SQL QUERIES -const SQL_COUNT_DOCUMENTS* = sql""" -SELECT COUNT(id) FROM documents -""" - const SQL_COUNT_TAGS* = sql""" SELECT COUNT(DISTINCT tag_id) FROM tags """
M
types.nim
→
types.nim
@@ -20,6 +20,14 @@ tag*: string
startswith*: bool endswith*: bool negated*: bool + Operation* = enum opRun, opPack, opUnpack + Settings* = object + store*: Datastore + address*: string + port*: int + operation*: Operation + directory*: string + file*: string proc newQueryOptions*(): QueryOptions = return QueryOptions(single: false, limit: 0, orderby: "", tags: "", search: "")