Package com.leansoft.bigqueue.page

Examples of com.leansoft.bigqueue.page.IMappedPage


        innerArray = new BigArrayImpl(path, name);
        // the ttl does not matter here since queue front index page is always cached
        this.queueFrontIndexPageFactory = new MappedPageFactoryImpl(QUEUE_FRONT_INDEX_PAGE_SIZE,
                ((BigArrayImpl)innerArray).getArrayDirectory() + QUEUE_FRONT_INDEX_PAGE_FOLDER,
                10 * 1000/*does not matter*/);
        IMappedPage queueFrontIndexPage = this.queueFrontIndexPageFactory.acquirePage(QUEUE_FRONT_PAGE_INDEX);

        ByteBuffer queueFrontIndexBuffer = queueFrontIndexPage.getLocal(0);
        queueFrontIndex.set(queueFrontIndexBuffer.getLong());

        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
View Full Code Here


        }
    }

    private void commitInternal() throws IOException {
        // persist the queue front
        IMappedPage queueFrontIndexPage = null;
        queueFrontIndexPage = this.queueFrontIndexPageFactory.acquirePage(QUEUE_FRONT_PAGE_INDEX);
        ByteBuffer queueFrontIndexBuffer = queueFrontIndexPage.getLocal(0);
        queueFrontIndexBuffer.putLong(queueFrontIndex.get());
        queueFrontIndexPage.setDirty(true);
    }
View Full Code Here

   
  }
 
  // find out array head/tail from the meta data
  void initArrayIndex() throws IOException {
    IMappedPage metaDataPage = this.metaPageFactory.acquirePage(META_DATA_PAGE_INDEX);
    ByteBuffer metaBuf = metaDataPage.getLocal(0);
    long head = metaBuf.getLong();
    long tail = metaBuf.getLong();
   
    arrayHeadIndex.set(head);
    arrayTailIndex.set(tail);
View Full Code Here

    if (this.isEmpty()) {
      headDataPageIndex = 0L;
      headDataItemOffset = 0;
    } else {
      IMappedPage previousIndexPage = null;
      long previousIndexPageIndex = -1;
      try {
        long previousIndex = this.arrayHeadIndex.get() - 1;
        if (previousIndex < 0) {
          previousIndex = Long.MAX_VALUE; // wrap
        }
        previousIndexPageIndex = Calculator.div(previousIndex, INDEX_ITEMS_PER_PAGE_BITS); // shift optimization
        previousIndexPage = this.indexPageFactory.acquirePage(previousIndexPageIndex);
        int previousIndexPageOffset = (int) (Calculator.mul(Calculator.mod(previousIndex, INDEX_ITEMS_PER_PAGE_BITS), INDEX_ITEM_LENGTH_BITS));
        ByteBuffer previousIndexItemBuffer = previousIndexPage.getLocal(previousIndexPageOffset);
        long previousDataPageIndex = previousIndexItemBuffer.getLong();
        int previousDataItemOffset = previousIndexItemBuffer.getInt();
        int perviousDataItemLength = previousIndexItemBuffer.getInt();
       
        headDataPageIndex = previousDataPageIndex;
View Full Code Here

   * Append the data into the head of the array
   */
  public long append(byte[] data) throws IOException {
    try {
      arrayReadLock.lock();
      IMappedPage toAppendDataPage = null;
      IMappedPage toAppendIndexPage = null;
      long toAppendIndexPageIndex = -1L;
      long toAppendDataPageIndex = -1L;
     
      long toAppendArrayIndex = -1L;
     
      try {
        appendLock.lock(); // only one thread can append
     
        if (this.isFull()) { // end of the world check:)
          throw new IOException("ring space of java long type used up, the end of the world!!!");
        }
       
        // prepare the data pointer
        if (this.headDataItemOffset + data.length > DATA_PAGE_SIZE) { // not enough space
          if (this.headDataPageIndex == Long.MAX_VALUE) {
            this.headDataPageIndex = 0L; // wrap
          } else {
            this.headDataPageIndex++;
          }
          this.headDataItemOffset = 0;
        }
       
        toAppendDataPageIndex = this.headDataPageIndex;
        int toAppendDataItemOffset  = this.headDataItemOffset;
       
        toAppendArrayIndex = this.arrayHeadIndex.get();
       
        // append data
        toAppendDataPage = this.dataPageFactory.acquirePage(toAppendDataPageIndex);
        ByteBuffer toAppendDataPageBuffer = toAppendDataPage.getLocal(toAppendDataItemOffset);
        toAppendDataPageBuffer.put(data);
        toAppendDataPage.setDirty(true);
        // update to next
        this.headDataItemOffset += data.length;
       
        toAppendIndexPageIndex = Calculator.div(toAppendArrayIndex, INDEX_ITEMS_PER_PAGE_BITS); // shift optimization
        toAppendIndexPage = this.indexPageFactory.acquirePage(toAppendIndexPageIndex);
        int toAppendIndexItemOffset = (int) (Calculator.mul(Calculator.mod(toAppendArrayIndex, INDEX_ITEMS_PER_PAGE_BITS), INDEX_ITEM_LENGTH_BITS));
       
        // update index
        ByteBuffer toAppendIndexPageBuffer = toAppendIndexPage.getLocal(toAppendIndexItemOffset);
        toAppendIndexPageBuffer.putLong(toAppendDataPageIndex);
        toAppendIndexPageBuffer.putInt(toAppendDataItemOffset);
        toAppendIndexPageBuffer.putInt(data.length);
        long currentTime = System.currentTimeMillis();
        toAppendIndexPageBuffer.putLong(currentTime);
        toAppendIndexPage.setDirty(true);
       
        // advance the head
        this.arrayHeadIndex.incrementAndGet();
       
        // update meta data
        IMappedPage metaDataPage = this.metaPageFactory.acquirePage(META_DATA_PAGE_INDEX);
        ByteBuffer metaDataBuf = metaDataPage.getLocal(0);
        metaDataBuf.putLong(this.arrayHeadIndex.get());
        metaDataBuf.putLong(this.arrayTailIndex.get());
        metaDataPage.setDirty(true);
 
      } finally {
       
        appendLock.unlock();
       
View Full Code Here

  public byte[] get(long index) throws IOException {
    try {
      arrayReadLock.lock();
      validateIndex(index);
     
      IMappedPage dataPage = null;
      long dataPageIndex = -1L;
      try {
        ByteBuffer indexItemBuffer = this.getIndexItemBuffer(index);
        dataPageIndex = indexItemBuffer.getLong();
        int dataItemOffset = indexItemBuffer.getInt();
        int dataItemLength = indexItemBuffer.getInt();
        dataPage = this.dataPageFactory.acquirePage(dataPageIndex);
        byte[] data = dataPage.getLocal(dataItemOffset, dataItemLength);
        return data;
      } finally {
        if (dataPage != null) {
          this.dataPageFactory.releasePage(dataPageIndex);
        }
View Full Code Here

    }
  }
 
  ByteBuffer getIndexItemBuffer(long index) throws IOException {
   
    IMappedPage indexPage = null;
    long indexPageIndex = -1L;
    try {
      indexPageIndex = Calculator.div(index, INDEX_ITEMS_PER_PAGE_BITS); // shift optimization
      indexPage = this.indexPageFactory.acquirePage(indexPageIndex);
      int indexItemOffset = (int) (Calculator.mul(Calculator.mod(index, INDEX_ITEMS_PER_PAGE_BITS), INDEX_ITEM_LENGTH_BITS));
     
      ByteBuffer indexItemBuffer = indexPage.getLocal(indexItemOffset);
      return indexItemBuffer;
    } finally {
      if (indexPage != null) {
        this.indexPageFactory.releasePage(indexPageIndex);
      }
View Full Code Here

        // the ttl does not matter here since queue front index page is always cached
        this.queueFrontIndexPageFactory = new MappedPageFactoryImpl(QUEUE_FRONT_INDEX_PAGE_SIZE,
                ((BigArrayImpl) innerArray).getArrayDirectory() + QUEUE_FRONT_INDEX_PAGE_FOLDER,
                10 * 1000/*does not matter*/);
        IMappedPage queueFrontIndexPage = this.queueFrontIndexPageFactory.acquirePage(QUEUE_FRONT_PAGE_INDEX);

        ByteBuffer queueFrontIndexBuffer = queueFrontIndexPage.getLocal(0);
        long front = queueFrontIndexBuffer.getLong();
        queueFrontIndex.set(front);
    }
View Full Code Here

            } else {
                nextQueueFrontIndex++;
            }
            this.queueFrontIndex.set(nextQueueFrontIndex);
            // persist the queue front
            IMappedPage queueFrontIndexPage = this.queueFrontIndexPageFactory.acquirePage(QUEUE_FRONT_PAGE_INDEX);
            ByteBuffer queueFrontIndexBuffer = queueFrontIndexPage.getLocal(0);
            queueFrontIndexBuffer.putLong(nextQueueFrontIndex);
            queueFrontIndexPage.setDirty(true);
            return data;
        } finally {
            queueFrontWriteLock.unlock();
        }
View Full Code Here

    public void removeAll() throws IOException {
        try {
            queueFrontWriteLock.lock();
            this.innerArray.removeAll();
            this.queueFrontIndex.set(0L);
            IMappedPage queueFrontIndexPage = this.queueFrontIndexPageFactory.acquirePage(QUEUE_FRONT_PAGE_INDEX);
            ByteBuffer queueFrontIndexBuffer = queueFrontIndexPage.getLocal(0);
            queueFrontIndexBuffer.putLong(0L);
            queueFrontIndexPage.setDirty(true);
        } finally {
            queueFrontWriteLock.unlock();
        }
    }
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.