Program: 171158
Project: 171158
Buggy source:
/** 
 * 1. traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id], also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance] - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType - traverse Structs - traverse Traits. 1b. Ensure that every newId has an associated Instance. 2. Traverse oldIdToInstance map create newInstances : List[ITypedReferenceableInstance] - create a ITypedReferenceableInstance. replace any old References ( ids or object references) with new Ids. 3. Traverse over newInstances - ask ClassStore to assign a position to the Id. - for Instances with Traits, assign a position for each Trait - invoke store on the nwInstance. Recovery: - on each newInstance, invoke releaseId and delete on its ClassStore and Traits' Stores.
 * @param i
 * @return
 * @throws org.apache.hadoop.metadata.storage.RepositoryException
 */
public ITypedReferenceableInstance create(IReferenceableInstance i) throws RepositoryException {
  DiscoverInstances discoverInstances=new DiscoverInstances(this);
  try {
    new ObjectGraphWalker(typeSystem,discoverInstances,i).walk();
  }
 catch (  MetadataException me) {
    throw new RepositoryException("String_Node_Str",me);
  }
  for (  Id oldId : discoverInstances.idToNewIdMap.keySet()) {
    if (!discoverInstances.idToInstanceMap.containsKey(oldId)) {
      throw new RepositoryException(String.format("String_Node_Str" + "String_Node_Str",oldId));
    }
  }
  List<ITypedReferenceableInstance> newInstances=new ArrayList<ITypedReferenceableInstance>();
  ITypedReferenceableInstance retInstance=null;
  Set<ClassType> classTypes=new TreeSet<ClassType>();
  Set<TraitType> traitTypes=new TreeSet<TraitType>();
  for (  IReferenceableInstance transientInstance : discoverInstances.idToInstanceMap.values()) {
    try {
      ClassType cT=typeSystem.getDataType(ClassType.class,transientInstance.getTypeName());
      ITypedReferenceableInstance newInstance=cT.convert(transientInstance,Multiplicity.REQUIRED);
      newInstances.add(newInstance);
      classTypes.add(cT);
      for (      String traitName : newInstance.getTraits()) {
        TraitType tT=typeSystem.getDataType(TraitType.class,traitName);
        traitTypes.add(tT);
      }
      if (newInstance.getId() == i.getId()) {
        retInstance=newInstance;
      }
      MapIds mapIds=new MapIds(this,discoverInstances.idToNewIdMap);
      new ObjectGraphWalker(typeSystem,mapIds,newInstances).walk();
    }
 catch (    MetadataException me) {
      throw new RepositoryException(String.format("String_Node_Str",transientInstance.getId()),me);
    }
  }
  for (  ClassType cT : classTypes) {
    HierarchicalTypeStore st=typeStores.get(cT.getName());
    st.acquireWriteLock();
  }
  for (  TraitType tT : traitTypes) {
    HierarchicalTypeStore st=typeStores.get(tT.getName());
    st.acquireWriteLock();
  }
  try {
    for (    ITypedReferenceableInstance instance : newInstances) {
      HierarchicalTypeStore st=typeStores.get(instance.getTypeName());
      st.assignPosition(instance.getId());
    }
    for (    ITypedReferenceableInstance instance : newInstances) {
      HierarchicalTypeStore st=typeStores.get(instance.getTypeName());
      st.store((ReferenceableInstance)instance);
    }
  }
 catch (  RepositoryException re) {
    for (    ITypedReferenceableInstance instance : newInstances) {
      HierarchicalTypeStore st=typeStores.get(instance.getTypeName());
      st.releaseId(instance.getId());
    }
    throw re;
  }
 finally {
    for (    ClassType cT : classTypes) {
      HierarchicalTypeStore st=typeStores.get(cT.getName());
      st.releaseWriteLock();
    }
    for (    TraitType tT : traitTypes) {
      HierarchicalTypeStore st=typeStores.get(tT.getName());
      st.releaseWriteLock();
    }
  }
  return retInstance;
}

Fixed version (ground truth):
/** 
 * 1. traverse the Object Graph from  i and create idToNewIdMap : Map[Id, Id], also create old Id to Instance Map: oldIdToInstance : Map[Id, IInstance] - traverse reference Attributes, List[ClassType], Maps where Key/value is ClassType - traverse Structs - traverse Traits. 1b. Ensure that every newId has an associated Instance. 2. Traverse oldIdToInstance map create newInstances : List[ITypedReferenceableInstance] - create a ITypedReferenceableInstance. replace any old References ( ids or object references) with new Ids. 3. Traverse over newInstances - ask ClassStore to assign a position to the Id. - for Instances with Traits, assign a position for each Trait - invoke store on the nwInstance. Recovery: - on each newInstance, invoke releaseId and delete on its ClassStore and Traits' Stores.
 * @param i
 * @return
 * @throws org.apache.hadoop.metadata.storage.RepositoryException
 */
public ITypedReferenceableInstance create(IReferenceableInstance i) throws RepositoryException {
  DiscoverInstances discoverInstances=new DiscoverInstances(this);
  try {
    new ObjectGraphWalker(typeSystem,discoverInstances,i).walk();
  }
 catch (  MetadataException me) {
    throw new RepositoryException("String_Node_Str",me);
  }
  for (  Id oldId : discoverInstances.idToNewIdMap.keySet()) {
    if (!discoverInstances.idToInstanceMap.containsKey(oldId)) {
      throw new RepositoryException(String.format("String_Node_Str" + "String_Node_Str",oldId));
    }
  }
  List<ITypedReferenceableInstance> newInstances=new ArrayList<ITypedReferenceableInstance>();
  ITypedReferenceableInstance retInstance=null;
  Set<ClassType> classTypes=new TreeSet<ClassType>();
  Set<TraitType> traitTypes=new TreeSet<TraitType>();
  for (  IReferenceableInstance transientInstance : discoverInstances.idToInstanceMap.values()) {
    try {
      ClassType cT=typeSystem.getDataType(ClassType.class,transientInstance.getTypeName());
      ITypedReferenceableInstance newInstance=cT.convert(transientInstance,Multiplicity.REQUIRED);
      newInstances.add(newInstance);
      classTypes.add(cT);
      for (      String traitName : newInstance.getTraits()) {
        TraitType tT=typeSystem.getDataType(TraitType.class,traitName);
        traitTypes.add(tT);
      }
      if (newInstance.getId() == i.getId()) {
        retInstance=newInstance;
      }
      MapIds mapIds=new MapIds(this,discoverInstances.idToNewIdMap);
      new ObjectGraphWalker(typeSystem,mapIds,newInstances).walk();
    }
 catch (    MetadataException me) {
      throw new RepositoryException(String.format("String_Node_Str",transientInstance.getId()),me);
    }
  }
  for (  ClassType cT : classTypes) {
    HierarchicalTypeStore st=typeStores.get(cT.getName());
    st.acquireWriteLock();
  }
  for (  TraitType tT : traitTypes) {
    HierarchicalTypeStore st=typeStores.get(tT.getName());
    st.acquireWriteLock();
  }
  try {
    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) {
      HierarchicalTypeStore st=typeStores.get(instance.getTypeName());
      st.releaseId(instance.getId());
    }
    throw re;
  }
 finally {
    for (    ClassType cT : classTypes) {
      HierarchicalTypeStore st=typeStores.get(cT.getName());
      st.releaseWriteLock();
    }
    for (    TraitType tT : traitTypes) {
      HierarchicalTypeStore st=typeStores.get(tT.getName());
      st.releaseWriteLock();
    }
  }
  return retInstance;
}