[exo-jcr-commits] exo-jcr SVN: r1284 - in jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl: storage/value/fs/operations and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Jan 5 04:11:11 EST 2010


Author: tolusha
Date: 2010-01-05 04:11:11 -0500 (Tue, 05 Jan 2010)
New Revision: 1284

Modified:
   jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCStorageConnection.java
   jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java
   jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SwapFile.java
Log:
EXOJCR-363: refactoring, using ValueFileIOHelper for write value operation

Modified: jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCStorageConnection.java
===================================================================
--- jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCStorageConnection.java	2010-01-05 09:07:48 UTC (rev 1283)
+++ jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/jdbc/JDBCStorageConnection.java	2010-01-05 09:11:11 UTC (rev 1284)
@@ -40,6 +40,7 @@
 import org.exoplatform.services.jcr.impl.storage.value.ValueStorageNotFoundException;
 import org.exoplatform.services.jcr.impl.storage.value.fs.operations.ValueFileIOHelper;
 import org.exoplatform.services.jcr.impl.util.io.FileCleaner;
+import org.exoplatform.services.jcr.impl.util.io.SpoolFile;
 import org.exoplatform.services.jcr.impl.util.io.SwapFile;
 import org.exoplatform.services.jcr.storage.WorkspaceStorageConnection;
 import org.exoplatform.services.jcr.storage.value.ValueIOChannel;
@@ -49,7 +50,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -76,6 +76,20 @@
 {
 
    /**
+    * Helper.
+    */
+   class WriteValueHelper extends ValueFileIOHelper
+   {
+      /**
+       * {@inheritDoc}
+       */
+      public void writeStreamedValue(File file, ValueData value) throws IOException
+      {
+         super.writeStreamedValue(file, value);
+      }
+   }
+
+   /**
     * Connection logger.
     */
    protected static final Log LOG = ExoLogger.getLogger("jcr.JDBCStorageConnection");
@@ -106,6 +120,8 @@
 
    protected final List<ValueIOChannel> valueChanges;
 
+   protected final WriteValueHelper writeValueHelper = new WriteValueHelper();
+
    /**
     * Read-only flag, if true the connection is marked as READ-ONLY.
     */
@@ -1843,57 +1859,6 @@
    }
 
    /**
-    * Writes value data to swap file.
-    * 
-    * @param cid
-    *          Property id
-    * @param orderNumber
-    *          Value order number
-    * @param version
-    *          persistent version (used for BLOB swapping)
-    * @param content
-    * @return ValueData
-    * @throws SQLException
-    *           database error
-    * @throws IOException
-    *           I/O error (swap)
-    */
-   protected SwapFile swapValueData(String cid, int orderNumber, int version, final InputStream content)
-      throws SQLException, IOException
-   {
-
-      byte[] spoolBuffer = new byte[ValueFileIOHelper.IOBUFFER_SIZE];
-      int read;
-      int len = 0;
-
-      SwapFile swapFile = SwapFile.get(swapDirectory, cid + orderNumber + "." + version);
-      OutputStream out = new FileOutputStream(swapFile);;
-
-      if (swapFile.isSpooled())
-      {
-         return swapFile;
-      }
-
-      try
-      {
-         if (content != null)
-            while ((read = content.read(spoolBuffer)) >= 0)
-            {
-               // spool to temp file
-               out.write(spoolBuffer, 0, read);
-               len += read;
-            }
-      }
-      finally
-      {
-         out.close();
-         swapFile.spoolDone();
-      }
-
-      return swapFile;
-   }
-
-   /**
     * Add Values to Property record.
     * 
     * @param data
@@ -1926,32 +1891,21 @@
             }
             else
             {
-               // it's StreamPersistedValueData
-               File file;
                StreamPersistedValueData streamData = (StreamPersistedValueData)vd;
 
-               if ((file = streamData.getTempFile()) != null)
+               SwapFile swapFile = SwapFile.get(swapDirectory, cid + i + "." + data.getPersistedVersion());
+               try
                {
-                  stream = new FileInputStream(streamData.getTempFile());
+                  writeValueHelper.writeStreamedValue(swapFile, streamData);
                }
-               else
+               finally
                {
-                  stream = streamData.getStream();
-
-                  // TODO spool on JDBC driver read - multiplexing the data to two stores, database and spool file, with one read.
-                  file = swapValueData(cid, i, data.getPersistedVersion(), stream);
+                  swapFile.spoolDone();
                }
 
-               long vlen = file.length();
-               if (vlen < 0)
+               long vlen = swapFile.length();
+               if (vlen <= Integer.MAX_VALUE)
                {
-                  // TODO not actual with SwapFile, but if it will be reworked can be so
-                  streamLength = stream.available();
-                  LOG.warn("Cannot obtain exact Value data length, will use available from the stream " + streamLength
-                     + ". Property " + data.getQPath().getAsString());
-               }
-               else if (vlen <= Integer.MAX_VALUE)
-               {
                   streamLength = (int)vlen;
                }
                else
@@ -1960,8 +1914,6 @@
                      + ". Property " + data.getQPath().getAsString());
                }
 
-               // set persistent file to ValueData, will be available for saving Property.
-               streamData.setPersistedFile(file);
                stream = streamData.getAsStream();
             }
             storageId = null;

Modified: jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java
===================================================================
--- jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java	2010-01-05 09:07:48 UTC (rev 1283)
+++ jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/storage/value/fs/operations/ValueFileIOHelper.java	2010-01-05 09:11:11 UTC (rev 1284)
@@ -119,65 +119,94 @@
    {
       if (value.isByteArray())
       {
-         OutputStream out = new FileOutputStream(file);
-         try
-         {
-            out.write(value.getAsByteArray());
-         }
-         finally
-         {
-            out.close();
-         }
+         writeByteArrayValue(file, value);
       }
       else
       {
-         // stream Value
+         writeStreamedValue(file, value);
+      }
+   }
 
-         if (value instanceof StreamPersistedValueData)
+   /**
+    * Write value array of bytes to a file.
+    * 
+    * @param file
+    *          File
+    * @param value
+    *          ValueData
+    * @throws IOException
+    *           if error occurs
+    */
+   protected void writeByteArrayValue(File file, ValueData value) throws IOException
+   {
+      OutputStream out = new FileOutputStream(file);
+      try
+      {
+         out.write(value.getAsByteArray());
+      }
+      finally
+      {
+         out.close();
+      }
+   }
+
+   /**
+    * Write streamed value to a file.
+    * 
+    * @param file
+    *          File
+    * @param value
+    *          ValueData
+    * @throws IOException
+    *           if error occurs
+    */
+   protected void writeStreamedValue(File file, ValueData value) throws IOException
+   {
+      // stream Value
+      if (value instanceof StreamPersistedValueData)
+      {
+         StreamPersistedValueData streamed = (StreamPersistedValueData)value;
+
+         if (streamed.isPersisted())
          {
-            StreamPersistedValueData streamed = (StreamPersistedValueData)value;
-
-            if (streamed.isPersisted())
+            // already persisted in another Value, copy it to this Value
+            copyClose(streamed.getAsStream(), new FileOutputStream(file));
+         }
+         else
+         {
+            // the Value not yet persisted, i.e. or in client stream or spooled to a temp file
+            File tempFile;
+            if ((tempFile = streamed.getTempFile()) != null)
             {
-               // already persisted in another Value, copy it to this Value
-               copyClose(streamed.getAsStream(), new FileOutputStream(file));
-            }
-            else
-            {
-               // the Value not yet persisted, i.e. or in client stream or spooled to a temp file
-               File tempFile;
-               if ((tempFile = streamed.getTempFile()) != null)
+               // it's spooled Value, try move its file to VS
+               if (!tempFile.renameTo(file))
                {
-                  // it's spooled Value, try move its file to VS
-                  if (!tempFile.renameTo(file))
+                  // not succeeded - copy bytes, temp file will be deleted by transient ValueData
+                  if (LOG.isDebugEnabled())
                   {
-                     // not succeeded - copy bytes, temp file will be deleted by transient ValueData
-                     if (LOG.isDebugEnabled())
-                     {
-                        LOG
-                           .debug("Value spool file move (rename) to Values Storage is not succeeded. Trying bytes copy. Spool file: "
-                              + tempFile.getAbsolutePath() + ". Destination: " + file.getAbsolutePath());
-                     }
+                     LOG
+                        .debug("Value spool file move (rename) to Values Storage is not succeeded. Trying bytes copy. Spool file: "
+                           + tempFile.getAbsolutePath() + ". Destination: " + file.getAbsolutePath());
+                  }
 
-                     copyClose(new FileInputStream(tempFile), new FileOutputStream(file));
-                  }
+                  copyClose(new FileInputStream(tempFile), new FileOutputStream(file));
                }
-               else
-               {
-                  // not spooled, use client InputStream
-                  copyClose(streamed.getStream(), new FileOutputStream(file));
-               }
+            }
+            else
+            {
+               // not spooled, use client InputStream
+               copyClose(streamed.getStream(), new FileOutputStream(file));
+            }
 
-               // link this Value to file in VS
-               streamed.setPersistedFile(file);
-            }
+            // link this Value to file in VS
+            streamed.setPersistedFile(file);
          }
-         else
-         {
-            // copy from Value stream to the file, e.g. from FilePersistedValueData to this Value
-            copyClose(value.getAsStream(), new FileOutputStream(file));
-         }
       }
+      else
+      {
+         // copy from Value stream to the file, e.g. from FilePersistedValueData to this Value
+         copyClose(value.getAsStream(), new FileOutputStream(file));
+      }
    }
 
    /**

Modified: jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SwapFile.java
===================================================================
--- jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SwapFile.java	2010-01-05 09:07:48 UTC (rev 1283)
+++ jcr/branches/1.12.0-OPT/exo.jcr.component.core/src/main/java/org/exoplatform/services/jcr/impl/util/io/SwapFile.java	2010-01-05 09:11:11 UTC (rev 1284)
@@ -96,6 +96,7 @@
          {
             CountDownLatch spoolLatch = swapped.spoolLatch;
             if (spoolLatch != null)
+            {
                try
                {
                   spoolLatch.await(); // wait till the file will be done
@@ -112,6 +113,8 @@
                      }
                   };
                }
+            }
+            swapped.spoolLatch = new CountDownLatch(1);
             return swapped;
          }
 



More information about the exo-jcr-commits mailing list