db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r538362 - /db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
Date Tue, 15 May 2007 22:59:44 GMT
Author: arminw
Date: Tue May 15 15:59:43 2007
New Revision: 538362

URL: http://svn.apache.org/viewvc?view=rev&rev=538362
Log:
optimize performance

Modified:
    db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java

Modified: db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
URL: http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java?view=diff&rev=538362&r1=538361&r2=538362
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
(original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
Tue May 15 15:59:43 2007
@@ -15,8 +15,8 @@
  * limitations under the License.
  */
 
-import java.lang.ref.WeakReference;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -42,7 +42,13 @@
  */
 public class SqlSelectStatement extends SqlQueryStatement implements SelectStatement
 {
-    private WeakReference fieldsForSelect;
+    /*
+     TODO: Currently we have to take care of references to metadata classes. These classes
+     may be set free, so it's not allowed to use hard references to metadata class instances,
because
+     the object with the reference may be cached. Rework this in next major version.
+     So SqlSelectStatement instances must not be cached via hard references.
+    */
+    private SelectFields selectFields;
     private boolean useOjbClassColumn;
 
     /**
@@ -176,23 +182,26 @@
      */
     protected FieldDescriptor[] getFieldsForSelect()
     {
-        if (fieldsForSelect == null || fieldsForSelect.get() == null)
+        if (selectFields == null)
         {
-            fieldsForSelect = new WeakReference(buildFieldsForSelect(getSearchClassDescriptor()));
+            selectFields = buildFieldsForSelect(getSearchClassDescriptor());
         }
-        return (FieldDescriptor[]) fieldsForSelect.get();
+        return selectFields.fields;
     }
 
     /**
-     * Return the Fields to be selected.
+     * Prepare the Fields to be selected.
      *
      * @param cld the ClassDescriptor
-     * @return the Fields to be selected
      */
-    protected FieldDescriptor[] buildFieldsForSelect(ClassDescriptor cld)
+    protected SelectFields buildFieldsForSelect(ClassDescriptor cld)
     {
         DescriptorRepository repository = cld.getRepository();
         Set fields = new ListOrderedSet();   // keep the order of the fields
+        //Map fieldIndices = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD);
+        Map fieldIndices = new HashMap();
+        // column indext starts with 1
+        int index = 0;
         
         // add Standard Fields
         // MBAIRD: if the object being queried on has multiple classes mapped to the table,
@@ -201,14 +210,18 @@
         FieldDescriptor fds[] = repository.getFieldDescriptorsForMultiMappedTable(cld);
         for (int i = 0; i < fds.length; i++)
         {
-            fields.add(fds[i]);
+            FieldDescriptor fld = fds[i];
+            fields.add(fld);
+            if(!fieldIndices.containsKey(fld)) fieldIndices.put(fld, new Integer(++index));
         }
 
         // add inherited Fields. This is important when querying for a class having a super-reference
         fds = cld.getFieldDescriptor(true);
         for (int i = 0; i < fds.length; i++)
         {
-            fields.add(fds[i]);
+            FieldDescriptor fld = fds[i];
+            fields.add(fld);
+            if(!fieldIndices.containsKey(fld)) fieldIndices.put(fld, new Integer(++index));
         }
 
         // add Fields of joined subclasses
@@ -219,13 +232,16 @@
             fds = subCld.getFieldDescriptions();
             for (int i = 0; i < fds.length; i++)
             {
-                fields.add(fds[i]);
+                FieldDescriptor fld = fds[i];
+                fields.add(fld);
+                if(!fieldIndices.containsKey(fld)) fieldIndices.put(fld, new Integer(++index));
             }
         }
 
         FieldDescriptor[] result = new FieldDescriptor[fields.size()];
         fields.toArray(result);
-        return result;
+
+        return new SelectFields(result, fieldIndices);
     }
 
     /**
@@ -456,16 +472,14 @@
     public int getColumnIndex(FieldDescriptor fld)
     {
         int index = JdbcType.MIN_INT;
-        FieldDescriptor[] fields = getFieldsForSelect();
-        if (fields != null)
+        // make sure that fields build once
+        if(selectFields == null) getFieldsForSelect();
+        if(selectFields.fieldIndices != null)
         {
-            for (int i = 0; i < fields.length; i++)
+            Integer id = (Integer) selectFields.fieldIndices.get(fld);
+            if(id != null)
             {
-                if (fields[i].equals(fld))
-                {
-                    index = i + 1;  // starts at 1
-                    break;
-                }
+                index = id.intValue();
             }
         }
         return index;
@@ -489,5 +503,17 @@
     protected void setUseOjbClassColumn(boolean useOjbClassColumn)
     {
         this.useOjbClassColumn = useOjbClassColumn;
+    }
+
+    static class SelectFields
+    {
+        FieldDescriptor[] fields;
+        Map fieldIndices;
+
+        public SelectFields(FieldDescriptor[] fields, Map fieldIndices)
+        {
+            this.fields = fields;
+            this.fieldIndices = fieldIndices;
+        }
     }
 }



---------------------------------------------------------------------
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