Sho SHIMIZU
Committed by Gerrit Code Review

Refactor: Simplify register() and unregister() in transactional stores

Change-Id: Ie39391774d54c209ec2b8ce61adc76d5ba4efa7e
......@@ -358,14 +358,6 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
.map(x -> (ContinuousResource) x)
.collect(Collectors.toList());
// short-circuit decision avoiding unnecessary distributed map operations
if (continuousValues.isEmpty()) {
return discreteTxStore.register(key, discreteValues);
}
if (discreteValues.isEmpty()) {
return continuousTxStore.register(key, continuousValues);
}
return discreteTxStore.register(key, discreteValues)
&& continuousTxStore.register(key, continuousValues);
}
......@@ -395,14 +387,6 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
.map(x -> (ContinuousResource) x)
.collect(Collectors.toList());
// short-circuit decision avoiding unnecessary distributed map operations
if (continuousValues.isEmpty()) {
return discreteTxStore.unregister(key, discreteValues);
}
if (discreteValues.isEmpty()) {
return continuousTxStore.unregister(key, continuousValues);
}
return discreteTxStore.unregister(key, discreteValues)
&& continuousTxStore.unregister(key, continuousValues);
}
......
......@@ -67,6 +67,11 @@ class TransactionalContinuousResourceStore {
}
boolean register(DiscreteResourceId key, List<ContinuousResource> values) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
return true;
}
Set<ContinuousResource> requested = new LinkedHashSet<>(values);
Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested);
if (oldValues == null) {
......@@ -95,6 +100,11 @@ class TransactionalContinuousResourceStore {
}
boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
return true;
}
Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
if (oldValues == null) {
log.trace("No-Op removing values. key {} did not exist", key);
......
......@@ -63,6 +63,11 @@ class TransactionalDiscreteResourceStore {
}
boolean register(DiscreteResourceId key, List<DiscreteResource> values) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
return true;
}
Set<DiscreteResource> requested = new LinkedHashSet<>(values);
Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, requested);
if (oldValues == null) {
......@@ -82,6 +87,11 @@ class TransactionalDiscreteResourceStore {
}
boolean unregister(DiscreteResourceId key, List<DiscreteResource> values) {
// short-circuit: receiving empty resource is regarded as success
if (values.isEmpty()) {
return true;
}
Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
if (oldValues == null) {
log.trace("No-Op removing values. key {} did not exist", key);
......