This closes #1313
This commit is contained in:
commit
2aeb797108
artemis-jdbc-store/src
main/java/org/apache/activemq/artemis/jdbc/store/file
test/java/org/apache/activemq/artemis/jdbc/file
|
@ -45,7 +45,7 @@ public class JDBCSequentialFile implements SequentialFile {
|
||||||
|
|
||||||
private boolean isOpen = false;
|
private boolean isOpen = false;
|
||||||
|
|
||||||
private boolean isCreated = false;
|
private boolean isLoaded = false;
|
||||||
|
|
||||||
private long id = -1;
|
private long id = -1;
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ public class JDBCSequentialFile implements SequentialFile {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean exists() {
|
public boolean exists() {
|
||||||
if (isCreated) return true;
|
if (isLoaded) return true;
|
||||||
try {
|
try {
|
||||||
return fileFactory.listFiles(extension).contains(filename);
|
return fileFactory.listFiles(extension).contains(filename);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -100,13 +100,17 @@ public class JDBCSequentialFile implements SequentialFile {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void open() throws Exception {
|
public void open() throws Exception {
|
||||||
try {
|
load();
|
||||||
if (!isOpen) {
|
|
||||||
synchronized (writeLock) {
|
|
||||||
dbDriver.openFile(this);
|
|
||||||
isCreated = true;
|
|
||||||
isOpen = true;
|
isOpen = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void load() {
|
||||||
|
try {
|
||||||
|
synchronized (writeLock) {
|
||||||
|
if (!isLoaded) {
|
||||||
|
dbDriver.openFile(this);
|
||||||
|
isLoaded = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
fileFactory.onIOError(e, "Error attempting to open JDBC file.", this);
|
fileFactory.onIOError(e, "Error attempting to open JDBC file.", this);
|
||||||
|
@ -141,11 +145,10 @@ public class JDBCSequentialFile implements SequentialFile {
|
||||||
@Override
|
@Override
|
||||||
public void delete() throws IOException, InterruptedException, ActiveMQException {
|
public void delete() throws IOException, InterruptedException, ActiveMQException {
|
||||||
try {
|
try {
|
||||||
if (isCreated) {
|
|
||||||
synchronized (writeLock) {
|
synchronized (writeLock) {
|
||||||
|
load();
|
||||||
dbDriver.deleteFile(this);
|
dbDriver.deleteFile(this);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
fileFactory.onIOError(e, "Error deleting JDBC file.", this);
|
fileFactory.onIOError(e, "Error deleting JDBC file.", this);
|
||||||
}
|
}
|
||||||
|
@ -298,6 +301,7 @@ public class JDBCSequentialFile implements SequentialFile {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long size() throws Exception {
|
public long size() throws Exception {
|
||||||
|
load();
|
||||||
return writePosition;
|
return writePosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -186,6 +186,36 @@ public class JDBCSequentialFileFactoryTest {
|
||||||
assertEquals(bufferSize, file.size());
|
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 {
|
private void checkData(JDBCSequentialFile file, ActiveMQBuffer expectedData) throws SQLException {
|
||||||
expectedData.resetReaderIndex();
|
expectedData.resetReaderIndex();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue