Class MessageBatch

  • All Implemented Interfaces:
    java.lang.Iterable<Message>

    public class MessageBatch
    extends java.lang.Object
    implements java.lang.Iterable<Message>
    Represents a message batch; multiple messages from the same sender to the same receiver(s). This class is unsynchronized.
    Since:
    3.3
    Author:
    Bela Ban
    • Field Detail

      • dest

        protected Address dest
        The destination address. Null if this is a multicast message batch, non-null if the batch is sent to a specific member
      • sender

        protected Address sender
        The sender of the message batch
      • cluster_name

        protected AsciiString cluster_name
        The name of the cluster in which the message batch is sent, this is equivalent to TpHeader.cluster_name
      • messages

        protected Message[] messages
        The storage of the messages; removed messages have a null element
      • index

        protected int index
        Index of the next message to be inserted
      • multicast

        protected boolean multicast
        Whether all messages have dest == null (multicast) or not
      • mode

        protected MessageBatch.Mode mode
        Whether this message batch contains only OOB messages, or only regular messages
      • length_visitor

        protected static final java.util.function.ToIntBiFunction<Message,​MessageBatch> length_visitor
      • total_size_visitor

        protected static final java.util.function.ToLongBiFunction<Message,​MessageBatch> total_size_visitor
    • Constructor Detail

      • MessageBatch

        public MessageBatch​(int capacity)
      • MessageBatch

        public MessageBatch​(java.util.Collection<Message> msgs)
      • MessageBatch

        public MessageBatch​(Address dest,
                            Address sender,
                            AsciiString cluster_name,
                            boolean multicast,
                            java.util.Collection<Message> msgs,
                            java.util.function.Predicate<Message> filter)
    • Method Detail

      • getDest

        public Address getDest()
      • getSender

        public Address getSender()
      • isMulticast

        public boolean isMulticast()
      • multicast

        public boolean multicast()
      • multicast

        public MessageBatch multicast​(boolean flag)
      • getCapacity

        public int getCapacity()
      • capacity

        public int capacity()
      • index

        public int index()
      • array

        public Message[] array()
        Returns the underlying message array. This is only intended for testing !
      • add

        public int add​(Message msg,
                       boolean resize)
        Adds a message to the table
        Parameters:
        msg - the message
        resize - whether or not to resize the table. If true, the method will always return 1
        Returns:
        always 1 if resize==true, else 1 if the message was added or 0 if not
      • add

        public int add​(MessageBatch batch,
                       boolean resize)
        Adds another batch to this one
        Parameters:
        batch - the batch to add to this batch
        resize - when true, this batch will be resized to accommodate the other batch
        Returns:
        the number of messages from the other batch that were added successfully. Will always be batch.size() unless resize==0: in this case, the number of messages that were added successfully is returned
      • replace

        public MessageBatch replace​(Message existing_msg,
                                    Message new_msg)
        Replaces a message in the batch with another one
        Parameters:
        existing_msg - The message to be replaced. The message has to be non-null and is found by identity (==) comparison
        new_msg - The message to replace the existing message with, can be null
        Returns:
      • replace

        public MessageBatch replace​(java.util.function.Predicate<Message> filter,
                                    Message replacement,
                                    boolean match_all)
        Replaces all messages which match a given filter with a replacement message
        Parameters:
        filter - the filter. If null, no changes take place. Note that filter needs to be able to handle null msgs
        replacement - the replacement message. Can be null, which essentially removes all messages matching filter
        match_all - whether to replace the first or all matches
        Returns:
        the MessageBatch
      • replaceIf

        public int replaceIf​(java.util.function.Predicate<Message> filter,
                             Message replacement,
                             boolean match_all)
        Replaces all messages that match a given filter with a replacement message
        Parameters:
        filter - the filter. If null, no changes take place. Note that filter needs to be able to handle null msgs
        replacement - the replacement message. Can be null, which essentially removes all messages matching filter
        match_all - whether to replace the first or all matches
        Returns:
        the number of matched messages
      • transferFrom

        public int transferFrom​(MessageBatch other,
                                boolean clear)
        Transfers messages from other to this batch. Optionally clears the other batch after the transfer
        Parameters:
        other - the other batch
        clear - If true, the transferred messages are removed from the other batch
        Returns:
        the number of transferred messages (may be 0 if the other batch was empty)
      • remove

        public MessageBatch remove​(Message msg)
        Removes the current message (found by indentity (==)) by nulling it in the message array
        Parameters:
        msg -
        Returns:
      • remove

        public MessageBatch remove​(java.util.function.Predicate<Message> filter)
        Removes all messages which match filter
        Parameters:
        filter - the filter. If null, no removal takes place
        Returns:
        the MessageBatch
      • getMatchingMessages

        public java.util.Collection<Message> getMatchingMessages​(short id,
                                                                 boolean remove)
        Removes and returns all messages which have a header with ID == id
      • map

        public <T> java.util.Collection<T> map​(java.util.function.BiFunction<Message,​MessageBatch,​T> visitor)
        Applies a function to all messages and returns a list of the function results
      • forEach

        public void forEach​(java.util.function.BiConsumer<Message,​MessageBatch> consumer)
      • size

        public int size()
        Returns the number of non-null messages
      • isEmpty

        public boolean isEmpty()
      • totalSize

        public long totalSize()
        Returns the size of the message batch (by calling Message.size() on all messages)
      • length

        public int length()
        Returns the total number of bytes of the message batch (by calling Message.getLength() on all messages)
      • iterator

        public MessageIterator iterator()
        Iterator which iterates only over non-null messages, skipping null messages
        Specified by:
        iterator in interface java.lang.Iterable<Message>
      • stream

        public java.util.stream.Stream<Message> stream()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • printHeaders

        public java.lang.String printHeaders()
      • resize

        protected void resize()
      • resize

        protected void resize​(int new_capacity)