db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/odmg ObjectEnvelopeOrdering.java
Date Sat, 04 Jun 2005 15:03:35 GMT
arminw      2005/06/04 08:03:35

  Modified:    src/java/org/apache/ojb/broker/metadata Tag: OJB_1_0_RELEASE
                        ObjectReferenceDescriptor.java
               src/java/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
                        ObjectEnvelopeOrdering.java
  Log:
  add first attempt to take care of database FK settings. With a custom attribute in the object-reference
descriptor it's possible to specify a FK setting:
  
  <reference-descriptor name="detail"
    class-ref="org.apache.ojb.odmg.CircularTest$ShopDetail"
    proxy="false"
    auto-retrieve="true"
    auto-update="none"
    auto-delete="none"
  >
     <foreignkey field-ref="detailFk"/>
     <attribute attribute-name="fk" attribute-value="true"/>
  </reference-descriptor>
  This can help to prevent FK constraint exceptions when using the odmg-api.
  This may change in future.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.37.2.3  +15 -0     db-ojb/src/java/org/apache/ojb/broker/metadata/ObjectReferenceDescriptor.java
  
  Index: ObjectReferenceDescriptor.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/metadata/ObjectReferenceDescriptor.java,v
  retrieving revision 1.37.2.2
  retrieving revision 1.37.2.3
  diff -u -r1.37.2.2 -r1.37.2.3
  --- ObjectReferenceDescriptor.java	22 Apr 2005 17:08:13 -0000	1.37.2.2
  +++ ObjectReferenceDescriptor.java	4 Jun 2005 15:03:35 -0000	1.37.2.3
  @@ -521,6 +521,21 @@
           return false;
       }
   
  +    /**
  +     * Returns <em>true</em> if a foreign key to the referenced object is
  +     * declared, else <em>false</em> is returned.
  +     */
  +    public boolean hasForeignKey()
  +    {
  +        /*
  +        arminw: Currently we don't have a ForeignKey descriptor object and
  +        a official xml-element to support FK settings. As a workaround I introduce
  +        a custom-attribute to handle FK settings in collection-/reference-decriptor
  +        */
  +        String result = getAttribute("fk");
  +        return result != null && result.equalsIgnoreCase("true");
  +    }
  +
       public String toString()
       {
           return new ToStringBuilder(this)
  
  
  
  No                   revision
  No                   revision
  1.1.2.6   +42 -33    db-ojb/src/java/org/apache/ojb/odmg/Attic/ObjectEnvelopeOrdering.java
  
  Index: ObjectEnvelopeOrdering.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/Attic/ObjectEnvelopeOrdering.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- ObjectEnvelopeOrdering.java	10 May 2005 19:08:54 -0000	1.1.2.5
  +++ ObjectEnvelopeOrdering.java	4 Jun 2005 15:03:35 -0000	1.1.2.6
  @@ -16,7 +16,6 @@
    */
   
   import java.util.ArrayList;
  -import java.util.HashMap;
   import java.util.Iterator;
   import java.util.List;
   import java.util.Map;
  @@ -78,8 +77,10 @@
    */
   class ObjectEnvelopeOrdering
   {
  -    private static final int CONCRETE_EDGE_WEIGHT = 2;
  +    private static final int CONCRETE_EDGE_WEIGHT = 3;
  +    private static final int CONCRETE_EDGE_WEIGHT_WITH_FK = 4;
       private static final int POTENTIAL_EDGE_WEIGHT = 1;
  +    private static final int POTENTIAL_EDGE_WEIGHT_WITH_FK = 2;
       private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
   
       private static Logger log = LoggerFactory.getLogger(ObjectEnvelopeOrdering.class);
  @@ -88,7 +89,7 @@
       private Map envelopes;
   
       private Vertex[] vertices;
  -    private Map edgeMap;
  +    private List edgeList;
   
       private int newOrderIndex;
       private Identity[] newOrder;
  @@ -132,6 +133,10 @@
               {
                   Vertex vertex = new Vertex(envelope);
                   vertexList.add(vertex);
  +                if (log.isDebugEnabled())
  +                {
  +                    log.debug("Add new Vertex object "+envelope.getIdentity()+" to VertexList");
  +                }
               }
               else
               {
  @@ -139,14 +144,14 @@
                   newOrder[newOrderIndex++] = envelope.getIdentity();
                   if (log.isDebugEnabled())
                   {
  -                    log.debug("add unmodified object, newOrderList: " + ArrayUtils.toString(newOrder));
  +                    log.debug("Add unmodified object "+envelope.getIdentity()+" to new
OrderList");
                   }
               }
           }
           vertices = (Vertex[]) vertexList.toArray(new Vertex[vertexList.size()]);
   
           // set up the edges
  -        edgeMap = new HashMap(2 * vertices.length, 0.75f);
  +        edgeList = new ArrayList(2 * vertices.length);
           for (int i = 0; i < vertices.length; i++)
           {
               addEdgesForVertex(vertices[i]);
  @@ -156,7 +161,7 @@
           {
               t2 = System.currentTimeMillis();
               log.debug("Building object envelope graph took " + (t2 - t1) + " ms");
  -            log.debug("Object envelope graph contains " + vertices.length + " vertices"
+ " and " + edgeMap.size()
  +            log.debug("Object envelope graph contains " + vertices.length + " vertices"
+ " and " + edgeList.size()
                       + " edges");
           }
   
  @@ -168,11 +173,16 @@
               iterationCount++;
   
               // update incoming edge counts
  -            for (Iterator it = edgeMap.values().iterator(); it.hasNext();)
  +            for (Iterator it = edgeList.iterator(); it.hasNext();)
               {
                   Edge edge = (Edge) it.next();
                   if (!edge.isProcessed())
                   {
  +                    if(log.isDebugEnabled())
  +                    {
  +                        final String msg = "Add weight '"+edge.getWeight()+"' for terminal
vertex " + edge.getTerminalVertex() + " of edge " + edge;
  +                        log.debug(msg);
  +                    }
                       edge.getTerminalVertex().incrementIncomingEdgeWeight(edge.getWeight());
                   }
               }
  @@ -245,7 +255,7 @@
   
       /**
        * Adds all edges for a given object envelope vertex. All edges are
  -     * added to the edgeMap map.
  +     * added to the edgeList map.
        * @param vertex the Vertex object to find edges for
        */
       private void addEdgesForVertex(Vertex vertex)
  @@ -273,7 +283,7 @@
        */
       private void addObjectReferenceEdges(Vertex vertex, ObjectReferenceDescriptor rds)
       {
  -        Object refObject = rds.getPersistentField().get(vertex.getEnvelope().getObject());
  +        Object refObject = rds.getPersistentField().get(vertex.getEnvelope().getRealObject());
           Class refClass = rds.getItemClass();
           for (int i = 0; i < vertices.length; i++)
           {
  @@ -281,24 +291,23 @@
               // ObjectEnvelope envelope = vertex.getEnvelope();
               Vertex refVertex = vertices[i];
               ObjectEnvelope refEnvelope = refVertex.getEnvelope();
  -            if (refObject == refEnvelope.getObject())
  +            if (refObject == refEnvelope.getRealObject())
               {
  -                edge = buildConcrete11Edge(vertex, refVertex);
  +                edge = buildConcrete11Edge(vertex, refVertex, rds.hasForeignKey());
               }
  -            else if (refClass.isInstance(refVertex.getEnvelope().getObject()))
  +            else if (refClass.isInstance(refVertex.getEnvelope().getRealObject()))
               {
  -                edge = buildPotential11Edge(vertex, refVertex);
  +                edge = buildPotential11Edge(vertex, refVertex, rds.hasForeignKey());
               }
               if (edge != null)
               {
  -                Edge existingEdge = (Edge) edgeMap.get(edge);
  -                if (existingEdge == null)
  +                if (!edgeList.contains(edge))
                   {
  -                    edgeMap.put(edge, edge);
  +                    edgeList.add(edge);
                   }
                   else
                   {
  -                    existingEdge.increaseWeightTo(edge.getWeight());
  +                    edge.increaseWeightTo(edge.getWeight());
                   }
               }
           }
  @@ -313,7 +322,7 @@
       private void addCollectionEdges(Vertex vertex, CollectionDescriptor cds)
       {
           ObjectEnvelope envelope = vertex.getEnvelope();
  -        Object col = cds.getPersistentField().get(envelope.getObject());
  +        Object col = cds.getPersistentField().get(envelope.getRealObject());
           Object[] refObjects;
           if (col == null || (ProxyHelper.isCollectionProxy(col) && !ProxyHelper.getCollectionProxy(col).isLoaded()))
           {
  @@ -331,9 +340,9 @@
               Vertex refVertex = vertices[i];
               ObjectEnvelope refEnvelope = refVertex.getEnvelope();
   
  -            if (refClass.isInstance(refEnvelope.getObject()))
  +            if (refClass.isInstance(refEnvelope.getRealObject()))
               {
  -                if (containsObject(refEnvelope.getObject(), refObjects))
  +                if (containsObject(refEnvelope.getRealObject(), refObjects))
                   {
                       if (cds.isMtoNRelation())
                       {
  @@ -358,14 +367,13 @@
               }
               if (edge != null)
               {
  -                Edge existingEdge = (Edge) edgeMap.get(edge);
  -                if (existingEdge == null)
  +                if (!edgeList.contains(edge))
                   {
  -                    edgeMap.put(edge, edge);
  +                    edgeList.add(edge);
                   }
                   else
                   {
  -                    existingEdge.increaseWeightTo(edge.getWeight());
  +                    edge.increaseWeightTo(edge.getWeight());
                   }
               }
           }
  @@ -413,7 +421,7 @@
        * @return an Edge object or null if the two database operations can
        *      be performed in any order
        */
  -    protected Edge buildConcrete11Edge(Vertex vertex1, Vertex vertex2)
  +    protected Edge buildConcrete11Edge(Vertex vertex1, Vertex vertex2, boolean fkToRef)
       {
           ModificationState state1 = vertex1.getEnvelope().getModificationState();
           ModificationState state2 = vertex2.getEnvelope().getModificationState();
  @@ -422,7 +430,7 @@
               if (state2.needsInsert())
               {
                   // (2) must be inserted before (1) can point to it
  -                return new Edge(vertex2, vertex1, CONCRETE_EDGE_WEIGHT);
  +                return new Edge(vertex2, vertex1, fkToRef ? CONCRETE_EDGE_WEIGHT_WITH_FK
: CONCRETE_EDGE_WEIGHT);
               }
           }
           else if (state1.needsDelete())
  @@ -430,7 +438,7 @@
               if (state2.needsDelete())
               {
                   // (1) points to (2) and must be deleted first 
  -                return new Edge(vertex1, vertex2, CONCRETE_EDGE_WEIGHT);
  +                return new Edge(vertex1, vertex2, fkToRef ? CONCRETE_EDGE_WEIGHT_WITH_FK
: CONCRETE_EDGE_WEIGHT);
               }
           }
           return null;
  @@ -462,7 +470,7 @@
        * @return an Edge object or null if the two database operations can
        *      be performed in any order
        */
  -    protected Edge buildPotential11Edge(Vertex vertex1, Vertex vertex2)
  +    protected Edge buildPotential11Edge(Vertex vertex1, Vertex vertex2, boolean fkToRef)
       {
           ModificationState state1 = vertex1.getEnvelope().getModificationState();
           ModificationState state2 = vertex2.getEnvelope().getModificationState();
  @@ -471,7 +479,7 @@
               if (state2.needsDelete())
               {
                   // old version of (1) might point to (2)
  -                return new Edge(vertex1, vertex2, POTENTIAL_EDGE_WEIGHT);
  +                return new Edge(vertex1, vertex2, fkToRef ? POTENTIAL_EDGE_WEIGHT_WITH_FK
: POTENTIAL_EDGE_WEIGHT);
               }
           }
           return null;
  @@ -748,8 +756,9 @@
           public String toString()
           {
               return new ToStringBuilder(this)
  -                    .append("initialIdentity", initialIdentity)
  -                    .append("terminalIdentity", terminalIdentity)
  +                    .append("initial", initialIdentity)
  +                    .append("terminal", terminalIdentity)
  +                    .append("weight", weight)
                       .append("processed", knownToBeProcessed)
                       .toString();
           }
  @@ -804,7 +813,7 @@
           public String toString()
           {
               return new ToStringBuilder(this)
  -                    .append("envelope", envelope)
  +                    .append("identity", envelope.getIdentity())
                       .append("processed", processed)
                       .append("incomingEdgeWeight", incomingEdgeWeight)
                       .toString();
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message