all repos — litestore @ 4e3574b66676a22648211862a2524a213f6bf824

A minimalist nosql document store.

Optimization for tags like query.
h3rald h3rald@h3rald.com
Sat, 15 Sep 2018 21:50:41 +0200
commit

4e3574b66676a22648211862a2524a213f6bf824

parent

14783e7abd15173c120d265f0a630482fbbd410e

2 files changed, 14 insertions(+), 3 deletions(-)

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

@@ -137,7 +137,11 @@ proc retrieveTags*(store: Datastore, options: QueryOptions = newQueryOptions()): JsonNode =

var query = prepareSelectTagsQuery(options) var raw_tags: seq[Row] if (options.like.len > 0): - raw_tags = store.db.getAllRows(query.sql, options.like.replace("*", "%")) + if (options.like[options.like.len-1] == '*'): + let str = options.like.substr(0, options.like.len-2) + raw_tags = store.db.getAllRows(query.sql, str, str & "{") + else: + raw_tags = store.db.getAllRows(query.sql, options.like.replace("*", "%")) else: raw_tags = store.db.getAllRows(query.sql) var tags = newSeq[JsonNode](0)

@@ -150,7 +154,11 @@ var query = SQL_COUNT_TAGS

if q.len > 0: query = q.sql if like.len > 0: - return store.db.getRow(query, like)[0].parseInt + if (like[like.len-1] == '*'): + let str = like.substr(0, like.len-2) + return store.db.getRow(query, str, str & "{")[0].parseInt + else: + return store.db.getRow(query, like)[0].parseInt return store.db.getRow(query)[0].parseInt proc retrieveTagsWithTotals*(store: Datastore): JsonNode =
M lib/utils.nimlib/utils.nim

@@ -117,7 +117,10 @@ result = result & "FROM tags "

if options.single: result = result & "WHERE tag_id = ?" elif options.like.len > 0: - result = result & "WHERE tag_id LIKE ? " + if options.like[options.like.len-1] == '*': + result = result & "WHERE tag_id BETWEEN ? AND ? " + else: + result = result & "WHERE tag_id LIKE ? " if group: result = result & "GROUP BY tag_id " if options.limit > 0: