JDBCSequentialFileFactory implements SequentialFileFactory, ActiveMQComponent { @Override public SequentialFile createSequentialFile(String fileName) { try { fileLocks.putIfAbsent(fileName, new Object()); JDBCSequentialFile file = new JDBCSequentialFile(this, fileName, executor, dbDriver, fileLocks.get(fileName)); files.add(file); return file; } catch (Exception e) { criticalErrorListener.onIOException(e, "Error whilst creating JDBC file", null); } return null; } JDBCSequentialFileFactory(final JDBCConnectionProvider connectionProvider,
                                    final SQLProvider sqlProvider,
                                    Executor executor,
                                    IOCriticalErrorListener criticalErrorListener); JDBCSequentialFileFactoryDriver getDbDriver(); @Override SequentialFileFactory setDatasync(boolean enabled); @Override boolean isDatasync(); @Override long getBufferSize(); @Override synchronized void start(); @Override synchronized void stop(); @Override SequentialFile createSequentialFile(String fileName); void sequentialFileClosed(SequentialFile file); int getNumberOfOpenFiles(); @Override int getMaxIO(); @Override List<String> listFiles(String extension); @Override boolean isSupportsCallbacks(); @Override void onIOError(Exception exception, String message, SequentialFile file); @Override ByteBuffer allocateDirectBuffer(final int size); @Override void releaseDirectBuffer(ByteBuffer buffer); @Override ByteBuffer newBuffer(final int size); @Override void clearBuffer(final ByteBuffer buffer); @Override ByteBuffer wrapBuffer(final byte[] bytes); @Override int getAlignment(); @Override JDBCSequentialFileFactory setAlignment(int alignment); @Override int calculateBlockSize(final int bytes); @Override void deactivateBuffer(); @Override void releaseBuffer(final ByteBuffer buffer); @Override void activateBuffer(SequentialFile file); @Override File getDirectory(); @Override boolean isStarted(); @Override void createDirs(); @Override void flush(); synchronized void destroy();  }
@Test public void testReadZeroBytesOnEmptyFile() throws Exception { JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); try { final ByteBuffer readBuffer = ByteBuffer.allocate(0); final int bytes = file.read(readBuffer); assertEquals(0, bytes); } finally { file.close(); } }
@Test public void testReadZeroBytesOnNotEmptyFile() throws Exception { final int fileLength = 8; JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); try { file.writeDirect(ByteBuffer.allocate(fileLength), true); assertEquals(fileLength, file.size()); final ByteBuffer readBuffer = ByteBuffer.allocate(0); final int bytes = file.read(readBuffer); assertEquals(0, bytes); } finally { file.close(); } }
@Test public void testReadOutOfBoundsOnEmptyFile() throws Exception { JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); try { final ByteBuffer readBuffer = ByteBuffer.allocate(1); file.position(1); final int bytes = file.read(readBuffer); assertTrue("bytes read should be < 0", bytes < 0); } finally { file.close(); } }
@Test public void testReadOutOfBoundsOnNotEmptyFile() throws Exception { final int fileLength = 8; JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); try { file.writeDirect(ByteBuffer.allocate(fileLength), true); assertEquals(fileLength, file.size()); file.position(fileLength + 1); final ByteBuffer readBuffer = ByteBuffer.allocate(fileLength); final int bytes = file.read(readBuffer); assertTrue("bytes read should be < 0", bytes < 0); } finally { file.close(); } }
@Test public void testAsyncAppendToFile() throws Exception { JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); int bufferSize = 1024; ActiveMQBuffer src = ActiveMQBuffers.fixedBuffer(bufferSize); for (int i = 0; i < bufferSize; i++) { src.writeByte((byte) 1); } IOCallbackCountdown callback = new IOCallbackCountdown(1); file.internalWrite(src, callback); callback.assertEmpty(5); checkData(file, src); }
@Test public void testWriteToFile() throws Exception { JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); ActiveMQBuffer src = ActiveMQBuffers.fixedBuffer(1); src.writeByte((byte)7); file.internalWrite(src, null); checkData(file, src); assertEquals(1, file.size()); file.close(); file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); int bufferSize = 1024; src = ActiveMQBuffers.fixedBuffer(bufferSize); for (int i = 0; i < bufferSize; i++) { src.writeByte((byte)i); } file.internalWrite(src, null, false); checkData(file, src); assertEquals(bufferSize, file.size()); }
@Test public void testCopyFile() throws Exception { JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); int bufferSize = 1024; ActiveMQBuffer src = ActiveMQBuffers.fixedBuffer(bufferSize); for (int i = 0; i < bufferSize; i++) { src.writeByte((byte) 5); } IOCallbackCountdown callback = new IOCallbackCountdown(1); file.internalWrite(src, callback); JDBCSequentialFile copy = (JDBCSequentialFile) factory.createSequentialFile("copy.txt"); file.copyTo(copy); checkData(file, src); checkData(copy, src); assertEquals(bufferSize, copy.size()); assertEquals(bufferSize, file.size()); }
@Test public void testCloneFile() throws Exception { JDBCSequentialFile file = (JDBCSequentialFile) factory.createSequentialFile("test.txt"); file.open(); int bufferSize = 1024; ActiveMQBuffer src = ActiveMQBuffers.fixedBuffer(bufferSize); for (int i = 0; i < bufferSize; i++) { src.writeByte((byte) 5); } IOCallbackCountdown callback = new IOCallbackCountdown(1); file.internalWrite(src, callback); assertEquals(bufferSize, file.size()); JDBCSequentialFile copy = (JDBCSequentialFile) file.cloneFile(); copy.open(); assertEquals(bufferSize, copy.size()); assertEquals(bufferSize, file.size()); }
@Test public void testGetFileSizeWorksWhenNotOpen() throws Exception { int testFileSize = 1024; String fileName = "testFile.txt"; SequentialFile file = factory.createSequentialFile(fileName); file.open(); ActiveMQBuffer buffer = ActiveMQBuffers.wrappedBuffer(new byte[1024]); file.write(buffer, true); file.close(); try { SequentialFile file2 = factory.createSequentialFile(fileName); assertEquals(testFileSize, file2.size()); } catch (Throwable t) { t.printStackTrace(); } }