Compare commits
5 Commits
b6ac652ade
...
7d3c24e6e1
Author | SHA1 | Date | |
---|---|---|---|
7d3c24e6e1 | |||
c155d25a37 | |||
583d5babf3 | |||
45659cdc59 | |||
3022bbdfb5 |
12
HACKING.md
12
HACKING.md
@ -1,12 +1,7 @@
|
|||||||
# looqs - Hacking
|
# looqs - Hacking
|
||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
Without elaborating here, I hacked looqs because I was not satisfied with the state of desktop search on Linux.
|
If you are interested on how to contribute, please see the file [CONTRIBUTING.md](CONTRIBUTING.md) which contains the instructions on how to submit patches etc.
|
||||||
|
|
||||||
Originally a set of CLI python scripts, it is now written in C++ and offers a GUI made using Qt. While a "web app" would have been an option, I prefer a desktop application for something like looqs. I chose Qt because I am more familiar with it than with any other GUI framework. To my knowledge, potential alternatives like GTK do not include as many "batteries" as Qt anyway, so the job presumably would have been harder there,
|
|
||||||
at least for me.
|
|
||||||
|
|
||||||
If you are interested in how to contribute, please see the file [CONTRIBUTING.md](CONTRIBUTING.md) which contains the instructions on how to submit patches etc.
|
|
||||||
|
|
||||||
## Security
|
## Security
|
||||||
The architecture ensures that the parsing of documents and the preview generation is sandboxed by [exile.h](https://github.com/quitesimpleorg/exile.h). looqs uses a multi-process architecture to achieve this.
|
The architecture ensures that the parsing of documents and the preview generation is sandboxed by [exile.h](https://github.com/quitesimpleorg/exile.h). looqs uses a multi-process architecture to achieve this.
|
||||||
@ -16,7 +11,8 @@ Qt code is considered trusted in this model. While one may critize this, it was
|
|||||||
Set the enviornment variable `LOOQS_DISABLE_SANDBOX=1` to disable sandboxing. It's intended for troublehshooting.
|
Set the enviornment variable `LOOQS_DISABLE_SANDBOX=1` to disable sandboxing. It's intended for troublehshooting.
|
||||||
|
|
||||||
## Database
|
## Database
|
||||||
The heart is sqlite, with the FTS5 extensions behind the full-text search. While FTS may not be sqlite's strong suit, I definitly did not want to run one of those oftenly recommended heavy (Java based) solutions. I explored other options like Postgresql, I've discard them due to some limitations back then.
|
The heart is sqlite, with the FTS5 extensions behind the full-text search. While FTS may not be sqlite's strong suit, I definitely did not want to run one of those oftenly recommended heavy (Java based) solutions. I explored other options like Postgresql, I've discard them due to some limitations back then. It's also natural to use sqlite as it's
|
||||||
|
used for metadata in general.
|
||||||
|
|
||||||
Down the road, alternatives will be explored of course if sqlite should not suffice anymore.
|
Down the road, alternatives will be explored of course if sqlite should not suffice anymore.
|
||||||
|
|
||||||
@ -27,7 +23,7 @@ looqs simply strips the tags and that seems to work fine so far. Naturally, this
|
|||||||
Naturally, looqs won't be able to index and render previews for everything. Such approach would create a huge bloated binary. In the future, there will be some plugin system of some sorts, either we will load .so objects or use subprocesses.
|
Naturally, looqs won't be able to index and render previews for everything. Such approach would create a huge bloated binary. In the future, there will be some plugin system of some sorts, either we will load .so objects or use subprocesses.
|
||||||
|
|
||||||
## Name
|
## Name
|
||||||
looqs looks for files. You as the user can also look inside them. The 'k' in "looks" was replaced by a 'q'. Originally, I wanted my projects to have "qs" (for quitesimple) in their name. While abandoned now, this got us to looqs.
|
looqs looks for files. You as the user can also look inside them. The 'k' in "looks" was replaced by a 'q'. Originally, I wanted my projects to have "qs" (for quitesimple) in their name. While that quirk is abandoned now, this got us to looqs.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,6 +30,8 @@ The screenshots in this section may occasionally be slightly outdated, but they
|
|||||||
## Current status
|
## Current status
|
||||||
Latest version: 2022-09-10, v0.7
|
Latest version: 2022-09-10, v0.7
|
||||||
|
|
||||||
|
Please keep in mind: looqs is still at an early stage and may exhibit some weirdness and contain bugs.
|
||||||
|
|
||||||
Please see [Changelog](CHANGELOG.md) for a human readable list of changes.
|
Please see [Changelog](CHANGELOG.md) for a human readable list of changes.
|
||||||
|
|
||||||
|
|
||||||
|
3
shared/migrations/4.sql
Normal file
3
shared/migrations/4.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
CREATE VIRTUAL TABLE fts_trigram USING fts5(content, content='',tokenize="trigram");
|
||||||
|
ALTER TABLE content ADD COLUMN fts_trigramid integer;
|
||||||
|
CREATE INDEX content_fts_trigramid ON content (fts_trigramid);
|
@ -3,5 +3,6 @@
|
|||||||
<file>1.sql</file>
|
<file>1.sql</file>
|
||||||
<file>2.sql</file>
|
<file>2.sql</file>
|
||||||
<file>3.sql</file>
|
<file>3.sql</file>
|
||||||
|
<file>4.sql</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
</RCC>
|
</RCC>
|
||||||
|
@ -110,6 +110,44 @@ unsigned int SqliteDbService::getFiles(QVector<FileData> &results, QString wildC
|
|||||||
return processedRows;
|
return processedRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool SqliteDbService::insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector<PageData> &pageData)
|
||||||
|
{
|
||||||
|
QString ftsInsertStatement;
|
||||||
|
QString contentInsertStatement;
|
||||||
|
if(useTrigrams)
|
||||||
|
{
|
||||||
|
ftsInsertStatement = "INSERT INTO fts_trigram(content) VALUES(?)";
|
||||||
|
contentInsertStatement = "INSERT INTO content(fileid, page, fts_trigramid) VALUES(?, ?, last_insert_rowid())";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ftsInsertStatement = "INSERT INTO fts(content) VALUES(?)";
|
||||||
|
contentInsertStatement = "INSERT INTO content(fileid, page, ftsid) VALUES(?, ?, last_insert_rowid())";
|
||||||
|
}
|
||||||
|
|
||||||
|
for(const PageData &data : pageData)
|
||||||
|
{
|
||||||
|
QSqlQuery ftsQuery(db);
|
||||||
|
ftsQuery.prepare(ftsInsertStatement);
|
||||||
|
ftsQuery.addBindValue(data.content);
|
||||||
|
if(!ftsQuery.exec())
|
||||||
|
{
|
||||||
|
Logger::error() << "Failed fts insertion " << ftsQuery.lastError() << Qt::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
QSqlQuery contentQuery(db);
|
||||||
|
contentQuery.prepare(contentInsertStatement);
|
||||||
|
contentQuery.addBindValue(fileid);
|
||||||
|
contentQuery.addBindValue(data.pagenumber);
|
||||||
|
if(!contentQuery.exec())
|
||||||
|
{
|
||||||
|
Logger::error() << "Failed content insertion " << contentQuery.lastError() << Qt::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &pageData)
|
SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &pageData)
|
||||||
{
|
{
|
||||||
QString absPath = fileInfo.absoluteFilePath();
|
QString absPath = fileInfo.absoluteFilePath();
|
||||||
@ -149,24 +187,18 @@ SaveFileResult SqliteDbService::saveFile(QFileInfo fileInfo, QVector<PageData> &
|
|||||||
}
|
}
|
||||||
|
|
||||||
int lastid = inserterQuery.lastInsertId().toInt();
|
int lastid = inserterQuery.lastInsertId().toInt();
|
||||||
for(const PageData &data : pageData)
|
if(!insertToFTS(false, db, lastid, pageData))
|
||||||
{
|
{
|
||||||
QSqlQuery ftsQuery(db);
|
db.rollback();
|
||||||
ftsQuery.prepare("INSERT INTO fts(content) VALUES(?)");
|
Logger::error() << "Failed to insert data to FTS index " << Qt::endl;
|
||||||
ftsQuery.addBindValue(data.content);
|
return DBFAIL;
|
||||||
ftsQuery.exec();
|
}
|
||||||
QSqlQuery contentQuery(db);
|
if(!insertToFTS(true, db, lastid, pageData))
|
||||||
contentQuery.prepare("INSERT INTO content(fileid, page, ftsid) VALUES(?, ?, last_insert_rowid())");
|
{
|
||||||
contentQuery.addBindValue(lastid);
|
db.rollback();
|
||||||
contentQuery.addBindValue(data.pagenumber);
|
Logger::error() << "Failed to insert data to FTS index " << Qt::endl;
|
||||||
if(!contentQuery.exec())
|
return DBFAIL;
|
||||||
{
|
|
||||||
db.rollback();
|
|
||||||
Logger::error() << "Failed content insertion " << contentQuery.lastError() << Qt::endl;
|
|
||||||
return DBFAIL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!db.commit())
|
if(!db.commit())
|
||||||
{
|
{
|
||||||
db.rollback();
|
db.rollback();
|
||||||
|
@ -13,6 +13,7 @@ class SqliteDbService
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
DatabaseFactory *dbFactory = nullptr;
|
DatabaseFactory *dbFactory = nullptr;
|
||||||
|
bool insertToFTS(bool useTrigrams, QSqlDatabase &db, int fileid, QVector<PageData> &pageData);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SqliteDbService(DatabaseFactory &dbFactory);
|
SqliteDbService(DatabaseFactory &dbFactory);
|
||||||
|
@ -82,11 +82,10 @@ QString SqliteSearch::escapeFtsArgument(QString ftsArg)
|
|||||||
{
|
{
|
||||||
value = value.mid(0, value.size() - 1);
|
value = value.mid(0, value.size() - 1);
|
||||||
}
|
}
|
||||||
result += "\"" + value + "\"*";
|
result += "\"" + value + "\"* ";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
value = "\"\"" + value + "\"\"";
|
|
||||||
result += "\"" + value + "\" ";
|
result += "\"" + value + "\" ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,9 +141,7 @@ QPair<QString, QVector<QString>> SqliteSearch::createSql(const Token &token)
|
|||||||
}
|
}
|
||||||
if(token.type == FILTER_CONTENT_CONTAINS)
|
if(token.type == FILTER_CONTENT_CONTAINS)
|
||||||
{
|
{
|
||||||
return {" content.id IN (SELECT fts.ROWID FROM fts WHERE fts.content MATCH ? ORDER BY "
|
return {" fts MATCH ? ", {escapeFtsArgument(value)}};
|
||||||
"rank) ",
|
|
||||||
{escapeFtsArgument(value)}};
|
|
||||||
}
|
}
|
||||||
throw LooqsGeneralException("Unknown token passed (should not happen)");
|
throw LooqsGeneralException("Unknown token passed (should not happen)");
|
||||||
}
|
}
|
||||||
@ -164,26 +161,14 @@ QSqlQuery SqliteSearch::makeSqlQuery(const LooqsQuery &query)
|
|||||||
auto tokens = query.getTokens();
|
auto tokens = query.getTokens();
|
||||||
for(const Token &token : tokens)
|
for(const Token &token : tokens)
|
||||||
{
|
{
|
||||||
if(token.type == FILTER_CONTENT_CONTAINS)
|
auto sql = createSql(token);
|
||||||
{
|
whereSql += sql.first;
|
||||||
if(!ftsAlreadyJoined)
|
bindValues.append(sql.second);
|
||||||
{
|
|
||||||
joinSql += " INNER JOIN fts ON content.ftsid = fts.ROWID ";
|
|
||||||
ftsAlreadyJoined = true;
|
|
||||||
}
|
|
||||||
whereSql += " fts.content MATCH ? ";
|
|
||||||
bindValues.append(escapeFtsArgument(token.value));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
auto sql = createSql(token);
|
|
||||||
whereSql += sql.first;
|
|
||||||
bindValues.append(sql.second);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString prepSql;
|
QString prepSql;
|
||||||
QString sortSql = createSortSql(query.getSortConditions());
|
QString sortSql = createSortSql(query.getSortConditions());
|
||||||
|
int bindIterations = 1;
|
||||||
if(isContentSearch)
|
if(isContentSearch)
|
||||||
{
|
{
|
||||||
if(sortSql.isEmpty())
|
if(sortSql.isEmpty())
|
||||||
@ -191,12 +176,24 @@ QSqlQuery SqliteSearch::makeSqlQuery(const LooqsQuery &query)
|
|||||||
if(std::find_if(tokens.begin(), tokens.end(),
|
if(std::find_if(tokens.begin(), tokens.end(),
|
||||||
[](const Token &t) -> bool { return t.type == FILTER_CONTENT_CONTAINS; }) != tokens.end())
|
[](const Token &t) -> bool { return t.type == FILTER_CONTENT_CONTAINS; }) != tokens.end())
|
||||||
{
|
{
|
||||||
sortSql = "ORDER BY rank";
|
sortSql = "ORDER BY prio, rank";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prepSql = "SELECT file.path AS path, content.page AS page, file.mtime AS mtime, file.size AS size, "
|
QString whereSqlTrigram = whereSql;
|
||||||
"file.filetype AS filetype FROM file INNER JOIN content ON file.id = content.fileid " +
|
whereSqlTrigram.replace("fts MATCH", "fts_trigram MATCH"); // A bit dirty...
|
||||||
joinSql + " WHERE 1=1 AND " + whereSql + " " + sortSql;
|
prepSql =
|
||||||
|
"SELECT DISTINCT path, page, mtime, size, filetype FROM ("
|
||||||
|
"SELECT file.path AS path, content.page AS page, file.mtime AS mtime, file.size AS size, "
|
||||||
|
"file.filetype AS filetype, 0 AS prio, fts.rank AS rank FROM file INNER JOIN content ON file.id = "
|
||||||
|
"content.fileid "
|
||||||
|
"INNER JOIN fts ON content.ftsid = fts.ROWID WHERE 1=1 AND " +
|
||||||
|
whereSql +
|
||||||
|
"UNION ALL SELECT file.path AS path, content.page AS page, file.mtime AS mtime, file.size AS size, "
|
||||||
|
"file.filetype AS filetype, 1 as prio, fts_trigram.rank AS rank FROM file INNER JOIN content ON file.id = "
|
||||||
|
"content.fileid " +
|
||||||
|
"INNER JOIN fts_trigram ON content.fts_trigramid = fts_trigram.ROWID WHERE 1=1 AND " + whereSqlTrigram +
|
||||||
|
" ) " + sortSql;
|
||||||
|
++bindIterations;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -216,11 +213,14 @@ QSqlQuery SqliteSearch::makeSqlQuery(const LooqsQuery &query)
|
|||||||
|
|
||||||
QSqlQuery dbquery(*db);
|
QSqlQuery dbquery(*db);
|
||||||
dbquery.prepare(prepSql);
|
dbquery.prepare(prepSql);
|
||||||
for(const QString &value : bindValues)
|
for(int i = 0; i < bindIterations; i++)
|
||||||
{
|
{
|
||||||
if(value != "")
|
for(const QString &value : bindValues)
|
||||||
{
|
{
|
||||||
dbquery.addBindValue(value);
|
if(value != "")
|
||||||
|
{
|
||||||
|
dbquery.addBindValue(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return dbquery;
|
return dbquery;
|
||||||
|
Loading…
Reference in New Issue
Block a user