From d31802f577dc6b05d94707be01c63b5bd69e8956 Mon Sep 17 00:00:00 2001 From: Benoit Lavenier <benoit.lavenier@e-is.pro> Date: Wed, 17 May 2023 09:15:30 +0200 Subject: [PATCH] IIndexDAO: Rename searchByPubkey() into getOldFromPubkey(), and simplify signature SqliteIIndex: Apply a reduce() inside getOldFromPubkey() (+ add an ORDER BY) IdentityDAL: rename getFromPubkey() into findByPub() as it return an array FileDAL: simplify searchJustIdentitiesByPubkey() --- app/lib/dal/fileDAL.ts | 8 +++---- app/lib/dal/indexDAL/abstract/IIndexDAO.ts | 2 +- app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts | 8 +++---- app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts | 21 +++++++++---------- app/lib/dal/sqliteDAL/IdentityDAL.ts | 4 ++-- 5 files changed, 19 insertions(+), 24 deletions(-) diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index 5579b7ba5..02e8c6d24 100644 --- a/app/lib/dal/fileDAL.ts +++ b/app/lib/dal/fileDAL.ts @@ -933,11 +933,9 @@ export class FileDAL implements ServerDAO { } async searchJustIdentitiesByPubkey(pubkey: string): Promise<DBIdentity[]> { - const pendings = await this.idtyDAL.getFromPubkey(pubkey); - const writtens = await this.iindexDAL.searchByPubkey(pubkey); - const nonPendings = Underscore.filter(writtens, (w: IindexEntry) => { - return Underscore.where(pendings, { pubkey: w.pub }).length == 0; - }); + const pendings = await this.idtyDAL.findByPub(pubkey); + const writtenIdty = await this.iindexDAL.getOldFromPubkey(pubkey); + const nonPendings = writtenIdty && Underscore.where(pendings, { pubkey: writtenIdty.pub }).length === 0 ? [writtenIdty] : []; const found = pendings.concat( nonPendings.map((i: any) => { // Use the correct field diff --git a/app/lib/dal/indexDAL/abstract/IIndexDAO.ts b/app/lib/dal/indexDAL/abstract/IIndexDAO.ts index ecf64d441..e5970f214 100644 --- a/app/lib/dal/indexDAL/abstract/IIndexDAO.ts +++ b/app/lib/dal/indexDAL/abstract/IIndexDAO.ts @@ -19,7 +19,7 @@ export interface IIndexDAO extends ReduceableDAO<IindexEntry> { searchThoseMatching(search: string): Promise<OldIindexEntry[]>; - searchByPubkey(search: string): Promise<OldIindexEntry[]>; + getOldFromPubkey(search: string): Promise<OldIindexEntry | null>; getFullFromUID(uid: string): Promise<FullIindexEntry>; diff --git a/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts b/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts index e7157b34e..fd5010024 100644 --- a/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts +++ b/app/lib/dal/indexDAL/leveldb/LevelDBIindex.ts @@ -281,13 +281,11 @@ export class LevelDBIindex extends LevelDBTable<IindexEntry[]> .concat(pubIdentities.filter((p) => p.pub)); } - async searchByPubkey(pub: string): Promise<OldIindexEntry[]> { + async getOldFromPubkey(pub: string): Promise<OldIindexEntry | null> { const identities = await this.findByPub(pub); if (!identities.length) { - return []; + return null; } - // TODO Why do we need to use reduce() on array ? This will merge items into one object - const mergedIdentities = OldTransformers.toOldIindexEntry(reduce(identities)) - return [mergedIdentities]; + return OldTransformers.toOldIindexEntry(reduce(identities)); } } diff --git a/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts b/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts index 2cf0aeb7f..806ee307a 100644 --- a/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts +++ b/app/lib/dal/indexDAL/sqlite/SqliteIIndex.ts @@ -1,4 +1,4 @@ -import { FullIindexEntry, IindexEntry, Indexer } from "../../../indexer"; +import {FullIindexEntry, IindexEntry, Indexer, reduce} from "../../../indexer"; import { SQLiteDriver } from "../../drivers/SQLiteDriver"; import { MonitorExecutionTime } from "../../../debug/MonitorExecutionTime"; import { IIndexDAO } from "../abstract/IIndexDAO"; @@ -212,6 +212,15 @@ export class SqliteIIndex extends SqliteTable<IindexEntry> return (await this.getFromUID(uid)) as FullIindexEntry; } + @MonitorExecutionTime() + async getOldFromPubkey(pub: string): Promise<OldIindexEntry | null> { + const identities = await this.find("SELECT * FROM iindex WHERE pub = ? order by writtenOn ASC", [pub]); + if (!identities.length) { + return null; + } + return OldTransformers.toOldIindexEntry(reduce(identities)); + } + @MonitorExecutionTime() async getMembers(): Promise<{ pubkey: string; uid: string | null }[]> { const members = await this.find( @@ -263,14 +272,4 @@ export class SqliteIIndex extends SqliteTable<IindexEntry> ]) ).map(OldTransformers.toOldIindexEntry); } - - @MonitorExecutionTime() - async searchByPubkey(pub: string): Promise<OldIindexEntry[]> { - // TODO Why not need reduce() here ? As done in the LevelDB implementation - return ( - await this.find("SELECT * FROM iindex WHERE pub = ?", [ - pub - ]) - ).map(OldTransformers.toOldIindexEntry); - } } diff --git a/app/lib/dal/sqliteDAL/IdentityDAL.ts b/app/lib/dal/sqliteDAL/IdentityDAL.ts index 9716b13f7..012a1646f 100644 --- a/app/lib/dal/sqliteDAL/IdentityDAL.ts +++ b/app/lib/dal/sqliteDAL/IdentityDAL.ts @@ -328,8 +328,8 @@ export class IdentityDAL extends AbstractSQLite<DBIdentity> { }); } - getFromPubkey(pubkey: string) { - return this.sqlFind({pubkey}); + findByPub(pub: string) { + return this.sqlFind({pubkey: pub}); } async trimExpiredIdentities(medianTime: number) { -- GitLab