org.apache.activemq.artemis.core.paging.impl.Page

Here are the examples of the java api class org.apache.activemq.artemis.core.paging.impl.Page taken from open source projects.

1. PageCursorProviderImpl#readPage()

View license
private void readPage(int pageId, PageCache cache) throws Exception {
    Page page = null;
    try {
        page = pagingStore.createPage(pageId);
        storageManager.beforePageRead();
        page.open();
        List<PagedMessage> pgdMessages = page.read(storageManager);
        cache.setMessages(pgdMessages.toArray(new PagedMessage[pgdMessages.size()]));
    } finally {
        try {
            if (page != null) {
                page.close(false);
            }
        } catch (Throwable ignored) {
        }
        storageManager.afterPageRead();
    }
}

2. PageCursorProviderImpl#cleanupComplete()

View license
// Protected as a way to inject testing
protected void cleanupComplete(ArrayList<PageSubscription> cursorList) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Address " + pagingStore.getAddress() + " is leaving page mode as all messages are consumed and acknowledged from the page store");
    }
    pagingStore.forceAnotherPage();
    Page currentPage = pagingStore.getCurrentPage();
    storeBookmark(cursorList, currentPage);
    pagingStore.stopPaging();
}

3. ReplicationEndpoint#handlePageEvent()

View license
/**
    * @param packet
    */
private void handlePageEvent(final ReplicationPageEventMessage packet) throws Exception {
    ConcurrentMap<Integer, Page> pages = getPageMap(packet.getStoreName());
    Page page = pages.remove(packet.getPageNumber());
    if (page == null) {
        page = getPage(packet.getStoreName(), packet.getPageNumber());
    }
    if (page != null) {
        if (packet.isDelete()) {
            if (deletePages) {
                page.delete(null);
            }
        } else {
            page.close(false);
        }
    }
}

4. ReplicationEndpoint#newPage()

View license
/**
    * @param pageId
    * @param map
    * @return
    */
private synchronized Page newPage(final int pageId, final SimpleString storeName, final ConcurrentMap<Integer, Page> map) throws Exception {
    Page page = map.get(pageId);
    if (page == null) {
        page = pageManager.getPageStore(storeName).createPage(pageId);
        page.open();
        map.put(pageId, page);
    }
    return page;
}

5. PageTest#testAdd()

Project: activemq-artemis
Source File: PageTest.java
View license
/**
    * 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");
    ArrayList<ActiveMQBuffer> buffers = 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().getAddress());
        ActiveMQTestBase.assertEqualsByteArrays(buffers.get(i).toByteBuffer().array(), msgs.get(i).getMessage().getBodyBuffer().toByteBuffer().array());
    }
    impl.delete(null);
    Assert.assertEquals(0, factory.listFiles(".page").size());
}

6. PageTest#testDamagedPage()

Project: activemq-artemis
Source File: PageTest.java
View license
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");
    ArrayList<ActiveMQBuffer> buffers = 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().getAddress());
        ActiveMQTestBase.assertEqualsByteArrays(buffers.get(i).toByteBuffer().array(), msgs.get(i).getMessage().getBodyBuffer().toByteBuffer().array());
    }
    impl.delete(null);
    Assert.assertEquals(0, factory.listFiles("page").size());
    Assert.assertEquals(1, factory.listFiles("invalidPage").size());
}

7. PagingStoreImplTest#testDepageMultiplePages()

View license
@Test
public void testDepageMultiplePages() throws Exception {
    SequentialFileFactory factory = new FakeSequentialFileFactory();
    SimpleString destination = new SimpleString("test");
    PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
    PagingStoreImpl store = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true);
    store.start();
    Assert.assertEquals(0, store.getNumberOfPages());
    store.startPaging();
    Assert.assertEquals(1, store.getNumberOfPages());
    List<ActiveMQBuffer> buffers = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        ActiveMQBuffer buffer = createRandomBuffer(i + 1L, 10);
        buffers.add(buffer);
        if (i == 5) {
            store.forceAnotherPage();
        }
        ServerMessage msg = createMessage(i, store, destination, buffer);
        final RoutingContextImpl ctx = new RoutingContextImpl(null);
        Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
    }
    Assert.assertEquals(2, store.getNumberOfPages());
    store.sync();
    int sequence = 0;
    for (int pageNr = 0; pageNr < 2; pageNr++) {
        Page page = store.depage();
        System.out.println("numberOfPages = " + store.getNumberOfPages());
        page.open();
        List<PagedMessage> msg = page.read(new NullStorageManager());
        page.close();
        Assert.assertEquals(5, msg.size());
        for (int i = 0; i < 5; i++) {
            Assert.assertEquals(sequence++, msg.get(i).getMessage().getMessageID());
            ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(pageNr * 5 + i), msg.get(i).getMessage().getBodyBuffer());
        }
    }
    Assert.assertEquals(1, store.getNumberOfPages());
    Assert.assertTrue(store.isPaging());
    ServerMessage msg = createMessage(1, store, destination, buffers.get(0));
    final RoutingContextImpl ctx = new RoutingContextImpl(null);
    Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
    Page newPage = store.depage();
    newPage.open();
    Assert.assertEquals(1, newPage.read(new NullStorageManager()).size());
    newPage.delete(null);
    Assert.assertEquals(1, store.getNumberOfPages());
    Assert.assertTrue(store.isPaging());
    Assert.assertNull(store.depage());
    Assert.assertFalse(store.isPaging());
    {
        final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
        Assert.assertFalse(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
    }
    store.startPaging();
    {
        final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
        Assert.assertTrue(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
    }
    Page page = store.depage();
    page.open();
    List<PagedMessage> msgs = page.read(new NullStorageManager());
    Assert.assertEquals(1, msgs.size());
    Assert.assertEquals(1L, msgs.get(0).getMessage().getMessageID());
    ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(0), msgs.get(0).getMessage().getBodyBuffer());
    Assert.assertEquals(1, store.getNumberOfPages());
    Assert.assertTrue(store.isPaging());
    Assert.assertNull(store.depage());
    Assert.assertEquals(0, store.getNumberOfPages());
    page.open();
}

8. PagingManagerImplTest#testPagingManager()

View license
@Test
public void testPagingManager() throws Exception {
    HierarchicalRepository<AddressSettings> addressSettings = new HierarchicalObjectRepository<>();
    addressSettings.setDefault(new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE));
    final StorageManager storageManager = new NullStorageManager();
    PagingStoreFactoryNIO storeFactory = new PagingStoreFactoryNIO(storageManager, getPageDirFile(), 100, null, getOrderedExecutor(), true, null);
    PagingManagerImpl managerImpl = new PagingManagerImpl(storeFactory, addressSettings);
    managerImpl.start();
    PagingStore store = managerImpl.getPageStore(new SimpleString("simple-test"));
    ServerMessage msg = createMessage(1L, new SimpleString("simple-test"), createRandomBuffer(10));
    final RoutingContextImpl ctx = new RoutingContextImpl(null);
    Assert.assertFalse(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
    store.startPaging();
    Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
    Page page = store.depage();
    page.open();
    List<PagedMessage> msgs = page.read(new NullStorageManager());
    page.close();
    Assert.assertEquals(1, msgs.size());
    ActiveMQTestBase.assertEqualsByteArrays(msg.getBodyBuffer().writerIndex(), msg.getBodyBuffer().toByteBuffer().array(), msgs.get(0).getMessage().getBodyBuffer().toByteBuffer().array());
    Assert.assertTrue(store.isPaging());
    Assert.assertNull(store.depage());
    final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
    Assert.assertFalse(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
}

9. ReplicationEndpoint#handlePageWrite()

View license
/**
    * @param packet
    */
private void handlePageWrite(final ReplicationPageWriteMessage packet) throws Exception {
    PagedMessage pgdMessage = packet.getPagedMessage();
    pgdMessage.initMessage(storageManager);
    ServerMessage msg = pgdMessage.getMessage();
    Page page = getPage(msg.getAddress(), packet.getPageNumber());
    page.write(pgdMessage);
}

10. PagingStoreImplTest#testDepageOnCurrentPage()

View license
@Test
public void testDepageOnCurrentPage() throws Exception {
    SequentialFileFactory factory = new FakeSequentialFileFactory();
    SimpleString destination = new SimpleString("test");
    PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
    PagingStoreImpl storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true);
    storeImpl.start();
    Assert.assertEquals(0, storeImpl.getNumberOfPages());
    storeImpl.startPaging();
    List<ActiveMQBuffer> buffers = new ArrayList<>();
    int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ActiveMQBuffer buffer = createRandomBuffer(i + 1L, 10);
        buffers.add(buffer);
        ServerMessage msg = createMessage(i, storeImpl, destination, buffer);
        final RoutingContextImpl ctx = new RoutingContextImpl(null);
        Assert.assertTrue(storeImpl.page(msg, ctx.getTransaction(), ctx.getContextListing(storeImpl.getStoreName()), lock));
    }
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    storeImpl.sync();
    Page page = storeImpl.depage();
    page.open();
    List<PagedMessage> msg = page.read(new NullStorageManager());
    Assert.assertEquals(numMessages, msg.size());
    Assert.assertEquals(1, storeImpl.getNumberOfPages());
    page = storeImpl.depage();
    Assert.assertNull(page);
    Assert.assertEquals(0, storeImpl.getNumberOfPages());
    for (int i = 0; i < numMessages; i++) {
        ActiveMQBuffer horn1 = buffers.get(i);
        ActiveMQBuffer horn2 = msg.get(i).getMessage().getBodyBuffer();
        horn1.resetReaderIndex();
        horn2.resetReaderIndex();
        for (int j = 0; j < horn1.writerIndex(); j++) {
            Assert.assertEquals(horn1.readByte(), horn2.readByte());
        }
    }
}

11. ReplicationEndpoint#getPage()

View license
private Page getPage(final SimpleString storeName, final int pageId) throws Exception {
    ConcurrentMap<Integer, Page> map = getPageMap(storeName);
    Page page = map.get(pageId);
    if (page == null) {
        page = newPage(pageId, storeName, map);
    }
    return page;
}