-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathoperations.ts
More file actions
236 lines (204 loc) · 7.77 KB
/
operations.ts
File metadata and controls
236 lines (204 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { JSONHeroPath } from "@jsonhero/path";
import { RunMetadataChangeOperation } from "../schemas/common.js";
import { dequal } from "dequal";
export type ApplyOperationResult = {
newMetadata: Record<string, unknown>;
unappliedOperations: RunMetadataChangeOperation[];
};
export function applyMetadataOperations(
currentMetadata: Record<string, unknown>,
operations: RunMetadataChangeOperation | RunMetadataChangeOperation[]
): ApplyOperationResult {
const unappliedOperations: RunMetadataChangeOperation[] = [];
// Start with a mutable copy of the current metadata
let newMetadata: Record<string, unknown> = structuredClone(currentMetadata);
for (const operation of Array.isArray(operations) ? operations : [operations]) {
switch (operation.type) {
case "set": {
if (operation.key.startsWith("$.")) {
const path = new JSONHeroPath(operation.key);
path.set(newMetadata, operation.value);
} else {
// Set the value directly
newMetadata[operation.key] = operation.value;
}
break;
}
case "delete": {
// Safely delete the key if it exists
if (operation.key in newMetadata) {
delete newMetadata[operation.key];
}
break;
}
case "append": {
if (operation.key.startsWith("$.")) {
const path = new JSONHeroPath(operation.key);
const currentValue = path.first(newMetadata);
if (currentValue === undefined) {
// Initialize as array with single item
path.set(newMetadata, [operation.value]);
} else if (Array.isArray(currentValue)) {
// Append to existing array
path.set(newMetadata, [...currentValue, operation.value]);
} else {
// Convert to array if not already
path.set(newMetadata, [currentValue, operation.value]);
}
} else {
// Ensure the value at key is an array or initialize as an array
const existingValue = newMetadata[operation.key];
if (Array.isArray(existingValue)) {
existingValue.push(operation.value);
} else if (existingValue === undefined) {
newMetadata[operation.key] = [operation.value];
} else {
// Convert to array if not already
newMetadata[operation.key] = [existingValue, operation.value];
}
}
break;
}
case "remove": {
if (operation.key.startsWith("$.")) {
const path = new JSONHeroPath(operation.key);
const currentValue = path.first(newMetadata);
if (Array.isArray(currentValue)) {
// Remove the value from array using deep equality check
const newArray = currentValue.filter((item) => !dequal(item, operation.value));
path.set(newMetadata, newArray);
} else {
unappliedOperations.push(operation);
}
} else {
// Remove matching values if the key points to an array
const existingValue = newMetadata[operation.key];
if (Array.isArray(existingValue)) {
newMetadata[operation.key] = existingValue.filter(
(item) => !dequal(item, operation.value)
);
} else {
unappliedOperations.push(operation);
}
}
break;
}
case "increment": {
let currentValue = operation.key.startsWith("$.")
? new JSONHeroPath(operation.key).first(newMetadata)
: newMetadata[operation.key];
const newValue = (typeof currentValue === "number" ? currentValue : 0) + operation.value;
if (operation.key.startsWith("$.")) {
new JSONHeroPath(operation.key).set(newMetadata, newValue);
} else {
newMetadata[operation.key] = newValue;
}
break;
}
case "update": {
// Update the metadata object with the new object
newMetadata = operation.value;
break;
}
default: {
// Log unsupported operation type
unappliedOperations.push(operation);
break;
}
}
}
return { newMetadata, unappliedOperations };
}
/**
* Collapses metadata operations to reduce payload size and avoid 413 "Request Entity Too Large" errors.
*
* When there are many operations queued up (e.g., 10k increment operations), sending them all
* individually can result in request payloads exceeding the server's 1MB limit. This function
* intelligently combines operations where possible to reduce the payload size:
*
* - **Increment operations**: Multiple increments on the same key are summed into a single increment
* - Example: increment("counter", 1) + increment("counter", 2) → increment("counter", 3)
*
* - **Set operations**: Multiple sets on the same key keep only the last one (since later sets override earlier ones)
* - Example: set("status", "processing") + set("status", "done") → set("status", "done")
*
* - **Delete operations**: Multiple deletes on the same key keep only one (duplicates are redundant)
* - Example: del("temp") + del("temp") → del("temp")
*
* - **Append, remove, and update operations**: Preserved as-is to maintain correctness since order matters
*
* @param operations Array of metadata change operations to collapse
* @returns Collapsed array with fewer operations that produce the same final result
*
* @example
* ```typescript
* const operations = [
* { type: "increment", key: "counter", value: 1 },
* { type: "increment", key: "counter", value: 2 },
* { type: "set", key: "status", value: "processing" },
* { type: "set", key: "status", value: "done" }
* ];
*
* const collapsed = collapseOperations(operations);
* // Result: [
* // { type: "increment", key: "counter", value: 3 },
* // { type: "set", key: "status", value: "done" }
* // ]
* ```
*/
export function collapseOperations(
operations: RunMetadataChangeOperation[]
): RunMetadataChangeOperation[] {
if (operations.length === 0) {
return operations;
}
// Maps to track collapsible operations
const incrementsByKey = new Map<string, number>();
const setsByKey = new Map<string, RunMetadataChangeOperation>();
const deletesByKey = new Set<string>();
const preservedOperations: RunMetadataChangeOperation[] = [];
// Process operations in order
for (const operation of operations) {
switch (operation.type) {
case "increment":
const currentIncrement = incrementsByKey.get(operation.key) || 0;
incrementsByKey.set(operation.key, currentIncrement + operation.value);
break;
case "set":
// Keep only the last set operation for each key
setsByKey.set(operation.key, operation);
break;
case "delete":
// Keep only one delete operation per key
deletesByKey.add(operation.key);
break;
case "append":
case "remove":
case "update":
// Preserve these operations as-is to maintain correctness
preservedOperations.push(operation);
break;
default:
// Handle any future operation types by preserving them
preservedOperations.push(operation);
break;
}
}
// Build the collapsed operations array
const collapsedOperations: RunMetadataChangeOperation[] = [];
// Add collapsed increment operations
for (const [key, value] of incrementsByKey) {
collapsedOperations.push({ type: "increment", key, value });
}
// Add collapsed set operations
for (const operation of setsByKey.values()) {
collapsedOperations.push(operation);
}
// Add collapsed delete operations
for (const key of deletesByKey) {
collapsedOperations.push({ type: "delete", key });
}
// Add preserved operations
collapsedOperations.push(...preservedOperations);
return collapsedOperations;
}