Skip to content

Commit e654f47

Browse files
committed
refactor: extract SharedRegistry class
1 parent f151d96 commit e654f47

File tree

2 files changed

+170
-58
lines changed

2 files changed

+170
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package io.neonbee.internal;
2+
3+
import static io.vertx.core.Future.succeededFuture;
4+
5+
import io.neonbee.NeonBee;
6+
import io.neonbee.logging.LoggingFacade;
7+
import io.vertx.core.Future;
8+
import io.vertx.core.Vertx;
9+
import io.vertx.core.json.JsonArray;
10+
import io.vertx.core.shareddata.AsyncMap;
11+
import io.vertx.core.shareddata.SharedData;
12+
13+
/**
14+
* A registry to manage values in the {@link SharedData} shared map.
15+
* <p>
16+
* This class is a generic implementation of a registry that can be used to store values in a shared map.
17+
*
18+
* @param <T> the type of the value to store in the shared registry
19+
*/
20+
public class SharedRegistry<T> implements Registry<T> {
21+
22+
private final LoggingFacade logger = LoggingFacade.create();
23+
24+
private final SharedData sharedData;
25+
26+
private final String registryName;
27+
28+
/**
29+
* Create a new {@link WriteSafeRegistry}.
30+
*
31+
* @param vertx the {@link Vertx} instance
32+
* @param registryName the name of the map registry
33+
*/
34+
public SharedRegistry(Vertx vertx, String registryName) {
35+
this(registryName, new SharedDataAccessor(vertx, SharedRegistry.class));
36+
}
37+
38+
/**
39+
* Create a new {@link WriteSafeRegistry}.
40+
*
41+
* @param registryName the name of the map registry
42+
* @param sharedData the shared data
43+
*/
44+
public SharedRegistry(String registryName, SharedData sharedData) {
45+
this.registryName = registryName;
46+
this.sharedData = sharedData;
47+
}
48+
49+
/**
50+
* Register a value in the async shared map of {@link NeonBee} by key.
51+
*
52+
* @param sharedMapKey the shared map key
53+
* @param value the value to register
54+
* @return the future
55+
*/
56+
@Override
57+
public Future<Void> register(String sharedMapKey, T value) {
58+
if (logger.isInfoEnabled()) {
59+
logger.info("register value: \"{}\" in shared map: \"{}\"", sharedMapKey, value);
60+
}
61+
62+
Future<AsyncMap<String, Object>> sharedMap = getSharedMap();
63+
64+
return sharedMap.compose(map -> map.get(sharedMapKey))
65+
.map(valueOrNull -> valueOrNull != null ? (JsonArray) valueOrNull : new JsonArray())
66+
.compose(valueArray -> {
67+
if (!valueArray.contains(value)) {
68+
valueArray.add(value);
69+
}
70+
71+
if (logger.isInfoEnabled()) {
72+
logger.info("Registered verticle {} in shared map.", value);
73+
}
74+
75+
return sharedMap.compose(map -> map.put(sharedMapKey, valueArray));
76+
});
77+
}
78+
79+
/**
80+
* Unregister the value in the {@link NeonBee} async shared map from the sharedMapKey.
81+
*
82+
* @param sharedMapKey the shared map key
83+
* @param value the value to unregister
84+
* @return the future
85+
*/
86+
@Override
87+
public Future<Void> unregister(String sharedMapKey, T value) {
88+
if (logger.isDebugEnabled()) {
89+
logger.debug("unregister value: \"{}\" from shared map: \"{}\"", sharedMapKey, value);
90+
}
91+
92+
Future<AsyncMap<String, Object>> sharedMap = getSharedMap();
93+
94+
return sharedMap.compose(map -> map.get(sharedMapKey)).map(jsonArray -> (JsonArray) jsonArray)
95+
.compose(values -> {
96+
if (values == null) {
97+
return succeededFuture();
98+
}
99+
100+
if (logger.isInfoEnabled()) {
101+
logger.info("Unregistered verticle {} in shared map.", value);
102+
}
103+
104+
values.remove(value);
105+
return sharedMap.compose(map -> map.put(sharedMapKey, values));
106+
});
107+
}
108+
109+
@Override
110+
public Future<JsonArray> get(String sharedMapKey) {
111+
return getSharedMap().compose(map -> map.get(sharedMapKey)).map(o -> (JsonArray) o);
112+
}
113+
114+
/**
115+
* Shared map that is used as registry.
116+
*
117+
* @return Future to the shared map
118+
*/
119+
public Future<AsyncMap<String, Object>> getSharedMap() {
120+
return sharedData.getAsyncMap(registryName);
121+
}
122+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.neonbee.internal;
22

3-
import static io.vertx.core.Future.succeededFuture;
4-
53
import java.util.function.Supplier;
64

75
import io.neonbee.NeonBee;
@@ -10,6 +8,7 @@
108
import io.vertx.core.Vertx;
119
import io.vertx.core.json.JsonArray;
1210
import io.vertx.core.shareddata.AsyncMap;
11+
import io.vertx.core.shareddata.SharedData;
1312

1413
/**
1514
* A registry to manage values in the {@link SharedDataAccessor} shared map.
@@ -22,7 +21,9 @@ public class WriteSafeRegistry<T> implements Registry<T> {
2221

2322
private final LoggingFacade logger = LoggingFacade.create();
2423

25-
private final SharedDataAccessor sharedDataAccessor;
24+
private final SharedData sharedData;
25+
26+
private final Registry<T> sharedRegistry;
2627

2728
private final String registryName;
2829

@@ -33,8 +34,30 @@ public class WriteSafeRegistry<T> implements Registry<T> {
3334
* @param registryName the name of the map registry
3435
*/
3536
public WriteSafeRegistry(Vertx vertx, String registryName) {
37+
this(registryName, new SharedDataAccessor(vertx, WriteSafeRegistry.class));
38+
}
39+
40+
/**
41+
* Create a new {@link WriteSafeRegistry}.
42+
*
43+
* @param registryName the name of the map registry
44+
* @param sharedData the shared data
45+
*/
46+
public WriteSafeRegistry(String registryName, SharedData sharedData) {
47+
this(registryName, sharedData, new SharedRegistry<>(registryName, sharedData));
48+
}
49+
50+
/**
51+
* Create a new {@link WriteSafeRegistry}.
52+
*
53+
* @param registryName the name of the map registry
54+
* @param sharedData the shared data
55+
* @param registry the shared registry
56+
*/
57+
WriteSafeRegistry(String registryName, SharedData sharedData, Registry<T> registry) {
3658
this.registryName = registryName;
37-
this.sharedDataAccessor = new SharedDataAccessor(vertx, this.getClass());
59+
this.sharedData = sharedData;
60+
this.sharedRegistry = registry;
3861
}
3962

4063
/**
@@ -48,12 +71,26 @@ public WriteSafeRegistry(Vertx vertx, String registryName) {
4871
public Future<Void> register(String sharedMapKey, T value) {
4972
logger.info("register value: \"{}\" in shared map: \"{}\"", sharedMapKey, value);
5073

51-
return lock(sharedMapKey, () -> addValue(sharedMapKey, value));
74+
return lock(sharedMapKey, () -> sharedRegistry.register(sharedMapKey, value));
5275
}
5376

5477
@Override
5578
public Future<JsonArray> get(String sharedMapKey) {
56-
return getSharedMap().compose(map -> map.get(sharedMapKey)).map(o -> (JsonArray) o);
79+
return sharedRegistry.get(sharedMapKey);
80+
}
81+
82+
/**
83+
* Unregister the value in the {@link NeonBee} async shared map from the sharedMapKey.
84+
*
85+
* @param sharedMapKey the shared map key
86+
* @param value the value to unregister
87+
* @return the future
88+
*/
89+
@Override
90+
public Future<Void> unregister(String sharedMapKey, T value) {
91+
logger.debug("unregister value: \"{}\" from shared map: \"{}\"", sharedMapKey, value);
92+
93+
return lock(sharedMapKey, () -> sharedRegistry.unregister(sharedMapKey, value));
5794
}
5895

5996
/**
@@ -65,7 +102,7 @@ public Future<JsonArray> get(String sharedMapKey) {
65102
*/
66103
protected Future<Void> lock(String sharedMapKey, Supplier<Future<Void>> futureSupplier) {
67104
logger.debug("Get lock for {}", sharedMapKey);
68-
return sharedDataAccessor.getLock(sharedMapKey).onFailure(throwable -> {
105+
return sharedData.getLock(sharedMapKey).onFailure(throwable -> {
69106
logger.error("Error acquiring lock for {}", sharedMapKey, throwable);
70107
}).compose(lock -> Future.<Void>future(event -> futureSupplier.get().onComplete(event))
71108
.onComplete(anyResult -> {
@@ -74,62 +111,15 @@ protected Future<Void> lock(String sharedMapKey, Supplier<Future<Void>> futureSu
74111
}));
75112
}
76113

77-
private Future<Void> addValue(String sharedMapKey, Object value) {
78-
Future<AsyncMap<String, Object>> sharedMap = getSharedMap();
79-
80-
return sharedMap.compose(map -> map.get(sharedMapKey))
81-
.map(valueOrNull -> valueOrNull != null ? (JsonArray) valueOrNull : new JsonArray())
82-
.compose(valueArray -> {
83-
if (!valueArray.contains(value)) {
84-
valueArray.add(value);
85-
}
86-
87-
if (logger.isInfoEnabled()) {
88-
logger.info("Registered verticle {} in shared map.", value);
89-
}
90-
91-
return sharedMap.compose(map -> map.put(sharedMapKey, valueArray));
92-
});
93-
}
94-
95-
/**
96-
* Unregister the value in the {@link NeonBee} async shared map from the sharedMapKey.
97-
*
98-
* @param sharedMapKey the shared map key
99-
* @param value the value to unregister
100-
* @return the future
101-
*/
102-
@Override
103-
public Future<Void> unregister(String sharedMapKey, T value) {
104-
logger.debug("unregister value: \"{}\" from shared map: \"{}\"", sharedMapKey, value);
105-
106-
return lock(sharedMapKey, () -> removeValue(sharedMapKey, value));
107-
}
108-
109-
private Future<Void> removeValue(String sharedMapKey, Object value) {
110-
Future<AsyncMap<String, Object>> sharedMap = getSharedMap();
111-
112-
return sharedMap.compose(map -> map.get(sharedMapKey)).map(jsonArray -> (JsonArray) jsonArray)
113-
.compose(values -> {
114-
if (values == null) {
115-
return succeededFuture();
116-
}
117-
118-
if (logger.isInfoEnabled()) {
119-
logger.info("Unregistered verticle {} in shared map.", value);
120-
}
121-
122-
values.remove(value);
123-
return sharedMap.compose(map -> map.put(sharedMapKey, values));
124-
});
125-
}
126-
127114
/**
128115
* Shared map that is used as registry.
116+
* <p>
117+
* It is not safe to write to the shared map directly. Use the {@link WriteSafeRegistry#register(String, Object)}
118+
* and {@link WriteSafeRegistry#unregister(String, Object)} methods.
129119
*
130120
* @return Future to the shared map
131121
*/
132122
public Future<AsyncMap<String, Object>> getSharedMap() {
133-
return sharedDataAccessor.getAsyncMap(registryName);
123+
return sharedData.getAsyncMap(registryName);
134124
}
135125
}

0 commit comments

Comments
 (0)