activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From martyntay...@apache.org
Subject activemq-artemis git commit: ARTEMIS-1204 Fix getSize() on a closed JDBC File
Date Mon, 05 Jun 2017 08:33:59 GMT
Repository: activemq-artemis
Updated Branches:
  refs/heads/1.x 06ee68c0a -> 63f0dcef6


ARTEMIS-1204 Fix getSize() on a closed JDBC File

(cherry picked from commit e7c426c5e112600254252d3565c87ab8d97320bb)


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/63f0dcef
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/63f0dcef
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/63f0dcef

Branch: refs/heads/1.x
Commit: 63f0dcef66c86552ca6de5c1e8f229dd06b4e053
Parents: 06ee68c
Author: Martyn Taylor <mtaylor@redhat.com>
Authored: Fri Jun 2 14:50:37 2017 +0100
Committer: Martyn Taylor <mtaylor@redhat.com>
Committed: Mon Jun 5 09:32:54 2017 +0100

----------------------------------------------------------------------
 .../jdbc/store/file/JDBCSequentialFile.java     | 26 ++++++++++-------
 .../file/JDBCSequentialFileFactoryTest.java     | 30 ++++++++++++++++++++
 2 files changed, 45 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/63f0dcef/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java
----------------------------------------------------------------------
diff --git a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java
b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java
index 6a668ef..4fa0b55 100644
--- a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java
+++ b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFile.java
@@ -45,7 +45,7 @@ public class JDBCSequentialFile implements SequentialFile {
 
    private boolean isOpen = false;
 
-   private boolean isCreated = false;
+   private boolean isLoaded = false;
 
    private long id = -1;
 
@@ -88,7 +88,7 @@ public class JDBCSequentialFile implements SequentialFile {
 
    @Override
    public boolean exists() {
-      if (isCreated) return true;
+      if (isLoaded) return true;
       try {
          return fileFactory.listFiles(extension).contains(filename);
       } catch (Exception e) {
@@ -99,13 +99,17 @@ public class JDBCSequentialFile implements SequentialFile {
    }
 
    @Override
-   public synchronized void open() throws Exception {
+   public void open() throws Exception {
+      load();
+      isOpen = true;
+   }
+
+   private void load() {
       try {
-         if (!isOpen) {
-            synchronized (writeLock) {
+         synchronized (writeLock) {
+            if (!isLoaded) {
                dbDriver.openFile(this);
-               isCreated = true;
-               isOpen = true;
+               isLoaded = true;
             }
          }
       } catch (SQLException e) {
@@ -141,10 +145,9 @@ public class JDBCSequentialFile implements SequentialFile {
    @Override
    public void delete() throws IOException, InterruptedException, ActiveMQException {
       try {
-         if (isCreated) {
-            synchronized (writeLock) {
-               dbDriver.deleteFile(this);
-            }
+         synchronized (writeLock) {
+            load();
+            dbDriver.deleteFile(this);
          }
       } catch (SQLException e) {
          fileFactory.onIOError(e, "Error deleting JDBC file.", this);
@@ -318,6 +321,7 @@ public class JDBCSequentialFile implements SequentialFile {
 
    @Override
    public long size() throws Exception {
+      load();
       return writePosition;
    }
 

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/63f0dcef/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/file/JDBCSequentialFileFactoryTest.java
----------------------------------------------------------------------
diff --git a/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/file/JDBCSequentialFileFactoryTest.java
b/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/file/JDBCSequentialFileFactoryTest.java
index b04b74f..9a58c8e 100644
--- a/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/file/JDBCSequentialFileFactoryTest.java
+++ b/artemis-jdbc-store/src/test/java/org/apache/activemq/artemis/jdbc/file/JDBCSequentialFileFactoryTest.java
@@ -172,6 +172,36 @@ public class JDBCSequentialFileFactoryTest {
       assertEquals(bufferSize, file.size());
    }
 
+   /**
+    * Using a real file system users are not required to call file.open() in order to read
the file size.  The file
+    * descriptor has enough information.  However, with JDBC we do require that some information
is loaded in order to
+    * get the underlying BLOB.  This tests ensures that file.size() returns the correct value,
without the user calling
+    * file.open() with JDBCSequentialFile.
+    *
+    * @throws Exception
+    */
+   @Test
+   public void testGetFileSizeWorksWhenNotOpen() throws Exception {
+      // Create test file with some data.
+      int testFileSize = 1024;
+      String fileName = "testFile.txt";
+      SequentialFile file = factory.createSequentialFile(fileName);
+      file.open();
+
+      // Write some data to the file
+      ActiveMQBuffer buffer = ActiveMQBuffers.wrappedBuffer(new byte[1024]);
+      file.write(buffer, true);
+      file.close();
+
+      try {
+         // Create a new pointer to the test file and ensure file.size() returns the correct
value.
+         SequentialFile file2 = factory.createSequentialFile(fileName);
+         assertEquals(testFileSize, file2.size());
+      } catch (Throwable t) {
+         t.printStackTrace();
+      }
+   }
+
    private void checkData(JDBCSequentialFile file, ActiveMQBuffer expectedData) throws SQLException
{
       expectedData.resetReaderIndex();
 


Mime
View raw message