Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigArrayImpl.removeBeforeIndex throws IndexOutOfBoundsException when called with headIndex #9

Open
imjorge opened this issue Apr 24, 2014 · 0 comments

Comments

@imjorge
Copy link

imjorge commented Apr 24, 2014

Calling BigArrayImpl.removeBeforeIndex with the headIndex seems a valid use case to remove all elements from the big array, even tough you could achieve the same thing with a call to removeAll.

The following code:

    public static void main(String[] args) throws Exception {
        String basePath = new File(System.getProperty("java.io.tmpdir"), "test").getPath();
        IBigArray bigArray = new BigArrayImpl(basePath, "demo");
        try {
            // append some items into the array
            for(int i = 0; i < 2; ++i) {
              bigArray.append(String.valueOf(i).getBytes());
            }

            // get current size of the array
            System.out.println("size: " + bigArray.size());

            for (long index = bigArray.getTailIndex(); index != bigArray.getHeadIndex(); index = (index == Long.MAX_VALUE ? 0 : index + 1)) {
                String item = new String(bigArray.get(index));
                System.out.println("item[" + index + "]=" + item);
            }

            bigArray.removeBeforeIndex(bigArray.getHeadIndex());

            System.out.println("size: " + bigArray.size());

            for (long index = bigArray.getTailIndex(); index != bigArray.getHeadIndex(); index = (index == Long.MAX_VALUE ? 0 : index + 1)) {
                String item = new String(bigArray.get(index));
                System.out.println("item[" + index + "]=" + item);
            }

            // empty the big array
            bigArray.removeAll();
        } finally {
            bigArray.close();
        }
    }

outputs to the console:

size: 2
item[0]=0
item[1]=1
Exception in thread "main" java.lang.IndexOutOfBoundsException
    at com.leansoft.bigqueue.BigArrayImpl.validateIndex(BigArrayImpl.java:458)
    at com.leansoft.bigqueue.BigArrayImpl.removeBeforeIndex(BigArrayImpl.java:198)
    at com.jorge.test.BigArrayMain.main(BigArrayMain.java:27)

Perhaps removeBeforeIndex should be changed to something like:

    @Override
    public void removeBeforeIndex(long index) throws IOException {
        try {
            arrayWriteLock.lock();

            if (index == arrayHeadIndex.get()) {
                this.indexPageFactory.deleteAllPages();
                this.dataPageFactory.deleteAllPages();
                this.metaPageFactory.deleteAllPages();

                this.commonInit();
            } else {
                validateIndex(index);

                long indexPageIndex = Calculator.div(index, INDEX_ITEMS_PER_PAGE_BITS);

                ByteBuffer indexItemBuffer = this.getIndexItemBuffer(index);
                long dataPageIndex = indexItemBuffer.getLong();

                long toRemoveIndexPageTimestamp = this.indexPageFactory.getPageFileLastModifiedTime(indexPageIndex);
                long toRemoveDataPageItemstamp = this.dataPageFactory.getPageFileLastModifiedTime(dataPageIndex);

                if (toRemoveIndexPageTimestamp > 0L) { 
                    this.indexPageFactory.deletePagesBefore(toRemoveIndexPageTimestamp);
                }
                if (toRemoveDataPageItemstamp > 0L) {
                    this.dataPageFactory.deletePagesBefore(toRemoveDataPageItemstamp);
                }

                // advance the tail to index
                this.arrayTailIndex.set(index);
            }
        } finally {
            arrayWriteLock.unlock();
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant