-
Notifications
You must be signed in to change notification settings - Fork 215
/
SudoStreamSnapshots.java
executable file
·302 lines (257 loc) · 11 KB
/
SudoStreamSnapshots.java
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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.models.streaming;
import static org.eclipse.ditto.base.model.json.FieldType.REGULAR;
import static org.eclipse.ditto.base.model.json.JsonSchemaVersion.V_2;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.entity.id.EntityId;
import org.eclipse.ditto.base.model.entity.id.EntityIdJsonDeserializer;
import org.eclipse.ditto.base.model.entity.type.EntityType;
import org.eclipse.ditto.base.model.entity.type.EntityTypeJsonDeserializer;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableCommand;
import org.eclipse.ditto.base.model.json.JsonSchemaVersion;
import org.eclipse.ditto.base.model.signals.commands.AbstractCommand;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonCollectors;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldDefinition;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonObjectBuilder;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.utils.jsr305.annotations.AllValuesAreNonnullByDefault;
/**
* Command which starts a stream of snapshots.
*/
@Immutable
@AllValuesAreNonnullByDefault
@JsonParsableCommand(typePrefix = StreamingMessage.TYPE_PREFIX, name = SudoStreamSnapshots.NAME)
public final class SudoStreamSnapshots extends AbstractCommand<SudoStreamSnapshots> implements StartStreamRequest {
static final String NAME = "SudoStreamSnapshots";
/**
* Type of this command.
*/
public static final String TYPE = TYPE_PREFIX + NAME;
private final int burst;
private final long timeoutMillis;
private final EntityId lowerBound;
private final List<String> namespaces;
private final JsonArray snapshotFields;
private SudoStreamSnapshots(final Integer burst,
final Long timeoutMillis,
final EntityId lowerBound,
final List<String> namespaces,
final JsonArray snapshotFields,
final DittoHeaders dittoHeaders) {
super(TYPE, dittoHeaders);
this.burst = burst;
this.timeoutMillis = timeoutMillis;
this.lowerBound = lowerBound;
this.namespaces = List.copyOf(namespaces);
this.snapshotFields = snapshotFields;
}
/**
* Creates a new {@code SudoStreamSnapshots} command.
*
* @param burst the amount of snapshots to read in a batch.
* @param timeoutMillis maximum time to wait for acknowledgement of each stream element.
* @param fields selected fields of snapshots.
* @param dittoHeaders the command headers of the request.
* @param entityType the entity type that should be streamed.
* @return the command.
* @throws NullPointerException if any argument is {@code null}.
*/
public static SudoStreamSnapshots of(final Integer burst,
final Long timeoutMillis,
final Collection<String> fields,
final DittoHeaders dittoHeaders,
final EntityType entityType) {
final JsonArray snapshotFields = fields.stream()
.map(JsonValue::of)
.collect(JsonCollectors.valuesToArray());
return new SudoStreamSnapshots(burst,
timeoutMillis,
LowerBound.emptyEntityId(entityType),
List.of(),
snapshotFields,
dittoHeaders);
}
/**
* Deserializes a {@code SudoStreamSnapshots} from the specified {@link JsonObject} argument.
*
* @param jsonObject the JSON object to be deserialized.
* @return the deserialized {@code SudoStreamSnapshots}.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain all required
* fields.
* @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} was not in the expected format.
*/
public static SudoStreamSnapshots fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return new SudoStreamSnapshots(jsonObject.getValueOrThrow(JsonFields.JSON_BURST),
jsonObject.getValueOrThrow(JsonFields.JSON_TIMEOUT_MILLIS),
deserializeLowerBoundEntityId(jsonObject),
jsonObject.getValue(JsonFields.JSON_NAMESPACES)
.map(a -> a.stream().map(JsonValue::asString).toList()).orElseGet(List::of),
jsonObject.getValue(JsonFields.JSON_SNAPSHOT_FIELDS).orElseGet(JsonArray::empty),
dittoHeaders);
}
private static EntityId deserializeLowerBoundEntityId(final JsonObject jsonObject) {
return EntityIdJsonDeserializer.deserializeEntityId(jsonObject,
JsonFields.JSON_LOWER_BOUND,
EntityTypeJsonDeserializer.deserializeEntityType(jsonObject, JsonFields.JSON_LOWER_BOUND_TYPE));
}
/**
* Create a copy of this command with a lower-bound set. The lower bound must be a full PID consisting of a prefix
* and an entity ID.
*
* @param lowerBound the lower bound.
* @return a copy of this command with lower-bound set.
*/
public SudoStreamSnapshots withLowerBound(final EntityId lowerBound) {
return new SudoStreamSnapshots(burst, timeoutMillis, lowerBound, namespaces, snapshotFields, getDittoHeaders());
}
/**
* Create a copy of this command with a namespace filter set. The namespace filter must be a list of namespaces.
*
* @param namespaces the namespaces filter.
* @return a copy of this command with namespace filter set.
*/
public SudoStreamSnapshots withNamespacesFilter(final List<String> namespaces) {
return new SudoStreamSnapshots(burst, timeoutMillis, lowerBound, namespaces, snapshotFields, getDittoHeaders());
}
/**
* Return the lower-bound PID to resume a stream.
*
* @return the lower-bound PID.
*/
public EntityId getLowerBound() {
return lowerBound;
}
/**
* @return a list of namespaces to filter the streamed snapshots
*/
public List<String> getNamespaces() {
return namespaces;
}
/**
* Return whether the command has a non-empty lower bound.
*
* @return whether the command has a non-empty lower bound.
*/
public boolean hasNonEmptyLowerBound() {
return !lowerBound.equals(LowerBound.emptyEntityId(lowerBound.getEntityType()));
}
/**
* Return snapshot fields to request for each streamed snapshot.
*
* @return the requested snapshot fields.
*/
public JsonArray getSnapshotFields() {
return snapshotFields;
}
@Override
public int getBurst() {
return burst;
}
@Override
public long getTimeoutMillis() {
return timeoutMillis;
}
@Override
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder,
final JsonSchemaVersion schemaVersion,
final Predicate<JsonField> thePredicate) {
final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(JsonFields.JSON_BURST, burst, predicate);
jsonObjectBuilder.set(JsonFields.JSON_TIMEOUT_MILLIS, timeoutMillis, predicate);
jsonObjectBuilder.set(JsonFields.JSON_SNAPSHOT_FIELDS, snapshotFields, predicate);
jsonObjectBuilder.set(JsonFields.JSON_LOWER_BOUND_TYPE, lowerBound.getEntityType().toString(), predicate);
jsonObjectBuilder.set(JsonFields.JSON_LOWER_BOUND, lowerBound.toString(), predicate);
jsonObjectBuilder.set(JsonFields.JSON_NAMESPACES, JsonArray.of(namespaces), predicate);
}
@Override
public String getTypePrefix() {
return TYPE_PREFIX;
}
@Override
public Category getCategory() {
return Category.QUERY;
}
@Override
public SudoStreamSnapshots setDittoHeaders(final DittoHeaders dittoHeaders) {
return new SudoStreamSnapshots(burst, timeoutMillis, lowerBound, namespaces, snapshotFields, dittoHeaders);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), burst, timeoutMillis, lowerBound, namespaces, snapshotFields);
}
@Override
public boolean equals(@Nullable final Object obj) {
if (obj instanceof final SudoStreamSnapshots that) {
return burst == that.burst &&
timeoutMillis == that.timeoutMillis &&
Objects.equals(lowerBound, that.lowerBound) &&
Objects.equals(namespaces, that.namespaces) &&
Objects.equals(snapshotFields, that.snapshotFields) &&
super.equals(that);
} else {
return false;
}
}
@Override
protected boolean canEqual(@Nullable final Object other) {
return other instanceof SudoStreamSnapshots;
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" + super.toString()
+ ", burst=" + burst
+ ", timeoutMillis=" + timeoutMillis
+ ", lowerBound=" + lowerBound
+ ", namespaces=" + namespaces
+ ", snapshotFields=" + snapshotFields
+ "]";
}
@Override
public JsonPointer getResourcePath() {
return JsonPointer.empty();
}
@Override
public String getResourceType() {
return TYPE;
}
static final class JsonFields {
private JsonFields() {
throw new AssertionError();
}
static final JsonFieldDefinition<Integer> JSON_BURST =
JsonFactory.newIntFieldDefinition("payload/burst", REGULAR, V_2);
static final JsonFieldDefinition<Long> JSON_TIMEOUT_MILLIS =
JsonFactory.newLongFieldDefinition("payload/timeoutMillis", REGULAR, V_2);
static final JsonFieldDefinition<String> JSON_LOWER_BOUND =
JsonFactory.newStringFieldDefinition("payload/lowerBound", REGULAR, V_2);
static final JsonFieldDefinition<JsonArray> JSON_NAMESPACES =
JsonFactory.newJsonArrayFieldDefinition("payload/namespaces", REGULAR, V_2);
static final JsonFieldDefinition<String> JSON_LOWER_BOUND_TYPE =
JsonFactory.newStringFieldDefinition("payload/lowerBoundType", REGULAR, V_2);
static final JsonFieldDefinition<JsonArray> JSON_SNAPSHOT_FIELDS =
JsonFactory.newJsonArrayFieldDefinition("payload/fields", REGULAR, V_2);
}
}