envio 2.32.2 → 3.0.0-alpha-main-clickhouse-sink

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.
@@ -1,40 +1,31 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
  'use strict';
3
3
 
4
+ var Utils = require("./Utils.res.js");
4
5
  var Js_exn = require("rescript/lib/js/js_exn.js");
5
6
  var Logging = require("./Logging.res.js");
7
+ var Belt_Array = require("rescript/lib/js/belt_Array.js");
6
8
  var Prometheus = require("./Prometheus.res.js");
7
9
  var EntityHistory = require("./db/EntityHistory.res.js");
8
10
  var ErrorHandling = require("./ErrorHandling.res.js");
11
+ var InMemoryStore = require("./InMemoryStore.res.js");
12
+ var InMemoryTable = require("./InMemoryTable.res.js");
9
13
  var InternalTable = require("./db/InternalTable.res.js");
10
14
  var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js");
15
+ var S$RescriptSchema = require("rescript-schema/src/S.res.js");
11
16
  var Caml_js_exceptions = require("rescript/lib/js/caml_js_exceptions.js");
12
17
 
13
18
  var StorageError = /* @__PURE__ */Caml_exceptions.create("Persistence.StorageError");
14
19
 
15
- var entityHistoryActionEnumConfig_name = EntityHistory.RowAction.name;
16
-
17
- var entityHistoryActionEnumConfig_variants = EntityHistory.RowAction.variants;
18
-
19
- var entityHistoryActionEnumConfig_schema = EntityHistory.RowAction.schema;
20
-
21
- var entityHistoryActionEnumConfig = {
22
- name: entityHistoryActionEnumConfig_name,
23
- variants: entityHistoryActionEnumConfig_variants,
24
- schema: entityHistoryActionEnumConfig_schema,
25
- default: "SET"
26
- };
27
-
28
- function make(userEntities, allEnums, storage, sql) {
20
+ function make(userEntities, allEnums, storage) {
29
21
  var allEntities = userEntities.concat([InternalTable.DynamicContractRegistry.config]);
30
- var allEnums$1 = allEnums.concat([entityHistoryActionEnumConfig]);
22
+ var allEnums$1 = allEnums.concat([EntityHistory.RowAction.config]);
31
23
  return {
32
24
  userEntities: userEntities,
33
25
  allEntities: allEntities,
34
26
  allEnums: allEnums$1,
35
27
  storageStatus: "Unknown",
36
- storage: storage,
37
- sql: sql
28
+ storage: storage
38
29
  };
39
30
  }
40
31
 
@@ -120,7 +111,7 @@ function getInitializedState(persistence) {
120
111
  }
121
112
  }
122
113
 
123
- async function setEffectCacheOrThrow(persistence, effect, items, invalidationsCount) {
114
+ function writeBatch(persistence, batch, config, inMemoryStore, isInReorgThreshold) {
124
115
  var match = persistence.storageStatus;
125
116
  if (typeof match !== "object") {
126
117
  return Js_exn.raiseError("Failed to access the indexer storage. The Persistence layer is not initialized.");
@@ -129,31 +120,93 @@ async function setEffectCacheOrThrow(persistence, effect, items, invalidationsCo
129
120
  return Js_exn.raiseError("Failed to access the indexer storage. The Persistence layer is not initialized.");
130
121
  }
131
122
  var cache = match._0.cache;
132
- var storage = persistence.storage;
133
- var effectName = effect.name;
134
- var c = cache[effectName];
135
- var effectCacheRecord;
136
- if (c !== undefined) {
137
- effectCacheRecord = c;
138
- } else {
139
- var c$1 = {
140
- effectName: effectName,
141
- count: 0
142
- };
143
- cache[effectName] = c$1;
144
- effectCacheRecord = c$1;
145
- }
146
- var initialize = effectCacheRecord.count === 0;
147
- await storage.setEffectCacheOrThrow(effect, items, initialize);
148
- effectCacheRecord.count = (effectCacheRecord.count + items.length | 0) - invalidationsCount | 0;
149
- return Prometheus.EffectCacheCount.set(effectCacheRecord.count, effectName);
123
+ var updatedEntities = Belt_Array.keepMapU(persistence.allEntities, (function (entityConfig) {
124
+ var updates = InMemoryTable.Entity.updates(InMemoryStore.getInMemTable(inMemoryStore, entityConfig));
125
+ if (Utils.$$Array.isEmpty(updates)) {
126
+ return ;
127
+ } else {
128
+ return {
129
+ entityConfig: entityConfig,
130
+ updates: updates
131
+ };
132
+ }
133
+ }));
134
+ return persistence.storage.writeBatch(batch, InMemoryTable.values(inMemoryStore.rawEvents), inMemoryStore.rollbackTargetCheckpointId, isInReorgThreshold, config, persistence.allEntities, Belt_Array.keepMapU(Object.keys(inMemoryStore.effects), (function (effectName) {
135
+ var inMemTable = inMemoryStore.effects[effectName];
136
+ var idsToStore = inMemTable.idsToStore;
137
+ var invalidationsCount = inMemTable.invalidationsCount;
138
+ var effect = inMemTable.effect;
139
+ var dict = inMemTable.dict;
140
+ if (idsToStore.length === 0) {
141
+ return ;
142
+ }
143
+ var items = new Array(idsToStore.length);
144
+ Belt_Array.forEachWithIndex(idsToStore, (function (index, id) {
145
+ items[index] = {
146
+ id: id,
147
+ output: dict[id]
148
+ };
149
+ }));
150
+ var effectName$1 = effect.name;
151
+ var c = cache[effectName$1];
152
+ var effectCacheRecord;
153
+ if (c !== undefined) {
154
+ effectCacheRecord = c;
155
+ } else {
156
+ var c$1 = {
157
+ effectName: effectName$1,
158
+ count: 0
159
+ };
160
+ cache[effectName$1] = c$1;
161
+ effectCacheRecord = c$1;
162
+ }
163
+ var shouldInitialize = effectCacheRecord.count === 0;
164
+ return effectCacheRecord.count = (effectCacheRecord.count + items.length | 0) - invalidationsCount | 0, Prometheus.EffectCacheCount.set(effectCacheRecord.count, effectName$1), {
165
+ effect: effect,
166
+ items: items,
167
+ shouldInitialize: shouldInitialize
168
+ };
169
+ })), updatedEntities);
170
+ }
171
+
172
+ async function prepareRollbackDiff(persistence, rollbackTargetCheckpointId, rollbackDiffCheckpointId) {
173
+ var inMemStore = InMemoryStore.make(persistence.allEntities, rollbackTargetCheckpointId);
174
+ var deletedEntities = {};
175
+ var setEntities = {};
176
+ await Promise.all(Belt_Array.map(persistence.allEntities, (async function (entityConfig) {
177
+ var entityTable = InMemoryStore.getInMemTable(inMemStore, entityConfig);
178
+ var match = await persistence.storage.getRollbackData(entityConfig, rollbackTargetCheckpointId);
179
+ match[0].forEach(function (data) {
180
+ Utils.Dict.push(deletedEntities, entityConfig.name, data.id);
181
+ InMemoryTable.Entity.set(entityTable, {
182
+ type: "DELETE",
183
+ entityId: data.id,
184
+ checkpointId: rollbackDiffCheckpointId
185
+ }, false, true);
186
+ });
187
+ var restoredEntities = S$RescriptSchema.parseOrThrow(match[1], entityConfig.rowsSchema);
188
+ return Belt_Array.forEach(restoredEntities, (function (entity) {
189
+ Utils.Dict.push(setEntities, entityConfig.name, entity.id);
190
+ InMemoryTable.Entity.set(entityTable, {
191
+ type: "SET",
192
+ entityId: entity.id,
193
+ entity: entity,
194
+ checkpointId: rollbackDiffCheckpointId
195
+ }, false, true);
196
+ }));
197
+ })));
198
+ return {
199
+ inMemStore: inMemStore,
200
+ deletedEntities: deletedEntities,
201
+ setEntities: setEntities
202
+ };
150
203
  }
151
204
 
152
205
  exports.StorageError = StorageError;
153
- exports.entityHistoryActionEnumConfig = entityHistoryActionEnumConfig;
154
206
  exports.make = make;
155
207
  exports.init = init;
156
208
  exports.getInitializedStorageOrThrow = getInitializedStorageOrThrow;
157
209
  exports.getInitializedState = getInitializedState;
158
- exports.setEffectCacheOrThrow = setEffectCacheOrThrow;
159
- /* Logging Not a pure module */
210
+ exports.writeBatch = writeBatch;
211
+ exports.prepareRollbackDiff = prepareRollbackDiff;
212
+ /* Utils Not a pure module */