Speed up time computation 77/83177/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 19 Jul 2019 13:52:48 +0000 (15:52 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 19 Jul 2019 13:54:04 +0000 (15:54 +0200)
When we are adding or refreshing a mapping, we can first access
the existing timestamp and only fallback to System.currentTimeMillis()
if it's not present.

This also simplifies things, as we end up reading the timestamp
(a synchronized operation) only once.

Change-Id: I39bfe212e7c746e66dc144ad1e036a2b02544c6c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/timebucket/implementation/TimeBucketMappingTimeoutService.java

index fbd9e4638fd150b688456f5149e859085979c189..27c420dd368d1ad1f11d1d807a9b07861a09b034 100644 (file)
@@ -8,20 +8,17 @@
 
 package org.opendaylight.lispflowmapping.implementation.timebucket.implementation;
 
+import java.util.Date;
 import org.opendaylight.lispflowmapping.implementation.MappingSystem;
 import org.opendaylight.lispflowmapping.implementation.timebucket.containers.TimeBucketWheel;
 import org.opendaylight.lispflowmapping.implementation.timebucket.interfaces.ISouthBoundMappingTimeoutService;
 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Created by Shakib Ahmed on 12/1/16.
  */
 public class TimeBucketMappingTimeoutService implements ISouthBoundMappingTimeoutService {
-    private static final Logger LOG = LoggerFactory.getLogger(TimeBucketWheel.class);
-
     private final TimeBucketWheel timeBucketWheel;
 
     public TimeBucketMappingTimeoutService(int numberOfBucket, long mappingRecordValidityInMillis,
@@ -31,20 +28,12 @@ public class TimeBucketMappingTimeoutService implements ISouthBoundMappingTimeou
 
     @Override
     public int addMapping(Eid key, MappingData mappingData) {
-        long timestamp = System.currentTimeMillis();
-        if (mappingData.getTimestamp() != null) {
-            timestamp = mappingData.getTimestamp().getTime();
-        }
-        return timeBucketWheel.add(key, mappingData, timestamp);
+        return timeBucketWheel.add(key, mappingData, getTimestamp(mappingData));
     }
 
     @Override
     public int refreshMapping(Eid key, MappingData newMappingData, int presentBucketId) {
-        long timestamp = System.currentTimeMillis();
-        if (newMappingData.getTimestamp() != null) {
-            timestamp = newMappingData.getTimestamp().getTime();
-        }
-        return timeBucketWheel.refreshMappping(key, newMappingData, timestamp, presentBucketId);
+        return timeBucketWheel.refreshMappping(key, newMappingData, getTimestamp(newMappingData), presentBucketId);
     }
 
     @Override
@@ -56,4 +45,9 @@ public class TimeBucketMappingTimeoutService implements ISouthBoundMappingTimeou
     public void removeExpiredMappings() {
         timeBucketWheel.clearExpiredMappingAndRotate();
     }
+
+    private static long getTimestamp(MappingData mappingData) {
+        final Date stamp = mappingData.getTimestamp();
+        return stamp != null ? stamp.getTime() : System.currentTimeMillis();
+    }
 }