Reduce the memory usage by RangeSet in backing data structure

- Typically resource query is more frequent than resource registration.
  This patch optimizes memory usage for resource query rather than registration

Change-Id: Ic680c09c83b4299e80d4493d01e642081549fa24
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java
index 7eaedc1..7d0ad68 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java
@@ -117,7 +117,7 @@
     @Override
     public Set<DiscreteResource> values() {
         return values.values().stream()
-                .flatMap(x -> x.resources(parent.id()).stream())
+                .flatMap(x -> x.values(parent.id()).stream())
                 .collect(Collectors.toCollection(LinkedHashSet::new));
     }
 
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResourcesSerializer.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResourcesSerializer.java
index 6f65392..8c39e31 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResourcesSerializer.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResourcesSerializer.java
@@ -43,7 +43,7 @@
 
         return EncodableDiscreteResources.of(parent,
                 resources.stream()
-                        .flatMap(x -> x.resources(parent.id()).stream())
+                        .flatMap(x -> x.values(parent.id()).stream())
                         .collect(Collectors.toCollection(LinkedHashSet::new)));
     }
 }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
index c79b23a..a4818e2 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
@@ -15,6 +15,10 @@
  */
 package org.onosproject.store.resource.impl;
 
+import com.google.common.collect.DiscreteDomain;
+import com.google.common.collect.Range;
+import com.google.common.collect.RangeSet;
+import com.google.common.collect.TreeRangeSet;
 import org.onlab.util.Tools;
 import org.onosproject.net.resource.DiscreteResource;
 import org.onosproject.net.resource.DiscreteResourceCodec;
@@ -24,39 +28,45 @@
 import java.util.LinkedHashSet;
 import java.util.Set;
 import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 
 /**
  * Represents discrete resources encoded by a codec.
  */
 final class EncodedDiscreteResources {
-    private final Set<Integer> rawValues;
+    private final RangeSet<Integer> rangeSet;
     private final DiscreteResourceCodec codec;
 
-    EncodedDiscreteResources(Set<Integer> rawValues, DiscreteResourceCodec codec) {
-        this.rawValues = rawValues;
+    EncodedDiscreteResources(RangeSet<Integer> rangeSet, DiscreteResourceCodec codec) {
+        this.rangeSet = rangeSet;
         this.codec = codec;
     }
 
     static EncodedDiscreteResources of(Set<DiscreteResource> resources, DiscreteResourceCodec codec) {
-        Set<Integer> rawValues = resources.stream()
+        RangeSet<Integer> rangeSet = TreeRangeSet.create();
+        resources.stream()
                 .map(x -> x.valueAs(Object.class))
                 .flatMap(Tools::stream)
                 .map(x -> codec.encode(x))
-                .collect(Collectors.toCollection(LinkedHashSet::new));
+                .map(Range::singleton)
+                .map(x -> x.canonical(DiscreteDomain.integers()))
+                .forEach(rangeSet::add);
 
-        return new EncodedDiscreteResources(rawValues, codec);
+        return new EncodedDiscreteResources(rangeSet, codec);
     }
 
-    Set<Integer> rawValues() {
-        return rawValues;
+    RangeSet<Integer> rangeSet() {
+        return rangeSet;
     }
 
     DiscreteResourceCodec codec() {
         return codec;
     }
 
-    Set<DiscreteResource> resources(DiscreteResourceId parent) {
-        return rawValues.stream()
+    Set<DiscreteResource> values(DiscreteResourceId parent) {
+        return rangeSet.asRanges().stream()
+                .flatMapToInt(x1 -> IntStream.range(x1.lowerEndpoint(), x1.upperEndpoint()))
+                .boxed()
                 .map(x -> codec.decode(x))
                 .map(x -> Resources.discrete(parent, x).resource())
                 .collect(Collectors.toCollection(LinkedHashSet::new));
@@ -64,10 +74,10 @@
 
     @SuppressWarnings("unchecked")
     boolean contains(DiscreteResource resource) {
-        return rawValues.contains(codec.encode(resource));
+        return rangeSet.contains(codec.encode(resource));
     }
 
     boolean isEmpty() {
-        return rawValues.isEmpty();
+        return rangeSet.isEmpty();
     }
 }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedResourcesSerializer.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedResourcesSerializer.java
index 587fb4c..61de641 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedResourcesSerializer.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedResourcesSerializer.java
@@ -19,18 +19,15 @@
 import com.esotericsoftware.kryo.Serializer;
 import com.esotericsoftware.kryo.io.Input;
 import com.esotericsoftware.kryo.io.Output;
-import com.google.common.collect.DiscreteDomain;
 import com.google.common.collect.Range;
+import com.google.common.collect.RangeSet;
 import com.google.common.collect.TreeRangeSet;
 import org.onlab.util.ClosedOpenRange;
 import org.onosproject.net.resource.DiscreteResourceCodec;
 
 import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.stream.Collectors;
-import java.util.stream.IntStream;
 
 /**
  * Kryo Serializer for {@link EncodedDiscreteResources}.
@@ -38,12 +35,7 @@
 final class EncodedResourcesSerializer extends Serializer<EncodedDiscreteResources> {
     @Override
     public void write(Kryo kryo, Output output, EncodedDiscreteResources object) {
-        TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
-        object.rawValues().stream()
-                .map(Range::singleton)
-                .map(x -> x.canonical(DiscreteDomain.integers()))
-                .forEach(rangeSet::add);
-        List<ClosedOpenRange> ranges = rangeSet.asRanges().stream()
+        List<ClosedOpenRange> ranges = object.rangeSet().asRanges().stream()
                 .map(ClosedOpenRange::of)
                 .collect(Collectors.toList());
         kryo.writeObject(output, ranges);
@@ -56,10 +48,10 @@
         List<ClosedOpenRange> ranges = kryo.readObject(input, ArrayList.class);
         DiscreteResourceCodec codec = (DiscreteResourceCodec) kryo.readClassAndObject(input);
 
-        HashSet<Integer> rawValues = ranges.stream()
-                .flatMapToInt(x -> IntStream.range(x.lowerBound(), x.upperBound()))
-                .boxed()
-                .collect(Collectors.toCollection(LinkedHashSet::new));
-        return new EncodedDiscreteResources(rawValues, codec);
+        RangeSet<Integer> rangeSet = TreeRangeSet.create();
+        ranges.stream()
+                .map(x -> Range.closedOpen(x.lowerBound(), x.upperBound()))
+                .forEach(rangeSet::add);
+        return new EncodedDiscreteResources(rangeSet, codec);
     }
 }