-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathEagerContextWatcher.java
More file actions
502 lines (466 loc) · 17.8 KB
/
Copy pathEagerContextWatcher.java
File metadata and controls
502 lines (466 loc) · 17.8 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package com.hubspot.jinjava.util;
import com.google.common.annotations.Beta;
import com.hubspot.jinjava.interpret.CannotReconstructValueException;
import com.hubspot.jinjava.interpret.Context;
import com.hubspot.jinjava.interpret.DeferredValue;
import com.hubspot.jinjava.interpret.DeferredValueException;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.interpret.LazyExpression;
import com.hubspot.jinjava.interpret.MetaContextVariables;
import com.hubspot.jinjava.interpret.OneTimeReconstructible;
import com.hubspot.jinjava.interpret.RevertibleObject;
import com.hubspot.jinjava.lib.tag.ForTag;
import com.hubspot.jinjava.lib.tag.eager.EagerExecutionResult;
import com.hubspot.jinjava.objects.collections.PyList;
import com.hubspot.jinjava.objects.collections.PyMap;
import com.hubspot.jinjava.objects.serialization.PyishObjectMapper;
import com.hubspot.jinjava.util.EagerExpressionResolver.EagerExpressionResult;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Beta
public class EagerContextWatcher {
/**
* Execute the specified functions within a protected context.
* Additionally, if the execution causes existing values on the context to become
* deferred, then their previous values will wrapped in a <code>set</code>
* tag that gets prepended to the returned result.
* The <code>function</code> is run in deferredExecutionMode=true, where the context needs to
* be protected from having values updated or set,
* such as when evaluating both the positive and negative nodes in an if statement.
* @param function Function to run within a "protected" child context
* @param interpreter JinjavaInterpreter to create a child from.
* @param eagerChildContextConfig Configuration for evaluation as defined in {@link EagerChildContextConfig}
* @return An <code>EagerExecutionResult</code> where:
* <code>result</code> is the string result of <code>function</code>.
* <code>prefixToPreserveState</code> is either blank or a <code>set</code> tag
* that preserves the state within the output for a second rendering pass.
*/
public static EagerExecutionResult executeInChildContext(
Function<JinjavaInterpreter, EagerExpressionResult> function,
JinjavaInterpreter interpreter,
EagerChildContextConfig eagerChildContextConfig
) {
final EagerExecutionResult initialResult;
final Map<String, Object> speculativeBindings;
if (eagerChildContextConfig.checkForContextChanges) {
final Set<Entry<String, Object>> entrySet = interpreter.getContext().entrySet();
final Map<String, Object> initiallyResolvedHashes = getInitiallyResolvedHashes(
entrySet,
interpreter.getContext()
);
final Map<String, String> initiallyResolvedAsStrings =
getInitiallyResolvedAsStrings(interpreter, entrySet, initiallyResolvedHashes);
initialResult = applyFunction(function, interpreter, eagerChildContextConfig);
speculativeBindings =
getAllSpeculativeBindings(
interpreter,
eagerChildContextConfig,
initiallyResolvedHashes,
initiallyResolvedAsStrings,
initialResult
);
} else {
Set<String> ignoredKeys = getAdditionalKeysToIgnore(
interpreter,
eagerChildContextConfig
);
initialResult = applyFunction(function, interpreter, eagerChildContextConfig);
speculativeBindings =
getBasicSpeculativeBindings(
interpreter,
eagerChildContextConfig,
ignoredKeys,
initialResult
);
}
return new EagerExecutionResult(initialResult.getResult(), speculativeBindings);
}
private static EagerExecutionResult applyFunction(
Function<JinjavaInterpreter, EagerExpressionResult> function,
JinjavaInterpreter interpreter,
EagerChildContextConfig eagerChildContextConfig
) {
// Don't create new call stacks to prevent hitting max recursion with this silent new scope
try (InterpreterScopeClosable c = interpreter.enterNonStackingScope()) {
if (eagerChildContextConfig.forceDeferredExecutionMode) {
interpreter.getContext().setDeferredExecutionMode(true);
}
interpreter
.getContext()
.setPartialMacroEvaluation(eagerChildContextConfig.partialMacroEvaluation);
return new EagerExecutionResult(
function.apply(interpreter),
eagerChildContextConfig.discardSessionBindings
? new HashMap<>()
: interpreter.getContext().getSessionBindings()
);
}
}
private static Map<String, String> getInitiallyResolvedAsStrings(
JinjavaInterpreter interpreter,
Set<Entry<String, Object>> entrySet,
Map<String, Object> initiallyResolvedHashes
) {
Map<String, String> initiallyResolvedAsStrings = new HashMap<>();
// This creates a stringified snapshot of the context
// so it can be disabled via the config because it may cause performance issues.
Stream<Entry<String, Object>> entryStream =
(interpreter.getConfig().getExecutionMode().useEagerContextReverting()
? entrySet
: interpreter.getContext().getCombinedScope().entrySet()).stream()
.filter(entry -> initiallyResolvedHashes.containsKey(entry.getKey()))
.filter(entry -> isResolvableForContextReverting(entry.getValue()) // TODO make this configurable
);
entryStream.forEach(entry ->
cacheRevertibleObject(
interpreter,
initiallyResolvedHashes,
initiallyResolvedAsStrings,
entry
)
);
return initiallyResolvedAsStrings;
}
private static Map<String, Object> getInitiallyResolvedHashes(
Set<Entry<String, Object>> entrySet,
Context context
) {
Map<String, Object> mapOfHashes = new HashMap<>();
entrySet
.stream()
.filter(entry ->
!MetaContextVariables.isMetaContextVariable(entry.getKey(), context)
)
.filter(entry ->
!(entry.getValue() instanceof DeferredValue) && entry.getValue() != null
)
.forEach(entry ->
mapOfHashes.put(entry.getKey(), getObjectOrHashCode(entry.getValue()))
); // Avoid NPE when getObjectOrHashCode(entry.getValue()) is null)
return mapOfHashes;
}
private static Set<String> getAdditionalKeysToIgnore(
JinjavaInterpreter interpreter,
EagerChildContextConfig eagerChildContextConfig
) {
// We don't need to reconstruct already deferred keys.
// This ternary expression is an optimization to call entrySet fewer times
return (
interpreter.getContext().isDeferredExecutionMode() &&
!eagerChildContextConfig.takeNewValue
)
? interpreter
.getContext()
.entrySet()
.stream()
.filter(entry -> entry.getValue() instanceof DeferredValue)
.map(Entry::getKey)
.collect(Collectors.toSet())
: Collections.emptySet();
}
private static Map<String, Object> getBasicSpeculativeBindings(
JinjavaInterpreter interpreter,
EagerChildContextConfig eagerChildContextConfig,
Set<String> ignoredKeys,
EagerExecutionResult eagerExecutionResult
) {
if (!eagerChildContextConfig.takeNewValue) {
eagerExecutionResult
.getSpeculativeBindings()
.putAll(
interpreter
.getContext()
.getScope()
.entrySet()
.stream()
.filter(entry ->
entry.getValue() instanceof OneTimeReconstructible &&
!(((OneTimeReconstructible) entry.getValue()).isReconstructed())
)
.peek(entry ->
((OneTimeReconstructible) entry.getValue()).setReconstructed(true)
)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue))
);
}
return eagerExecutionResult
.getSpeculativeBindings()
.entrySet()
.stream()
.filter(entry ->
!MetaContextVariables.isMetaContextVariable(
entry.getKey(),
interpreter.getContext()
)
)
.filter(entry -> !ignoredKeys.contains(entry.getKey()))
.filter(entry -> !ForTag.LOOP.equals(entry.getKey()))
.map(entry -> {
if (
eagerExecutionResult.getResult().isFullyResolved() ||
eagerChildContextConfig.takeNewValue
) {
return entry;
}
Object contextValue = interpreter.getContext().get(entry.getKey());
if (
contextValue instanceof DeferredValue &&
((DeferredValue) contextValue).getOriginalValue() != null
) {
if (
!eagerChildContextConfig.takeNewValue &&
!EagerExpressionResolver.isResolvableObject(
((DeferredValue) contextValue).getOriginalValue()
)
) {
throw new CannotReconstructValueException(entry.getKey());
}
return new AbstractMap.SimpleImmutableEntry<>(entry.getKey(), contextValue);
}
return null;
})
.filter(Objects::nonNull)
.filter(entry -> entry.getValue() != null)
.filter(entry -> !isDeferredWithOriginalValueNull(entry.getValue()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}
private static Map<String, Object> getAllSpeculativeBindings(
JinjavaInterpreter interpreter,
EagerChildContextConfig eagerChildContextConfig,
Map<String, Object> initiallyResolvedHashes,
Map<String, String> initiallyResolvedAsStrings,
EagerExecutionResult eagerExecutionResult
) {
Map<String, Object> speculativeBindings = eagerExecutionResult
.getSpeculativeBindings()
.entrySet()
.stream()
.filter(entry ->
entry.getValue() != null &&
!entry.getValue().equals(interpreter.getContext().get(entry.getKey()))
)
.filter(entry ->
!(interpreter.getContext().get(entry.getKey()) instanceof DeferredValue)
)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
speculativeBindings.putAll(
initiallyResolvedHashes
.keySet()
.stream()
.map(key ->
new AbstractMap.SimpleImmutableEntry<>(key, interpreter.getContext().get(key))
)
.filter(entry ->
!Objects.equals(
initiallyResolvedHashes.get(entry.getKey()),
getObjectOrHashCode(entry.getValue())
)
)
.collect(
Collectors.toMap(
Entry::getKey,
entry ->
getOriginalValue(
interpreter,
eagerChildContextConfig,
initiallyResolvedHashes,
initiallyResolvedAsStrings,
entry,
eagerExecutionResult.getResult().isFullyResolved()
)
)
)
);
speculativeBindings =
speculativeBindings
.entrySet()
.stream()
.filter(entry ->
!MetaContextVariables.isMetaContextVariable(
entry.getKey(),
interpreter.getContext()
)
)
.filter(entry -> entry.getValue() != null)
.filter(entry -> !isDeferredWithOriginalValueNull(entry.getValue()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
return speculativeBindings;
}
/**
* This is an optimization used to filter so that we don't reconstruct unnecessary tags like {@code {% set num = null %}}
* because {@code num} is already null when it hasn't been set to anything.
*/
private static boolean isDeferredWithOriginalValueNull(Object value) {
return (
value instanceof DeferredValue && ((DeferredValue) value).getOriginalValue() == null
);
}
private static void cacheRevertibleObject(
JinjavaInterpreter interpreter,
Map<String, Object> initiallyResolvedHashes,
Map<String, String> initiallyResolvedAsStrings,
Entry<String, Object> entry
) {
RevertibleObject revertibleObject = interpreter
.getRevertibleObjects()
.get(entry.getKey());
Object hashCode = initiallyResolvedHashes.get(entry.getKey());
try {
if (revertibleObject == null || !hashCode.equals(revertibleObject.getHashCode())) {
revertibleObject =
new RevertibleObject(
hashCode,
PyishObjectMapper.getAsPyishStringOrThrow(entry.getValue())
);
interpreter.getRevertibleObjects().put(entry.getKey(), revertibleObject);
}
revertibleObject
.getPyishString()
.ifPresent(pyishString ->
initiallyResolvedAsStrings.put(entry.getKey(), pyishString)
);
} catch (Exception e) {
interpreter
.getRevertibleObjects()
.put(entry.getKey(), new RevertibleObject(hashCode));
}
}
private static Object getOriginalValue(
JinjavaInterpreter interpreter,
EagerChildContextConfig eagerChildContextConfig,
Map<String, Object> initiallyResolvedHashes,
Map<String, String> initiallyResolvedAsStrings,
Entry<String, Object> e,
boolean isFullyResolved
) {
if (eagerChildContextConfig.takeNewValue || isFullyResolved) {
return e.getValue();
}
if (
e.getValue() instanceof DeferredValue &&
initiallyResolvedHashes
.get(e.getKey())
.equals(getObjectOrHashCode(((DeferredValue) e.getValue()).getOriginalValue()))
) {
return e.getValue();
}
// This is necessary if a state-changing function, such as .update()
// or .append() is run against a variable in the context.
// It will revert the effects when takeNewValue is false.
if (initiallyResolvedAsStrings.containsKey(e.getKey())) {
// convert to new list or map
try {
return interpreter.resolveELExpression(
initiallyResolvedAsStrings.get(e.getKey()),
interpreter.getLineNumber()
);
} catch (DeferredValueException ignored) {}
}
// Previous value could not be mapped to a string
throw new CannotReconstructValueException(e.getKey());
}
private static Object getObjectOrHashCode(Object o) {
if (o instanceof LazyExpression) {
o = ((LazyExpression) o).get();
}
if (o instanceof PyList && isResolvableForContextReverting(o)) {
return o.hashCode();
}
if (o instanceof PyMap && isResolvableForContextReverting(o)) {
return o.hashCode() + ((PyMap) o).keySet().hashCode();
}
return o;
}
private static boolean isResolvableForContextReverting(Object o) {
return EagerExpressionResolver.isResolvableObject(o, 4, 400);
}
public static class EagerChildContextConfig {
private final boolean takeNewValue;
private final boolean discardSessionBindings;
private final boolean partialMacroEvaluation;
private final boolean checkForContextChanges;
private final boolean forceDeferredExecutionMode;
private EagerChildContextConfig(
boolean takeNewValue,
boolean discardSessionBindings,
boolean partialMacroEvaluation,
boolean checkForContextChanges,
boolean forceDeferredExecutionMode
) {
this.takeNewValue = takeNewValue;
this.discardSessionBindings = discardSessionBindings;
this.partialMacroEvaluation = partialMacroEvaluation;
this.checkForContextChanges = checkForContextChanges;
this.forceDeferredExecutionMode = forceDeferredExecutionMode;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private boolean takeNewValue;
private boolean discardSessionBindings;
private boolean partialMacroEvaluation;
private boolean checkForContextChanges;
private boolean forceDeferredExecutionMode;
private Builder() {}
/**
* @param takeNewValue If a value is updated (not replaced) either take the new value or
* take the previous value and put it into the
* <code>EagerExecutionResult.prefixToPreserveState</code>.
*/
public Builder withTakeNewValue(boolean takeNewValue) {
this.takeNewValue = takeNewValue;
return this;
}
/**
* @param discardSessionBindings Discard the session bindings from the child context
* created while executing the provided function.
*/
public Builder withDiscardSessionBindings(boolean discardSessionBindings) {
this.discardSessionBindings = discardSessionBindings;
return this;
}
/**
* @param partialMacroEvaluation Allow macro functions to be partially evaluated rather than
* needing an explicit result during this render.
*/
public Builder withPartialMacroEvaluation(boolean partialMacroEvaluation) {
this.partialMacroEvaluation = partialMacroEvaluation;
return this;
}
/**
* @param checkForContextChanges Hash and serialize values on the context to determine if changes
* have been made to any values on the context.
*/
public Builder withCheckForContextChanges(boolean checkForContextChanges) {
this.checkForContextChanges = checkForContextChanges;
return this;
}
/**
* @param forceDeferredExecutionMode Start the evaluation of the specified function in deferred execution mode.
*/
public Builder withForceDeferredExecutionMode(boolean forceDeferredExecutionMode) {
this.forceDeferredExecutionMode = forceDeferredExecutionMode;
return this;
}
public EagerChildContextConfig build() {
return new EagerChildContextConfig(
takeNewValue,
discardSessionBindings,
partialMacroEvaluation,
checkForContextChanges,
forceDeferredExecutionMode
);
}
}
}
}