From 96c1a940cf1c91b389ee39872ec3ab0444252b2d Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Mon, 7 Apr 2008 00:58:24 +0000 Subject: MergeSortBuffer optionally accepts a Storage now. --- .../amazon/carbonado/cursor/MergeSortBuffer.java | 80 ++++++++++++++++++---- 1 file changed, 67 insertions(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java b/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java index 1057d01..b02f9bf 100644 --- a/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java +++ b/src/main/java/com/amazon/carbonado/cursor/MergeSortBuffer.java @@ -120,10 +120,11 @@ public class MergeSortBuffer extends AbstractCollection TEMP_DIR = tempDir; } - private final Storage mStorage; private final String mTempDir; private final int mMaxArrayCapacity; + private Preparer mPreparer; + private S[] mElements; private int mSize; private int mTotalSize; @@ -136,14 +137,23 @@ public class MergeSortBuffer extends AbstractCollection private volatile boolean mStop; /** - * @param storage storage type of elements + * @since 1.2 + */ + public MergeSortBuffer() { + this(null, TEMP_DIR, MAX_ARRAY_CAPACITY); + } + + /** + * @param storage storage for elements; if null use first Storable to + * prepare reloaded Storables */ public MergeSortBuffer(Storage storage) { this(storage, TEMP_DIR, MAX_ARRAY_CAPACITY); } /** - * @param storage storage type of elements + * @param storage storage for elements; if null use first Storable to + * prepare reloaded Storables * @param tempDir directory to store temp files for merging, or null for default */ public MergeSortBuffer(Storage storage, String tempDir) { @@ -151,7 +161,8 @@ public class MergeSortBuffer extends AbstractCollection } /** - * @param storage storage type of elements + * @param storage storage for elements; if null use first Storable to + * prepare reloaded Storables * @param tempDir directory to store temp files for merging, or null for default * @param maxArrayCapacity maximum amount of storables to keep in an array * before serializing to a file @@ -159,12 +170,13 @@ public class MergeSortBuffer extends AbstractCollection */ @SuppressWarnings("unchecked") public MergeSortBuffer(Storage storage, String tempDir, int maxArrayCapacity) { - if (storage == null) { - throw new IllegalArgumentException(); - } - mStorage = storage; mTempDir = tempDir; mMaxArrayCapacity = maxArrayCapacity; + + if (storage != null) { + mPreparer = new FromStorage(storage); + } + int cap = Math.min(MIN_ARRAY_CAPACITY, maxArrayCapacity); mElements = (S[]) new Storable[cap]; } @@ -178,6 +190,10 @@ public class MergeSortBuffer extends AbstractCollection } public boolean add(S storable) { + if (mPreparer == null) { + mPreparer = new FromStorable(storable); + } + Comparator comparator = comparator(); arrayPrep: @@ -320,13 +336,17 @@ public class MergeSortBuffer extends AbstractCollection InputStream in = new BufferedInputStream(new RAFInputStream(raf)); - pq.add(new InputIter(comparator, mStorage, in)); + pq.add(new InputIter(comparator, mPreparer, in)); } return new Merger(pq); } public void clear() { + if (mPreparer instanceof FromStorable) { + mPreparer = null; + } + if (mTotalSize > 0) { mSize = 0; mTotalSize = 0; @@ -369,6 +389,40 @@ public class MergeSortBuffer extends AbstractCollection } } + private static interface Preparer { + S prepare(); + } + + private static class FromStorage implements Preparer { + private final Storage mStorage; + + FromStorage(Storage storage) { + if (storage == null) { + throw new IllegalArgumentException(); + } + mStorage = storage; + } + + public S prepare() { + return mStorage.prepare(); + } + } + + private static class FromStorable implements Preparer { + private final S mStorable; + + FromStorable(S storable) { + if (storable == null) { + throw new IllegalArgumentException(); + } + mStorable = (S) storable.prepare(); + } + + public S prepare() { + return (S) mStorable.prepare(); + } + } + /** * Simple interator interface that supports peeking at next element. */ @@ -443,14 +497,14 @@ public class MergeSortBuffer extends AbstractCollection * Iterator that reads from an input stream of serialized Storables. */ private static class InputIter extends Iter { - private Storage mStorage; + private final Preparer mPreparer; private InputStream mIn; private S mNext; - InputIter(Comparator comparator, Storage storage, InputStream in) { + InputIter(Comparator comparator, Preparer preparer, InputStream in) { super(comparator); - mStorage = storage; + mPreparer = preparer; mIn = in; } @@ -460,7 +514,7 @@ public class MergeSortBuffer extends AbstractCollection } if (mIn != null) { try { - S next = mStorage.prepare(); + S next = mPreparer.prepare(); next.readFrom(mIn); mNext = next; } catch (EOFException e) { -- cgit v1.2.3