Skip to content

Commit

Permalink
DB optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
f-r00t committed Apr 19, 2024
1 parent 65e04b8 commit 5ff05e5
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions src/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ async function createTables(DB) {
hash TEXT,
reply TEXT,
UNIQUE (timestamp)
replies INT default 0
)`
);

Expand All @@ -267,8 +268,12 @@ async function createTables(DB) {
)`
);



tx.executeSql(
`CREATE TABLE IF NOT EXISTS misc (
lastSyncGroup TEXT
lastSyncDM TEXT
)`
);

tx.executeSql(
`CREATE TABLE IF NOT EXISTS boards_subscriptions (
Expand Down Expand Up @@ -389,10 +394,6 @@ async function createTables(DB) {
`REPLACE INTO privateboards_messages_db2 SELECT * FROM privateboards_messages_db`
);

tx.executeSql(
`DROP TABLE privateboards_messages_db`
);

tx.executeSql(
`CREATE TABLE IF NOT EXISTS privateboards_messages_db (
board TEXT,
Expand All @@ -418,6 +419,15 @@ async function createTables(DB) {

}

if (dbVersion === 8) {
tx.executeSql(
`ALTER TABLE
privateboards_messages_db
ADD
replies INT default 0`
);
}


/* Setup default preference values */
tx.executeSql(
Expand Down Expand Up @@ -495,7 +505,7 @@ async function createTables(DB) {
`);

tx.executeSql(
`PRAGMA user_version = 8`
`PRAGMA user_version = 9`
);
});

Expand Down Expand Up @@ -775,7 +785,22 @@ export async function saveGroupMessage(group, type, message, timestamp, nickname
);
});

Globals.updateGroups();

if (reply) {

await database.transaction((tx) => {
tx.executeSql(
`UPDATE privateboards_messages_db
SET replies = replies + ?
WHERE hash = ?`,
[1, reply]
);

});

}

Globals.updateGroups();

}

Expand Down Expand Up @@ -1320,38 +1345,23 @@ export async function getMessages(conversation=false, limit=25) {

export async function getGroupMessages(group=false, limit=25) {

const stack = new Error().stack;
const callerInfo = stack.split('\n');

if (limit < 25) limit = 25;

const starttime = Date.now();

const [data] = await database.executeSql(
`
SELECT
pm.nickname,
pm.type,
pm.message,
pm.timestamp,
pm.board,
pm.address,
pm.hash,
pm.reply,
COALESCE(rc.reply_count, 0) AS replies
FROM
privateboards_messages_db pm
LEFT JOIN (
SELECT
reply,
COUNT(*) AS reply_count
FROM
privateboards_messages_db
WHERE
reply != ''
GROUP BY
reply
) rc ON pm.hash = rc.reply
WHERE
pm.reply = '' ${group ? ' AND pm.board = "' + group + '"' : ''}
ORDER BY
pm.timestamp DESC
LIMIT ${limit}`
SELECT
*
FROM
privateboards_messages_db
${group ? 'WHERE board = "' + group + '"' : ''}
ORDER BY
timestamp DESC
LIMIT ${limit}`
);

const [count] = await database.executeSql(
Expand Down

0 comments on commit 5ff05e5

Please sign in to comment.