Search in sources :

Example 36 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class SequentialFileFactoryTestBase method testWriteandRead.

@Test
public void testWriteandRead() throws Exception {
    SequentialFile sf = factory.createSequentialFile("write.amq");
    sf.open();
    String s1 = "aardvark";
    byte[] bytes1 = s1.getBytes(StandardCharsets.UTF_8);
    ActiveMQBuffer bb1 = wrapBuffer(bytes1);
    String s2 = "hippopotamus";
    byte[] bytes2 = s2.getBytes(StandardCharsets.UTF_8);
    ActiveMQBuffer bb2 = wrapBuffer(bytes2);
    String s3 = "echidna";
    byte[] bytes3 = s3.getBytes(StandardCharsets.UTF_8);
    ActiveMQBuffer bb3 = wrapBuffer(bytes3);
    long initialPos = sf.position();
    sf.write(bb1, true);
    long bytesWritten = sf.position() - initialPos;
    Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesWritten);
    initialPos = sf.position();
    sf.write(bb2, true);
    bytesWritten = sf.position() - initialPos;
    Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesWritten);
    initialPos = sf.position();
    sf.write(bb3, true);
    bytesWritten = sf.position() - initialPos;
    Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesWritten);
    sf.position(0);
    ByteBuffer rb1 = factory.newBuffer(bytes1.length);
    ByteBuffer rb2 = factory.newBuffer(bytes2.length);
    ByteBuffer rb3 = factory.newBuffer(bytes3.length);
    int bytesRead = sf.read(rb1);
    Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesRead);
    for (int i = 0; i < bytes1.length; i++) {
        Assert.assertEquals(bytes1[i], rb1.get(i));
    }
    bytesRead = sf.read(rb2);
    Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesRead);
    for (int i = 0; i < bytes2.length; i++) {
        Assert.assertEquals(bytes2[i], rb2.get(i));
    }
    bytesRead = sf.read(rb3);
    Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesRead);
    for (int i = 0; i < bytes3.length; i++) {
        Assert.assertEquals(bytes3[i], rb3.get(i));
    }
    sf.close();
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) ByteBuffer(java.nio.ByteBuffer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 37 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class SequentialFileFactoryTestBase method testOpenClose.

@Test
public void testOpenClose() throws Exception {
    SequentialFile sf = factory.createSequentialFile("openclose.amq");
    sf.open();
    sf.fill(512);
    String s1 = "cheesecake";
    byte[] bytes1 = s1.getBytes(StandardCharsets.UTF_8);
    long initialPos = sf.position();
    sf.write(wrapBuffer(bytes1), true);
    long bytesWritten = sf.position() - initialPos;
    Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesWritten);
    sf.close();
    try {
        sf.write(wrapBuffer(bytes1), true);
        Assert.fail("Should throw exception");
    } catch (Exception e) {
    }
    sf.open();
    sf.write(wrapBuffer(bytes1), true);
    sf.close();
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) Test(org.junit.Test)

Example 38 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class PageTest method testDamagedPage.

protected void testDamagedPage(final SequentialFileFactory factory, final int numberOfElements) throws Exception {
    SequentialFile file = factory.createSequentialFile("00010.page");
    Page impl = new Page(new SimpleString("something"), new NullStorageManager(), factory, file, 10);
    Assert.assertEquals(10, impl.getPageId());
    impl.open();
    Assert.assertEquals(1, factory.listFiles("page").size());
    SimpleString simpleDestination = new SimpleString("Test");
    addPageElements(simpleDestination, impl, numberOfElements);
    impl.sync();
    long positionA = file.position();
    // Add one record that will be damaged
    addPageElements(simpleDestination, impl, 1);
    long positionB = file.position();
    // Add more 10 as they will need to be ignored
    addPageElements(simpleDestination, impl, 10);
    // Damage data... position the file on the middle between points A and B
    file.position(positionA + (positionB - positionA) / 2);
    ByteBuffer buffer = ByteBuffer.allocate((int) (positionB - file.position()));
    for (int i = 0; i < buffer.capacity(); i++) {
        buffer.put((byte) 'Z');
    }
    buffer.rewind();
    file.writeDirect(buffer, true);
    impl.close();
    file = factory.createSequentialFile("00010.page");
    file.open();
    impl = new Page(new SimpleString("something"), new NullStorageManager(), factory, file, 10);
    List<PagedMessage> msgs = impl.read(new NullStorageManager());
    Assert.assertEquals(numberOfElements, msgs.size());
    Assert.assertEquals(numberOfElements, impl.getNumberOfMessages());
    for (int i = 0; i < msgs.size(); i++) {
        Assert.assertEquals(simpleDestination, msgs.get(i).getMessage().getAddressSimpleString());
    }
    impl.delete(null);
    Assert.assertEquals(0, factory.listFiles("page").size());
    Assert.assertEquals(1, factory.listFiles("invalidPage").size());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) NullStorageManager(org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Page(org.apache.activemq.artemis.core.paging.impl.Page) ByteBuffer(java.nio.ByteBuffer)

Example 39 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class PageTest method testAdd.

/**
 * Validate if everything we add is recovered
 */
protected void testAdd(final SequentialFileFactory factory, final int numberOfElements) throws Exception {
    SequentialFile file = factory.createSequentialFile("00010.page");
    Page impl = new Page(new SimpleString("something"), new NullStorageManager(), factory, file, 10);
    Assert.assertEquals(10, impl.getPageId());
    impl.open();
    Assert.assertEquals(1, factory.listFiles("page").size());
    SimpleString simpleDestination = new SimpleString("Test");
    addPageElements(simpleDestination, impl, numberOfElements);
    impl.sync();
    impl.close();
    file = factory.createSequentialFile("00010.page");
    file.open();
    impl = new Page(new SimpleString("something"), new NullStorageManager(), factory, file, 10);
    List<PagedMessage> msgs = impl.read(new NullStorageManager());
    Assert.assertEquals(numberOfElements, msgs.size());
    Assert.assertEquals(numberOfElements, impl.getNumberOfMessages());
    for (int i = 0; i < msgs.size(); i++) {
        Assert.assertEquals(simpleDestination, msgs.get(i).getMessage().getAddressSimpleString());
    }
    impl.delete(null);
    Assert.assertEquals(0, factory.listFiles(".page").size());
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) NullStorageManager(org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Page(org.apache.activemq.artemis.core.paging.impl.Page)

Example 40 with SequentialFile

use of org.apache.activemq.artemis.core.io.SequentialFile in project activemq-artemis by apache.

the class JDBCSequentialFileFactoryTest method testGetFileSizeWorksWhenNotOpen.

/**
 * 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();
    }
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) JDBCSequentialFile(org.apache.activemq.artemis.jdbc.store.file.JDBCSequentialFile) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Aggregations

SequentialFile (org.apache.activemq.artemis.core.io.SequentialFile)53 Test (org.junit.Test)21 ByteBuffer (java.nio.ByteBuffer)15 ArrayList (java.util.ArrayList)10 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)9 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)7 Pair (org.apache.activemq.artemis.api.core.Pair)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 SequentialFileFactory (org.apache.activemq.artemis.core.io.SequentialFileFactory)4 NIOSequentialFileFactory (org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory)4 Page (org.apache.activemq.artemis.core.paging.impl.Page)4 JDBCSequentialFile (org.apache.activemq.artemis.jdbc.store.file.JDBCSequentialFile)4 SimpleEncoding (org.apache.activemq.artemis.tests.unit.core.journal.impl.fakes.SimpleEncoding)4 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 IOCriticalErrorListener (org.apache.activemq.artemis.core.io.IOCriticalErrorListener)3 PagedMessage (org.apache.activemq.artemis.core.paging.PagedMessage)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2