all repos — litestore @ 02943db68373dbf8f647784c1b3135d94047da93

A minimalist nosql document store.

Added numeric primary keys and explicit docid in searchcontents table.
h3rald h3rald@h3rald.com
Wed, 22 Apr 2015 22:28:39 +0200
commit

02943db68373dbf8f647784c1b3135d94047da93

parent

e465690e6e082935801e5c6a10a37663def8bb05

4 files changed, 19 insertions(+), 17 deletions(-)

jump to
M lib/api_v1.nimlib/api_v1.nim

@@ -152,7 +152,7 @@ let orig_limit = options.limit

let orig_offset = options.offset options.limit = 0 options.offset = 0 - options.select = @["COUNT(id)"] + options.select = @["COUNT(documents.id)"] let total = LS.store.retrieveRawDocuments(options)[0].num if docs.len == 0: result = resError(Http404, "No documents found.")
M lib/core.nimlib/core.nim

@@ -140,11 +140,11 @@ id = $genOid()

# Store document try: store.begin() - var res = store.db.execAffectedRows(SQL_INSERT_DOCUMENT, id, data, contenttype, binary, searchable, currentTime()) + var res = store.db.insertID(SQL_INSERT_DOCUMENT, id, data, contenttype, binary, searchable, currentTime()) if res > 0: if binary <= 0 and searchable >= 0: # Add to search index - store.db.exec(SQL_INSERT_SEARCHCONTENT, id, data.toPlainText) + store.db.exec(SQL_INSERT_SEARCHCONTENT, res, id, data.toPlainText) store.addDocumentSystemTags(id, contenttype) if store.hasMirror and id.startsWith(store.mount): # Add dir tag
M lib/queries.nimlib/queries.nim

@@ -5,7 +5,8 @@ # SQL QUERIES

const SQL_CREATE_DOCUMENTS_TABLE* = sql""" CREATE TABLE documents ( -id TEXT PRIMARY KEY, +rowid INTEGER PRIMARY KEY, +id TEST, data TEXT, content_type TEXT, binary INTEGER,

@@ -16,8 +17,8 @@ """

const SQL_CREATE_SEARCHCONTENTS_TABLE* = sql""" CREATE VIRTUAL TABLE searchcontents USING fts4( -document_id TEXT, -content TEXT, +id TEXT, +data TEXT, tokenize=porter) """

@@ -83,19 +84,19 @@ """

const SQL_INSERT_SEARCHCONTENT* = sql""" INSERT INTO searchcontents -(document_id, content) -VALUES (?, ?) +(docid, id, data) +VALUES (?, ?, ?) """ const SQL_DELETE_SEARCHCONTENT* = sql""" DELETE FROM searchcontents WHERE -document_id = ? +id = ? """ const SQL_UPDATE_SEARCHCONTENT* = sql""" UPDATE searchcontents -SET content = ? -WHERE document_id = ? +SET data = ? +WHERE id = ? """ const SQL_SELECT_DOCUMENTS_BY_TAG* = sql"""

@@ -131,7 +132,7 @@ """

const SQL_DELETE_SEARCHCONTENTS_BY_TAG* = sql""" DELETE FROM searchcontents -WHERE document_id IN +WHERE id IN (SELECT document_id FROM tags WHERE tag_id = ?) """
M lib/utils.nimlib/utils.nim

@@ -22,14 +22,14 @@

proc prepareSelectDocumentsQuery*(options: var QueryOptions): string = result = "SELECT " if options.search.len > 0: - if options.select[0] != "COUNT(id)": + if options.select[0] != "COUNT(documents.id)": var rank = "rank(matchinfo(searchcontents, 'pcxnal'), 1.20, 0.75, 5.0, 0.5) AS rank" options.select.add("snippet(searchcontents) AS highlight") options.select.add("ranktable.rank AS rank") options.select.add(rank) options.orderby = "rank DESC" # Create inner select - var innerSelect = "SELECT document_id as id, " & rank & " FROM searchcontents WHERE searchcontents MATCH '" & options.search.replace("'", "''") & "' " + var innerSelect = "SELECT docid, " & rank & " FROM searchcontents WHERE searchcontents MATCH '" & options.search.replace("'", "''") & "' " if options.tags.len > 0: innerSelect = innerSelect & options.tags.selectDocumentsByTags() innerSelect = innerSelect & " ORDER BY rank DESC "

@@ -39,11 +39,12 @@ if options.offset > 0:

innerSelect = innerSelect & " OFFSET " & $options.offset result = result & options.select.join(", ") result = result & " FROM documents, searchcontents, (" & innerSelect & ") AS ranktable " - result = result & "WHERE documents.id = ranktable.id AND documents.id = searchcontents.document_id " + result = result & "WHERE ranktable.docid = documents.rowid AND documents.id = searchcontents.id " else: result = result & options.select.join(", ") result = result & " FROM documents, searchcontents " - result = result & "WHERE documents.id = searchcontents.document_id " + result = result & "WHERE searchcontents.id = documents.id " + options.orderby = "" else: result = result & options.select.join(", ") result = result & " FROM documents WHERE 1=1 "

@@ -62,7 +63,7 @@ result = result & "OFFSET " & $options.offset & " "

debug(result.replace("$", "$$")) proc prepareSelectTagsQuery*(options: QueryOptions): string = - result = "SELECT tag_id, COUNT(document_ID) " + result = "SELECT tag_id, COUNT(document_id) " result = result & "FROM tags " if options.single: result = result & "WHERE tag_id = ?"