[NO ISSUE][*DB][STO] Minor performance improvements

Change-Id: Ia95198caf8f8368e56411fc077bdf0684042bf8f
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/9584
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Michael Blow <mblow@apache.org>
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
Reviewed-by: Till Westmann <tillw@apache.org>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/DatasetResourceReference.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/DatasetResourceReference.java
index 6fd1c6a..297f08e 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/DatasetResourceReference.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/DatasetResourceReference.java
@@ -25,14 +25,19 @@
 import org.apache.asterix.common.utils.StorageConstants;
 import org.apache.hyracks.storage.common.LocalResource;
 
+@SuppressWarnings("squid:S2160") // don't override equals
 public class DatasetResourceReference extends ResourceReference {
 
-    private int datasetId;
-    private int partitionId;
-    private long resourceId;
+    private final int datasetId;
+    private final int partitionId;
+    private final long resourceId;
 
-    private DatasetResourceReference() {
-        super();
+    private DatasetResourceReference(LocalResource localResource) {
+        super(Paths.get(localResource.getPath(), StorageConstants.METADATA_FILE_NAME).toString());
+        final DatasetLocalResource dsResource = (DatasetLocalResource) localResource.getResource();
+        datasetId = dsResource.getDatasetId();
+        partitionId = dsResource.getPartition();
+        resourceId = localResource.getId();
     }
 
     public static DatasetResourceReference of(LocalResource localResource) {
@@ -53,39 +58,6 @@
     }
 
     private static DatasetResourceReference parse(LocalResource localResource) {
-        final DatasetResourceReference datasetResourceReference = new DatasetResourceReference();
-        final String filePath = Paths.get(localResource.getPath(), StorageConstants.METADATA_FILE_NAME).toString();
-        parse(datasetResourceReference, filePath);
-        assignIds(localResource, datasetResourceReference);
-        return datasetResourceReference;
-    }
-
-    private static void assignIds(LocalResource localResource, DatasetResourceReference lrr) {
-        final DatasetLocalResource dsResource = (DatasetLocalResource) localResource.getResource();
-        lrr.datasetId = dsResource.getDatasetId();
-        lrr.partitionId = dsResource.getPartition();
-        lrr.resourceId = localResource.getId();
-    }
-
-    @Override
-    public boolean equals(Object o) {
-        if (this == o) {
-            return true;
-        }
-        if (o instanceof ResourceReference) {
-            ResourceReference that = (ResourceReference) o;
-            return getRelativePath().toString().equals(that.getRelativePath().toString());
-        }
-        return false;
-    }
-
-    @Override
-    public int hashCode() {
-        return getRelativePath().toString().hashCode();
-    }
-
-    @Override
-    public String toString() {
-        return getRelativePath().toString();
+        return new DatasetResourceReference(localResource);
     }
 }
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/ResourceReference.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/ResourceReference.java
index c3b6229..7791926 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/ResourceReference.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/storage/ResourceReference.java
@@ -28,15 +28,29 @@
 
 public class ResourceReference {
 
-    protected String root;
-    protected String partition;
-    protected String dataverse; // == DataverseName.getCanonicalForm()
-    protected String dataset;
-    protected String rebalance;
-    protected String index;
-    protected String name;
+    protected final String root;
+    protected final String partition;
+    protected final String dataverse; // == DataverseName.getCanonicalForm()
+    protected final String dataset;
+    protected final String rebalance;
+    protected final String index;
+    protected final String name;
+    private volatile Path relativePath;
 
-    protected ResourceReference() {
+    protected ResourceReference(String path) {
+        // format: root/partition/dataverse/dataset/rebalanceCount/index/fileName
+        final String[] tokens = StringUtils.split(path, File.separatorChar);
+        if (tokens.length < 6) {
+            throw new IllegalStateException("Unrecognized path structure: " + path);
+        }
+        int offset = tokens.length;
+        name = tokens[--offset];
+        index = tokens[--offset];
+        rebalance = tokens[--offset];
+        dataset = tokens[--offset];
+        dataverse = tokens[--offset]; //TODO(MULTI_PART_DATAVERSE_NAME):REVISIT
+        partition = tokens[--offset];
+        root = tokens[--offset];
     }
 
     public static ResourceReference ofIndex(String indexPath) {
@@ -44,9 +58,7 @@
     }
 
     public static ResourceReference of(String localResourcePath) {
-        ResourceReference lrr = new ResourceReference();
-        parse(lrr, localResourcePath);
-        return lrr;
+        return new ResourceReference(localResourcePath);
     }
 
     public String getPartition() {
@@ -74,7 +86,10 @@
     }
 
     public Path getRelativePath() {
-        return Paths.get(root, partition, dataverse, dataset, rebalance, index);
+        if (relativePath == null) {
+            relativePath = Paths.get(root, partition, dataverse, dataset, rebalance, index);
+        }
+        return relativePath;
     }
 
     public ResourceReference getDatasetReference() {
@@ -86,22 +101,6 @@
         return Paths.get(root, partition, dataverse, dataset, rebalance, index, name);
     }
 
-    protected static void parse(ResourceReference ref, String path) {
-        // format: root/partition/dataverse/dataset/rebalanceCount/index/fileName
-        final String[] tokens = StringUtils.split(path, File.separatorChar);
-        if (tokens.length < 6) {
-            throw new IllegalStateException("Unrecognized path structure: " + path);
-        }
-        int offset = tokens.length;
-        ref.name = tokens[--offset];
-        ref.index = tokens[--offset];
-        ref.rebalance = tokens[--offset];
-        ref.dataset = tokens[--offset];
-        ref.dataverse = tokens[--offset]; //TODO(MULTI_PART_DATAVERSE_NAME):REVISIT
-        ref.partition = tokens[--offset];
-        ref.root = tokens[--offset];
-    }
-
     public int getPartitionNum() {
         return Integer.parseInt(partition.substring(StorageConstants.PARTITION_DIR_PREFIX.length()));
     }
@@ -113,14 +112,14 @@
         }
         if (o instanceof ResourceReference) {
             ResourceReference that = (ResourceReference) o;
-            return getRelativePath().toString().equals(that.getRelativePath().toString());
+            return getRelativePath().equals(that.getRelativePath());
         }
         return false;
     }
 
     @Override
     public int hashCode() {
-        return getRelativePath().toString().hashCode();
+        return getRelativePath().hashCode();
     }
 
     @Override