@peerbit/shared-log 10.3.23 → 10.4.0-b4531d8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ranges.ts CHANGED
@@ -2370,7 +2370,11 @@ export const matchEntriesInRangeQuery = (range: {
2370
2370
  }),
2371
2371
  ]);
2372
2372
 
2373
- if (range.start2 === range.end2) {
2373
+ // if range2 has length 0 or range 2 is equal to range 1 only make one query
2374
+ if (
2375
+ range.start2 === range.end2 ||
2376
+ (range.start1 === range.start2 && range.end1 === range.end2)
2377
+ ) {
2374
2378
  return c1;
2375
2379
  }
2376
2380
 
@@ -2392,6 +2396,45 @@ export const matchEntriesInRangeQuery = (range: {
2392
2396
  return new Or(ors);
2393
2397
  };
2394
2398
 
2399
+ export const createAssignedRangesQuery = (
2400
+ changes: {
2401
+ range: {
2402
+ start1: number | bigint;
2403
+ end1: number | bigint;
2404
+ start2: number | bigint;
2405
+ end2: number | bigint;
2406
+ mode: ReplicationIntent;
2407
+ };
2408
+ }[],
2409
+ options?: { strict?: boolean },
2410
+ ) => {
2411
+ let ors: Query[] = [];
2412
+ let onlyStrict = true;
2413
+ // TODO what if the ranges are many many?
2414
+ for (const change of changes) {
2415
+ const matchRange = matchEntriesInRangeQuery(change.range);
2416
+ ors.push(matchRange);
2417
+ if (change.range.mode === ReplicationIntent.NonStrict) {
2418
+ onlyStrict = false;
2419
+ }
2420
+ }
2421
+
2422
+ // entry is assigned to a range boundary, meaning it is due to be inspected
2423
+ if (!options?.strict) {
2424
+ if (!onlyStrict || changes.length === 0) {
2425
+ ors.push(
2426
+ new BoolQuery({
2427
+ key: "assignedToRangeBoundary",
2428
+ value: true,
2429
+ }),
2430
+ );
2431
+ }
2432
+ }
2433
+
2434
+ // entry is not sufficiently replicated, and we are to still keep it
2435
+ return new Or(ors);
2436
+ };
2437
+
2395
2438
  export type ReplicationChanges<
2396
2439
  T extends ReplicationRangeIndexable<any> = ReplicationRangeIndexable<any>,
2397
2440
  > = ReplicationChange<T>[];
@@ -2564,35 +2607,10 @@ export const toRebalance = <R extends "u32" | "u64">(
2564
2607
  rebalanceHistory: Cache<string>,
2565
2608
  ): AsyncIterable<EntryReplicated<R>> => {
2566
2609
  const change = mergeReplicationChanges(changeOrChanges, rebalanceHistory);
2567
-
2568
- const assignedRangesQuery = (changes: ReplicationChanges) => {
2569
- let ors: Query[] = [];
2570
- let onlyStrict = true;
2571
- for (const change of changes) {
2572
- const matchRange = matchEntriesInRangeQuery(change.range);
2573
- ors.push(matchRange);
2574
- if (change.range.mode === ReplicationIntent.NonStrict) {
2575
- onlyStrict = false;
2576
- }
2577
- }
2578
-
2579
- // entry is assigned to a range boundary, meaning it is due to be inspected
2580
- if (!onlyStrict || changes.length === 0) {
2581
- ors.push(
2582
- new BoolQuery({
2583
- key: "assignedToRangeBoundary",
2584
- value: true,
2585
- }),
2586
- );
2587
- }
2588
-
2589
- // entry is not sufficiently replicated, and we are to still keep it
2590
- return new Or(ors);
2591
- };
2592
2610
  return {
2593
2611
  [Symbol.asyncIterator]: async function* () {
2594
2612
  const iterator = index.iterate({
2595
- query: assignedRangesQuery(change),
2613
+ query: createAssignedRangesQuery(change),
2596
2614
  });
2597
2615
 
2598
2616
  while (iterator.done() !== true) {