Search in sources :

Example 1 with DataPacker

use of edu.iu.dsc.tws.api.comms.packing.DataPacker in project twister2 by DSC-SPIDAL.

the class FixedSchemaDataDeserializer method build.

/**
 * Builds the message from the data buffers in the partialObject. Since this method
 * supports multi-messages it iterates through the buffers and builds all the messages separately
 *
 * @param partialObject message object that needs to be built
 * @param edge the edge value associated with this message
 */
@Override
public void build(Object partialObject, int edge) {
    InMessage currentMessage = (InMessage) partialObject;
    DataPacker dataPacker = currentMessage.getDataType().getDataPacker();
    Queue<DataBuffer> buffers = currentMessage.getBuffers();
    MessageHeader header = currentMessage.getHeader();
    if (header == null) {
        throw new RuntimeException("Header must be built before the message");
    }
    List<DataBuffer> builtBuffers = new ArrayList<>();
    // get the number of objects deserialized
    DataBuffer buffer = buffers.peek();
    while (buffer != null) {
        int currentLocation = 0;
        int remaining = buffer.getSize();
        if (header.getNumberTuples() == 0) {
            builtBuffers.add(buffer);
            break;
        }
        // if we are at the beginning
        int currentObjectLength = currentMessage.getUnPkCurrentObjectLength();
        if (currentMessage.getUnPkBuffers() == 0) {
            currentLocation = DFWIOUtils.HEADER_SIZE;
            remaining = remaining - DFWIOUtils.HEADER_SIZE;
        } else {
            // source(int) + last_buffer_indicator(byte)
            currentLocation = DFWIOUtils.SHORT_HEADER_SIZE;
            remaining = remaining - DFWIOUtils.SHORT_HEADER_SIZE;
        }
        if (currentObjectLength == -1 || currentMessage.getUnPkBuffers() == 0) {
            currentObjectLength = this.messageSchema.getMessageSize();
            // starting to build a new object
            currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
        }
        while (remaining > 0) {
            // read the values from the buffer
            ObjectBuilderImpl dataBuilder = currentMessage.getDataBuilder();
            int bytesRead = dataPacker.readDataFromBuffer(dataBuilder, currentLocation, buffer);
            dataBuilder.incrementCompletedSizeBy(bytesRead);
            currentLocation += bytesRead;
            remaining = remaining - bytesRead;
            // okay we are done with this object
            if (dataBuilder.isBuilt()) {
                currentMessage.addCurrentObject();
                currentMessage.setUnPkCurrentObjectLength(-1);
            } else {
                // lets break the inner while loop
                break;
            }
        }
        // lets remove this buffer
        buffers.poll();
        builtBuffers.add(buffer);
        // increment the unpacked buffers
        currentMessage.incrementUnPkBuffers();
        // lets check weather we have read everythong
        int readObjectNumber = currentMessage.getUnPkNumberObjects();
        // we need to get number of tuples and get abs because we are using -1 for single messages
        if (readObjectNumber == Math.abs(currentMessage.getHeader().getNumberTuples())) {
            break;
        }
        // lets move to next
        buffer = buffers.peek();
    }
    if (builtBuffers.size() > 0) {
        ChannelMessage channelMessage = new ChannelMessage(currentMessage.getOriginatingId(), currentMessage.getDataType(), MessageDirection.IN, currentMessage.getReleaseListener());
        channelMessage.addBuffers(builtBuffers);
        channelMessage.setHeader(currentMessage.getHeader());
        channelMessage.incrementRefCount();
        currentMessage.addBuiltMessage(channelMessage);
    }
}
Also used : ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) DataPacker(edu.iu.dsc.tws.api.comms.packing.DataPacker) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Example 2 with DataPacker

use of edu.iu.dsc.tws.api.comms.packing.DataPacker in project twister2 by DSC-SPIDAL.

the class KeyedDataSerializer method serializeSingleMessage.

/**
 * Builds the body of the message. Based on the message type different build methods are called
 *
 * @param payload the message that needs to be built
 * @param sendMessage the send message object that contains all the metadata
 * @param targetBuffer the data targetBuffer to which the built message needs to be copied
 * @return true if the body was built and copied to the targetBuffer successfully,false otherwise.
 */
public boolean serializeSingleMessage(Object payload, OutMessage sendMessage, DataBuffer targetBuffer) {
    Object value = null;
    Object key = new byte[0];
    DataPacker keyPacker = sendMessage.getKeyType().getDataPacker();
    DataPacker valuePacker = sendMessage.getDataType().getDataPacker();
    if ((sendMessage.getFlags() & MessageFlags.SYNC_BARRIER) == MessageFlags.SYNC_BARRIER) {
        value = payload;
        keyPacker = MessageTypes.BYTE_ARRAY.getDataPacker();
        valuePacker = MessageTypes.BYTE_ARRAY.getDataPacker();
    } else {
        Tuple tuple = (Tuple) payload;
        value = tuple.getValue();
        key = tuple.getKey();
    }
    return serializeKeyedData(value, key, valuePacker, keyPacker, sendMessage.getSerializationState(), targetBuffer);
}
Also used : DataPacker(edu.iu.dsc.tws.api.comms.packing.DataPacker) Tuple(edu.iu.dsc.tws.api.comms.structs.Tuple)

Example 3 with DataPacker

use of edu.iu.dsc.tws.api.comms.packing.DataPacker in project twister2 by DSC-SPIDAL.

the class SnapshotImpl method pack.

public byte[] pack() {
    Map<String, byte[]> packedValues = new HashMap<>();
    Map<String, byte[]> packedKeys = new HashMap<>();
    int totalLength = 0;
    for (String key : values.keySet()) {
        DataPacker packer = packers.getOrDefault(key, ObjectPacker.getInstance());
        byte[] dataBytes = packer.packToByteArray(values.get(key));
        byte[] keyBytes = StringPacker.getInstance().packToByteArray(key);
        packedValues.put(key, dataBytes);
        packedKeys.put(key, keyBytes);
        totalLength += dataBytes.length + keyBytes.length + (2 * Integer.BYTES);
    }
    ByteBuffer byteBuffer = ByteBuffer.allocate(totalLength + Long.BYTES);
    byteBuffer.putLong(this.version);
    for (String key : values.keySet()) {
        byte[] keyBytes = packedKeys.get(key);
        byteBuffer.putInt(keyBytes.length);
        byteBuffer.put(keyBytes);
        byte[] dataBytes = packedValues.get(key);
        byteBuffer.putInt(dataBytes.length);
        byteBuffer.put(dataBytes);
    }
    return byteBuffer.array();
}
Also used : HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) DataPacker(edu.iu.dsc.tws.api.comms.packing.DataPacker)

Example 4 with DataPacker

use of edu.iu.dsc.tws.api.comms.packing.DataPacker in project twister2 by DSC-SPIDAL.

the class DataDeserializer method build.

/**
 * Builds the message from the data buffers in the partialObject. Since this method
 * supports multi-messages it iterates through the buffers and builds all the messages separately
 *
 * @param partialObject message object that needs to be built
 * @param edge the edge value associated with this message
 */
@Override
public void build(Object partialObject, int edge) {
    InMessage currentMessage = (InMessage) partialObject;
    DataPacker dataPacker = currentMessage.getDataType().getDataPacker();
    Queue<DataBuffer> buffers = currentMessage.getBuffers();
    MessageHeader header = currentMessage.getHeader();
    if (header == null) {
        throw new RuntimeException("Header must be built before the message");
    }
    List<DataBuffer> builtBuffers = new ArrayList<>();
    // get the number of objects deserialized
    DataBuffer buffer = buffers.peek();
    while (buffer != null) {
        int currentLocation = 0;
        int remaining = buffer.getSize();
        if (header.getNumberTuples() == 0) {
            builtBuffers.add(buffer);
            break;
        }
        // if we are at the beginning
        int currentObjectLength = currentMessage.getUnPkCurrentObjectLength();
        if (currentMessage.getUnPkBuffers() == 0) {
            currentLocation = DFWIOUtils.HEADER_SIZE;
            remaining = remaining - DFWIOUtils.HEADER_SIZE;
        } else {
            // source(int) + last_buffer_indicator(byte)
            currentLocation = DFWIOUtils.SHORT_HEADER_SIZE;
            remaining = remaining - DFWIOUtils.SHORT_HEADER_SIZE;
        }
        if (currentObjectLength == -1 || currentMessage.getUnPkBuffers() == 0) {
            currentObjectLength = buffer.getByteBuffer().getInt(currentLocation);
            remaining = remaining - Integer.BYTES;
            currentLocation += Integer.BYTES;
            // starting to build a new object
            currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
        }
        while (remaining > 0) {
            // read the values from the buffer
            ObjectBuilderImpl dataBuilder = currentMessage.getDataBuilder();
            int bytesRead = dataPacker.readDataFromBuffer(dataBuilder, currentLocation, buffer);
            dataBuilder.incrementCompletedSizeBy(bytesRead);
            currentLocation += bytesRead;
            remaining = remaining - bytesRead;
            // okay we are done with this object
            if (dataBuilder.isBuilt()) {
                currentMessage.addCurrentObject();
                currentMessage.setUnPkCurrentObjectLength(-1);
            } else {
                // lets break the inner while loop
                break;
            }
            // could have next object length?
            if (remaining >= Integer.BYTES) {
                currentObjectLength = buffer.getByteBuffer().getInt(currentLocation);
                remaining = remaining - Integer.BYTES;
                currentLocation += Integer.BYTES;
                currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
                currentMessage.setUnPkCurrentObjectLength(currentObjectLength);
            } else {
                // we have to break here as we cannot read further
                break;
            }
        }
        // lets remove this buffer
        buffers.poll();
        builtBuffers.add(buffer);
        // increment the unpacked buffers
        currentMessage.incrementUnPkBuffers();
        // lets check weather we have read everythong
        int readObjectNumber = currentMessage.getUnPkNumberObjects();
        // we need to get number of tuples and get abs because we are using -1 for single messages
        if (readObjectNumber == Math.abs(currentMessage.getHeader().getNumberTuples())) {
            break;
        }
        // lets move to next
        buffer = buffers.peek();
    }
    if (builtBuffers.size() > 0) {
        ChannelMessage channelMessage = new ChannelMessage(currentMessage.getOriginatingId(), currentMessage.getDataType(), MessageDirection.IN, currentMessage.getReleaseListener());
        channelMessage.addBuffers(builtBuffers);
        channelMessage.setHeader(currentMessage.getHeader());
        channelMessage.incrementRefCount();
        currentMessage.addBuiltMessage(channelMessage);
    }
}
Also used : ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) DataPacker(edu.iu.dsc.tws.api.comms.packing.DataPacker) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)

Example 5 with DataPacker

use of edu.iu.dsc.tws.api.comms.packing.DataPacker in project twister2 by DSC-SPIDAL.

the class FixedSchemaKeyedDataDeSerializer method build.

/**
 * Builds the message from the data buffers in the partialObject. Since this method
 * supports multi-messages it iterates through the buffers and builds all the messages separately
 *
 * @param partialObject message object that needs to be built
 * @param edge the edge value associated with this message
 */
@Override
public void build(Object partialObject, int edge) {
    InMessage currentMessage = (InMessage) partialObject;
    MessageType keyType = currentMessage.getKeyType();
    DataPacker keyPacker = keyType.getDataPacker();
    DataPacker dataPacker = currentMessage.getDataType().getDataPacker();
    Queue<DataBuffer> buffers = currentMessage.getBuffers();
    MessageHeader header = currentMessage.getHeader();
    if (header == null) {
        throw new RuntimeException("Header must be built before the message");
    }
    List<DataBuffer> builtBuffers = new ArrayList<>();
    // get the number of objects deserialized
    DataBuffer buffer = buffers.peek();
    while (buffer != null) {
        int currentLocation = 0;
        int remaining = buffer.getSize();
        if (header.getNumberTuples() == 0) {
            builtBuffers.add(buffer);
            break;
        }
        // if we are at the beginning
        int currentObjectLength = currentMessage.getUnPkCurrentObjectLength();
        int currentKeyLength = currentMessage.getUnPkCurrentKeyLength();
        if (currentMessage.getUnPkBuffers() == 0) {
            currentLocation = DFWIOUtils.HEADER_SIZE;
            remaining = remaining - DFWIOUtils.HEADER_SIZE;
        } else {
            currentLocation = DFWIOUtils.SHORT_HEADER_SIZE;
            remaining = remaining - DFWIOUtils.SHORT_HEADER_SIZE;
        }
        if (currentObjectLength == -1 || currentMessage.getUnPkBuffers() == 0) {
            currentObjectLength = messageSchema.getMessageSize();
        }
        if (currentKeyLength == -1) {
            // we have to set the current object length
            currentObjectLength = messageSchema.getMessageSize() - messageSchema.getKeySize();
            currentKeyLength = messageSchema.getKeySize();
            currentMessage.getKeyBuilder().init(keyPacker, currentKeyLength);
            try {
                currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
                currentMessage.setUnPkCurrentKeyLength(currentKeyLength);
                currentMessage.setUnPkCurrentObjectLength(currentObjectLength);
                // we are going to read the key first
                currentMessage.setReadingKey(true);
            } catch (NegativeArraySizeException e) {
                throw new RuntimeException(e);
            }
        }
        while (remaining > 0) {
            if (currentMessage.isReadingKey()) {
                ObjectBuilderImpl keyBuilder = currentMessage.getKeyBuilder();
                int bytesRead = keyPacker.readDataFromBuffer(keyBuilder, currentLocation, buffer);
                keyBuilder.incrementCompletedSizeBy(bytesRead);
                currentLocation += bytesRead;
                remaining = remaining - bytesRead;
                currentMessage.setReadingKey(!keyBuilder.isBuilt());
                if (keyBuilder.isBuilt()) {
                    // done reading key
                    currentMessage.setReadingKey(false);
                } else {
                    break;
                }
            }
            if (!currentMessage.isReadingKey()) {
                ObjectBuilderImpl dataBuilder = currentMessage.getDataBuilder();
                // read the values from the buffer
                int byteRead = dataPacker.readDataFromBuffer(dataBuilder, currentLocation, buffer);
                dataBuilder.incrementCompletedSizeBy(byteRead);
                currentLocation += byteRead;
                remaining = remaining - byteRead;
                // okay we are done with this object
                if (dataBuilder.isBuilt()) {
                    // lets add the object
                    currentMessage.addCurrentKeyedObject();
                } else {
                    // lets break the inner while loop
                    break;
                }
                if (remaining > 0) {
                    currentObjectLength = messageSchema.getMessageSize();
                    // we have to set the current object length
                    currentObjectLength = currentObjectLength - messageSchema.getKeySize();
                    currentKeyLength = messageSchema.getKeySize();
                    currentMessage.getKeyBuilder().init(keyPacker, currentKeyLength);
                    currentMessage.getDataBuilder().init(dataPacker, currentObjectLength);
                    currentMessage.setUnPkCurrentKeyLength(currentKeyLength);
                    currentMessage.setUnPkCurrentObjectLength(currentObjectLength);
                    // we are going to read the key first
                    currentMessage.setReadingKey(true);
                } else {
                    // we have to break here as we cannot read further
                    break;
                }
            }
        }
        // lets remove this buffer
        buffers.poll();
        builtBuffers.add(buffer);
        // increment the unpacked buffers
        currentMessage.incrementUnPkBuffers();
        // lets check weather we have read everythong
        int readObjectNumber = currentMessage.getUnPkNumberObjects();
        // we need to get number of tuples and get abs because we are using -1 for single messages
        if (readObjectNumber == Math.abs(currentMessage.getHeader().getNumberTuples())) {
            break;
        }
        // lets move to next
        buffer = buffers.peek();
    }
    if (builtBuffers.size() > 0) {
        ChannelMessage channelMessage = new ChannelMessage(currentMessage.getOriginatingId(), currentMessage.getDataType(), MessageDirection.IN, currentMessage.getReleaseListener());
        channelMessage.addBuffers(builtBuffers);
        channelMessage.setHeader(currentMessage.getHeader());
        channelMessage.incrementRefCount();
        currentMessage.addBuiltMessage(channelMessage);
    }
}
Also used : ArrayList(java.util.ArrayList) InMessage(edu.iu.dsc.tws.comms.dfw.InMessage) DataPacker(edu.iu.dsc.tws.api.comms.packing.DataPacker) ChannelMessage(edu.iu.dsc.tws.api.comms.messaging.ChannelMessage) MessageHeader(edu.iu.dsc.tws.api.comms.messaging.MessageHeader) MessageType(edu.iu.dsc.tws.api.comms.messaging.types.MessageType) DataBuffer(edu.iu.dsc.tws.api.comms.packing.DataBuffer)

Aggregations

DataPacker (edu.iu.dsc.tws.api.comms.packing.DataPacker)6 ChannelMessage (edu.iu.dsc.tws.api.comms.messaging.ChannelMessage)4 MessageHeader (edu.iu.dsc.tws.api.comms.messaging.MessageHeader)4 DataBuffer (edu.iu.dsc.tws.api.comms.packing.DataBuffer)4 InMessage (edu.iu.dsc.tws.comms.dfw.InMessage)4 ArrayList (java.util.ArrayList)4 MessageType (edu.iu.dsc.tws.api.comms.messaging.types.MessageType)2 Tuple (edu.iu.dsc.tws.api.comms.structs.Tuple)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1