Sho SHIMIZU
Committed by Gerrit Code Review

Fast path in the resource store for discrete resource

Change-Id: I179376f8a6b43b1f2867212b2456c17b9b5602f5
...@@ -545,9 +545,9 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -545,9 +545,9 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
545 * @param id ID of resource to be checked 545 * @param id ID of resource to be checked
546 * @return the resource which is regarded as the same as the specified resource 546 * @return the resource which is regarded as the same as the specified resource
547 */ 547 */
548 - // Naive implementation, which traverses all elements in the set 548 + // Naive implementation, which traverses all elements in the set when continuous resource
549 - // computational complexity: O(n) where n is the number of elements 549 + // computational complexity: O(1) when discrete resource. O(n) when continuous resource
550 - // in the associated set 550 + // where n is the number of elements in the associated set
551 private Optional<Resource> lookup(TransactionalMap<DiscreteResourceId, Set<Resource>> childTxMap, ResourceId id) { 551 private Optional<Resource> lookup(TransactionalMap<DiscreteResourceId, Set<Resource>> childTxMap, ResourceId id) {
552 if (!id.parent().isPresent()) { 552 if (!id.parent().isPresent()) {
553 return Optional.of(Resource.ROOT); 553 return Optional.of(Resource.ROOT);
...@@ -558,6 +558,19 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -558,6 +558,19 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
558 return Optional.empty(); 558 return Optional.empty();
559 } 559 }
560 560
561 + // short-circuit if discrete resource
562 + // check the existence in the set: O(1) operation
563 + if (id instanceof DiscreteResourceId) {
564 + DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource();
565 + if (values.contains(discrete)) {
566 + return Optional.of(discrete);
567 + } else {
568 + return Optional.empty();
569 + }
570 + }
571 +
572 + // continuous resource case
573 + // iterate over the values in the set: O(n) operation
561 return values.stream() 574 return values.stream()
562 .filter(x -> x.id().equals(id)) 575 .filter(x -> x.id().equals(id))
563 .findFirst(); 576 .findFirst();
......