Sho SHIMIZU
Committed by Gerrit Code Review

Make resource retrieval more efficient when specifing resource type

This resolves ONOS-4666

Change-Id: I9d09b60531ca48b36fc20f43498cda62f1badb8b
...@@ -111,6 +111,18 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat ...@@ -111,6 +111,18 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
111 Set<Resource> getChildResources(DiscreteResourceId parent); 111 Set<Resource> getChildResources(DiscreteResourceId parent);
112 112
113 /** 113 /**
114 + * Returns a set of the child resources of the specified parent and whose type is
115 + * the specified class.
116 + *
117 + * @param parent ID of the parent of the resources to be returned
118 + * @param cls class instance of the children
119 + * @param <T> type of the resource
120 + * @return a set of the child resources of the specified parent and whose type is
121 + * the specified class
122 + */
123 + <T> Set<Resource> getChildResources(DiscreteResourceId parent, Class<T> cls);
124 +
125 + /**
114 * Returns a collection of the resources which are children of the specified parent and 126 * Returns a collection of the resources which are children of the specified parent and
115 * whose type is the specified class. 127 * whose type is the specified class.
116 * 128 *
......
...@@ -165,9 +165,9 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent ...@@ -165,9 +165,9 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
165 checkNotNull(parent); 165 checkNotNull(parent);
166 checkNotNull(cls); 166 checkNotNull(cls);
167 167
168 - // naive implementation 168 + return store.getChildResources(parent, cls).stream()
169 - return getAvailableResources(parent).stream() 169 + // We access store twice in this method, then the store may be updated by others
170 - .filter(resource -> resource.isTypeOf(cls)) 170 + .filter(store::isAvailable)
171 .collect(Collectors.toSet()); 171 .collect(Collectors.toSet());
172 } 172 }
173 173
...@@ -177,9 +177,11 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent ...@@ -177,9 +177,11 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
177 checkNotNull(parent); 177 checkNotNull(parent);
178 checkNotNull(cls); 178 checkNotNull(cls);
179 179
180 - // naive implementation 180 + return store.getChildResources(parent, cls).stream()
181 - return getAvailableResources(parent).stream() 181 + // We access store twice in this method, then the store may be updated by others
182 - .flatMap(resource -> Tools.stream(resource.valueAs(cls))) 182 + .filter(store::isAvailable)
183 + .map(x -> x.valueAs(cls))
184 + .flatMap(Tools::stream)
183 .collect(Collectors.toSet()); 185 .collect(Collectors.toSet());
184 } 186 }
185 187
......
...@@ -32,6 +32,7 @@ import org.onosproject.store.service.Versioned; ...@@ -32,6 +32,7 @@ import org.onosproject.store.service.Versioned;
32 import java.util.LinkedHashSet; 32 import java.util.LinkedHashSet;
33 import java.util.List; 33 import java.util.List;
34 import java.util.Set; 34 import java.util.Set;
35 +import java.util.stream.Collectors;
35 import java.util.stream.Stream; 36 import java.util.stream.Stream;
36 37
37 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; 38 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
...@@ -79,6 +80,13 @@ class ConsistentContinuousResourceSubStore { ...@@ -79,6 +80,13 @@ class ConsistentContinuousResourceSubStore {
79 return children.value(); 80 return children.value();
80 } 81 }
81 82
83 + <T> Set<ContinuousResource> getChildResources(DiscreteResourceId parent, Class<T> cls) {
84 + // naive implementation
85 + return getChildResources(parent).stream()
86 + .filter(x -> x.isTypeOf(cls))
87 + .collect(Collectors.toCollection(LinkedHashSet::new));
88 + }
89 +
82 public boolean isAvailable(ContinuousResource resource) { 90 public boolean isAvailable(ContinuousResource resource) {
83 // check if it's registered or not. 91 // check if it's registered or not.
84 Versioned<Set<ContinuousResource>> children = childMap.get(resource.parent().get().id()); 92 Versioned<Set<ContinuousResource>> children = childMap.get(resource.parent().get().id());
......
...@@ -76,6 +76,16 @@ class ConsistentDiscreteResourceSubStore { ...@@ -76,6 +76,16 @@ class ConsistentDiscreteResourceSubStore {
76 return children.value().values(); 76 return children.value().values();
77 } 77 }
78 78
79 + <T> Set<DiscreteResource> getChildResources(DiscreteResourceId parent, Class<T> cls) {
80 + Versioned<DiscreteResources> children = childMap.get(parent);
81 +
82 + if (children == null) {
83 + return ImmutableSet.of();
84 + }
85 +
86 + return children.value().valuesOf(cls);
87 + }
88 +
79 boolean isAvailable(DiscreteResource resource) { 89 boolean isAvailable(DiscreteResource resource) {
80 return getResourceAllocations(resource.id()).isEmpty(); 90 return getResourceAllocations(resource.id()).isEmpty();
81 } 91 }
......
...@@ -296,6 +296,17 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour ...@@ -296,6 +296,17 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
296 .build(); 296 .build();
297 } 297 }
298 298
299 + @Override
300 + public <T> Set<Resource> getChildResources(DiscreteResourceId parent, Class<T> cls) {
301 + checkNotNull(parent);
302 + checkNotNull(cls);
303 +
304 + return ImmutableSet.<Resource>builder()
305 + .addAll(discreteStore.getChildResources(parent, cls))
306 + .addAll(continuousStore.getChildResources(parent, cls))
307 + .build();
308 + }
309 +
299 // computational complexity: O(n) where n is the number of the children of the parent 310 // computational complexity: O(n) where n is the number of the children of the parent
300 @Override 311 @Override
301 public <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { 312 public <T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
......
...@@ -91,4 +91,13 @@ interface DiscreteResources { ...@@ -91,4 +91,13 @@ interface DiscreteResources {
91 * @return all resources 91 * @return all resources
92 */ 92 */
93 Set<DiscreteResource> values(); 93 Set<DiscreteResource> values();
94 +
95 + /**
96 + * Returns all of resources this instance holds and filtered by the specified type.
97 + *
98 + * @param cls class instance of the resource value
99 + * @param <T> type of the resource value
100 + * @return all of resources this instance holds and filtered by the specified type
101 + */
102 + <T> Set<DiscreteResource> valuesOf(Class<T> cls);
94 } 103 }
......
...@@ -68,4 +68,9 @@ final class EmptyDiscreteResources implements DiscreteResources { ...@@ -68,4 +68,9 @@ final class EmptyDiscreteResources implements DiscreteResources {
68 .add("values", ImmutableSet.of()) 68 .add("values", ImmutableSet.of())
69 .toString(); 69 .toString();
70 } 70 }
71 +
72 + @Override
73 + public <T> Set<DiscreteResource> valuesOf(Class<T> cls) {
74 + return ImmutableSet.of();
75 + }
71 } 76 }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.store.resource.impl; 16 package org.onosproject.store.resource.impl;
17 17
18 import com.google.common.base.MoreObjects; 18 import com.google.common.base.MoreObjects;
19 +import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Sets; 20 import com.google.common.collect.Sets;
20 import org.onosproject.net.resource.DiscreteResource; 21 import org.onosproject.net.resource.DiscreteResource;
21 import org.onosproject.net.resource.DiscreteResourceCodec; 22 import org.onosproject.net.resource.DiscreteResourceCodec;
...@@ -114,6 +115,13 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -114,6 +115,13 @@ final class EncodableDiscreteResources implements DiscreteResources {
114 .collect(Collectors.toCollection(LinkedHashSet::new)); 115 .collect(Collectors.toCollection(LinkedHashSet::new));
115 } 116 }
116 117
118 + @Override
119 + public <T> Set<DiscreteResource> valuesOf(Class<T> cls) {
120 + return Optional.ofNullable(values.get(cls))
121 + .map(x -> x.values(parent.id()))
122 + .orElse(ImmutableSet.of());
123 + }
124 +
117 DiscreteResource parent() { 125 DiscreteResource parent() {
118 return parent; 126 return parent;
119 } 127 }
......
...@@ -25,6 +25,7 @@ import java.util.LinkedHashSet; ...@@ -25,6 +25,7 @@ import java.util.LinkedHashSet;
25 import java.util.Objects; 25 import java.util.Objects;
26 import java.util.Optional; 26 import java.util.Optional;
27 import java.util.Set; 27 import java.util.Set;
28 +import java.util.stream.Collectors;
28 29
29 final class GenericDiscreteResources implements DiscreteResources { 30 final class GenericDiscreteResources implements DiscreteResources {
30 private final Set<DiscreteResource> values; 31 private final Set<DiscreteResource> values;
...@@ -87,6 +88,13 @@ final class GenericDiscreteResources implements DiscreteResources { ...@@ -87,6 +88,13 @@ final class GenericDiscreteResources implements DiscreteResources {
87 } 88 }
88 89
89 @Override 90 @Override
91 + public <T> Set<DiscreteResource> valuesOf(Class<T> cls) {
92 + return values.stream()
93 + .filter(x -> x.isTypeOf(cls))
94 + .collect(Collectors.toCollection(LinkedHashSet::new));
95 + }
96 +
97 + @Override
90 public int hashCode() { 98 public int hashCode() {
91 return Objects.hash(values); 99 return Objects.hash(values);
92 } 100 }
......
...@@ -93,6 +93,11 @@ final class UnifiedDiscreteResources implements DiscreteResources { ...@@ -93,6 +93,11 @@ final class UnifiedDiscreteResources implements DiscreteResources {
93 .collect(Collectors.toCollection(LinkedHashSet::new)); 93 .collect(Collectors.toCollection(LinkedHashSet::new));
94 } 94 }
95 95
96 + public <T> Set<DiscreteResource> valuesOf(Class<T> cls) {
97 + return Stream.concat(encodables.valuesOf(cls).stream(), generics.valuesOf(cls).stream())
98 + .collect(Collectors.toCollection(LinkedHashSet::new));
99 + }
100 +
96 @Override 101 @Override
97 public int hashCode() { 102 public int hashCode() {
98 return Objects.hash(generics, encodables); 103 return Objects.hash(generics, encodables);
......