From 36ab9d81e5e333dec79f4c2f8959ca90eee85804 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Wed, 13 Oct 2021 23:10:51 +0200 Subject: [PATCH] Still no work properly... --- libs/database/init.js | 5 ++++- libs/utils/multithread/db.test.js | 35 ++++++++++++++++++------------- tests/multihread.test.js | 18 ++++------------ 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/libs/database/init.js b/libs/database/init.js index 72877dd..34ccd8c 100644 --- a/libs/database/init.js +++ b/libs/database/init.js @@ -5,7 +5,10 @@ import { open } from 'sqlite' const log = initLog("database") -export let sqlite = undefined +export let sqlite = await open({ + filename: 'ddnet.sqlite', + driver: sqlite3.cached.Database +}) /** * This initializes both the sqlite db and mongodb. diff --git a/libs/utils/multithread/db.test.js b/libs/utils/multithread/db.test.js index 72e6104..ed05477 100644 --- a/libs/utils/multithread/db.test.js +++ b/libs/utils/multithread/db.test.js @@ -1,24 +1,31 @@ import Finish from '../../../schemas/Finish-copy.js' import initLog from '../log.js' +import init from '../../database/init.js' +import { sqlite } from '../../database/init.js' +import dotenv from 'dotenv' + +dotenv.config() +init() const log = initLog('DB TEST') export function main(data) { return new Promise( - (resolve, reject) => { - const {err, finish} = data - - Finish.create({ - map: finish.Map, - time: finish.Time, - date: finish.Timestamp === '0000-00-00 00:00:00' ? new Date('January 1, 1970 00:00:00 UTC') : new Date(`${finish.Timestamp}+00:00`), - serverLocation: finish.Server ?? '', - player: finish.Name - }).then(() => { - log(`Added finish ~/~ -> At ${finish.Timestamp} «${finish.Name}» completed «${finish.Map}» in ${finish.Time} s`) - }) - - resolve(true) + async (resolve, reject) => { + const rows = await sqlite.all(`SELECT * FROM race ORDER BY Timestamp LIMIT 5000 OFFSET ${data.offset + 1}`) + let finishes = [] + rows.map( + finish => finishes.push({ + map: finish.Map, + time: finish.Time, + date: finish.Timestamp === '0000-00-00 00:00:00' ? new Date('January 1, 1970 00:00:00 UTC') : new Date(`${finish.Timestamp}+00:00`), + serverLocation: finish.Server ?? '', + player: finish.Name + }) + ) + await Finish.insertMany(finishes) + log('Inserted!') + resolve() } ) } \ No newline at end of file diff --git a/tests/multihread.test.js b/tests/multihread.test.js index 9a9b117..23bd224 100644 --- a/tests/multihread.test.js +++ b/tests/multihread.test.js @@ -1,5 +1,4 @@ import { initWorkers, spread } from '../libs/utils/multithread.js' -import { sqlite } from '../libs/database/init.js' import Finish from '../schemas/Finish-copy.js' import initLog from '../libs/utils/log.js' @@ -14,19 +13,10 @@ export default async function() { let offset = -1 while (offset < 10000000) { - await sqlite.each( - `SELECT * FROM race ORDER BY Timestamp LIMIT 5000 OFFSET ${offset + 1}`, - [], - (err, finish) => { - spread( - './db.test.js', - { - err, - finish, - } - ).then( - result => {} - ) + await spread( + './db.test.js', + { + offset } ) offset += 5000