fix(sqlite): removed unique idx on phone, optimized queries

This commit is contained in:
teidesu 2021-08-04 12:58:17 +03:00
parent 860a0e623f
commit 781384cb86

View file

@ -91,7 +91,7 @@ const SCHEMA = `
"full" blob "full" blob
); );
create index idx_entities_username on entities (username); create index idx_entities_username on entities (username);
create unique index idx_entities_phone on entities (phone); create index idx_entities_phone on entities (phone);
` `
const RESET = ` const RESET = `
@ -147,10 +147,10 @@ const STATEMENTS = {
upsertEnt: upsertEnt:
'insert or replace into entities (id, hash, type, username, phone, updated, "full") values (?, ?, ?, ?, ?, ?, ?)', 'insert or replace into entities (id, hash, type, username, phone, updated, "full") values (?, ?, ?, ?, ?, ?, ?)',
getEntById: 'select * from entities where id = ?', getEntById: 'select * from entities where id = ?',
getEntByPhone: 'select * from entities where phone = ?', getEntByPhone: 'select * from entities where phone = ? limit 1',
getEntByUser: 'select * from entities where username = ?', getEntByUser: 'select * from entities where username = ? limit 1',
delStaleState: 'delete from state where expires < ?' delStaleState: 'delete from state where expires < ?',
} as const } as const
const EMPTY_BUFFER = Buffer.alloc(0) const EMPTY_BUFFER = Buffer.alloc(0)
@ -297,7 +297,6 @@ export class SqliteStorage implements ITelegramStorage /*, IStateStorage */ {
this._pendingUnimportant = {} this._pendingUnimportant = {}
}, params?.unimportantSavesDelay ?? 30000) }, params?.unimportantSavesDelay ?? 30000)
this._vacuumInterval = params?.vacuumInterval ?? 300_000 this._vacuumInterval = params?.vacuumInterval ?? 300_000
// todo: add support for workers (idk if really needed, but still) // todo: add support for workers (idk if really needed, but still)
@ -465,21 +464,26 @@ export class SqliteStorage implements ITelegramStorage /*, IStateStorage */ {
return this._setToKv('self', self) return this._setToKv('self', self)
} }
getUpdatesState(): [number, number, number] | null { getUpdatesState(): [number, number, number, number] | null {
const pts = this._getFromKv('pts') const pts = this._getFromKv('pts')
if (pts == null) return null if (pts == null) return null
return [pts, this._getFromKv('date')!, this._getFromKv('seq')!] return [
} pts,
this._getFromKv('qts')!,
setCommonPts(val: [number, number] | null): void { this._getFromKv('date')!,
return this._setToKv('cpts', val) this._getFromKv('seq')!,
]
} }
setUpdatesPts(val: number): void { setUpdatesPts(val: number): void {
return this._setToKv('pts', val) return this._setToKv('pts', val)
} }
setUpdatesQts(val: number): void {
return this._setToKv('qts', val)
}
setUpdatesDate(val: number): void { setUpdatesDate(val: number): void {
return this._setToKv('date', val) return this._setToKv('date', val)
} }
@ -607,7 +611,7 @@ export class SqliteStorage implements ITelegramStorage /*, IStateStorage */ {
const full = this._readFullPeer(row.full) const full = this._readFullPeer(row.full)
this._addToCache(id, { this._addToCache(id, {
peer: getInputPeer(row), peer: getInputPeer(row),
full full,
}) })
return full return full
} }
@ -689,7 +693,7 @@ export class SqliteStorage implements ITelegramStorage /*, IStateStorage */ {
// expired or does not exist // expired or does not exist
const item: FsmItem = { const item: FsmItem = {
expires: now + window * 1000, expires: now + window * 1000,
value: limit value: limit,
} }
this._statements.setState.run( this._statements.setState.run(