all repos — litestore @ a3dd540952bc22fe0e0662d0a97e091a39896b61

A minimalist nosql document store.

Implemented directory pack.
h3rald h3rald@h3rald.com
Sun, 11 Jan 2015 19:33:48 +0100
commit

a3dd540952bc22fe0e0662d0a97e091a39896b61

parent

c5e97550c3aa07c2abe812a1321491c63984a368

3 files changed, 34 insertions(+), 8 deletions(-)

jump to
M litestore.nimlitestore.nim

@@ -6,7 +6,9 @@ os,

oids, times, json, - pegs + pegs, + mimetypes, + base64 import types, contenttypes,

@@ -65,9 +67,15 @@ return $(%(stores))

# Manage Documents -proc createDocument*(store: Datastore, data = "", contenttype = "text/plain", binary = -1, searchable = 1): string = +proc createDocument*(store: Datastore, id="", data = "", contenttype = "text/plain", binary = -1, searchable = 1): string = var binary = checkIfBinary(binary, contenttype) - result = $genOid() + var data = data + if binary == 1: + data = data.encode(data.len*2) + if id == "": + result = $genOid() + else: + result = id # Store document store.db.exec(SQL_INSERT_DOCUMENT, result, data, contenttype, binary, searchable, getTime().getGMTime().format("yyyy-MM-dd'T'hh:mm:ss'Z'")) if binary == 0 and searchable == 1:

@@ -76,8 +84,11 @@ store.db.exec(SQL_INSERT_SEARCHCONTENT, result, data)

store.addDocumentSystemTags(result, contenttype) return result -proc updateDocument*(store: Datastore, id: string, data: string, contenttype = "text/plain", binary = -1, searchable = true) = +proc updateDocument*(store: Datastore, id: string, data: string, contenttype = "text/plain", binary = -1, searchable = 1) = var binary = checkIfBinary(binary, contenttype) + var data = data + if binary == 1: + data = data.encode(data.len*2) store.db.exec(SQL_UPDATE_DOCUMENT, data, contenttype, binary, searchable, getTime().getGMTime().format("yyyy-MM-dd'T'hh:mm:ss'Z'"), id) store.deleteDocumentSystemTags(id) store.addDocumentSystemTags(id, contenttype)

@@ -133,7 +144,19 @@

# TODO Pack/Unpack Directories proc packDir*(store: Datastore, dir: string) = - discard + if not dir.dirExists: + raise newException(EDirectoryNotFound, "Directory '$1' not found." % dir) + for f in dir.walkDirRec(): + var d_id = f + var d_contents = f.readFile + var d_ct = CONTENT_TYPES.getMimetype(f.splitFile.ext, "application/octet-stream") + var d_binary = 0 + var d_searchable = 1 + if d_ct.isBinary: + d_binary = 1 + d_searchable = 0 + discard store.createDocument(d_id, d_contents, d_ct, d_binary, d_searchable) + store.db.exec(SQL_INSERT_TAG, "$dir:"&dir, d_id) proc unpackDir*(store: Datastore, dir: string) = discard

@@ -158,4 +181,6 @@ store.createTag "test", id3

var opts = newQueryOptions() #opts.tags = "test,test2" #opts.search = "another yet" +store.packDir("nimcache") echo store.retrieveDocuments(opts) +
M types.nimtypes.nim

@@ -5,6 +5,7 @@ EDatastoreExists* = object of Exception

EDatastoreDoesNotExist* = object of Exception EDatastoreUnavailable* = object of Exception EInvalidTag* = object of Exception + EDirectoryNotFound* = object of Exception Datastore* = object db*: TDbConn path*: string
M utils.nimutils.nim

@@ -8,7 +8,7 @@

let PEG_USER_TAG* = peg""" ^[a-zA-Z0-9_-?~:.@#^%!]+$ """ -proc dbQuote(s: string): string = +proc dbQuote*(s: string): string = result = "'" for c in items(s): if c == '\'': add(result, "''")

@@ -40,7 +40,7 @@ result = result & "AND id = ?"

if options.tags.len > 0: result = result & options.tags.selectDocumentsByTags() if options.search.len > 0: - result = result & "AND content MATCH \"" & options.search.dbQuote & "\"" + result = result & "AND content MATCH \"" & options.search & "\"" if options.orderby.validOrderBy(): result = result & "ORDER BY " & options.orderby & " " if options.limit > 0:

@@ -73,7 +73,7 @@ proc checkIfBinary*(binary:int, contenttype:string): int =

if binary == -1 and contenttype.isBinary: return 1 else: - return 0 + return binary proc addDocumentSystemTags*(store: Datastore, docid, contenttype: string) = var splittype = contenttype.split("/")