From 546efae9625889c67293a3759ccc5156df6ffd63 Mon Sep 17 00:00:00 2001
From: Harish Butani <hbutani@hortonworks.com>
Date: Tue, 23 Dec 2014 12:29:28 -0800
Subject: [PATCH] fix minor issues in Walker, Stores: get MemRepo:create to work

---
 typesystem/src/main/java/org/apache/hadoop/metadata/storage/Id.java                           |  2 +-
 typesystem/src/main/java/org/apache/hadoop/metadata/storage/MapIds.java                       |  2 +-
 typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/AttributeStores.java       | 52 +++++++++++++++++++++++++++-------------------------
 typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/HierarchicalTypeStore.java | 15 +++++++++------
 typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/MemRepository.java         |  8 ++++++++
 typesystem/src/main/java/org/apache/hadoop/metadata/types/ObjectGraphWalker.java              | 11 ++++++++---
 typesystem/src/test/java/org/apache/hadoop/metadata/StorageTest.java                          |  4 +++-
 7 files changed, 57 insertions(+), 37 deletions(-)

diff --git a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/Id.java b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/Id.java
index bb57b62..7b80324 100644
--- a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/Id.java
+++ b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/Id.java
@@ -42,7 +42,7 @@ public class Id implements ITypedReferenceableInstance {
     }
 
     public Id(String className) {
-        this(-System.currentTimeMillis(), 0, className);
+        this(-System.nanoTime(), 0, className);
     }
 
     public boolean isUnassigned() {
diff --git a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/MapIds.java b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/MapIds.java
index 652d889..c86e8a1 100644
--- a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/MapIds.java
+++ b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/MapIds.java
@@ -56,7 +56,7 @@ public class MapIds implements ObjectGraphWalker.NodeProcessor {
         } else if ( nd.aInfo.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS ) {
             if ( nd.value != null && nd.value instanceof  IReferenceableInstance ) {
                 Id oldId = ((IReferenceableInstance)nd.value).getId();
-                Id newId = idToNewIdMap.get(ref.getId());
+                Id newId = idToNewIdMap.get(oldId);
                 /*
                  * Replace Instances with Ids, irrespective of whether they map to newIds or not.
                  */
diff --git a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/AttributeStores.java b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/AttributeStores.java
index 7ed702c..1d00554 100644
--- a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/AttributeStores.java
+++ b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/AttributeStores.java
@@ -59,23 +59,23 @@ public class AttributeStores {
                 } else if ( i.dataType() == DataTypes.SHORT_TYPE ) {
                     new ShortAttributeStore(i);
                 } else if ( i.dataType() == DataTypes.INT_TYPE ) {
-                    new IntAttributeStore(i);
+                    return new IntAttributeStore(i);
                 } else if ( i.dataType() == DataTypes.LONG_TYPE ) {
-                    new LongAttributeStore(i);
+                    return new LongAttributeStore(i);
                 } else if ( i.dataType() == DataTypes.FLOAT_TYPE ) {
-                    new FloatAttributeStore(i);
+                    return new FloatAttributeStore(i);
                 } else if ( i.dataType() == DataTypes.DOUBLE_TYPE ) {
-                    new DoubleAttributeStore(i);
+                    return new DoubleAttributeStore(i);
                 } else if ( i.dataType() == DataTypes.BIGINTEGER_TYPE ) {
-                    new BigIntStore(i);
+                    return new BigIntStore(i);
                 } else if ( i.dataType() == DataTypes.BIGDECIMAL_TYPE ) {
-                    new BigDecimalStore(i);
+                    return new BigDecimalStore(i);
                 } else if ( i.dataType() == DataTypes.DATE_TYPE ) {
-                    new DateStore(i);
+                    return new DateStore(i);
                 } else if ( i.dataType() == DataTypes.STRING_TYPE ) {
-                    new StringStore(i);
+                    return new StringStore(i);
                 }  else if ( i.dataType() == DataTypes.STRING_TYPE ) {
-                    new StringStore(i);
+                    return new StringStore(i);
                 } else {
                     throw new RepositoryException(String.format("Unknown datatype %s", i.dataType()));
                 }
@@ -237,8 +237,8 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            list.size(pos+1);
+            nullList.size(pos+1);
         }
     }
 
@@ -269,8 +269,8 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            list.size(pos+1);
+            nullList.size(pos+1);
         }
     }
 
@@ -301,8 +301,8 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            list.size(pos+1);
+            nullList.size(pos+1);
         }
     }
 
@@ -333,8 +333,8 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            list.size(pos+1);
+            nullList.size(pos+1);
         }
     }
 
@@ -365,8 +365,8 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            list.size(pos+1);
+            nullList.size(pos+1);
         }
     }
 
@@ -397,8 +397,8 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            list.size(pos+1);
+            nullList.size(pos+1);
         }
     }
 
@@ -429,8 +429,8 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            list.size(pos+1);
+            nullList.size(pos+1);
         }
     }
 
@@ -445,8 +445,10 @@ public class AttributeStores {
 
         @Override
         public void ensureCapacity(int pos) throws RepositoryException {
-            list.ensureCapacity(pos);
-            nullList.ensureCapacity(pos);
+            while (list.size() < pos + 1) {
+                list.add((T)null);
+            }
+            nullList.size(pos+1);
         }
     }
 
diff --git a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/HierarchicalTypeStore.java b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/HierarchicalTypeStore.java
index 9205398..07c3a04 100644
--- a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/HierarchicalTypeStore.java
+++ b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/HierarchicalTypeStore.java
@@ -31,10 +31,7 @@ import org.apache.hadoop.metadata.types.AttributeInfo;
 import org.apache.hadoop.metadata.types.HierarchicalType;
 import org.apache.hadoop.metadata.types.IConstructableType;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 public class HierarchicalTypeStore {
@@ -80,6 +77,10 @@ public class HierarchicalTypeStore {
         superTypeStores = b1.build();
 
         nextPos = 0;
+        idPosMap = new HashMap<Id, Integer>();
+        freePositions = new ArrayList<Integer>();
+
+        lock = new ReentrantReadWriteLock();
     }
 
     /**
@@ -141,7 +142,7 @@ public class HierarchicalTypeStore {
     }
 
     void releaseWriteLock() {
-        lock.readLock().unlock();
+        lock.writeLock().unlock();
     }
 
     /**
@@ -183,7 +184,9 @@ public class HierarchicalTypeStore {
     }
 
     public void ensureCapacity(int pos) throws RepositoryException {
-        typeNameList.ensureCapacity(pos);
+        while (typeNameList.size() < pos + 1) {
+            typeNameList.add(null);
+        }
         for(Map.Entry<AttributeInfo, IAttributeStore> e : attrStores.entrySet()) {
             IAttributeStore attributeStore = e.getValue();
             attributeStore.ensureCapacity(pos);
diff --git a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/MemRepository.java b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/MemRepository.java
index abb6011..f3461dc 100644
--- a/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/MemRepository.java
+++ b/typesystem/src/main/java/org/apache/hadoop/metadata/storage/memory/MemRepository.java
@@ -176,11 +176,19 @@ public class MemRepository implements IRepository {
             for (ITypedReferenceableInstance instance : newInstances) {
                 HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
                 st.assignPosition(instance.getId());
+                for(String traitName : instance.getTraits()) {
+                    HierarchicalTypeStore tt = typeStores.get(traitName);
+                    tt.assignPosition(instance.getId());
+                }
             }
 
             for (ITypedReferenceableInstance instance : newInstances) {
                 HierarchicalTypeStore st = typeStores.get(instance.getTypeName());
                 st.store((ReferenceableInstance)instance);
+                for(String traitName : instance.getTraits()) {
+                    HierarchicalTypeStore tt = typeStores.get(traitName);
+                    // ??
+                }
             }
         } catch(RepositoryException re) {
             for (ITypedReferenceableInstance instance : newInstances) {
diff --git a/typesystem/src/main/java/org/apache/hadoop/metadata/types/ObjectGraphWalker.java b/typesystem/src/main/java/org/apache/hadoop/metadata/types/ObjectGraphWalker.java
index 71cd4b9..00cb98a 100644
--- a/typesystem/src/main/java/org/apache/hadoop/metadata/types/ObjectGraphWalker.java
+++ b/typesystem/src/main/java/org/apache/hadoop/metadata/types/ObjectGraphWalker.java
@@ -32,7 +32,7 @@ public class ObjectGraphWalker {
         this.nodeProcessor = nodeProcessor;
         queue = new LinkedList<IReferenceableInstance>();
         processedIds = new HashSet<Id>();
-        queue.add(start);
+        visitReferenceableInstance(start);
     }
 
     public ObjectGraphWalker(TypeSystem typeSystem, NodeProcessor nodeProcessor,
@@ -42,7 +42,9 @@ public class ObjectGraphWalker {
         this.nodeProcessor = nodeProcessor;
         queue = new LinkedList<IReferenceableInstance>();
         processedIds = new HashSet<Id>();
-        queue.addAll(roots);
+        for(IReferenceableInstance r : roots) {
+            visitReferenceableInstance(r);
+        }
     }
 
     public void walk() throws MetadataException {
@@ -145,7 +147,10 @@ public class ObjectGraphWalker {
         IReferenceableInstance ref = (IReferenceableInstance) val;
 
         if (!processedIds.contains(ref.getId())) {
-            queue.add(ref);
+            processedIds.add(ref.getId());
+            if ( !(ref instanceof  Id) ) {
+                queue.add(ref);
+            }
         }
     }
 
diff --git a/typesystem/src/test/java/org/apache/hadoop/metadata/StorageTest.java b/typesystem/src/test/java/org/apache/hadoop/metadata/StorageTest.java
index f193baf..9a91c3e 100644
--- a/typesystem/src/test/java/org/apache/hadoop/metadata/StorageTest.java
+++ b/typesystem/src/test/java/org/apache/hadoop/metadata/StorageTest.java
@@ -21,7 +21,9 @@ public class StorageTest extends BaseTest {
 
         Referenceable hrDept = createDeptEg1(ts);
 
-        //ITypedReferenceableInstance hrDept2 = ms.getRepository().create(hrDept);
+        ITypedReferenceableInstance hrDept2 = ms.getRepository().create(hrDept);
+
+        //System.out.println(hrDept2);
 
     }
 
--
libgit2 0.27.1