From a9398c230f338f62d1d1e5b7f9fa43f6a17f429b Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 13 Jan 2007 20:26:28 +0000 Subject: MergeSortBuffer detects misconfiguration early. --- .../amazon/carbonado/cursor/MergeSortBuffer.java | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java b/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java index 0094183..807a0b6 100644 --- a/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java +++ b/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java @@ -123,6 +123,8 @@ public class MergeSortBuffer extends AbstractCollection } public boolean add(S storable) { + Comparator comparator = comparator(); + arrayPrep: if (mSize >= mElements.length) { if (mElements.length < mMaxArrayCapacity) { @@ -152,7 +154,7 @@ public class MergeSortBuffer extends AbstractCollection } } - Arrays.sort(mElements, mComparator); + Arrays.sort(mElements, comparator); RandomAccessFile raf; try { @@ -248,6 +250,8 @@ public class MergeSortBuffer extends AbstractCollection } private Iterator iterator(List filesToMerge) { + Comparator comparator = comparator(); + if (mSerializer == null) { return new ObjectArrayIterator(mElements, 0, mSize); } @@ -256,7 +260,7 @@ public class MergeSortBuffer extends AbstractCollection // next buffer to pull an element from. PriorityQueue> pq = new PriorityQueue>(1 + mFilesInUse.size()); - pq.add(new ArrayIter(mComparator, mElements, mSize)); + pq.add(new ArrayIter(comparator, mElements, mSize)); for (RandomAccessFile raf : filesToMerge) { try { raf.seek(0); @@ -266,7 +270,7 @@ public class MergeSortBuffer extends AbstractCollection InputStream in = new BufferedInputStream(new RAFInputStream(raf)); - pq.add(new InputIter(mComparator, mSerializer, mStorage, in)); + pq.add(new InputIter(comparator, mSerializer, mStorage, in)); } return new Merger(pq); @@ -286,10 +290,7 @@ public class MergeSortBuffer extends AbstractCollection public void sort() { // Sort current in-memory results. Anything residing in files has // already been sorted. - if (mComparator == null) { - throw new IllegalStateException("Buffer was not prepared"); - } - Arrays.sort(mElements, 0, mSize, mComparator); + Arrays.sort(mElements, 0, mSize, comparator()); } public void close() { @@ -303,6 +304,14 @@ public class MergeSortBuffer extends AbstractCollection mStop = true; } + private Comparator comparator() { + Comparator comparator = mComparator; + if (comparator == null) { + throw new IllegalStateException("Buffer was not prepared with a Comparator"); + } + return comparator; + } + private void interruptCheck(int count) { if ((count & ~0xff) == 0 && (mStop || Thread.interrupted())) { close(); -- cgit v1.2.3