@luvio/environments 0.146.0 → 0.146.2

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.
@@ -275,8 +275,9 @@ class DurableTTLStore {
275
275
  }
276
276
  }
277
277
 
278
- function flushInMemoryStoreValuesToDurableStore(store, durableStore, durableStoreErrorHandler, redirects, additionalDurableStoreOperations = []) {
278
+ function flushInMemoryStoreValuesToDurableStore(store, durableStore, durableStoreErrorHandler, redirects, additionalDurableStoreOperations = [], enableDurableMetadataRefresh = false) {
279
279
  const durableRecords = create(null);
280
+ const refreshedDurableRecords = create(null);
280
281
  const evictedRecords = create(null);
281
282
  const { records, metadata: storeMetadata, visitedIds, refreshedIds, } = store.fallbackStringKeyInMemoryStore;
282
283
  // TODO: W-8909393 Once metadata is stored in its own segment we need to
@@ -286,32 +287,36 @@ function flushInMemoryStoreValuesToDurableStore(store, durableStore, durableStor
286
287
  for (let i = 0, len = keys$1.length; i < len; i += 1) {
287
288
  const key = keys$1[i];
288
289
  const record = records[key];
290
+ const wasVisited = visitedIds[key] !== undefined;
289
291
  // this record has been evicted, evict from DS
290
- if (record === undefined) {
292
+ if (wasVisited && record === undefined) {
291
293
  evictedRecords[key] = true;
292
294
  continue;
293
295
  }
294
296
  const metadata = storeMetadata[key];
295
- durableRecords[key] = {
296
- data: record,
297
- };
298
- if (metadata !== undefined) {
299
- durableRecords[key].metadata = {
300
- ...metadata,
301
- metadataVersion: DURABLE_METADATA_VERSION,
302
- };
303
- }
297
+ const entries = wasVisited === true || enableDurableMetadataRefresh === false
298
+ ? durableRecords
299
+ : refreshedDurableRecords;
300
+ setRecordTo(entries, key, record, metadata);
304
301
  }
305
302
  const durableStoreOperations = additionalDurableStoreOperations;
306
- // publishes
307
303
  const recordKeys = keys(durableRecords);
308
304
  if (recordKeys.length > 0) {
305
+ // publishes with data
309
306
  durableStoreOperations.push({
310
307
  type: 'setEntries',
311
308
  entries: durableRecords,
312
309
  segment: DefaultDurableSegment,
313
310
  });
314
311
  }
312
+ if (keys(refreshedDurableRecords).length > 0) {
313
+ // publishes with only metadata updates
314
+ durableStoreOperations.push({
315
+ type: 'setMetadata',
316
+ entries: refreshedDurableRecords,
317
+ segment: DefaultDurableSegment,
318
+ });
319
+ }
315
320
  // redirects
316
321
  redirects.forEach((value, key) => {
317
322
  durableStoreOperations.push({
@@ -338,6 +343,17 @@ function flushInMemoryStoreValuesToDurableStore(store, durableStore, durableStor
338
343
  }
339
344
  return Promise.resolve();
340
345
  }
346
+ function setRecordTo(entries, key, record, metadata) {
347
+ entries[key] = {
348
+ data: record,
349
+ };
350
+ if (metadata !== undefined) {
351
+ entries[key].metadata = {
352
+ ...metadata,
353
+ metadataVersion: DURABLE_METADATA_VERSION,
354
+ };
355
+ }
356
+ }
341
357
 
342
358
  const DurableEnvironmentEventDiscriminator = 'durable';
343
359
  function isDurableEnvironmentEvent(event) {
@@ -484,7 +500,7 @@ function isUnfulfilledSnapshot(cachedSnapshotResult) {
484
500
  * @param durableStore A DurableStore implementation
485
501
  * @param instrumentation An instrumentation function implementation
486
502
  */
487
- function makeDurable(environment, { durableStore, instrumentation, useRevivingStore }) {
503
+ function makeDurable(environment, { durableStore, instrumentation, useRevivingStore, enableDurableMetadataRefresh = false, }) {
488
504
  let stagingStore = null;
489
505
  const durableTTLStore = new DurableTTLStore(durableStore);
490
506
  const mergeKeysPromiseMap = new Map();
@@ -519,6 +535,7 @@ function makeDurable(environment, { durableStore, instrumentation, useRevivingSt
519
535
  const defaultSegmentKeys = [];
520
536
  const adapterContextSegmentKeys = [];
521
537
  const redirectSegmentKeys = [];
538
+ const metadataRefreshSegmentKeys = [];
522
539
  const messagingSegmentKeys = [];
523
540
  let shouldBroadcast = false;
524
541
  for (let i = 0, len = changes.length; i < len; i++) {
@@ -526,7 +543,12 @@ function makeDurable(environment, { durableStore, instrumentation, useRevivingSt
526
543
  // we only care about changes to the data which is stored in the default
527
544
  // segment or the adapter context
528
545
  if (change.segment === DefaultDurableSegment) {
529
- defaultSegmentKeys.push(...change.ids);
546
+ if (change.type === 'setMetadata') {
547
+ metadataRefreshSegmentKeys.push(...change.ids);
548
+ }
549
+ else {
550
+ defaultSegmentKeys.push(...change.ids);
551
+ }
530
552
  }
531
553
  else if (change.segment === AdapterContextSegment) {
532
554
  adapterContextSegmentKeys.push(...change.ids);
@@ -596,6 +618,20 @@ function makeDurable(environment, { durableStore, instrumentation, useRevivingSt
596
618
  }
597
619
  shouldBroadcast = true;
598
620
  }
621
+ // process metadata only refreshes
622
+ if (metadataRefreshSegmentKeys.length > 0) {
623
+ const entries = await durableStore.getMetadata(metadataRefreshSegmentKeys, DefaultDurableSegment);
624
+ if (entries !== undefined) {
625
+ const entryKeys = keys(entries);
626
+ for (let i = 0, len = entryKeys.length; i < len; i++) {
627
+ const entryKey = entryKeys[i];
628
+ const { metadata } = entries[entryKey];
629
+ if (metadata !== undefined) {
630
+ environment.putStoreMetadata(entryKey, metadata, false);
631
+ }
632
+ }
633
+ }
634
+ }
599
635
  if (shouldBroadcast) {
600
636
  await environment.storeBroadcast(rebuildSnapshot, environment.snapshotAvailable);
601
637
  }
@@ -666,7 +702,7 @@ function makeDurable(environment, { durableStore, instrumentation, useRevivingSt
666
702
  if (stagingStore === null) {
667
703
  return Promise.resolve();
668
704
  }
669
- const promise = flushInMemoryStoreValuesToDurableStore(stagingStore, durableStore, durableStoreErrorHandler, new Map(pendingStoreRedirects), additionalDurableStoreOperations);
705
+ const promise = flushInMemoryStoreValuesToDurableStore(stagingStore, durableStore, durableStoreErrorHandler, new Map(pendingStoreRedirects), additionalDurableStoreOperations, enableDurableMetadataRefresh);
670
706
  pendingStoreRedirects.clear();
671
707
  stagingStore = null;
672
708
  return promise;
@@ -16,6 +16,9 @@ export interface DurableStoreEntry<T = unknown> {
16
16
  data: T;
17
17
  metadata?: DurableStoreMetadata;
18
18
  }
19
+ export interface DurableStoreMetadataEntry {
20
+ metadata?: DurableStoreMetadata;
21
+ }
19
22
  export declare function isDeprecatedDurableStoreEntry(durableRecord: unknown): boolean;
20
23
  export interface DeprecatedDurableStoreEntry1<T = unknown> {
21
24
  data: T;
@@ -27,7 +30,10 @@ export interface DeprecatedDurableStoreEntry1<T = unknown> {
27
30
  export interface DurableStoreEntries<EntryTypes> {
28
31
  [key: string]: DurableStoreEntry<Extract<EntryTypes, unknown>>;
29
32
  }
30
- export type DurableStoreOperationType = 'setEntries' | 'evictEntries';
33
+ export interface DurableStoreMetadataEntries {
34
+ [key: string]: DurableStoreMetadataEntry;
35
+ }
36
+ export type DurableStoreOperationType = 'setEntries' | 'evictEntries' | 'setMetadata';
31
37
  export interface BaseOperation {
32
38
  type: DurableStoreOperationType;
33
39
  segment: string;
@@ -36,11 +42,15 @@ export interface DurableStoreSetOperation<T> extends BaseOperation {
36
42
  type: 'setEntries';
37
43
  entries: DurableStoreEntries<T>;
38
44
  }
45
+ export interface DurableStoreSetMetadataOperation extends BaseOperation {
46
+ type: 'setMetadata';
47
+ entries: DurableStoreMetadataEntries;
48
+ }
39
49
  export interface DurableStoreEvictOperation extends BaseOperation {
40
50
  type: 'evictEntries';
41
51
  ids: string[];
42
52
  }
43
- export type DurableStoreOperation<T> = DurableStoreSetOperation<T> | DurableStoreEvictOperation;
53
+ export type DurableStoreOperation<T> = DurableStoreSetOperation<T> | DurableStoreEvictOperation | DurableStoreSetMetadataOperation;
44
54
  export type DefaultDurableSegmentName = 'DEFAULT';
45
55
  export type RedirectDurableSegmentName = 'REDIRECT_KEYS';
46
56
  export type MessagingDurableSegmentName = 'MESSAGING';
@@ -92,6 +102,17 @@ export interface DurableStore {
92
102
  * @returns {Promise<DurableStoreEntries | undefined>}
93
103
  */
94
104
  getEntries<T>(entryIds: string[], segment: string): Promise<DurableStoreEntries<T> | undefined>;
105
+ /**
106
+ * Given alist of cache entryId's this method will asynchronously return only the DurableStoreMetadata
107
+ * of DurableStoreEntries
108
+ *
109
+ * Will return undefined if nothing is found
110
+ *
111
+ * @param {string[]} entryIds
112
+ * @param {string} segment The durable store segment to query
113
+ * @returns {Promise<DurableStoreMetadataEntries | undefined>}
114
+ */
115
+ getMetadata(entryIds: string[], segment: string): Promise<DurableStoreMetadataEntries | undefined>;
95
116
  /**
96
117
  * This method retrieves all entries in the given store segment.
97
118
  *
@@ -111,6 +132,14 @@ export interface DurableStore {
111
132
  * @returns {Promise<void>}
112
133
  */
113
134
  setEntries<T>(entries: DurableStoreEntries<T>, segment: string): Promise<void>;
135
+ /**
136
+ * This method saves only the metadata to the durable store in the given store segment
137
+ *
138
+ * @param {DurableStoreMetadataEntries} entries A key/value map Object
139
+ * @param {string} segment The durable store segment in which to set the entries
140
+ * @returns {Promise<void>}
141
+ */
142
+ setMetadata(entries: DurableStoreMetadataEntries, segment: string): Promise<void>;
114
143
  /**
115
144
  * This method sends a collection of mutation operations to the durable store.
116
145
  *
@@ -1,4 +1,4 @@
1
- export { DurableStore, DurableStoreEntries, DurableStoreEntry, DurableStoreChange, OnDurableStoreChangedListener, DefaultDurableSegment, DefaultDurableSegmentName, DurableStoreOperation, DurableStoreOperationType, DURABLE_METADATA_VERSION, } from './DurableStore';
1
+ export { DurableStore, DurableStoreEntries, DurableStoreEntry, DurableStoreMetadataEntry, DurableStoreChange, OnDurableStoreChangedListener, DefaultDurableSegment, DefaultDurableSegmentName, DurableStoreOperation, DurableStoreOperationType, DURABLE_METADATA_VERSION, DurableStoreMetadataEntries, DurableStoreMetadata, } from './DurableStore';
2
2
  export { DurableTTLOverride, DefaultDurableTTLOverride } from './DurableTTLStore';
3
3
  export { makeDurable, DurableEnvironment } from './makeDurable';
4
4
  export { publishDurableStoreEntries } from './makeDurable/revive';
@@ -1,4 +1,4 @@
1
1
  import type { InMemoryStore } from '@luvio/engine';
2
2
  import type { DurableStore, DurableStoreOperation } from '../DurableStore';
3
3
  import type { DurableStoreRejectionHandler } from './error';
4
- export declare function flushInMemoryStoreValuesToDurableStore(store: InMemoryStore, durableStore: DurableStore, durableStoreErrorHandler: DurableStoreRejectionHandler, redirects: Map<string, string>, additionalDurableStoreOperations?: DurableStoreOperation<unknown>[]): Promise<void>;
4
+ export declare function flushInMemoryStoreValuesToDurableStore(store: InMemoryStore, durableStore: DurableStore, durableStoreErrorHandler: DurableStoreRejectionHandler, redirects: Map<string, string>, additionalDurableStoreOperations?: DurableStoreOperation<unknown>[], enableDurableMetadataRefresh?: boolean): Promise<void>;
@@ -34,6 +34,7 @@ export declare const ADAPTER_CONTEXT_ID_SUFFIX = "__NAMED_CONTEXT";
34
34
  interface MakeDurableOptions {
35
35
  durableStore: DurableStore;
36
36
  instrumentation?: InstrumentationFunction;
37
+ enableDurableMetadataRefresh?: boolean;
37
38
  useRevivingStore?: boolean;
38
39
  }
39
40
  /**
@@ -45,5 +46,5 @@ interface MakeDurableOptions {
45
46
  * @param durableStore A DurableStore implementation
46
47
  * @param instrumentation An instrumentation function implementation
47
48
  */
48
- export declare function makeDurable(environment: Environment, { durableStore, instrumentation, useRevivingStore }: MakeDurableOptions): DurableEnvironment;
49
+ export declare function makeDurable(environment: Environment, { durableStore, instrumentation, useRevivingStore, enableDurableMetadataRefresh, }: MakeDurableOptions): DurableEnvironment;
49
50
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luvio/environments",
3
- "version": "0.146.0",
3
+ "version": "0.146.2",
4
4
  "description": "Luvio Environments",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,7 +27,7 @@
27
27
  "watch": "yarn build --watch"
28
28
  },
29
29
  "dependencies": {
30
- "@luvio/engine": "^0.146.0"
30
+ "@luvio/engine": "^0.146.2"
31
31
  },
32
32
  "volta": {
33
33
  "extends": "../../../package.json"
@@ -36,9 +36,9 @@
36
36
  {
37
37
  "path": "./dist/environments.js",
38
38
  "maxSize": {
39
- "none": "45.5 kB",
40
- "min": "13.0 kB",
41
- "compressed": "8.8 kB"
39
+ "none": "47 kB",
40
+ "min": "14 kB",
41
+ "compressed": "9.5 kB"
42
42
  }
43
43
  }
44
44
  ],