[exo-jcr-commits] exo-jcr SVN: r1128 - in jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation: db and 1 other directory.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Dec 18 08:08:01 EST 2009


Author: sergiykarpenko
Date: 2009-12-18 08:08:00 -0500 (Fri, 18 Dec 2009)
New Revision: 1128

Added:
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBSingleDbJDBCConnection.java
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/SingleDbJDBCConnection.java
Modified:
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/DBConstants.java
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/NewJDBCStorageConnection.java
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBConnectionFactory.java
   jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/MultiDbJDBCConnection.java
Log:
EXOJCR-302: get item data optimized

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/DBConstants.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/DBConstants.java	2009-12-18 10:26:05 UTC (rev 1127)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/DBConstants.java	2009-12-18 13:08:00 UTC (rev 1128)
@@ -97,7 +97,7 @@
    /**
     * FIND_NODE_BY_ID.
     */
-   protected String FIND_NODE_BY_ID;
+   protected String FIND_ITEM_BY_ID_NEW;
 
    /**
     * FIND_CHILD_PROPERTY_BY_PATH.

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/NewJDBCStorageConnection.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/NewJDBCStorageConnection.java	2009-12-18 10:26:05 UTC (rev 1127)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/NewJDBCStorageConnection.java	2009-12-18 13:08:00 UTC (rev 1128)
@@ -741,11 +741,19 @@
       ResultSet item = null;
       try
       {
-         item = findNodeByIdentifier(id);
+         item = findItemByIdentifierNew(id);
          if (item.next())
          {
-            //itemData(null, item, item.getInt(COLUMN_CLASS), null);
-            return loadNodeRecord(item, null);
+            int itemType = item.getInt(COLUMN_CLASS);
+            if (itemType == I_CLASS_NODE)
+            {
+               return loadNodeRecord(item, null);
+            }
+            else
+            {
+               return loadPropertyRecord(item, null);
+            }
+
          }
          return null;
       }
@@ -1161,6 +1169,11 @@
          }
 
          // PRIMARY
+         if (!item.next() || !item.getString(COLUMN_NAME).equals(Constants.JCR_PRIMARYTYPE.getAsString()))
+         {
+            throw new SQLException("Node finded but primaryType property not " + cid);
+         }
+
          byte[] data = item.getBytes(COLUMN_VDATA);
          InternalQName ptName = InternalQName.parse(new String((data != null ? data : new byte[]{})));
 
@@ -1252,6 +1265,60 @@
       }
    }
 
+   protected PersistedPropertyData loadPropertyRecord(ResultSet item, QPath parentPath) throws RepositoryException,
+      SQLException, IOException
+   {
+      String cid = item.getString(COLUMN_ID);
+      String cname = item.getString(COLUMN_NAME);
+      int cversion = item.getInt(COLUMN_VERSION);
+
+      String cpid = item.getString(COLUMN_PARENTID);
+      // if parent ID is empty string - it's a root node
+      // cpid = cpid.equals(Constants.ROOT_PARENT_UUID) ? null : cpid;
+
+      try
+      {
+
+         int cptype = item.getInt(COLUMN_PTYPE);
+         boolean cpmultivalued = item.getBoolean(COLUMN_PMULTIVALUED);
+         try
+         {
+            QPath qpath =
+               QPath.makeChildPath(parentPath == null ? traverseQPath(cpid) : parentPath, InternalQName.parse(cname));
+
+            PersistedPropertyData pdata =
+               new PersistedPropertyData(getIdentifier(cid), qpath, getIdentifier(cpid), cversion, cptype,
+                  cpmultivalued);
+
+            List<ValueData> data = new ArrayList<ValueData>();
+
+            do
+            {
+               final int orderNum = item.getInt(COLUMN_VORDERNUM);
+               final String storageId = item.getString(COLUMN_VSTORAGE_DESC);
+               ValueData vdata =
+                  item.wasNull() ? readValueData(cid, orderNum, pdata.getPersistedVersion(), item
+                     .getBinaryStream(COLUMN_VDATA)) : readValueData(pdata, orderNum, storageId);
+               data.add(vdata);
+            }
+            while (item.next() && item.getString(COLUMN_ID) == cid);
+            //TODO avoid situation when there is valueDatas from another property (impossible but exception throwing need)
+
+            pdata.setValues(data);
+            return pdata;
+         }
+         catch (IllegalNameException e)
+         {
+            throw new RepositoryException(e);
+         }
+      }
+      catch (InvalidItemStateException e)
+      {
+         throw new InvalidItemStateException("FATAL: Can't build item path for name " + cname + " id: "
+            + getIdentifier(cid) + ". " + e);
+      }
+   }
+
    /**
     * Build ItemData.
     * 
@@ -2023,7 +2090,7 @@
 
    protected abstract int addPropertyRecord(PropertyData prop) throws SQLException;
 
-   protected abstract ResultSet findNodeByIdentifier(String identifier) throws SQLException;
+   protected abstract ResultSet findItemByIdentifierNew(String identifier) throws SQLException;
 
    protected abstract ResultSet findItemByIdentifier(String identifier) throws SQLException;
 

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBConnectionFactory.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBConnectionFactory.java	2009-12-18 10:26:05 UTC (rev 1127)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBConnectionFactory.java	2009-12-18 13:08:00 UTC (rev 1128)
@@ -17,7 +17,6 @@
 package org.exoplatform.services.jcr.impl.storage.jdbc.optimisation.db;
 
 import org.exoplatform.services.jcr.impl.storage.jdbc.db.GenericConnectionFactory;
-import org.exoplatform.services.jcr.impl.storage.jdbc.db.HSQLDBSingleDbJDBCConnection;
 import org.exoplatform.services.jcr.impl.util.io.FileCleaner;
 import org.exoplatform.services.jcr.storage.WorkspaceStorageConnection;
 import org.exoplatform.services.jcr.storage.value.ValueStoragePluginProvider;

Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBSingleDbJDBCConnection.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBSingleDbJDBCConnection.java	                        (rev 0)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/HSQLDBSingleDbJDBCConnection.java	2009-12-18 13:08:00 UTC (rev 1128)
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2003-2007 eXo Platform SAS.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see<http://www.gnu.org/licenses/>.
+ */
+package org.exoplatform.services.jcr.impl.storage.jdbc.optimisation.db;
+
+import org.exoplatform.services.jcr.impl.util.io.FileCleaner;
+import org.exoplatform.services.jcr.storage.value.ValueStoragePluginProvider;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * Created by The eXo Platform SAS
+ * 
+ * 26.08.2009
+ * 
+ * @author <a href="mailto:dezder at bk.ru">Denis Grebenyuk</a>
+ * @version $Id:$
+ */
+public class HSQLDBSingleDbJDBCConnection extends SingleDbJDBCConnection
+{
+
+   /**
+      * HSQLDB Singledatabase JDBC Connection constructor.
+      * 
+      * @param dbConnection
+      *          JDBC connection, shoudl be opened before
+      * @param readOnly
+      *          boolean if true the dbConnection was marked as READ-ONLY.
+      * @param containerName
+      *          Workspace Storage Container name (see configuration)
+      * @param valueStorageProvider
+      *          External Value Storages provider
+      * @param maxBufferSize
+      *          Maximum buffer size (see configuration)
+      * @param swapDirectory
+      *          Swap directory File (see configuration)
+      * @param swapCleaner
+      *          Swap cleaner (internal FileCleaner).
+      * @throws SQLException
+      * 
+      * @see org.exoplatform.services.jcr.impl.util.io.FileCleaner
+      */
+   public HSQLDBSingleDbJDBCConnection(Connection dbConnection, boolean readOnly, String containerName,
+      ValueStoragePluginProvider valueStorageProvider, int maxBufferSize, File swapDirectory, FileCleaner swapCleaner)
+      throws SQLException
+   {
+      super(dbConnection, readOnly, containerName, valueStorageProvider, maxBufferSize, swapDirectory, swapCleaner);
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected final void prepareQueries() throws SQLException
+   {
+
+      super.prepareQueries();
+
+      FIND_ITEM_BY_NAME =
+         "select * from JCR_SITEM"
+            + " where PARENT_ID=? and CONTAINER_NAME=? and NAME=? and I_INDEX=? order by I_CLASS, VERSION DESC";
+      FIND_PROPERTY_BY_NAME =
+         "select V.DATA"
+            + " from JCR_SITEM I, JCR_SVALUE V"
+            + " where I.PARENT_ID=? and I.I_CLASS=2 and I.CONTAINER_NAME=? and I.NAME=? and I.ID=V.PROPERTY_ID order by V.ORDER_NUM";
+      FIND_NODES_BY_PARENTID =
+         "select * from JCR_SITEM" + " where PARENT_ID=? and I_CLASS=1 and CONTAINER_NAME=?" + " order by N_ORDER_NUM";
+      FIND_NODES_COUNT_BY_PARENTID =
+         "select count(ID) from JCR_SITEM" + " where PARENT_ID=? and I_CLASS=1 and CONTAINER_NAME=?";
+      FIND_PROPERTIES_BY_PARENTID =
+         "select * from JCR_SITEM" + " where PARENT_ID=? and I_CLASS=2 and CONTAINER_NAME=?" + " order by ID";
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findItemByName(String parentId, String name, int index) throws SQLException
+   {
+      if (findItemByName == null)
+         findItemByName = dbConnection.prepareStatement(FIND_ITEM_BY_NAME);
+      else
+         findItemByName.clearParameters();
+
+      findItemByName.setString(1, parentId);
+      findItemByName.setString(2, containerName);
+      findItemByName.setString(3, name);
+      findItemByName.setInt(4, index);
+      return findItemByName.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findPropertyByName(String parentCid, String name) throws SQLException
+   {
+      if (findPropertyByName == null)
+         findPropertyByName = dbConnection.prepareStatement(FIND_PROPERTY_BY_NAME);
+      else
+         findPropertyByName.clearParameters();
+
+      findPropertyByName.setString(1, parentCid);
+      findPropertyByName.setString(2, containerName);
+      findPropertyByName.setString(3, name);
+      return findPropertyByName.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findChildNodesByParentIdentifier(String parentCid) throws SQLException
+   {
+      if (findNodesByParentId == null)
+         findNodesByParentId = dbConnection.prepareStatement(FIND_NODES_BY_PARENTID);
+      else
+         findNodesByParentId.clearParameters();
+
+      findNodesByParentId.setString(1, parentCid);
+      findNodesByParentId.setString(2, containerName);
+      return findNodesByParentId.executeQuery();
+   }
+   
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findChildNodesCountByParentIdentifier(String parentCid) throws SQLException
+   {
+      if (findNodesCountByParentId == null)
+         findNodesCountByParentId = dbConnection.prepareStatement(FIND_NODES_COUNT_BY_PARENTID);
+      else
+         findNodesCountByParentId.clearParameters();
+
+      findNodesCountByParentId.setString(1, parentCid);
+      findNodesCountByParentId.setString(2, containerName);
+      return findNodesCountByParentId.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findChildPropertiesByParentIdentifier(String parentCid) throws SQLException
+   {
+      if (findPropertiesByParentId == null)
+         findPropertiesByParentId = dbConnection.prepareStatement(FIND_PROPERTIES_BY_PARENTID);
+      else
+         findPropertiesByParentId.clearParameters();
+
+      findPropertiesByParentId.setString(1, parentCid);
+      findPropertiesByParentId.setString(2, containerName);
+      return findPropertiesByParentId.executeQuery();
+   }
+}

Modified: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/MultiDbJDBCConnection.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/MultiDbJDBCConnection.java	2009-12-18 10:26:05 UTC (rev 1127)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/MultiDbJDBCConnection.java	2009-12-18 13:08:00 UTC (rev 1128)
@@ -53,7 +53,7 @@
 
    protected PreparedStatement findItemById;
 
-   protected PreparedStatement findNodeById;
+   protected PreparedStatement findItemByIdNew;
 
    protected PreparedStatement findItemByPath;
 
@@ -170,10 +170,17 @@
 
       FIND_ITEM_BY_ID = "select * from JCR_MITEM where ID=?";
 
-      FIND_NODE_BY_ID =
-         "select I.*, P.DATA from JCR_MITEM I, (select I2.PARENT_ID, V.DATA from JCR_MITEM I2, JCR_MVALUE V where I2.I_CLASS=2 and I2.PARENT_ID=?"
-            + " and I2.NAME='[http://www.jcp.org/jcr/1.0]primaryType' and I2.ID=V.PROPERTY_ID) P"
-            + " where I.ID=P.PARENT_ID";
+      FIND_ITEM_BY_ID_NEW =
+      //         "select I.*, P.DATA from JCR_MITEM I, (select I2.PARENT_ID, V.DATA from JCR_MITEM I2, JCR_MVALUE V where I2.I_CLASS=2 and I2.PARENT_ID=?"
+         //            + " and I2.NAME='[http://www.jcp.org/jcr/1.0]primaryType' and I2.ID=V.PROPERTY_ID) P"
+         //            + " where I.ID=P.PARENT_ID";
+
+         //         "select I2.*, V.DATA from JCR_MITEM I LEFT OUTER JOIN JCR_MVALUE V ON (V.PROPERTY_ID=I.ID), (select * from JCR_MITEM where ID=? AND I_CLASS=1) I2"
+         //            + " where I2.ID=I.ID and I2.ID=I.PARENT_ID and I.NAME IN ('[http://www.jcp.org/jcr/1.0]primaryType')";// order by I.I_CLASS, I.N_ORDER_NUM"
+
+         "select I.*, V.ORDER_NUM, V.DATA, V.STORAGE_DESC from JCR_MITEM I LEFT OUTER JOIN JCR_MVALUE V ON (V.PROPERTY_ID=I.ID)"
+            + " where (I.ID=?) or (I.PARENT_ID=? and I.I_CLASS=2 and I.NAME IN ('[http://www.jcp.org/jcr/1.0]primaryType')) order by I.I_CLASS, I.N_ORDER_NUM";// order by I.I_CLASS, I.N_ORDER_NUM"
+
       FIND_ITEM_BY_NAME =
          "select * from JCR_MITEM" + " where PARENT_ID=? and NAME=? and I_INDEX=? order by I_CLASS, VERSION DESC";
 
@@ -576,13 +583,14 @@
    }
 
    @Override
-   protected ResultSet findNodeByIdentifier(String identifier) throws SQLException
+   protected ResultSet findItemByIdentifierNew(String identifier) throws SQLException
    {
-      if (findNodeById == null)
-         findNodeById = dbConnection.prepareStatement(FIND_NODE_BY_ID);
+      if (findItemByIdNew == null)
+         findItemByIdNew = dbConnection.prepareStatement(FIND_ITEM_BY_ID_NEW);
       else
-         findNodeById.clearParameters();
-      findNodeById.setString(1, identifier);
-      return findNodeById.executeQuery();
+         findItemByIdNew.clearParameters();
+      findItemByIdNew.setString(1, identifier);
+      findItemByIdNew.setString(2, identifier);
+      return findItemByIdNew.executeQuery();
    }
 }

Added: jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/SingleDbJDBCConnection.java
===================================================================
--- jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/SingleDbJDBCConnection.java	                        (rev 0)
+++ jcr/trunk/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/optimisation/db/SingleDbJDBCConnection.java	2009-12-18 13:08:00 UTC (rev 1128)
@@ -0,0 +1,608 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.impl.storage.jdbc.optimisation.db;
+
+import org.exoplatform.services.jcr.datamodel.NodeData;
+import org.exoplatform.services.jcr.datamodel.PropertyData;
+import org.exoplatform.services.jcr.datamodel.ValueData;
+import org.exoplatform.services.jcr.impl.Constants;
+import org.exoplatform.services.jcr.impl.storage.jdbc.optimisation.NewJDBCStorageConnection;
+import org.exoplatform.services.jcr.impl.util.io.FileCleaner;
+import org.exoplatform.services.jcr.storage.value.ValueStoragePluginProvider;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.List;
+
+/**
+ * Single database connection implementation.
+ * 
+ * Created by The eXo Platform SAS 27.04.2006
+ * 
+ * @author <a href="mailto:peter.nedonosko at exoplatform.com.ua">Peter
+ *         Nedonosko</a>
+ * @version $Id: SingleDbJDBCConnection.java 20950 2008-10-06 14:23:07Z
+ *          pnedonosko $
+ */
+public class SingleDbJDBCConnection extends NewJDBCStorageConnection
+{
+
+   protected PreparedStatement findItemById;
+
+   protected PreparedStatement findItemByIdNew;
+
+   protected PreparedStatement findItemByPath;
+
+   protected PreparedStatement findItemByName;
+
+   protected PreparedStatement findChildPropertyByPath;
+
+   protected PreparedStatement findPropertyByName;
+
+   protected PreparedStatement findDescendantNodes;
+
+   protected PreparedStatement findDescendantProperties;
+
+   protected PreparedStatement findReferences;
+
+   protected PreparedStatement findValuesByPropertyId;
+
+   protected PreparedStatement findValuesStorageDescriptorsByPropertyId;
+
+   protected PreparedStatement findValuesDataByPropertyId;
+
+   protected PreparedStatement findValueByPropertyIdOrderNumber;
+
+   protected PreparedStatement findNodesByParentId;
+
+   protected PreparedStatement findNodesCountByParentId;
+
+   protected PreparedStatement findPropertiesByParentId;
+
+   protected PreparedStatement insertItem;
+
+   protected PreparedStatement insertNode;
+
+   protected PreparedStatement insertProperty;
+
+   protected PreparedStatement insertReference;
+
+   protected PreparedStatement insertValue;
+
+   protected PreparedStatement updateItem;
+
+   protected PreparedStatement updateItemPath;
+
+   protected PreparedStatement updateNode;
+
+   protected PreparedStatement updateProperty;
+
+   protected PreparedStatement updateValue;
+
+   protected PreparedStatement deleteItem;
+
+   protected PreparedStatement deleteNode;
+
+   protected PreparedStatement deleteProperty;
+
+   protected PreparedStatement deleteReference;
+
+   protected PreparedStatement deleteValue;
+
+   protected PreparedStatement renameNode;
+
+   /**
+    * Singledatabase JDBC Connection constructor.
+    * 
+    * @param dbConnection JDBC connection, shoudl be opened before
+    * @param readOnly, boolean if true the dbConnection was marked as READ-ONLY.
+    * @param containerName Workspace Storage Container name (see configuration)
+    * @param valueStorageProvider External Value Storages provider
+    * @param maxBufferSize Maximum buffer size (see configuration)
+    * @param swapDirectory Swap directory (see configuration)
+    * @param swapCleaner Swap cleaner (internal FileCleaner).
+    * @throws SQLException in case of database error
+    * @see org.exoplatform.services.jcr.impl.util.io.FileCleaner
+    */
+   public SingleDbJDBCConnection(Connection dbConnection, boolean readOnly, String containerName,
+      ValueStoragePluginProvider valueStorageProvider, int maxBufferSize, File swapDirectory, FileCleaner swapCleaner)
+      throws SQLException
+   {
+
+      super(dbConnection, readOnly, containerName, valueStorageProvider, maxBufferSize, swapDirectory, swapCleaner);
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   protected String getInternalId(final String identifier)
+   {
+      return containerName + identifier;
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   protected String getIdentifier(final String internalId)
+   {
+
+      if (internalId == null) // possible for root parent
+         return null;
+
+      return internalId.substring(containerName.length());
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected void prepareQueries() throws SQLException
+   {
+
+      JCR_PK_ITEM = "JCR_PK_SITEM";
+      JCR_FK_ITEM_PARENT = "JCR_FK_SITEM_PARENT";
+      JCR_IDX_ITEM_PARENT = "JCR_IDX_SITEM_PARENT";
+      JCR_IDX_ITEM_PARENT_NAME = "JCR_IDX_SITEM_PARENT_NAME";
+      JCR_IDX_ITEM_PARENT_ID = "JCR_IDX_SITEM_PARENT_ID";
+      JCR_PK_VALUE = "JCR_PK_SVALUE";
+      JCR_FK_VALUE_PROPERTY = "JCR_FK_SVALUE_PROPERTY";
+      JCR_IDX_VALUE_PROPERTY = "JCR_IDX_SVALUE_PROPERTY";
+      JCR_PK_REF = "JCR_PK_SREF";
+      JCR_IDX_REF_PROPERTY = "JCR_IDX_SREF_PROPERTY";
+
+      FIND_ITEM_BY_ID = "select * from JCR_SITEM where ID=?";
+
+      FIND_ITEM_BY_NAME =
+         "select * from JCR_SITEM"
+            + " where CONTAINER_NAME=? and PARENT_ID=? and NAME=? and I_INDEX=? order by I_CLASS, VERSION DESC";
+
+      FIND_ITEM_BY_ID_NEW =
+         "select I.*, V.ORDER_NUM, V.DATA, V.STORAGE_DESC"
+            + " from JCR_SITEM I LEFT OUTER JOIN JCR_SVALUE V ON (V.PROPERTY_ID=I.ID)"
+            + " where I.ID=? OR (I.I_CLASS=2 and I.CONTAINER_NAME=? and I.PARENT_ID=? and I.NAME IN ('[http://www.jcp.org/jcr/1.0]primaryType')) order by V.ORDER_NUM";
+
+      FIND_PROPERTY_BY_NAME =
+         "select V.DATA"
+            + " from JCR_SITEM I, JCR_SVALUE V"
+            + " where I.I_CLASS=2 and I.CONTAINER_NAME=? and I.PARENT_ID=? and I.NAME=? and I.ID=V.PROPERTY_ID order by V.ORDER_NUM";
+
+      FIND_REFERENCES =
+         "select P.ID, P.PARENT_ID, P.VERSION, P.P_TYPE, P.P_MULTIVALUED, P.NAME" + " from JCR_SREF R, JCR_SITEM P"
+            + " where R.NODE_ID=? and P.CONTAINER_NAME=? and P.ID=R.PROPERTY_ID and P.I_CLASS=2";
+
+      FIND_VALUES_BY_PROPERTYID =
+         "select PROPERTY_ID, ORDER_NUM, DATA, STORAGE_DESC from JCR_SVALUE where PROPERTY_ID=? order by ORDER_NUM";
+
+      FIND_VALUES_VSTORAGE_DESC_BY_PROPERTYID = "select distinct STORAGE_DESC from JCR_SVALUE where PROPERTY_ID=?";
+
+      FIND_VALUE_BY_PROPERTYID_OREDERNUMB = "select DATA from JCR_SVALUE where PROPERTY_ID=? and ORDER_NUM=?";
+
+      FIND_NODES_BY_PARENTID =
+         "select * from JCR_SITEM" + " where I_CLASS=1 and CONTAINER_NAME=? and PARENT_ID=?" + " order by N_ORDER_NUM";
+
+      FIND_NODES_COUNT_BY_PARENTID =
+         "select count(ID) from JCR_SITEM" + " where I_CLASS=1 and CONTAINER_NAME=? and PARENT_ID=?";
+
+      FIND_PROPERTIES_BY_PARENTID =
+         "select * from JCR_SITEM" + " where I_CLASS=2 and CONTAINER_NAME=? and PARENT_ID=?" + " order by ID";
+
+      INSERT_NODE =
+         "insert into JCR_SITEM(ID, PARENT_ID, NAME, CONTAINER_NAME, VERSION, I_CLASS, I_INDEX, N_ORDER_NUM) VALUES(?,?,?,?,?,"
+            + I_CLASS_NODE + ",?,?)";
+      INSERT_PROPERTY =
+         "insert into JCR_SITEM(ID, PARENT_ID, NAME, CONTAINER_NAME, VERSION, I_CLASS, I_INDEX, P_TYPE, P_MULTIVALUED) VALUES(?,?,?,?,?,"
+            + I_CLASS_PROPERTY + ",?,?,?)";
+
+      INSERT_VALUE = "insert into JCR_SVALUE(DATA, ORDER_NUM, PROPERTY_ID, STORAGE_DESC) VALUES(?,?,?,?)";
+      INSERT_REF = "insert into JCR_SREF(NODE_ID, PROPERTY_ID, ORDER_NUM) VALUES(?,?,?)";
+
+      RENAME_NODE = "update JCR_SITEM set PARENT_ID=?, NAME=?, VERSION=?, I_INDEX=?, N_ORDER_NUM=? where ID=?";
+
+      UPDATE_NODE = "update JCR_SITEM set VERSION=?, I_INDEX=?, N_ORDER_NUM=? where ID=?";
+      UPDATE_PROPERTY = "update JCR_SITEM set VERSION=?, P_TYPE=? where ID=?";
+      //UPDATE_VALUE = "update JCR_SVALUE set DATA=?, STORAGE_DESC=? where PROPERTY_ID=?, ORDER_NUM=?";
+
+      DELETE_ITEM = "delete from JCR_SITEM where ID=?";
+      DELETE_VALUE = "delete from JCR_SVALUE where PROPERTY_ID=?";
+      DELETE_REF = "delete from JCR_SREF where PROPERTY_ID=?";
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int addNodeRecord(NodeData data) throws SQLException
+   {
+      if (insertNode == null)
+         insertNode = dbConnection.prepareStatement(INSERT_NODE);
+      else
+         insertNode.clearParameters();
+
+      insertNode.setString(1, getInternalId(data.getIdentifier()));
+      // if root then parent identifier equals space string
+      insertNode.setString(2, data.getParentIdentifier() == null ? Constants.ROOT_PARENT_UUID : getInternalId(data
+         .getParentIdentifier()));
+      insertNode.setString(3, data.getQPath().getName().getAsString());
+      insertNode.setString(4, containerName);
+      insertNode.setInt(5, data.getPersistedVersion());
+      insertNode.setInt(6, data.getQPath().getIndex());
+      insertNode.setInt(7, data.getOrderNumber());
+      return insertNode.executeUpdate();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int addPropertyRecord(PropertyData data) throws SQLException
+   {
+      if (insertProperty == null)
+         insertProperty = dbConnection.prepareStatement(INSERT_PROPERTY);
+      else
+         insertProperty.clearParameters();
+
+      insertProperty.setString(1, getInternalId(data.getIdentifier()));
+      insertProperty.setString(2, getInternalId(data.getParentIdentifier()));
+      insertProperty.setString(3, data.getQPath().getName().getAsString());
+      insertProperty.setString(4, containerName);
+      insertProperty.setInt(5, data.getPersistedVersion());
+      insertProperty.setInt(6, data.getQPath().getIndex());
+      insertProperty.setInt(7, data.getType());
+      insertProperty.setBoolean(8, data.isMultiValued());
+
+      return insertProperty.executeUpdate();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int addReference(PropertyData data) throws SQLException, IOException
+   {
+      if (insertReference == null)
+         insertReference = dbConnection.prepareStatement(INSERT_REF);
+      else
+         insertReference.clearParameters();
+
+      List<ValueData> values = data.getValues();
+      int added = 0;
+      for (int i = 0; i < values.size(); i++)
+      {
+         ValueData vdata = values.get(i);
+         String refNodeIdentifier = new String(vdata.getAsByteArray());
+
+         insertReference.setString(1, getInternalId(refNodeIdentifier));
+         insertReference.setString(2, getInternalId(data.getIdentifier()));
+         insertReference.setInt(3, i);
+         added += insertReference.executeUpdate();
+      }
+      return added;
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int deleteReference(String propertyCid) throws SQLException
+   {
+      if (deleteReference == null)
+         deleteReference = dbConnection.prepareStatement(DELETE_REF);
+      else
+         deleteReference.clearParameters();
+
+      deleteReference.setString(1, propertyCid);
+      return deleteReference.executeUpdate();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int deleteItemByIdentifier(String cid) throws SQLException
+   {
+      if (deleteItem == null)
+         deleteItem = dbConnection.prepareStatement(DELETE_ITEM);
+      else
+         deleteItem.clearParameters();
+
+      deleteItem.setString(1, cid);
+      return deleteItem.executeUpdate();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findChildNodesByParentIdentifier(String parentCid) throws SQLException
+   {
+      if (findNodesByParentId == null)
+         findNodesByParentId = dbConnection.prepareStatement(FIND_NODES_BY_PARENTID);
+      else
+         findNodesByParentId.clearParameters();
+
+      findNodesByParentId.setString(1, containerName);
+      findNodesByParentId.setString(2, parentCid);
+      return findNodesByParentId.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findChildNodesCountByParentIdentifier(String parentCid) throws SQLException
+   {
+      if (findNodesCountByParentId == null)
+         findNodesCountByParentId = dbConnection.prepareStatement(FIND_NODES_COUNT_BY_PARENTID);
+      else
+         findNodesCountByParentId.clearParameters();
+
+      findNodesCountByParentId.setString(1, containerName);
+      findNodesCountByParentId.setString(2, parentCid);
+      return findNodesCountByParentId.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findChildPropertiesByParentIdentifier(String parentCid) throws SQLException
+   {
+      if (findPropertiesByParentId == null)
+         findPropertiesByParentId = dbConnection.prepareStatement(FIND_PROPERTIES_BY_PARENTID);
+      else
+         findPropertiesByParentId.clearParameters();
+
+      findPropertiesByParentId.setString(1, containerName);
+      findPropertiesByParentId.setString(2, parentCid);
+      return findPropertiesByParentId.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findItemByName(String parentId, String name, int index) throws SQLException
+   {
+      if (findItemByName == null)
+         findItemByName = dbConnection.prepareStatement(FIND_ITEM_BY_NAME);
+      else
+         findItemByName.clearParameters();
+
+      findItemByName.setString(1, containerName);
+      findItemByName.setString(2, parentId);
+      findItemByName.setString(3, name);
+      findItemByName.setInt(4, index);
+      return findItemByName.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findPropertyByName(String parentCid, String name) throws SQLException
+   {
+      if (findPropertyByName == null)
+         findPropertyByName = dbConnection.prepareStatement(FIND_PROPERTY_BY_NAME);
+      else
+         findPropertyByName.clearParameters();
+
+      findPropertyByName.setString(1, containerName);
+      findPropertyByName.setString(2, parentCid);
+      findPropertyByName.setString(3, name);
+      return findPropertyByName.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findItemByIdentifier(String cid) throws SQLException
+   {
+      if (findItemById == null)
+         findItemById = dbConnection.prepareStatement(FIND_ITEM_BY_ID);
+      else
+         findItemById.clearParameters();
+
+      findItemById.setString(1, cid);
+      return findItemById.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findReferences(String cid) throws SQLException
+   {
+      if (findReferences == null)
+         findReferences = dbConnection.prepareStatement(FIND_REFERENCES);
+      else
+         findReferences.clearParameters();
+
+      findReferences.setString(1, cid);
+      findReferences.setString(2, containerName);
+      return findReferences.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int updateNodeByIdentifier(int version, int index, int orderNumb, String cid) throws SQLException
+   {
+      if (updateNode == null)
+         updateNode = dbConnection.prepareStatement(UPDATE_NODE);
+      else
+         updateNode.clearParameters();
+
+      updateNode.setInt(1, version);
+      updateNode.setInt(2, index);
+      updateNode.setInt(3, orderNumb);
+      updateNode.setString(4, cid);
+      return updateNode.executeUpdate();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int updatePropertyByIdentifier(int version, int type, String cid) throws SQLException
+   {
+      if (updateProperty == null)
+         updateProperty = dbConnection.prepareStatement(UPDATE_PROPERTY);
+      else
+         updateProperty.clearParameters();
+
+      updateProperty.setInt(1, version);
+      updateProperty.setInt(2, type);
+      updateProperty.setString(3, cid);
+      return updateProperty.executeUpdate();
+   }
+
+   // -------- values processing ------------
+
+   /**
+    * {@inheritDoc}
+    */
+   protected int addValueData(String cid, int orderNumber, InputStream stream, int streamLength, String storageDesc)
+      throws SQLException
+   {
+
+      if (insertValue == null)
+         insertValue = dbConnection.prepareStatement(INSERT_VALUE);
+      else
+         insertValue.clearParameters();
+
+      if (stream == null)
+      {
+         // [PN] store vd reference to external storage etc.
+         insertValue.setNull(1, Types.BINARY);
+         insertValue.setString(4, storageDesc);
+      }
+      else
+      {
+         insertValue.setBinaryStream(1, stream, streamLength);
+         insertValue.setNull(4, Types.VARCHAR);
+      }
+
+      insertValue.setInt(2, orderNumber);
+      insertValue.setString(3, cid);
+      return insertValue.executeUpdate();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   protected int deleteValueData(String cid) throws SQLException
+   {
+      if (deleteValue == null)
+         deleteValue = dbConnection.prepareStatement(DELETE_VALUE);
+      else
+         deleteValue.clearParameters();
+
+      deleteValue.setString(1, cid);
+      return deleteValue.executeUpdate();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   protected ResultSet findValuesByPropertyId(String cid) throws SQLException
+   {
+      if (findValuesByPropertyId == null)
+         findValuesByPropertyId = dbConnection.prepareStatement(FIND_VALUES_BY_PROPERTYID);
+      else
+         findValuesByPropertyId.clearParameters();
+
+      findValuesByPropertyId.setString(1, cid);
+      return findValuesByPropertyId.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected ResultSet findValuesStorageDescriptorsByPropertyId(String cid) throws SQLException
+   {
+      if (findValuesStorageDescriptorsByPropertyId == null)
+         findValuesStorageDescriptorsByPropertyId =
+            dbConnection.prepareStatement(FIND_VALUES_VSTORAGE_DESC_BY_PROPERTYID);
+      else
+         findValuesStorageDescriptorsByPropertyId.clearParameters();
+
+      findValuesStorageDescriptorsByPropertyId.setString(1, cid);
+      return findValuesStorageDescriptorsByPropertyId.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Deprecated
+   protected ResultSet findValueByPropertyIdOrderNumber(String cid, int orderNumb) throws SQLException
+   {
+      if (findValueByPropertyIdOrderNumber == null)
+         findValueByPropertyIdOrderNumber = dbConnection.prepareStatement(FIND_VALUE_BY_PROPERTYID_OREDERNUMB);
+      else
+         findValueByPropertyIdOrderNumber.clearParameters();
+
+      findValueByPropertyIdOrderNumber.setString(1, cid);
+      findValueByPropertyIdOrderNumber.setInt(2, orderNumb);
+      return findValueByPropertyIdOrderNumber.executeQuery();
+   }
+
+   /**
+    * {@inheritDoc}
+    */
+   @Override
+   protected int renameNode(NodeData data) throws SQLException
+   {
+      if (renameNode == null)
+         renameNode = dbConnection.prepareStatement(RENAME_NODE);
+      else
+         renameNode.clearParameters();
+
+      renameNode.setString(1, data.getParentIdentifier() == null ? Constants.ROOT_PARENT_UUID : getInternalId(data
+         .getParentIdentifier()));
+      renameNode.setString(2, data.getQPath().getName().getAsString());
+      renameNode.setInt(3, data.getPersistedVersion());
+      renameNode.setInt(4, data.getQPath().getIndex());
+      renameNode.setInt(5, data.getOrderNumber());
+      renameNode.setString(6, getInternalId(data.getIdentifier()));
+      return renameNode.executeUpdate();
+   }
+
+   @Override
+   protected ResultSet findItemByIdentifierNew(String identifier) throws SQLException
+   {
+      if (findItemByIdNew == null)
+         findItemByIdNew = dbConnection.prepareStatement(FIND_ITEM_BY_ID_NEW);
+      else
+         findItemByIdNew.clearParameters();
+      findItemByIdNew.setString(1, identifier);
+      findItemByIdNew.setString(2, containerName);
+      findItemByIdNew.setString(3, identifier);
+      return findItemByIdNew.executeQuery();
+   }
+
+}



More information about the exo-jcr-commits mailing list