-
Notifications
You must be signed in to change notification settings - Fork 214
/
SnapshotAdapter.java
106 lines (88 loc) · 4.17 KB
/
SnapshotAdapter.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
/*
* 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.utils.persistence;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import javax.annotation.Nullable;
import org.eclipse.ditto.internal.utils.extension.DittoExtensionIds;
import org.eclipse.ditto.internal.utils.extension.DittoExtensionPoint;
import com.typesafe.config.Config;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.persistence.SelectedSnapshot;
import org.apache.pekko.persistence.SnapshotOffer;
/**
* Adapter capable of transforming Snapshots (in {@link #toSnapshotStore(Object)}) done in an Akka PersistentActor to
* another representation before persisting to the Snapshot-Store.
* Also intercepts loading a Database's Snapshot representation (in {@link #fromSnapshotStore(SnapshotOffer)})
* and is able to transform the Snapshot object before offering it to the Akka PersistentActor.
* Implement this interface for persisting Snapshots in a DB-optimized way (e.g. in BSON format for MongoDB).
*
* @param <T> the domain model type to do a Snapshot for.
*/
public interface SnapshotAdapter<T> extends DittoExtensionPoint {
/**
* Converts a "domain model snapshot" type to the Object which should be persisted into the Snapshot-Store.
*
* @param snapshot the domain model type to do a Snapshot for.
* @return the transformed Database type which should be persisted into Snapshot-Store.
*/
Object toSnapshotStore(T snapshot);
/**
* Converts a "database snapshot" (directly loaded from the database) type to a domain model snapshot type.
*
* @param snapshotOffer the SnapshotOffer as offered from Akka Persistence including the db snapshot.
* @return the transformed domain model type which is offered to the PersistentActor or {@code null}.
*/
@Nullable
T fromSnapshotStore(SnapshotOffer snapshotOffer);
/**
* Converts a "database selected snapshot" (directly loaded from the database) type to a domain model snapshot type.
*
* @param selectedSnapshot the SelectedSnapshot as offered from Akka Persistence including the db snapshot.
* @return the transformed domain model type which is offered to the PersistentActor or {@code null}.
*/
@Nullable
T fromSnapshotStore(SelectedSnapshot selectedSnapshot);
/**
* Loads the implementation of {@code SnapshotAdapter} which is configured for the
* {@code ActorSystem}.
*
* @param actorSystem the actorSystem in which the {@code SnapshotAdapter} should be loaded.
* @param config the config the extension is configured.
* @return the {@code SnapshotAdapter} implementation.
* @throws NullPointerException if {@code actorSystem} is {@code null}.
*/
static <T> SnapshotAdapter<T> get(final ActorSystem actorSystem, final Config config) {
checkNotNull(actorSystem, "actorSystem");
checkNotNull(config, "config");
final var extensionIdConfig = ExtensionId.computeConfig(config);
return DittoExtensionIds.get(actorSystem)
.computeIfAbsent(extensionIdConfig, ExtensionId::new)
.get(actorSystem);
}
/**
* ID of the actor system extension to provide signal enrichment for connectivity.
*/
final class ExtensionId extends DittoExtensionPoint.ExtensionId<SnapshotAdapter> {
private static final String CONFIG_KEY = "snapshot-adapter";
private ExtensionId(final ExtensionIdConfig<SnapshotAdapter> extensionIdConfig) {
super(extensionIdConfig);
}
static ExtensionIdConfig<SnapshotAdapter> computeConfig(final Config config) {
return ExtensionIdConfig.of(SnapshotAdapter.class, config, CONFIG_KEY);
}
@Override
protected String getConfigKey() {
return CONFIG_KEY;
}
}
}