all repos — litestore @ 8657e0b6bb9664120917962361a74b71414c79e9

A minimalist nosql document store.

Implemented upgrade procedure, added system_documents table.
h3rald h3rald@h3rald.com
Sat, 08 Feb 2020 11:37:10 +0100
commit

8657e0b6bb9664120917962361a74b71414c79e9

parent

4c5ac09be46163ac6b07d37fb133a652137db7b9

M .gitignore.gitignore

@@ -15,3 +15,4 @@ nakefile

LiteStore_UserGuide.htm jester_integration js +*_backup
M src/litestore.nimsrc/litestore.nim

@@ -75,6 +75,11 @@ fail(200, "Unable to create datastore '$1'" % [LS.file])

if (open): try: LS.store = LS.file.openDatastore() + try: + LS.store.upgradeDatastore() + except: + echo getCurrentExceptionMsg() + fail(203, "Unable to upgrade datastore '$1'" % [LS.file]) if LS.mount: try: LS.store.mountDir(LS.directory)
M src/litestorepkg/lib/core.nimsrc/litestorepkg/lib/core.nim

@@ -41,9 +41,10 @@ let data = db.open(file, "", "", "")

LOG.debug("Creating tables") data.exec(SQL_CREATE_DOCUMENTS_TABLE) data.exec(SQL_CREATE_SEARCHDATA_TABLE) + data.exec(SQL_CREATE_SYSTEM_DOCUMENTS_TABLE) data.exec(SQL_CREATE_TAGS_TABLE) data.exec(SQL_CREATE_INFO_TABLE) - data.exec(SQL_INSERT_INFO, 1, 0, 0) + data.exec(SQL_INSERT_INFO, 2, 0) LOG.debug("Creating indexes") data.createIndexes() LOG.debug("Database created")

@@ -64,6 +65,29 @@ except:

raise newException(EDatastoreUnavailable, "Datastore '$1' cannot destroyed." % store.path) +proc retrieveInfo*(store: Datastore): array[0..1, int] = + var data = store.db.getRow(SQL_SELECT_INFO) + return [data[0].parseInt, data[1].parseInt] + +proc upgradeDatastore*(store: Datastore) = + let info = store.retrieveInfo() + if info[0] == 1: + LOG.debug("Upgrading datastore to version 2...") + let bkp_path = store.path & "__v1_backup" + copyFile(store.path, bkp_path) + try: + store.db.exec(SQL_CREATE_SYSTEM_DOCUMENTS_TABLE) + store.db.exec(SQL_UPDATE_VERSION, 2) + LOG.debug("Done.") + except: + store.closeDatastore() + store.path.removeFile() + copyFile(bkp_path, store.path) + let e = getCurrentException() + LOG.error(getCurrentExceptionMsg()) + LOG.debug(e.getStackTrace()) + LOG.error("Unable to upgrade datastore '$1'." % store.path) + proc openDatastore*(file: string): Datastore = if not file.fileExists: raise newException(EDatastoreDoesNotExist,

@@ -85,10 +109,6 @@ result.mount = ""

except: raise newException(EDatastoreUnavailable, "Datastore '$1' cannot be opened." % file) - -proc retrieveInfo*(store: Datastore): array[0..1, int] = - var data = store.db.getRow(SQL_SELECT_INFO) - return [data[0].parseInt, data[1].parseInt] proc hasMirror(store: Datastore): bool = return store.mount.len > 0
M src/litestorepkg/lib/queries.nimsrc/litestorepkg/lib/queries.nim

@@ -3,25 +3,18 @@

# SQL QUERIES -const SQL_CREATE_DOCUMENTS_TABLE* = sql""" -CREATE TABLE documents ( -docid INTEGER PRIMARY KEY, -id TEXT UNIQUE NOT NULL, -data TEXT, -content_type TEXT, -binary INTEGER, -searchable INTEGER, -created TEXT, -modified TEXT) -""" const SQL_CREATE_INDEX_DOCUMENTS_DOCID* = sql"CREATE INDEX IF NOT EXISTS documents_docid ON documents(docid)" SQL_CREATE_INDEX_DOCUMENTS_ID* = sql"CREATE INDEX IF NOT EXISTS documents_id ON documents(id)" + SQL_CREATE_INDEX_SYSTEM_DOCUMENTS_DOCID* = sql"CREATE INDEX IF NOT EXISTS system_documents_docid ON documents(docid)" + SQL_CREATE_INDEX_SYSTEM_DOCUMENTS_ID* = sql"CREATE INDEX IF NOT EXISTS system_documents_id ON documents(id)" SQL_CREATE_INDEX_TAGS_DOCUMENT_ID* = sql"CREATE INDEX IF NOT EXISTS tags_document_id ON tags(document_id)" SQL_CREATE_INDEX_TAGS_TAG_ID* = sql"CREATE INDEX IF NOT EXISTS tags_tag_id ON tags(tag_id)" SQL_DROP_INDEX_DOCUMENTS_DOCID* = sql"DROP INDEX IF EXISTS documents_docid" SQL_DROP_INDEX_DOCUMENTS_ID* = sql"DROP INDEX IF EXISTS documents_id" + SQL_DROP_INDEX_SYSTEM_DOCUMENTS_DOCID* = sql"DROP INDEX IF EXISTS system_documents_docid" + SQL_DROP_INDEX_SYSTEM_DOCUMENTS_ID* = sql"DROP INDEX IF EXISTS system_documents_id" SQL_DROP_INDEX_TAGS_DOCUMENT_ID* = sql"DROP INDEX IF EXISTS tags_document_id" SQL_DROP_INDEX_TAGS_TAG_ID* = sql"DROP INDEX IF EXISTS tags_tag_id"

@@ -31,6 +24,30 @@ SQL_REBUILD* = sql"INSERT INTO searchdata(searchdata) VALUES('rebuild')"

SQL_VACUUM* = sql"VACUUM" +const SQL_CREATE_DOCUMENTS_TABLE* = sql""" +CREATE TABLE documents ( +docid INTEGER PRIMARY KEY, +id TEXT UNIQUE NOT NULL, +data TEXT, +content_type TEXT, +binary INTEGER, +searchable INTEGER, +created TEXT, +modified TEXT) +""" + +const SQL_CREATE_SYSTEM_DOCUMENTS_TABLE* = sql""" +CREATE TABLE system_documents ( + docid INTEGER PRIMARY KEY, + id TEXT UNIQUE NOT NULL, + data TEXT, + content_type TEXT, + binary INTEGER, + created TEXT, + modified TEXT +) +""" + const SQL_CREATE_SEARCHDATA_TABLE* = sql""" CREATE VIRTUAL TABLE searchdata USING fts4( id TEXT UNIQUE NOT NULL,

@@ -58,10 +75,14 @@ (version, total_documents)

VALUES (?, ?) """ +const SQL_UPDATE_VERSION* = sql""" +UPDATE info +SET version = ? +""" + const SQL_SELECT_INFO* = sql""" SELECT * FROM info """ - const SQL_SET_TOTAL_DOCS* = sql""" UPDATE info