Author: bpapez
Date: Tue May 15 11:37:53 2007
New Revision: 17300

URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D17300&repname=
=3Djahia
Log:
Performance improvement when reading all containers of a site (MAN-16)

Modified:
    branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/containers/JahiaC=
ontainerUtilsDB.java

Modified: branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/containers/=
JahiaContainerUtilsDB.java
URL: https://svndev.jahia.net/websvn/diff.php?path=3D/branches/JAHIA-4-1-BR=
ANCH/src/java/org/jahia/services/containers/JahiaContainerUtilsDB.java&rev=
=3D17300&repname=3Djahia
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/containers/JahiaC=
ontainerUtilsDB.java (original)
+++ branches/JAHIA-4-1-BRANCH/src/java/org/jahia/services/containers/JahiaC=
ontainerUtilsDB.java Tue May 15 11:37:53 2007
@@ -19,6 +19,7 @@
 import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
@@ -27,8 +28,10 @@
 import java.util.TreeSet;
 import java.util.Vector;
 =

+import org.jahia.bin.Jahia;
 import org.jahia.data.containers.JahiaContainerDefinition;
 import org.jahia.exceptions.JahiaException;
+import org.jahia.params.ParamBean;
 import org.jahia.registries.JahiaContainerDefinitionsRegistry;
 import org.jahia.registries.ServicesRegistry;
 import org.jahia.services.cache.Cache;
@@ -36,7 +39,7 @@
 import org.jahia.services.database.ConnectionDispenser;
 import org.jahia.services.pages.ContentPage;
 import org.jahia.services.version.EntryLoadRequest;
-import org.jahia.utils.JahiaTools;
+
 =

 /**
  * JahiaContainerUtilsDB
@@ -939,188 +942,114 @@
      * @throws raises a JahiaException if a data access error occurs
      * @throws raises a JahiaException if cannot free resources
      */
-    public Vector db_get_container_ids_in_container_list (int listID, Entr=
yLoadRequest loadVersion,
-                                                          boolean fromAllC=
ontainerLists)
-            throws JahiaException {
-        Connection dbConn =3D null;
-        PreparedStatement stmt =3D null;
-        ResultSet rs =3D null;
-        Vector theList =3D null;
-        Vector tempRank =3D new Vector ();
-        try {
-            dbConn =3D ConnectionDispenser.getConnection ();
+    public Vector db_get_container_ids_in_container_list(int listID, Entry=
LoadRequest loadVersion,
+            boolean fromAllContainerLists) throws JahiaException {
 =

-            if ( containerIDsByContainerListCache !=3D null && !fromAllCon=
tainerLists ){
-                theList =3D (Vector)this.containerIDsByContainerListCache
-                    .get(getCtnIDsByCtnListCacheKey(listID,loadVersion));
-            }
-            if ( theList !=3D null ){
-                return (Vector)theList.clone();
-            } else {
-                theList =3D new Vector();
-            }
+        boolean compareMode =3D (Jahia.getThreadParamBean() !=3D null && P=
aramBean.COMPARE.equals(Jahia
+                .getThreadParamBean().getOperationMode()));
 =

+        Vector theList =3D null;
+        Object cacheKey =3D getCtnIDsByCtnListCacheKey(listID, loadVersion=
);
+        if (!compareMode
+                && containerIDsByContainerListCache !=3D null
+                && !fromAllContainerLists
+                && !(loadVersion !=3D null && loadVersion.getWorkflowState=
() <=3D EntryLoadRequest.VERSIONED_WORKFLOW_STATE)) {
+            theList =3D (Vector) this.containerIDsByContainerListCache.get=
(cacheKey);
+            if (theList !=3D null) {
+                theList =3D new Vector(theList);
+            }
+        }
+        if (theList =3D=3D null) {
+            List result;
+            Connection dbConn =3D null;
+            PreparedStatement stmt =3D null;
+            ResultSet rs =3D null;
 =

-            // Get all containers IDs.
-            if (loadVersion =3D=3D null) {
+            try {
+                dbConn =3D ConnectionDispenser.getConnection();
                 if (fromAllContainerLists) {
-                    stmt =3D dbConn.prepareStatement("SELECT DISTINCT id_j=
ahia_ctn_entries FROM jahia_ctn_entries");
+                    result =3D new ArrayList(10000);
+                    // Get all containers IDs.
+                    if (loadVersion =3D=3D null || loadVersion.isVersioned=
() && compareMode) {
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, rank_jahia_ctn_entries FROM jahia_ctn_entries ORDER BY rank_=
jahia_ctn_entries");
+                    } else if (loadVersion.isStaging()) {
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, version_id, rank_jahia_ctn_entries, workflow_state FROM jahi=
a_ctn_entries WHERE workflow_state>=3D1 ORDER BY rank_jahia_ctn_entries, wo=
rkflow_state DESC, id_jahia_ctn_entries");
+                    } else if (loadVersion.isVersioned() && !compareMode) {
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, version_id, rank_jahia_ctn_entries FROM jahia_ctn_entries WH=
ERE workflow_state<=3D1 AND version_id<=3D? ORDER BY rank_jahia_ctn_entries=
, id_jahia_ctn_entries, version_id DESC");
+                        stmt.setInt(1, loadVersion.getVersionID());
+                    } else {
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, rank_jahia_ctn_entries FROM jahia_ctn_entries WHERE workflow=
_state=3D1 ORDER BY rank_jahia_ctn_entries, id_jahia_ctn_entries");
+                    }
                 } else {
-                    stmt =3D dbConn.prepareStatement("SELECT DISTINCT id_j=
ahia_ctn_entries FROM jahia_ctn_entries WHERE listid_jahia_ctn_entries=3D?"=
);
-                    stmt.setInt(1, listID);
-                }
-                rs =3D stmt.executeQuery ();
-                while (rs.next ()) {
-                    int ctnID =3D rs.getInt (1);
-                    theList.add (new Integer (ctnID));
-                }
-                rs.close();
-            } else
-            // is it a staging load ?
-                if (loadVersion.isStaging ()) {
-                    if (fromAllContainerLists) {
-                        stmt =3D dbConn.prepareStatement("SELECT id_jahia_=
ctn_entries, rank_jahia_ctn_entries, version_id, workflow_state FROM jahia_=
ctn_entries WHERE workflow_state>=3D1 ORDER BY id_jahia_ctn_entries ASC, wo=
rkflow_state DESC");
+                    result =3D new ArrayList(128);
+                    if (loadVersion =3D=3D null || loadVersion.isVersioned=
() && compareMode) {
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, rank_jahia_ctn_entries FROM jahia_ctn_entries WHERE listid_j=
ahia_ctn_entries=3D? ORDER BY rank_jahia_ctn_entries");
+                        stmt.setInt(1, listID);
+                    } else if (loadVersion.isStaging()) {
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, version_id, rank_jahia_ctn_entries, workflow_state  FROM jah=
ia_ctn_entries WHERE listid_jahia_ctn_entries=3D? AND workflow_state>=3D1 O=
RDER BY rank_jahia_ctn_entries, workflow_state DESC, id_jahia_ctn_entries");
+                        stmt.setInt(1, listID);
+                    } else if (loadVersion.isVersioned() && !compareMode) {
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, version_id, rank_jahia_ctn_entries  FROM jahia_ctn_entries W=
HERE listid_jahia_ctn_entries=3D? AND workflow_state<=3D1 AND version_id<=
=3D? ORDER BY rank_jahia_ctn_entries, id_jahia_ctn_entries, version_id DESC=
");
+                        stmt.setInt(1, listID);
+                        stmt.setInt(2, loadVersion.getVersionID());
                     } else {
-                        stmt =3D dbConn.prepareStatement("SELECT id_jahia_=
ctn_entries, rank_jahia_ctn_entries, version_id, workflow_state FROM jahia_=
ctn_entries WHERE listid_jahia_ctn_entries=3D? AND workflow_state>=3D1 ORDE=
R BY id_jahia_ctn_entries ASC, workflow_state DESC");
+                        stmt =3D dbConn
+                                .prepareStatement("SELECT DISTINCT id_jahi=
a_ctn_entries, rank_jahia_ctn_entries FROM jahia_ctn_entries WHERE listid_j=
ahia_ctn_entries=3D? AND workflow_state=3D1 ORDER BY rank_jahia_ctn_entries=
, id_jahia_ctn_entries");
                         stmt.setInt(1, listID);
                     }
-                    rs =3D stmt.executeQuery ();
-                    int previousCtnID, ctnID, rank, versionID;
-                    previousCtnID =3D -2;
-                    // rank sort not very optimized, but this is only when=
 we're in staging....
-                    while (rs.next ()) {
-                        ctnID =3D rs.getInt (1);
-                        rank =3D rs.getInt (2);
-                        versionID =3D rs.getInt (3);
+                }
+                rs =3D stmt.executeQuery();
+                Set deletedInStaging =3D null;
+                Set addedCtnIds =3D null;
+                if (loadVersion !=3D null && loadVersion.isStaging() && !l=
oadVersion.isWithMarkedForDeletion()) {
+                    deletedInStaging =3D new HashSet();
+                    addedCtnIds =3D new HashSet();                    =

+                }
+                while (rs.next()) {
+                    Integer ctnID =3D new Integer(rs.getInt(1));
 =

-                        if ( !theList.contains(new Integer(ctnID)) ){
-                            if (previousCtnID !=3D ctnID) {
-                                if (loadVersion.isWithMarkedForDeletion())=
 {
-                                    if (rank !=3D 0) {
-                                        int index =3D 0;
-                                        while ( (index < tempRank.size()) =
&&
-                                               ( ( (Integer) tempRank.elem=
entAt(
-                                            index)).intValue() < rank)) {
-                                            index++;
-                                        }
-                                        theList.insertElementAt(new Intege=
r(ctnID),
-                                            index);
-                                        tempRank.insertElementAt(new Integ=
er(rank),
-                                            index);
-                                    }
-                                    else {
-                                        // if the ranking is at zero we ad=
d it at the end
-                                        // of the list.
-                                        theList.add(new Integer(ctnID));
-                                    }
-                                }
-                                else {
-                                    if (versionID !=3D -1) {
-                                        if (rank !=3D 0) {
-                                            int index =3D 0;
-                                            while ( (index < tempRank.size=
()) &&
-                                                   ( ( (Integer) tempRank.=
elementAt(
-                                                index)).intValue() < rank)=
) {
-                                                index++;
-                                            }
-                                            theList.insertElementAt(new In=
teger(
-                                                ctnID), index);
-                                            tempRank.insertElementAt(new I=
nteger(
-                                                rank), index);
-                                        }
-                                        else {
-                                            // if the ranking is at zero w=
e add it at the end
-                                            // of the list.
-                                            theList.add(new Integer(ctnID)=
);
-                                        }
-                                    }
-                                }
-                            }
+                    if (loadVersion !=3D null && loadVersion.isStaging() &=
& !loadVersion.isWithMarkedForDeletion()) {
+                        long version =3D rs.getLong(2);
+                        if (!deletedInStaging.contains(ctnID) && !addedCtn=
Ids.contains(ctnID) && version > -1) {
+                            result.add(ctnID);
+                            addedCtnIds.add(ctnID);
+                        } else if (!compareMode && version =3D=3D -1) {
+                            deletedInStaging.add(ctnID);
                         }
-                        previousCtnID =3D ctnID;
-                    }
-                    rs.close();
-                } else
-                // is it a version ID ?
-                    if (loadVersion.isVersioned ()) {
-                        if (fromAllContainerLists) {
-                            stmt =3D dbConn.prepareStatement("SELECT id_ja=
hia_ctn_entries, rank_jahia_ctn_entries, version_id FROM jahia_ctn_entries =
WHERE workflow_state<=3D1 AND version_id<=3D? ORDER BY id_jahia_ctn_entries=
 ASC, version_id DESC");
-                            stmt.setInt(1, loadVersion.getVersionID());
-                        } else {
-                            stmt =3D dbConn.prepareStatement("SELECT id_ja=
hia_ctn_entries, rank_jahia_ctn_entries, version_id FROM jahia_ctn_entries =
WHERE listid_jahia_ctn_entries=3D? AND workflow_state<=3D1 AND version_id<=
=3D? ORDER BY id_jahia_ctn_entries ASC, version_id DESC");
-                            stmt.setInt(1, listID);
-                            stmt.setInt(2, loadVersion.getVersionID());
-                        }
-                        rs =3D stmt.executeQuery ();
-                        int previousCtnID, ctnID, rank, versionID;
-                        previousCtnID =3D -2;
-                        // rank sort not very optimized, but this is only =
when we're in versionID mode....
-                        while (rs.next ()) {
-                            ctnID =3D rs.getInt (1);
-                            rank =3D rs.getInt (2);
-                            versionID =3D rs.getInt (3);
-                            if ( !theList.contains(new Integer(ctnID)) ){
-                                if ( (previousCtnID !=3D ctnID) && (versio=
nID !=3D -1)) {
-                                    if (rank !=3D 0) {
-                                        int index =3D 0;
-                                        while ( (index < tempRank.size()) =
&&
-                                               ( ( (Integer) tempRank.elem=
entAt(index)).
-                                                intValue() < rank)) {
-                                            index++;
-                                        }
-                                        theList.insertElementAt(new Intege=
r(ctnID),
-                                            index);
-                                        tempRank.insertElementAt(new Integ=
er(rank),
-                                            index);
-                                    }
-                                    else {
-                                        // if the ranking is at zero we ad=
d it at the end
-                                        // of the list.
-                                        theList.add(new Integer(ctnID));
-                                    }
-                                }
-                            }
-                            previousCtnID =3D ctnID;
-                        }
-                        rs.close();
-                    }
-                    // normal case
-                    else {
-                        if (fromAllContainerLists) {
-                            stmt =3D dbConn.prepareStatement("SELECT id_ja=
hia_ctn_entries, rank_jahia_ctn_entries FROM jahia_ctn_entries WHERE workfl=
ow_state=3D1 ORDER BY rank_jahia_ctn_entries, id_jahia_ctn_entries ASC");
-                        } else {
-                            stmt =3D dbConn.prepareStatement("SELECT id_ja=
hia_ctn_entries, rank_jahia_ctn_entries FROM jahia_ctn_entries WHERE listid=
_jahia_ctn_entries=3D? AND workflow_state=3D1 ORDER BY rank_jahia_ctn_entri=
es, id_jahia_ctn_entries ASC");
-                            stmt.setInt(1, listID);
-                        }
-                        rs =3D stmt.executeQuery ();
-                        while (rs.next ()) {
-                            theList.add (new Integer (rs.getInt (1)));
-                        }
-                        rs.close();
+                    } else {
+                        result.add(ctnID);
                     }
-        } catch (SQLException se) {
-            String errorMsg =3D "Error in db_get_container_ids_in_containe=
r_list : " + se.getMessage ();
-            logger.debug (errorMsg + " -> BAILING OUT", se);
-            throw new JahiaException ("Cannot load containers from the dat=
abase",
-                    errorMsg, JahiaException.DATABASE_ERROR,
-                    JahiaException.CRITICAL_SEVERITY, se);
-        } finally {
-            try {
+                }
+                theList =3D new Vector(result);
+                rs.close();
 =

-                if (stmt !=3D null) stmt.close ();
-            } catch (SQLException ex) {
-                logger.debug ("Cannot free resources", ex);
+            } catch (SQLException se) {
+                String errorMsg =3D "Error in db_get_container_ids_in_cont=
ainer_list : " + se.getMessage();
+                logger.debug(errorMsg + " -> BAILING OUT", se);
+                throw new JahiaException("Cannot load containers from the =
database", errorMsg,
+                        JahiaException.DATABASE_ERROR, JahiaException.CRIT=
ICAL_SEVERITY, se);
+            } finally {
+                try {
+                    if (stmt !=3D null)
+                        stmt.close();
+                } catch (SQLException ex) {
+                    logger.debug("Cannot free resources", ex);
+                }
             }
         }
-        if ( theList =3D=3D null ){
-            return null;
-        }
-        if ( loadVersion =3D=3D null ||
-             loadVersion.getWorkflowState()>=3DEntryLoadRequest.ACTIVE_WOR=
KFLOW_STATE ){
-            this.containerIDsByContainerListCache.put(getCtnIDsByCtnListCa=
cheKey(listID,loadVersion),theList);
+        =

+        if(!compareMode && containerIDsByContainerListCache !=3D null && !=
(loadVersion !=3D null && loadVersion.getWorkflowState()<=3DEntryLoadReques=
t.VERSIONED_WORKFLOW_STATE)) {
+            this.containerIDsByContainerListCache.put(cacheKey, theList);
         }
 =

-        return (Vector)theList.clone();
+        return theList;
     } // db_get_container_ids_in_container_list
 =

     /**
@@ -1889,17 +1818,22 @@
             stmt =3D dbConn.prepareStatement("SELECT ctnid_jahia_fields_da=
ta, id_jahia_fields_data FROM jahia_fields_data WHERE pageid_jahia_fields_d=
ata=3D? ORDER BY ctnid_jahia_fields_data, rank_jahia_fields_data, id_jahia_=
fields_data ASC");
             stmt.setInt(1, pageID);
             rs =3D stmt.executeQuery ();
-
+            Integer prevCtnId =3D null;
+            List theList =3D null;
+            Set addedField =3D null;
             while (rs.next ()) {
-                int ctnid =3D rs.getInt (1);
-                Vector theList =3D (Vector) result.get (new Integer (ctnid=
));
-                if (theList =3D=3D null) {
+                Integer ctnid =3D new Integer(rs.getInt (1));
+                if (!ctnid.equals(prevCtnId)){
                     theList =3D new Vector ();
-                    result.put (new Integer (ctnid), theList);
+                    addedField =3D new HashSet();
+                    result.put (ctnid, theList);
                 }
+                prevCtnId =3D ctnid;
+                    =

                 Integer fieldID =3D new Integer (rs.getInt (2));
-                if (!theList.contains (fieldID)) {
+                if (!addedField.contains (fieldID)) {
                     theList.add (fieldID);
+                    addedField.add (fieldID);
                 }
             }
             rs.close();
@@ -2489,9 +2423,7 @@
 =

             while (rs.next ()) {
                 Integer curContainerID =3D new Integer (rs.getInt (1));
-                if (!containerIDs.contains (curContainerID)) {
-                    containerIDs.add (curContainerID);
-                }
+                containerIDs.add (curContainerID);
             }
         } catch (SQLException se) {
             String errorMsg =3D "Error in getActiveOrStagedContainerIDsInP=
age : " +

_______________________________________________
cvs_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list

Reply via email to