Bug 8503: Remove empty structures in DAO
[lispflowmapping.git] / mappingservice / mapcache / src / main / java / org / opendaylight / lispflowmapping / mapcache / MultiTableMapCache.java
index 59b4621d2abf18c49840d86d55d4ef626ea5226e..413b5eae6ba29bdd8c22d9ca4e0270f1ac1382d9 100644 (file)
@@ -33,23 +33,24 @@ public class MultiTableMapCache implements IMapCache {
         this.dao = dao;
     }
 
-    private ILispDAO getVniTable(Eid eid) {
-        long vni = 0;
+    private long getVni(Eid eid) {
         if (eid.getVirtualNetworkId() == null) {
-            vni = 0;
+            return 0;
         } else {
-            vni = eid.getVirtualNetworkId().getValue();
+            return eid.getVirtualNetworkId().getValue();
         }
-        return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
+    }
+
+    private ILispDAO getVniTable(Eid eid) {
+        return (ILispDAO) dao.getSpecific(getVni(eid), SubKeys.VNI);
+    }
+
+    private void removeVniTable(Eid eid) {
+        dao.removeSpecific(getVni(eid), SubKeys.VNI);
     }
 
     private ILispDAO getOrInstantiateVniTable(Eid eid) {
-        long vni = 0;
-        if (eid.getVirtualNetworkId() == null) {
-            vni = 0;
-        } else {
-            vni = eid.getVirtualNetworkId().getValue();
-        }
+        long vni = getVni(eid);
         ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
         if (table == null) {
             table = dao.putNestedTable(vni, SubKeys.VNI);
@@ -146,10 +147,16 @@ public class MultiTableMapCache implements IMapCache {
             ILispDAO db = getSDInnerDao(key, table);
             if (db != null) {
                 db.remove(SourceDestKeyHelper.getSrcBinary(key));
+                if (db.isEmpty()) {
+                    removeSDInnerDao(key, table);
+                }
             }
         } else {
             table.remove(key);
         }
+        if (table.isEmpty()) {
+            removeVniTable(eid);
+        }
     }
 
     // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src.
@@ -170,6 +177,10 @@ public class MultiTableMapCache implements IMapCache {
         return (ILispDAO) mappingsDb.getSpecific(SourceDestKeyHelper.getDstBinary(address), SubKeys.LCAF_SRCDST);
     }
 
+    private void removeSDInnerDao(Eid address, ILispDAO mappingsDb) {
+        mappingsDb.removeSpecific(SourceDestKeyHelper.getDstBinary(address), SubKeys.LCAF_SRCDST);
+    }
+
     @Override
     public void addData(Eid eid, String subKey, Object data) {
         Eid key = MaskUtil.normalize(eid);
@@ -210,10 +221,16 @@ public class MultiTableMapCache implements IMapCache {
             ILispDAO db = getSDInnerDao(key, table);
             if (db != null) {
                 db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key), subKey);
+                if (db.isEmpty()) {
+                    removeSDInnerDao(key, table);
+                }
             }
         } else {
             table.removeSpecific(key, subKey);
         }
+        if (table.isEmpty()) {
+            removeVniTable(eid);
+        }
     }
 
     @Override