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 ...@@ -358,14 +358,6 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
358 .map(x -> (ContinuousResource) x) 358 .map(x -> (ContinuousResource) x)
359 .collect(Collectors.toList()); 359 .collect(Collectors.toList());
360 360
361 - // short-circuit decision avoiding unnecessary distributed map operations
362 - if (continuousValues.isEmpty()) {
363 - return discreteTxStore.register(key, discreteValues);
364 - }
365 - if (discreteValues.isEmpty()) {
366 - return continuousTxStore.register(key, continuousValues);
367 - }
368 -
369 return discreteTxStore.register(key, discreteValues) 361 return discreteTxStore.register(key, discreteValues)
370 && continuousTxStore.register(key, continuousValues); 362 && continuousTxStore.register(key, continuousValues);
371 } 363 }
...@@ -395,14 +387,6 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -395,14 +387,6 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
395 .map(x -> (ContinuousResource) x) 387 .map(x -> (ContinuousResource) x)
396 .collect(Collectors.toList()); 388 .collect(Collectors.toList());
397 389
398 - // short-circuit decision avoiding unnecessary distributed map operations
399 - if (continuousValues.isEmpty()) {
400 - return discreteTxStore.unregister(key, discreteValues);
401 - }
402 - if (discreteValues.isEmpty()) {
403 - return continuousTxStore.unregister(key, continuousValues);
404 - }
405 -
406 return discreteTxStore.unregister(key, discreteValues) 390 return discreteTxStore.unregister(key, discreteValues)
407 && continuousTxStore.unregister(key, continuousValues); 391 && continuousTxStore.unregister(key, continuousValues);
408 } 392 }
......
...@@ -67,6 +67,11 @@ class TransactionalContinuousResourceStore { ...@@ -67,6 +67,11 @@ class TransactionalContinuousResourceStore {
67 } 67 }
68 68
69 boolean register(DiscreteResourceId key, List<ContinuousResource> values) { 69 boolean register(DiscreteResourceId key, List<ContinuousResource> values) {
70 + // short-circuit: receiving empty resource is regarded as success
71 + if (values.isEmpty()) {
72 + return true;
73 + }
74 +
70 Set<ContinuousResource> requested = new LinkedHashSet<>(values); 75 Set<ContinuousResource> requested = new LinkedHashSet<>(values);
71 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested); 76 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, requested);
72 if (oldValues == null) { 77 if (oldValues == null) {
...@@ -95,6 +100,11 @@ class TransactionalContinuousResourceStore { ...@@ -95,6 +100,11 @@ class TransactionalContinuousResourceStore {
95 } 100 }
96 101
97 boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) { 102 boolean unregister(DiscreteResourceId key, List<ContinuousResource> values) {
103 + // short-circuit: receiving empty resource is regarded as success
104 + if (values.isEmpty()) {
105 + return true;
106 + }
107 +
98 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>()); 108 Set<ContinuousResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
99 if (oldValues == null) { 109 if (oldValues == null) {
100 log.trace("No-Op removing values. key {} did not exist", key); 110 log.trace("No-Op removing values. key {} did not exist", key);
......
...@@ -63,6 +63,11 @@ class TransactionalDiscreteResourceStore { ...@@ -63,6 +63,11 @@ class TransactionalDiscreteResourceStore {
63 } 63 }
64 64
65 boolean register(DiscreteResourceId key, List<DiscreteResource> values) { 65 boolean register(DiscreteResourceId key, List<DiscreteResource> values) {
66 + // short-circuit: receiving empty resource is regarded as success
67 + if (values.isEmpty()) {
68 + return true;
69 + }
70 +
66 Set<DiscreteResource> requested = new LinkedHashSet<>(values); 71 Set<DiscreteResource> requested = new LinkedHashSet<>(values);
67 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, requested); 72 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, requested);
68 if (oldValues == null) { 73 if (oldValues == null) {
...@@ -82,6 +87,11 @@ class TransactionalDiscreteResourceStore { ...@@ -82,6 +87,11 @@ class TransactionalDiscreteResourceStore {
82 } 87 }
83 88
84 boolean unregister(DiscreteResourceId key, List<DiscreteResource> values) { 89 boolean unregister(DiscreteResourceId key, List<DiscreteResource> values) {
90 + // short-circuit: receiving empty resource is regarded as success
91 + if (values.isEmpty()) {
92 + return true;
93 + }
94 +
85 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>()); 95 Set<DiscreteResource> oldValues = childMap.putIfAbsent(key, new LinkedHashSet<>());
86 if (oldValues == null) { 96 if (oldValues == null) {
87 log.trace("No-Op removing values. key {} did not exist", key); 97 log.trace("No-Op removing values. key {} did not exist", key);
......