Github user maryannxue commented on a diff in the pull request:

    https://github.com/apache/phoenix/pull/171#discussion_r65293343
  
    --- Diff: 
phoenix-core/src/main/java/org/apache/phoenix/iterate/DeferredByteBufferSegmentQueue.java
 ---
    @@ -0,0 +1,123 @@
    +package org.apache.phoenix.iterate;
    +
    +import org.apache.commons.io.output.DeferredFileOutputStream;
    +import org.apache.phoenix.memory.MemoryManager;
    +import org.apache.phoenix.memory.MemoryManager.MemoryChunk;
    +
    +import java.io.*;
    +import java.util.*;
    +
    +public abstract class DeferredByteBufferSegmentQueue<T> extends 
BufferSegmentQueue<T> {
    +
    +    final MemoryChunk chunk;
    +
    +    public DeferredByteBufferSegmentQueue(int index, int thresholdBytes,
    +                                          boolean hasMaxQueueSize, 
MemoryManager memoryManager) {
    +        super(index, thresholdBytes, hasMaxQueueSize);
    +        chunk = memoryManager.allocate(thresholdBytes);
    +    }
    +
    +    abstract protected void writeToBuffer(OutputStream outputStream, T e);
    +    abstract protected T readFromBuffer(DataInput dataInput);
    +
    +
    +    @Override
    +    protected SegmentQueueFileIterator 
createSegmentQueueFileIterator(SegmentQueueFileIterator iterator){
    +        return new DeferredSegmentQueueFileIterator(iterator);
    +    }
    +
    +    @Override
    +    protected SegmentQueueFileIterator createSegmentQueueFileIterator(){
    +        return new DeferredSegmentQueueFileIterator();
    +    }
    +
    +    @Override
    +    protected void flush(T entry) throws IOException {
    +        Queue<T> inMemQueue = getInMemoryQueue();
    +        int resultSize = sizeOf(entry);
    +        maxResultSize = Math.max(maxResultSize, resultSize);
    +        totalResultSize = hasMaxQueueSize ? maxResultSize * 
inMemQueue.size() : (totalResultSize + resultSize);
    +        if (totalResultSize >= thresholdBytes) {
    +            this.file = File.createTempFile(UUID.randomUUID().toString(), 
null);
    +
    +            DeferredFileOutputStream spoolTo = new 
DeferredFileOutputStream(thresholdBytes, file) {
    +                @Override
    +                protected void thresholdReached() throws IOException {
    +                    try {
    +                        super.thresholdReached();
    +                    } finally {
    +                            chunk.close();
    +                    }
    +                }
    +            };
    +
    +            int resSize = inMemQueue.size();
    +            for (int i = 0; i < resSize; i++) {
    +                writeToBuffer(spoolTo, inMemQueue.poll());
    +            }
    +
    +            spoolTo.write(EOF); // end
    +            spoolTo.flush();
    +            flushedCount = resSize;
    +            inMemQueue.clear();
    +            flushBuffer = true;
    +        }
    +    }
    +
    +    private class DeferredSegmentQueueFileIterator extends 
SegmentQueueFileIterator {
    +        private DataInputStream dataInput;
    +
    +        public DeferredSegmentQueueFileIterator() {
    +            super();
    +        }
    +
    +        public DeferredSegmentQueueFileIterator(SegmentQueueFileIterator 
iterator) {
    +            super(iterator);
    +        }
    +
    +
    +
    +        @Override
    +        protected void init(long readIndex) {
    +            this.isEnd = false;
    +            this.readIndex = readIndex;
    +            this.next = null;
    +
    +            try {
    +                BufferedInputStream bufferedInputStream = new 
BufferedInputStream(new FileInputStream(file));
    +                bufferedInputStream.skip(readIndex);
    +                this.dataInput = new DataInputStream(bufferedInputStream);
    +            } catch (IOException e) {
    --- End diff --
    
    Like I said in the above comment, you are not using 
DeferredFileOutputStream as a deferred stream, you are just using it as an 
ordinary FileOutputStream. So maybe forget about handling the 
DeferredFileOutputStream yourself and use the adapted SpoolingResultIterator 
instead.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to