skip diff in some cases

This commit is contained in:
Kieran 2023-06-13 16:29:51 +01:00
parent 79011f8ca2
commit 3ffc9bc81f
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
4 changed files with 18 additions and 5 deletions

View File

@ -118,9 +118,13 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
return this.Queries.get(id);
}
Query<T extends NoteStore>(type: { new (): T }, req: RequestBuilder): Query {
Query<T extends NoteStore>(type: { new(): T }, req: RequestBuilder): Query {
const existing = this.Queries.get(req.id);
if (existing) {
// if same instance, just return query
if (existing.fromInstance === req.instance) {
return existing;
}
const filters = !req.options?.skipDiff
? req.buildDiff(this.#relayCache, existing.flatFilters)
: req.build(this.#relayCache);
@ -137,7 +141,7 @@ export class NostrSystem extends ExternalStore<SystemSnapshot> implements System
const store = new type();
const filters = req.build(this.#relayCache);
const q = new Query(req.id, store, req.options?.leaveOpen);
const q = new Query(req.id, req.instance, store, req.options?.leaveOpen);
this.Queries.set(req.id, q);
for (const subQ of filters) {
this.SendQuery(q, subQ);

View File

@ -111,6 +111,11 @@ export class Query implements QueryBase {
*/
id: string;
/**
* RequestBuilder instance
*/
fromInstance: string;
/**
* Which relays this query has already been executed on
*/
@ -138,9 +143,10 @@ export class Query implements QueryBase {
#log = debug("Query");
constructor(id: string, feed: NoteStore, leaveOpen?: boolean) {
constructor(id: string, instance: string, feed: NoteStore, leaveOpen?: boolean) {
this.id = id;
this.#feed = feed;
this.fromInstance = instance;
this.#leaveOpen = leaveOpen ?? false;
this.#checkTraces();
}

View File

@ -5,6 +5,7 @@ import { RelayCache, splitAllByWriteRelays, splitByWriteRelays } from "./GossipM
import { mergeSimilar } from "./RequestMerger";
import { FlatReqFilter, expandFilter } from "./RequestExpander";
import debug from "debug";
import { v4 as uuid } from "uuid";
/**
* Which strategy is used when building REQ filters
@ -50,11 +51,13 @@ export interface RequestBuilderOptions {
*/
export class RequestBuilder {
id: string;
instance: string;
#builders: Array<RequestFilterBuilder>;
#options?: RequestBuilderOptions;
#log = debug("RequestBuilder");
constructor(id: string) {
this.instance = uuid();
this.id = id;
this.#builders = [];
}

View File

@ -10,7 +10,7 @@ window.crypto.getRandomValues = getRandomValues as any;
describe("query", () => {
test("progress", () => {
const q = new Query("test", new FlatNoteStore());
const q = new Query("test", "", new FlatNoteStore());
const opt = {
read: true,
write: true,
@ -66,7 +66,7 @@ describe("query", () => {
});
it("should merge all sub-query filters", () => {
const q = new Query("test", new FlatNoteStore());
const q = new Query("test", "", new FlatNoteStore());
const c0 = new Connection("wss://test.com", { read: true, write: true });
q.sendToRelay(c0, {
filters: [