Skip to content
Snippets Groups Projects
Commit d31802f5 authored by Benoit Lavenier's avatar Benoit Lavenier
Browse files

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()
parent 9ef06bf3
No related branches found
No related tags found
1 merge request!1422Optimize response time of `/wot/requirements/:search`
...@@ -933,11 +933,9 @@ export class FileDAL implements ServerDAO { ...@@ -933,11 +933,9 @@ export class FileDAL implements ServerDAO {
} }
async searchJustIdentitiesByPubkey(pubkey: string): Promise<DBIdentity[]> { async searchJustIdentitiesByPubkey(pubkey: string): Promise<DBIdentity[]> {
const pendings = await this.idtyDAL.getFromPubkey(pubkey); const pendings = await this.idtyDAL.findByPub(pubkey);
const writtens = await this.iindexDAL.searchByPubkey(pubkey); const writtenIdty = await this.iindexDAL.getOldFromPubkey(pubkey);
const nonPendings = Underscore.filter(writtens, (w: IindexEntry) => { const nonPendings = writtenIdty && Underscore.where(pendings, { pubkey: writtenIdty.pub }).length === 0 ? [writtenIdty] : [];
return Underscore.where(pendings, { pubkey: w.pub }).length == 0;
});
const found = pendings.concat( const found = pendings.concat(
nonPendings.map((i: any) => { nonPendings.map((i: any) => {
// Use the correct field // Use the correct field
......
...@@ -19,7 +19,7 @@ export interface IIndexDAO extends ReduceableDAO<IindexEntry> { ...@@ -19,7 +19,7 @@ export interface IIndexDAO extends ReduceableDAO<IindexEntry> {
searchThoseMatching(search: string): Promise<OldIindexEntry[]>; searchThoseMatching(search: string): Promise<OldIindexEntry[]>;
searchByPubkey(search: string): Promise<OldIindexEntry[]>; getOldFromPubkey(search: string): Promise<OldIindexEntry | null>;
getFullFromUID(uid: string): Promise<FullIindexEntry>; getFullFromUID(uid: string): Promise<FullIindexEntry>;
......
...@@ -281,13 +281,11 @@ export class LevelDBIindex extends LevelDBTable<IindexEntry[]> ...@@ -281,13 +281,11 @@ export class LevelDBIindex extends LevelDBTable<IindexEntry[]>
.concat(pubIdentities.filter((p) => p.pub)); .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); const identities = await this.findByPub(pub);
if (!identities.length) { if (!identities.length) {
return []; return null;
} }
// TODO Why do we need to use reduce() on array ? This will merge items into one object return OldTransformers.toOldIindexEntry(reduce(identities));
const mergedIdentities = OldTransformers.toOldIindexEntry(reduce(identities))
return [mergedIdentities];
} }
} }
import { FullIindexEntry, IindexEntry, Indexer } from "../../../indexer"; import {FullIindexEntry, IindexEntry, Indexer, reduce} from "../../../indexer";
import { SQLiteDriver } from "../../drivers/SQLiteDriver"; import { SQLiteDriver } from "../../drivers/SQLiteDriver";
import { MonitorExecutionTime } from "../../../debug/MonitorExecutionTime"; import { MonitorExecutionTime } from "../../../debug/MonitorExecutionTime";
import { IIndexDAO } from "../abstract/IIndexDAO"; import { IIndexDAO } from "../abstract/IIndexDAO";
...@@ -212,6 +212,15 @@ export class SqliteIIndex extends SqliteTable<IindexEntry> ...@@ -212,6 +212,15 @@ export class SqliteIIndex extends SqliteTable<IindexEntry>
return (await this.getFromUID(uid)) as FullIindexEntry; 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() @MonitorExecutionTime()
async getMembers(): Promise<{ pubkey: string; uid: string | null }[]> { async getMembers(): Promise<{ pubkey: string; uid: string | null }[]> {
const members = await this.find( const members = await this.find(
...@@ -263,14 +272,4 @@ export class SqliteIIndex extends SqliteTable<IindexEntry> ...@@ -263,14 +272,4 @@ export class SqliteIIndex extends SqliteTable<IindexEntry>
]) ])
).map(OldTransformers.toOldIindexEntry); ).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);
}
} }
...@@ -328,8 +328,8 @@ export class IdentityDAL extends AbstractSQLite<DBIdentity> { ...@@ -328,8 +328,8 @@ export class IdentityDAL extends AbstractSQLite<DBIdentity> {
}); });
} }
getFromPubkey(pubkey: string) { findByPub(pub: string) {
return this.sqlFind({pubkey}); return this.sqlFind({pubkey: pub});
} }
async trimExpiredIdentities(medianTime: number) { async trimExpiredIdentities(medianTime: number) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment