-
Notifications
You must be signed in to change notification settings - Fork 215
/
Metadata.java
472 lines (426 loc) · 17.1 KB
/
Metadata.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
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
/*
* 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.thingsearch.service.persistence.write.model;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.acks.DittoAcknowledgementLabel;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.signals.acks.Acknowledgement;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.StartedTimer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.things.model.ThingId;
import org.eclipse.ditto.things.model.signals.events.ThingEvent;
import org.eclipse.ditto.thingsearch.api.UpdateReason;
import org.eclipse.ditto.thingsearch.api.commands.sudo.UpdateThingResponse;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
/**
* Data class holding information about a "thingEntities" database record.
*/
public final class Metadata {
private final ThingId thingId;
private final long thingRevision;
@Nullable private final PolicyId policyId;
@Nullable private final Long policyRevision;
@Nullable private final Instant modified;
private final List<ThingEvent<?>> events;
private final List<StartedTimer> timers;
private final List<ActorSelection> ackRecipients;
private final boolean invalidateThing;
private final boolean invalidatePolicy;
private final List<UpdateReason> updateReasons;
private Metadata(final ThingId thingId,
final long thingRevision,
@Nullable final PolicyId policyId,
@Nullable final Long policyRevision,
@Nullable final Instant modified,
final List<ThingEvent<?>> events,
final Collection<StartedTimer> timers,
final Collection<ActorSelection> ackRecipients,
final boolean invalidateThing,
final boolean invalidatePolicy,
final Collection<UpdateReason> updateReasons) {
this.thingId = thingId;
this.thingRevision = thingRevision;
this.policyId = policyId;
this.policyRevision = policyRevision;
this.modified = modified;
this.events = events;
this.timers = List.copyOf(timers);
this.ackRecipients = List.copyOf(ackRecipients);
this.invalidateThing = invalidateThing;
this.invalidatePolicy = invalidatePolicy;
this.updateReasons = List.copyOf(updateReasons);
}
/**
* Create an Metadata object.
*
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param policyId the Policy ID if the Thing has one.
* @param policyRevision the Policy revision if the Thing has a policy, or null if it does not.
* @param timer an optional timer measuring the search updater's consistency lag.
* @return the new Metadata object.
*/
public static Metadata of(final ThingId thingId,
final long thingRevision,
@Nullable final PolicyId policyId,
@Nullable final Long policyRevision,
@Nullable final StartedTimer timer) {
return new Metadata(thingId, thingRevision, policyId, policyRevision, null,
List.of(), null != timer ? List.of(timer) : List.of(), List.of(), false, false,
List.of(UpdateReason.UNKNOWN));
}
/**
* Create an Metadata object retaining the original ackRecipient of an event.
*
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param policyId the Policy ID if the Thing has one.
* @param policyRevision the Policy revision if the Thing has a policy, or null if it does not.
* @param timer an optional timer measuring the search updater's consistency lag.
* @param ackRecipient the ackRecipient.
* @return the new Metadata object.
*/
public static Metadata of(final ThingId thingId,
final long thingRevision,
@Nullable final PolicyId policyId,
@Nullable final Long policyRevision,
final List<ThingEvent<?>> events,
@Nullable final StartedTimer timer,
@Nullable final ActorSelection ackRecipient) {
return new Metadata(thingId, thingRevision, policyId, policyRevision, null, events,
null != timer ? List.of(timer) : List.of(),
null != ackRecipient ? List.of(ackRecipient) : List.of(), false, false, List.of(UpdateReason.UNKNOWN));
}
/**
* Create an Metadata object retaining the original ackRecipient of an event.
*
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param policyId the Policy ID if the Thing has one.
* @param policyRevision the Policy revision if the Thing has a policy, or null if it does not.
* @param modified the timestamp of the last change incorporated into the search index, or null if not known.
* @param events the events included in the metadata causing the search update.
* @param timers the timers measuring the search updater's consistency lag.
* @param ackRecipient the ackRecipient.
* @param updateReasons the update reasons.
* @return the new Metadata object.
*/
public static Metadata of(final ThingId thingId,
final long thingRevision,
@Nullable final PolicyId policyId,
@Nullable final Long policyRevision,
@Nullable final Instant modified,
final List<ThingEvent<?>> events,
final Collection<StartedTimer> timers,
final Collection<ActorSelection> ackRecipient,
final Collection<UpdateReason> updateReasons) {
return new Metadata(thingId, thingRevision, policyId, policyRevision, modified, events, timers, ackRecipient,
false, false, updateReasons);
}
/**
* Create an Metadata object with timestamp for the last modification.
*
* @param thingId the Thing ID.
* @param thingRevision the Thing revision.
* @param policyId the Policy ID if the Thing has one.
* @param policyRevision the Policy revision if the Thing has a policy, or null if it does not.
* @param modified the timestamp of the last change incorporated into the search index, or null if not known.
* @param timer an optional timer measuring the search updater's consistency lag.
* @return the new Metadata object.
*/
public static Metadata of(final ThingId thingId,
final long thingRevision,
@Nullable final PolicyId policyId,
@Nullable final Long policyRevision,
@Nullable final Instant modified,
@Nullable final StartedTimer timer) {
return new Metadata(thingId, thingRevision, policyId, policyRevision, modified,
List.of(), null != timer ? List.of(timer) : List.of(), List.of(), false, false,
List.of(UpdateReason.UNKNOWN));
}
/**
* Return a Metadata object for a deleted Thing.
*
* @param thingId the ID of the deleted thing.
* @return the Metadata object.
*/
public static Metadata ofDeleted(final ThingId thingId) {
return Metadata.of(thingId, -1, null, null, null);
}
/**
* Recover the metadata from an UpdateThingResponse.
*
* @param updateThingResponse the response.
* @return the metadata.
*/
public static Metadata fromResponse(final UpdateThingResponse updateThingResponse) {
return of(updateThingResponse.getThingId(), updateThingResponse.getThingRevision(),
updateThingResponse.getPolicyId().orElse(null),
updateThingResponse.getPolicyRevision().orElse(null),
null);
}
/**
* Create a copy of this object containing only the IDs and revisions of the thing and policy.
*
* @return the exported metadata.
*/
public Metadata export() {
return Metadata.of(thingId, thingRevision, policyId, policyRevision, null);
}
/**
* Create a copy of this metadata requesting cache invalidation.
*
* @param invalidateThing whether to invalidate the cached thing.
* @param invalidatePolicy whether to invalidate the cached policy enforcer.
* @return the copy.
*/
public Metadata invalidateCaches(final boolean invalidateThing, final boolean invalidatePolicy) {
return new Metadata(thingId, thingRevision, policyId, policyRevision, modified, events, timers, ackRecipients,
invalidateThing, invalidatePolicy, updateReasons);
}
/**
* Create a copy of this metadata with senders replaced by the argument.
*
* @return the copy.
*/
public Metadata withAckRecipient(final ActorSelection ackRecipient) {
return new Metadata(thingId, thingRevision, policyId, policyRevision, modified, events, timers, List.of(ackRecipient),
invalidateThing, invalidatePolicy, updateReasons);
}
/**
* Create a copy of this metadata with senders replaced by the argument.
*
* @return the copy.
*/
public Metadata withUpdateReason(final UpdateReason reason) {
return new Metadata(thingId, thingRevision, policyId, policyRevision, modified, events, timers, ackRecipients,
invalidateThing, invalidatePolicy, List.of(reason));
}
/**
* @return the Thing ID.
*/
public ThingId getThingId() {
return thingId;
}
/**
* Returns the revision of the Thing according to the search index.
*
* @return the revision of the Thing according to the search index.
*/
public long getThingRevision() {
return thingRevision;
}
/**
* @return the policyId of the Thing according to the search index.
*/
public Optional<PolicyId> getPolicyId() {
return Optional.ofNullable(policyId);
}
/**
* @return namespace field as to be written in the persistence.
*/
public String getNamespaceInPersistence() {
return thingId.getNamespace();
}
/**
* @return the policyId-field as to be written in the persistence.
*/
public String getPolicyIdInPersistence() {
return getPolicyId().map(PolicyId::toString).orElse("");
}
/**
* Returns the revision of the Policy according to the search index.
*
* @return the revision of the Policy according to the search index.
*/
public Optional<Long> getPolicyRevision() {
return Optional.ofNullable(policyRevision);
}
/**
* Returns the timestamp of the last change if any exists.
*
* @return the optional timestamp.
*/
public Optional<Instant> getModified() {
return Optional.ofNullable(modified);
}
/**
* Returns the known thing events.
*
* @return the known thing events.
*/
public List<ThingEvent<?>> getEvents() {
return Collections.unmodifiableList(events);
}
/**
* Returns the correlationIds of the known thing events.
*
* @return the correlation ids.
*/
public List<String> getEventsCorrelationIds() {
return events.stream()
.map(Event::getDittoHeaders)
.map(DittoHeaders::getCorrelationId)
.flatMap(Optional::stream)
.toList();
}
/**
* Returns the timers measuring the consistency lag.
*
* @return the timers.
*/
public List<StartedTimer> getTimers() {
return timers;
}
/**
* Return selections to receive ACKs.
*
* @return the senders.
*/
public List<ActorSelection> getAckRecipients() {
return ackRecipients;
}
/**
* Return the reason of the update.
*
* @return the update reason.
*/
public List<UpdateReason> getUpdateReasons() {
return updateReasons;
}
/**
* Returns whether an acknowledgement for the successful adding to the search index is requested.
*
* @return whether {@code "search-persisted"} is requested.
*/
public boolean isShouldAcknowledge() {
return !ackRecipients.isEmpty();
}
/**
* Returns whether this metadata should invalidate the cached thing.
*
* @return whether to invalidate the cached thing.
*/
public boolean shouldInvalidateThing() {
return invalidateThing;
}
/**
* Returns whether this metadata should invalidate the cached policy.
*
* @return whether to invalidate the cached policy.
*/
public boolean shouldInvalidatePolicy() {
return invalidatePolicy;
}
/**
* Prepend new timers and senders to the timers and senders stored in this object.
*
* @param newMetadata a previous metadata record.
* @return the new metadata with concatenated senders.
*/
public Metadata append(final Metadata newMetadata) {
final List<ThingEvent<?>> newEvents = Stream.concat(events.stream(), newMetadata.events.stream()).toList();
final List<StartedTimer> newTimers = Stream.concat(timers.stream(), newMetadata.timers.stream()).toList();
final List<ActorSelection> newAckRecipients = Stream.concat(ackRecipients.stream(), newMetadata.ackRecipients.stream()).toList();
final List<UpdateReason> newReasons = Stream.concat(updateReasons.stream(), newMetadata.updateReasons.stream())
.toList();
return new Metadata(newMetadata.thingId, newMetadata.thingRevision, newMetadata.policyId,
newMetadata.policyRevision, newMetadata.modified, newEvents, newTimers, newAckRecipients,
invalidateThing || newMetadata.invalidateThing,
invalidatePolicy || newMetadata.invalidatePolicy,
newReasons);
}
/**
* Send negative acknowledgements to senders.
*/
public void sendNAck() {
send(Acknowledgement.of(DittoAcknowledgementLabel.SEARCH_PERSISTED, thingId,
HttpStatus.INTERNAL_SERVER_ERROR, DittoHeaders.empty()));
}
/**
* Send positive acknowledgements to senders.
*/
public void sendAck() {
send(Acknowledgement.of(DittoAcknowledgementLabel.SEARCH_PERSISTED, thingId, HttpStatus.NO_CONTENT,
DittoHeaders.empty()));
}
/**
* Send weak acknowledgement to senders.
*
* @param payload the payload of the weak acknowledgement.
*/
public void sendWeakAck(@Nullable final JsonValue payload) {
send(Acknowledgement.weak(DittoAcknowledgementLabel.SEARCH_PERSISTED, thingId, DittoHeaders.empty(), payload));
}
private void send(final Acknowledgement ack) {
timers.forEach(timer -> {
if (timer.isRunning()) {
timer.tag("success", ack.isSuccess()).stop();
}
});
ackRecipients.forEach(ackRecipient -> ackRecipient.tell(ack, ActorRef.noSender()));
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Metadata that = (Metadata) o;
return thingRevision == that.thingRevision &&
Objects.equals(policyRevision, that.policyRevision) &&
Objects.equals(thingId, that.thingId) &&
Objects.equals(policyId, that.policyId) &&
Objects.equals(modified, that.modified) &&
Objects.equals(events, that.events) &&
Objects.equals(timers, that.timers) &&
Objects.equals(ackRecipients, that.ackRecipients) &&
invalidateThing == that.invalidateThing &&
invalidatePolicy == that.invalidatePolicy &&
Objects.equals(updateReasons, that.updateReasons);
}
@Override
public int hashCode() {
return Objects.hash(thingId, thingRevision, policyId, policyRevision, modified, events, timers, ackRecipients,
invalidateThing, invalidatePolicy, updateReasons);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"thingId=" + thingId +
", thingRevision=" + thingRevision +
", policyId=" + policyId +
", policyRevision=" + policyRevision +
", modified=" + modified +
", events=" + events +
", timers=[" + timers.size() + " timers]" +
", ackRecipients=" + ackRecipients +
", invalidateThing=" + invalidateThing +
", invalidatePolicy=" + invalidatePolicy +
", updateReasons=" + updateReasons +
"]";
}
}