Skip to content

Commit 9113b4f

Browse files
authored
Deprecate validateAndCollect in favor of explicitly calling loadCollectors (#1063)
1 parent d92caed commit 9113b4f

File tree

6 files changed

+463
-52
lines changed

6 files changed

+463
-52
lines changed

doc/collector-context.md

+56-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### CollectorContext
22

3-
There could be use cases where we want collect the information while we are validating the data. A simple example could be fetching some value from a database or from a microservice based on the data (which could be a text or a JSON object. It should be noted that this should be a simple operation or validation might take more time to complete.) in a given JSON node and the schema keyword we are using.
3+
There could be use cases where we want collect the information while we are validating the data. A simple example could be fetching some value from a database or from a microservice based on the data (which could be a text or a JSON object. It should be noted that this should be a simple operation or validation might take more time to complete.) in a given JSON node and the schema keyword we are using.
44

55
The fetched data can be stored somewhere so that it can be used later after the validation is done. Since the current validation logic already parses the data and schema, both validation and collecting the required information can be done in one go.
66

@@ -10,6 +10,12 @@ The `CollectorContext` and `Collector` classes are designed to work with this us
1010

1111
The `CollectorContext` is stored as a variable on the `ExecutionContext` that is used during the validation. This allows users to add objects to context at many points in the framework like Formats and Validators where the `ExecutionContext` is available as a parameter.
1212

13+
By default the `CollectorContext` created by the library contains maps backed by `HashMap`. If the `CollectorContext` needs to be shared by multiple threads then a `ConcurrentHashMap` needs to be used.
14+
15+
```java
16+
CollectorContext collectorContext = new CollectorContext(new ConcurrentHashMap<>(), new ConcurrentHashMap<>());
17+
```
18+
1319
Collectors are added to `CollectorContext`. Collectors allow to collect the objects. A `Collector` is added to `CollectorContext` with a name and corresponding `Collector` instance.
1420

1521
```java
@@ -28,29 +34,44 @@ However there might be use cases where we want to add a simple Object like Strin
2834

2935
```java
3036
CollectorContext collectorContext = executionContext.getCollectorContext();
31-
collectorContext.add(SAMPLE_COLLECTOR, "sample-string")
37+
collectorContext.add(SAMPLE_COLLECTOR, "sample-string");
3238
```
3339

34-
To use the `CollectorContext` while validating, the `validateAndCollect` method has to be invoked on the `JsonSchema` class.
35-
This method returns a `ValidationResult` that contains the errors encountered during validation and a `ExecutionContext` instance that contains the `CollectorContext`.
36-
Objects constructed by collectors or directly added to `CollectorContext` can be retrieved from `CollectorContext` by using the name they were added with.
37-
38-
To collect across multiple validation runs, the `CollectorContext` needs to be explicitly reused by passing the `ExecutionContext` as a parameter to the validation.
40+
Implementations that need to modify values the `CollectorContext` should do so in a thread-safe manner.
3941

4042
```java
41-
ValidationResult validationResult = jsonSchema.validateAndCollect(jsonNode);
42-
ExecutionContext executionContext = validationResult.getExecutionContext();
4343
CollectorContext collectorContext = executionContext.getCollectorContext();
44-
List<String> contextValue = (List<String>) collectorContext.get(SAMPLE_COLLECTOR);
44+
AtomicInteger count = (AtomicInteger) collectorContext.getCollectorMap().computeIfAbsent(SAMPLE_COLLECTOR,
45+
(key) -> new AtomicInteger(0));
46+
count.incrementAndGet();
47+
```
48+
49+
To use the `CollectorContext` while validating, the `CollectorContext` should be instantiated outside and set for every validation execution.
4550

46-
// Do something with contextValue
47-
...
51+
At the end of all the runs the `CollectorContext.loadCollectors()` method can be called if needed for the `Collector` implementations to aggregate values.
4852

49-
// To collect more information for subsequent runs reuse the context
50-
validationResult = jsonSchema.validateAndCollect(executionContext, jsonNode);
53+
```java
54+
// This creates a CollectorContext that can be used by multiple threads although this is not neccessary in this example
55+
CollectorContext collectorContext = new CollectorContext(new ConcurrentHashMap<>(), new ConcurrentHashMap<>());
56+
// This adds a custom collect keyword that sets values in the CollectorContext whenever it gets processed
57+
JsonMetaSchema metaSchema = JsonMetaSchema.builder(JsonMetaSchema.getV202012()).keyword(new CollectKeyword()).build();
58+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(VersionFlag.V202012, builder -> builder.metaSchema(metaSchema));
59+
JsonSchema schema = factory.getSchema("{\n"
60+
+ " \"collect\": true\n"
61+
+ "}");
62+
for (int i = 0; i < 50; i++) {
63+
// The shared CollectorContext is set on the ExecutionContext for every run to aggregate data from all the runs
64+
schema.validate("1", InputFormat.JSON, executionContext -> {
65+
executionContext.setCollectorContext(collectorContext);
66+
});
67+
}
68+
// This is called for Collector implementations to aggregate data
69+
collectorContext.loadCollectors();
70+
AtomicInteger result = (AtomicInteger) collectorContext.get("collect");
71+
assertEquals(50, result.get());
5172
```
5273

53-
There might be use cases where a collector needs to collect the data at multiple touch points. For example one use case might be collecting data in a validator and a formatter. If you are using a `Collector` rather than a `Object`, the combine method of the `Collector` allows to define how we want to combine the data into existing `Collector`. `CollectorContext` `combineWithCollector` method calls the combine method on the `Collector`. User just needs to call the `CollectorContext` `combineWithCollector` method every time some data needs to merged into existing `Collector`. The `collect` method on the `Collector` is called by the framework at the end of validation to return the data that was collected.
74+
There might be use cases where a collector needs to collect the data at multiple touch points. For example one use case might be collecting data in a validator and a formatter. If you are using a `Collector` rather than a `Object`, the combine method of the `Collector` allows to define how we want to combine the data into existing `Collector`. `CollectorContext` `combineWithCollector` method calls the combine method on the `Collector`. User just needs to call the `CollectorContext` `combineWithCollector` method every time some data needs to merged into existing `Collector`. The `collect` method on the `Collector` is called by explicitly calling `CollectorContext.loadCollectors()` at the end of processing.
5475

5576
```java
5677
class CustomCollector implements Collector<List<String>> {
@@ -70,16 +91,25 @@ class CustomCollector implements Collector<List<String>> {
7091

7192
@Override
7293
public void combine(Object object) {
73-
returnList.add(referenceMap.get((String) object));
94+
synchronized(returnList) {
95+
returnList.add(referenceMap.get((String) object));
96+
}
7497
}
7598
}
99+
```
76100

77-
CollectorContext collectorContext = executionContext.getCollectorContext();
78-
if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
79-
collectorContext.add(SAMPLE_COLLECTOR, new CustomCollector());
101+
```java
102+
private class CustomValidator extends AbstractJsonValidator {
103+
@Override
104+
public Set<ValidationMessage> validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
105+
JsonNodePath instanceLocation) {
106+
CollectorContext collectorContext = executionContext.getCollectorContext();
107+
CustomCollector customCollector = (CustomCollector) collectorContext.getCollectorMap().computeIfAbsent(SAMPLE_COLLECTOR,
108+
key -> new CustomCollector());
109+
customCollector.combine(node.textValue());
110+
return Collections.emptySet();
111+
}
80112
}
81-
collectorContext.combineWithCollector(SAMPLE_COLLECTOR, node.textValue());
82-
83113
```
84114

85115
One important thing to note when using Collectors is if we call get method on `CollectorContext` before the validation is complete, we would get back a `Collector` instance that was added to `CollectorContext`.
@@ -96,12 +126,9 @@ List<String> data = collectorContext.get(SAMPLE_COLLECTOR);
96126
If you are using simple objects and if the data needs to be collected from multiple touch points, logic is straightforward as shown.
97127

98128
```java
99-
CollectorContext collectorContext = executionContext.getCollectorContext();
100-
// If collector name is not added to context add one.
101-
if (collectorContext.get(SAMPLE_COLLECTOR) == null) {
102-
collectorContext.add(SAMPLE_COLLECTOR, new ArrayList<String>());
129+
List<String> returnList = (List<String>) collectorContext.getCollectorMap()
130+
.computeIfAbsent(SAMPLE_COLLECTOR, key -> new ArrayList<String>());
131+
synchronized(returnList) {
132+
returnList.add(node.textValue());
103133
}
104-
// In this case we are adding a list to CollectorContext.
105-
List<String> returnList = (List<String>) collectorContext.get(SAMPLE_COLLECTOR);
106-
107-
```
134+
```

src/main/java/com/networknt/schema/CollectorContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void combineWithCollector(String name, Object data) {
140140
/**
141141
* Loads data from all collectors.
142142
*/
143-
void loadCollectors() {
143+
public void loadCollectors() {
144144
Set<Entry<String, Object>> entrySet = this.collectorMap.entrySet();
145145
for (Entry<String, Object> entry : entrySet) {
146146
if (entry.getValue() instanceof Collector<?>) {

0 commit comments

Comments
 (0)