Search in sources :

Example 11 with IFrame

use of org.apache.hyracks.api.comm.IFrame in project asterixdb by apache.

the class ResultWriterOperatorDescriptor method createPushRuntime.

@Override
public IOperatorNodePushable createPushRuntime(final IHyracksTaskContext ctx, IRecordDescriptorProvider recordDescProvider, final int partition, final int nPartitions) throws HyracksDataException {
    final IDatasetPartitionManager dpm = ctx.getDatasetPartitionManager();
    final IFrame frame = new VSizeFrame(ctx);
    final FrameOutputStream frameOutputStream = new FrameOutputStream(ctx.getInitialFrameSize());
    frameOutputStream.reset(frame, true);
    PrintStream printStream = new PrintStream(frameOutputStream);
    final RecordDescriptor outRecordDesc = recordDescProvider.getInputRecordDescriptor(getActivityId(), 0);
    final IResultSerializer resultSerializer = resultSerializerFactory.createResultSerializer(outRecordDesc, printStream);
    final FrameTupleAccessor frameTupleAccessor = new FrameTupleAccessor(outRecordDesc);
    return new AbstractUnaryInputSinkOperatorNodePushable() {

        private IFrameWriter datasetPartitionWriter;

        private boolean failed = false;

        @Override
        public void open() throws HyracksDataException {
            try {
                datasetPartitionWriter = dpm.createDatasetPartitionWriter(ctx, rsId, ordered, asyncMode, partition, nPartitions);
                datasetPartitionWriter.open();
                resultSerializer.init();
            } catch (HyracksException e) {
                throw HyracksDataException.create(e);
            }
        }

        @Override
        public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
            frameTupleAccessor.reset(buffer);
            for (int tIndex = 0; tIndex < frameTupleAccessor.getTupleCount(); tIndex++) {
                resultSerializer.appendTuple(frameTupleAccessor, tIndex);
                if (!frameOutputStream.appendTuple()) {
                    frameOutputStream.flush(datasetPartitionWriter);
                    resultSerializer.appendTuple(frameTupleAccessor, tIndex);
                    frameOutputStream.appendTuple();
                }
            }
        }

        @Override
        public void fail() throws HyracksDataException {
            failed = true;
            datasetPartitionWriter.fail();
        }

        @Override
        public void close() throws HyracksDataException {
            try {
                if (!failed && frameOutputStream.getTupleCount() > 0) {
                    frameOutputStream.flush(datasetPartitionWriter);
                }
            } catch (Exception e) {
                datasetPartitionWriter.fail();
                throw e;
            } finally {
                datasetPartitionWriter.close();
            }
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("{ ");
            sb.append("\"rsId\": \"").append(rsId).append("\", ");
            sb.append("\"ordered\": ").append(ordered).append(", ");
            sb.append("\"asyncMode\": ").append(asyncMode).append(" }");
            return sb.toString();
        }
    };
}
Also used : PrintStream(java.io.PrintStream) IFrameWriter(org.apache.hyracks.api.comm.IFrameWriter) IFrame(org.apache.hyracks.api.comm.IFrame) IResultSerializer(org.apache.hyracks.api.dataflow.value.IResultSerializer) RecordDescriptor(org.apache.hyracks.api.dataflow.value.RecordDescriptor) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) ByteBuffer(java.nio.ByteBuffer) VSizeFrame(org.apache.hyracks.api.comm.VSizeFrame) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IOException(java.io.IOException) HyracksException(org.apache.hyracks.api.exceptions.HyracksException) IDatasetPartitionManager(org.apache.hyracks.api.dataset.IDatasetPartitionManager) AbstractUnaryInputSinkOperatorNodePushable(org.apache.hyracks.dataflow.std.base.AbstractUnaryInputSinkOperatorNodePushable) FrameOutputStream(org.apache.hyracks.dataflow.common.comm.io.FrameOutputStream) FrameTupleAccessor(org.apache.hyracks.dataflow.common.comm.io.FrameTupleAccessor)

Example 12 with IFrame

use of org.apache.hyracks.api.comm.IFrame in project asterixdb by apache.

the class DelimitedDataTupleParserFactory method createTupleParser.

@Override
public ITupleParser createTupleParser(final IHyracksTaskContext ctx) {
    return new ITupleParser() {

        @Override
        public void parse(InputStream in, IFrameWriter writer) throws HyracksDataException {
            try {
                IValueParser[] valueParsers = new IValueParser[valueParserFactories.length];
                for (int i = 0; i < valueParserFactories.length; ++i) {
                    valueParsers[i] = valueParserFactories[i].createValueParser();
                }
                IFrame frame = new VSizeFrame(ctx);
                FrameTupleAppender appender = new FrameTupleAppender();
                appender.reset(frame, true);
                ArrayTupleBuilder tb = new ArrayTupleBuilder(valueParsers.length);
                DataOutput dos = tb.getDataOutput();
                FieldCursorForDelimitedDataParser cursor = new FieldCursorForDelimitedDataParser(new InputStreamReader(in), fieldDelimiter, quote);
                while (cursor.nextRecord()) {
                    tb.reset();
                    for (int i = 0; i < valueParsers.length; ++i) {
                        if (!cursor.nextField()) {
                            break;
                        }
                        // Eliminate double quotes in the field that we are going to parse
                        if (cursor.isDoubleQuoteIncludedInThisField) {
                            cursor.eliminateDoubleQuote(cursor.buffer, cursor.fStart, cursor.fEnd - cursor.fStart);
                            cursor.fEnd -= cursor.doubleQuoteCount;
                            cursor.isDoubleQuoteIncludedInThisField = false;
                        }
                        valueParsers[i].parse(cursor.buffer, cursor.fStart, cursor.fEnd - cursor.fStart, dos);
                        tb.addFieldEndOffset();
                    }
                    FrameUtils.appendToWriter(writer, appender, tb.getFieldEndOffsets(), tb.getByteArray(), 0, tb.getSize());
                }
                appender.write(writer, true);
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : IFrameWriter(org.apache.hyracks.api.comm.IFrameWriter) DataOutput(java.io.DataOutput) InputStreamReader(java.io.InputStreamReader) IFrame(org.apache.hyracks.api.comm.IFrame) InputStream(java.io.InputStream) ArrayTupleBuilder(org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder) IOException(java.io.IOException) VSizeFrame(org.apache.hyracks.api.comm.VSizeFrame) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) FrameTupleAppender(org.apache.hyracks.dataflow.common.comm.io.FrameTupleAppender) IValueParser(org.apache.hyracks.dataflow.common.data.parsers.IValueParser)

Example 13 with IFrame

use of org.apache.hyracks.api.comm.IFrame in project asterixdb by apache.

the class ResultPrinter method print.

public void print(ResultReader resultReader) throws HyracksDataException {
    printPrefix();
    final IFrameTupleAccessor fta = resultReader.getFrameTupleAccessor();
    final IFrame frame = new VSizeFrame(resultDisplayFrameMgr);
    while (resultReader.read(frame) > 0) {
        final ByteBuffer frameBuffer = frame.getBuffer();
        final byte[] frameBytes = frameBuffer.array();
        fta.reset(frameBuffer);
        final int last = fta.getTupleCount();
        for (int tIndex = 0; tIndex < last; tIndex++) {
            final int start = fta.getTupleStartOffset(tIndex);
            int length = fta.getTupleEndOffset(tIndex) - start;
            if (conf.fmt() == SessionConfig.OutputFormat.CSV && ((length > 0) && (frameBytes[start + length - 1] == '\n'))) {
                length--;
            }
            String result = new String(frameBytes, start, length, UTF_8);
            if (wrapArray && notFirst) {
                output.out().print(", ");
            }
            notFirst = true;
            displayRecord(result);
        }
        frameBuffer.clear();
    }
    printPostfix();
}
Also used : IFrame(org.apache.hyracks.api.comm.IFrame) IFrameTupleAccessor(org.apache.hyracks.api.comm.IFrameTupleAccessor) ByteBuffer(java.nio.ByteBuffer) VSizeFrame(org.apache.hyracks.api.comm.VSizeFrame)

Example 14 with IFrame

use of org.apache.hyracks.api.comm.IFrame in project asterixdb by apache.

the class VariableFramesMemoryManagerTest method testNormalIncomingFrames.

@Test
public void testNormalIncomingFrames() throws HyracksDataException {
    HashMap<Integer, Integer> tupleSet = prepareTuples();
    for (IFrame frame : frameList) {
        assertTrue(framesMemoryManager.insertFrame(frame.getBuffer()) >= 0);
    }
    assertEquals(NUM_MIN_FRAME, framesMemoryManager.getNumFrames());
    assertEveryTupleInFTAIsInFrameMemoryManager(tupleSet, framesMemoryManager);
}
Also used : IFrame(org.apache.hyracks.api.comm.IFrame) Test(org.junit.Test)

Example 15 with IFrame

use of org.apache.hyracks.api.comm.IFrame in project asterixdb by apache.

the class VariableFramesMemoryManagerTest method testRandomTuplesAreAllStoredInBuffer.

@Test
public void testRandomTuplesAreAllStoredInBuffer() throws HyracksDataException {
    Map<Integer, Integer> tupleSet = prepareRandomTuples();
    for (IFrame frame : frameList) {
        if (framesMemoryManager.insertFrame(frame.getBuffer()) < 0) {
            fta.reset(frame.getBuffer());
            for (int i = 0; i < fta.getTupleCount(); ++i) {
                int id = parseTuple(fta.getBuffer(), fta.getTupleStartOffset(i) + fta.getFieldStartOffset(i, 0) + fta.getFieldSlotsLength());
                tupleSet.remove(id);
            //                    System.out.println(
            //                            "can't appended id:" + id + ",frameSize:" + frame.getInitialFrameSize());
            }
        }
    }
    assertEveryTupleInFTAIsInFrameMemoryManager(tupleSet, framesMemoryManager);
    framesMemoryManager.reset();
}
Also used : IFrame(org.apache.hyracks.api.comm.IFrame) Test(org.junit.Test)

Aggregations

IFrame (org.apache.hyracks.api.comm.IFrame)27 VSizeFrame (org.apache.hyracks.api.comm.VSizeFrame)13 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)9 FrameTupleAppender (org.apache.hyracks.dataflow.common.comm.io.FrameTupleAppender)8 Test (org.junit.Test)8 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)7 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)7 ByteBuffer (java.nio.ByteBuffer)6 RunMergingFrameReader (org.apache.hyracks.dataflow.std.sort.RunMergingFrameReader)6 Map (java.util.Map)5 TreeMap (java.util.TreeMap)5 ArrayTupleBuilder (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)4 DataOutput (java.io.DataOutput)3 FixedSizeFrame (org.apache.hyracks.api.comm.FixedSizeFrame)3 IFrameTupleAccessor (org.apache.hyracks.api.comm.IFrameTupleAccessor)3 IFrameWriter (org.apache.hyracks.api.comm.IFrameWriter)3 RecordDescriptor (org.apache.hyracks.api.dataflow.value.RecordDescriptor)3 FrameTupleAccessor (org.apache.hyracks.dataflow.common.comm.io.FrameTupleAccessor)3 IOException (java.io.IOException)2