Skip to content

Commit

Permalink
database: Fixes regarding of db speed
Browse files Browse the repository at this point in the history
Added indexes, users.watched reverted to 60s
  • Loading branch information
sogehige committed Aug 8, 2018
1 parent 653017b commit 955b7e8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/bot/_interface.js
Expand Up @@ -28,11 +28,18 @@ class Module {
this._prepare(opts.settings)
this._sockets()
this._cleanEmptySettingsValues()
this._indexDbs()
this.status()
}

async _indexDbs () {
if (cluster.isMaster) {
// add indexing to settings
global.db.engine.index({ table: this.collection.settings, index: 'category' })
if (!global.db.engine.connected) {
new Timeout().recursive({ this: this, uid: `${this.constructor.name}._indexDbs`, wait: 1000, fnc: this._indexDbs })
} else {
// add indexing to settings
global.db.engine.index({ table: this.collection.settings, index: 'category' })
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/bot/databases/mongodb.js
Expand Up @@ -28,8 +28,15 @@ class IMongoDB extends Interface {
if (!opts.index) throw new Error('Missing index option')
if (!opts.table) throw new Error('Missing table option')

await this.client.db(this.dbName).collection(opts.table).dropIndexes()
await this.client.db(this.dbName).collection(opts.table).createIndex(opts.index, { unique: opts.unique })
try {
let db = await this.client.db(this.dbName)
await db.collection(opts.table).dropIndexes()
await db.collection(opts.table).createIndex(opts.index, { unique: opts.unique })
return
} catch (e) {
// indexes will be created when collection is available
setTimeout(() => this.index(opts), 5000)
}
}

async connect () {
Expand Down Expand Up @@ -57,6 +64,7 @@ class IMongoDB extends Interface {
await db.collection('cache').createIndex('key', { unique: true })
await db.collection('customTranslations').createIndex('key')
await db.collection('stats').createIndex('whenOnline')
await db.collection('cache.hosts').createIndex('username', { unique: true })
}
this.connected = true
}
Expand Down
2 changes: 2 additions & 0 deletions src/bot/databases/nedb.js
Expand Up @@ -47,6 +47,7 @@ class INeDB extends Interface {
case 'users.points':
case 'users.messages':
case 'users.watched':
case 'cache.hosts':
this.table[table].removeIndex('username')
break
case 'cache':
Expand All @@ -66,6 +67,7 @@ class INeDB extends Interface {
this.table[table].ensureIndex({fieldName: 'timestamp'})
break
case 'users':
case 'cache.hosts':
this.table[table].ensureIndex({fieldName: 'username', unique: true})
break
case 'users.online':
Expand Down
2 changes: 1 addition & 1 deletion src/bot/users.js
Expand Up @@ -532,7 +532,7 @@ Users.prototype.delete = function (username) {
}

Users.prototype.updateWatchTime = async function (lastUpdate) {
let timeout = 1000
let timeout = 60000
try {
// count watching time when stream is online
if (await global.cache.isOnline()) {
Expand Down

0 comments on commit 955b7e8

Please sign in to comment.