Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlite3 support #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions lib/sqlite3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
'use strict';

const winston = require.main.require('winston');
const nconf = require.main.require('nconf');

const database = require.main.require('./src/database');
const pubsub = require.main.require('./src/pubsub');

async function initDB() {
const { db } = database;
db.exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS "searchtopic" USING fts5(
"content",
"uid" UNINDEXED,
"cid" UNINDEXED)`);
db.exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS "searchpost" USING fts5(
"content",
"uid" UNINDEXED,
"cid" UNINDEXED)`);
}

async function handleError(err) {
if (err && /no such table/i.test(err.message)) {
winston.warn('dbsearch was not initialized');
await initDB();
return;
}
throw err;
}

exports.createIndices = async function (language) {
if (nconf.get('isPrimary') && !nconf.get('jobsDisabled')) {
await initDB();
}
};

exports.changeIndexLanguage = async function (language) {
pubsub.publish('dbsearch-language-changed', language);
};

exports.searchIndex = async function (key, data, ids) {
const { db } = database;
if (!ids.length) {
return;
}

ids = ids.map(id => parseInt(id, 10));
try {
const upsert = db.prepare(`
REPLACE INTO "search${key}" ("rowid", "content", "uid", "cid")
VALUES (@id, @content, @uid, @cid)`);
for (const [ i, id ] of ids.entries()) {
const { content, uid, cid } = data[i];
upsert.run({ id, content, uid, cid });
}
} catch (err) {
winston.error(`Error indexing ${err.stack}`);
await handleError(err);
await exports.searchIndex(key, data, ids);
}
};

exports.search = async function (key, data, limit) {
const { db } = database;
const { content, matchWords, uid, cid } = data;
if (!content) {
return [];
}
const query = parseQuery(content, matchWords);
const [ params, uidList ] = listParams({ query, limit }, uid, 'uid');
const [ , cidList ] = listParams(params, cid, 'cid');
const conditions = [ `"content" MATCH @query` ];
if (uidList.length > 0) {
conditions.push(`uid IN (${uidList})`);
}
if (cidList.length > 0) {
conditions.push(`cid IN (${cidList})`);
}
try {
const rows = db.prepare(`
SELECT rowid FROM "search${key}"
WHERE ${conditions.join(' AND ')}
LIMIT @limit`).all(params);
return rows.map(r => r.rowid);
} catch (err) {
await handleError(err);
return [];
}
};

exports.searchRemove = async function (key, ids) {
const { db } = database;
if (!key || !ids.length) {
return;
}
const [ params, idList ] = listParams({}, ids);
try {
db.prepare(`
DELETE FROM "search${key}"
WHERE "rowid" IN (${idList})`).run(params);
} catch (err) {
await handleError(err);
}
};

function listParams(params, keys, prefix) {
const keyList = [];
if (Array.isArray(keys)) {
for (const [ i, k ] of keys.entries()) {
const name = prefix + i;
params[name] = parseInt(k);
keyList.push(`@${name}`);
}
}
return [params, keyList];
}

function parseQuery(content, matchWords) {
const words = content.trim().split(/\s+/);
const sep = (matchWords === 'any') ? ' OR ' : ' ';
return words.join(sep);
}