Package com.leansoft.bigqueue.page

Examples of com.leansoft.bigqueue.page.IMappedPage


  @Test
  public void testSingleThread() throws IOException {
 
    mappedPageFactory = new MappedPageFactoryImpl(1024 * 1024 * 128, testDir + "/test_single_thread", 2 * 1000);
   
    IMappedPage mappedPage = mappedPageFactory.acquirePage(0); // first acquire
    assertNotNull(mappedPage);
    IMappedPage mappedPage0 = mappedPageFactory.acquirePage(0); // second acquire
    assertSame(mappedPage, mappedPage0);
   
    IMappedPage mappedPage1 = mappedPageFactory.acquirePage(1);
    assertNotSame(mappedPage0, mappedPage1);
   
    mappedPageFactory.releasePage(0); // release first acquire
    mappedPageFactory.releasePage(0); // release second acquire
    TestUtil.sleepQuietly(2200);// let page0 expire
    mappedPageFactory.acquirePage(2);// trigger mark&sweep and purge old page0
    mappedPage = mappedPageFactory.acquirePage(0);// create a new page0
    assertNotSame(mappedPage, mappedPage0);
    TestUtil.sleepQuietly(1000);// let the async cleaner do the job
    assertTrue(!mappedPage.isClosed());
    assertTrue(mappedPage0.isClosed());
   
   
    for(long i = 0; i < 100; i++) {
      assertNotNull(mappedPageFactory.acquirePage(i));
    }
    assertTrue(mappedPageFactory.getCacheSize() == 100);
    Set<Long> indexSet = mappedPageFactory.getExistingBackFileIndexSet();
    assertTrue(indexSet.size() == 100);
    for(long i = 0; i < 100; i++) {
      assertTrue(indexSet.contains(i));
    }
   
    this.mappedPageFactory.deletePage(0);
    assertTrue(mappedPageFactory.getCacheSize() == 99);
    indexSet = mappedPageFactory.getExistingBackFileIndexSet();
    assertTrue(indexSet.size() == 99);
   
    this.mappedPageFactory.deletePage(1);
    assertTrue(mappedPageFactory.getCacheSize() == 98);
    indexSet = mappedPageFactory.getExistingBackFileIndexSet();
    assertTrue(indexSet.size() == 98);
   
    for(long i = 2; i < 50; i++) {
      this.mappedPageFactory.deletePage(i);
    }
    assertTrue(mappedPageFactory.getCacheSize() == 50);
    indexSet = mappedPageFactory.getExistingBackFileIndexSet();
    assertTrue(indexSet.size() == 50);
   
    this.mappedPageFactory.deleteAllPages();
    assertTrue(mappedPageFactory.getCacheSize() == 0);
    indexSet = mappedPageFactory.getExistingBackFileIndexSet();
    assertTrue(indexSet.size() == 0);
   
    long start = System.currentTimeMillis();
    for(long i = 0; i < 5; i++) {
      assertNotNull(this.mappedPageFactory.acquirePage(i));
      TestUtil.sleepQuietly(1000);
    }
    indexSet = mappedPageFactory.getPageIndexSetBefore(start - 1000);
    assertTrue(indexSet.size() == 0);
    indexSet = mappedPageFactory.getPageIndexSetBefore(start + 2500);
    assertTrue(indexSet.size() == 3);
    indexSet = mappedPageFactory.getPageIndexSetBefore(start + 5000);
    assertTrue(indexSet.size() == 5);
   
    mappedPageFactory.deletePagesBefore(start + 2500);
    indexSet = mappedPageFactory.getExistingBackFileIndexSet();
    assertTrue(indexSet.size() == 2);
    assertTrue(mappedPageFactory.getCacheSize() == 2);
   
    mappedPageFactory.releaseCachedPages();
    assertTrue(mappedPageFactory.getCacheSize() == 0);
   
    assertTrue(((MappedPageFactoryImpl)mappedPageFactory).getLockMapSize() == 0);
    mappedPageFactory.deleteAllPages();
   
    start = System.currentTimeMillis();
    for(int i = 0; i <= 100; i++) {
      IMappedPage mappedPageI = mappedPageFactory.acquirePage(i);
      mappedPageI.getLocal(0).put(("hello " + i).getBytes());
      mappedPageI.setDirty(true);
      ((MappedPageImpl)mappedPageI).flush();
      long currentTime = System.currentTimeMillis();
      long iPageFileLastModifiedTime = mappedPageFactory.getPageFileLastModifiedTime(i);
      assertTrue(iPageFileLastModifiedTime >= start);
      assertTrue(iPageFileLastModifiedTime <= currentTime);
View Full Code Here


    }
   
    // validate
    IMappedPage[] firstPageArray = sharedMap.get(0);
    for(int j = 0; j < pageNumLimit; j++) {
      IMappedPage page = firstPageArray[j];
      assertTrue(!page.isClosed());
    }
    for(int i = 1; i < threadNum; i++) {
      IMappedPage[] pageArray = sharedMap.get(i);
      for(int j = 0; j < pageNumLimit; j++) {
        assertSame(firstPageArray[j], pageArray[j]);
View Full Code Here

  @Test
  public void testSingleThread() throws IOException {
    int pageSize = 1024 * 1024 * 32;
    mappedPageFactory = new MappedPageFactoryImpl(pageSize, testDir + "/test_single_thread", 2 * 1000);
   
    IMappedPage mappedPage = this.mappedPageFactory.acquirePage(0);
    assertNotNull(mappedPage);
   
    ByteBuffer buffer = mappedPage.getLocal(0);
    assertTrue(buffer.limit() == pageSize);
    assertTrue(buffer.position() == 0);
   
   
    for(int i = 0; i < 10000; i++) {
      String hello = "hello world";
      int length = hello.getBytes().length;
      mappedPage.getLocal(i * 20).put(hello.getBytes());
      assertTrue(Arrays.equals(mappedPage.getLocal(i * 20 , length), hello.getBytes()));
    }
   
    buffer = ByteBuffer.allocateDirect(16);
    buffer.putInt(1);
    buffer.putInt(2);
    buffer.putLong(3L);
    for(int i = 0; i < 10000; i++) {
      buffer.flip();
      mappedPage.getLocal(i * 20).put(buffer);
    }
    for(int i = 0; i < 10000; i++) {
      ByteBuffer buf = mappedPage.getLocal(i * 20);
      assertTrue(1 == buf.getInt());
      assertTrue(2 == buf.getInt());
      assertTrue(3L == buf.getLong());
    }
  }
View Full Code Here

    }
   
    public void run() {
      for(int i = 0; i < pageNumLimit; i++) {
        try {
          IMappedPage page = this.pageFactory.acquirePage(i);
          sharedPageSet.add(page);
          localBufferList.add(page.getLocal(0));
         
          int startPosition = this.id * 2048;
         
          for(int j = 0; j < 100; j++) {
            String helloj = "hello world " + j;
            int length = helloj.getBytes().length;
            page.getLocal(startPosition + j * 20).put(helloj.getBytes());
            assertTrue(Arrays.equals(page.getLocal(startPosition + j * 20 , length), helloj.getBytes()));
          }
         
          ByteBuffer buffer = ByteBuffer.allocateDirect(16);
          buffer.putInt(1);
          buffer.putInt(2);
          buffer.putLong(3L);
          for(int j = 0; j < 100; j++) {
            buffer.flip();
            page.getLocal(startPosition + j * 20).put(buffer);
          }
          for(int j = 0; j < 100; j++) {
            ByteBuffer buf = page.getLocal(startPosition + j * 20);
            assertTrue(1 == buf.getInt());
            assertTrue(2 == buf.getInt());
            assertTrue(3L == buf.getLong());
          }
         
View Full Code Here

      // the ttl does not matter here since queue front index page is always cached
      this.indexPageFactory = new MappedPageFactoryImpl(QUEUE_FRONT_INDEX_PAGE_SIZE,
          innerArray.arrayDirectory + QUEUE_FRONT_INDEX_PAGE_FOLDER_PREFIX + fanoutId,
          10 * 1000/*does not matter*/);
     
      IMappedPage indexPage = this.indexPageFactory.acquirePage(QUEUE_FRONT_PAGE_INDEX);
     
      ByteBuffer indexBuffer = indexPage.getLocal(0);
      index.set(indexBuffer.getLong());
      validateAndAdjustIndex();
    }
View Full Code Here

      this.persistIndex();
    }
   
    void persistIndex() throws IOException {
      // persist index
      IMappedPage indexPage = this.indexPageFactory.acquirePage(QUEUE_FRONT_PAGE_INDEX);
      ByteBuffer indexBuffer = indexPage.getLocal(0);
      indexBuffer.putLong(index.get());
      indexPage.setDirty(true);
    }
View Full Code Here

TOP

Related Classes of com.leansoft.bigqueue.page.IMappedPage

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.