Example #1
0
 /**
  * Adds a message if not yet received
  * @param seqno
  * @param msg
  * @return -1 if not added because seqno < next_to_remove, 0 if not added because already present,
  *          1 if added successfully
  */
 public byte add2(long seqno, Message msg) {
     Segment segment=current_segment;
     if(segment == null || !segment.contains(seqno)) {
         segment=findOrCreateSegment(seqno);
         if(segment != null)
             current_segment=segment;
     }
     if(segment == null)
         return -1;
     return segment.add(seqno, msg);
 }
Example #2
0
 /**
  * Removes a message whose seqno is equal to <code>next_to_remove</code>, increments the latter. Returns message
  * that was removed, or null, if no message can be removed. Messages are thus removed in order.
  */
 public Message remove() {
     long next=next_to_remove.get();
     Segment segment=current_remove_segment;
     if(segment == null || !segment.contains(next)) {
         segment=findSegment(next);
         if(segment != null)
             current_remove_segment=segment;
     }
     if(segment == null)
         return null;
     Message retval=segment.remove(next);
     if(retval != null) {
         next_to_remove.compareAndSet(next, next +1);
         if(segment.allRemoved())
             segments.remove(next / segment_capacity);
     }
     return retval;
 }
Example #3
0
    /**
     * Removes as many messages as possible (in sequence, without gaps)
     * @param max Max number of messages to be removed
     * @return Tuple<List<Message>,Long>: a tuple of the message list and the highest seqno removed
     */
    public Tuple<List<Message>,Long> removeMany(final int max) {
        List<Message> list=null; // we remove msgs.size() messages *max*
        Tuple<List<Message>,Long> retval=null;

        int count=0;
        boolean looping=true;
        while(count < max && looping) {
            long next=next_to_remove.get();
            Segment segment=current_remove_segment;
            if(segment == null || !segment.contains(next)) {
                segment=findSegment(next);
                if(segment != null)
                    current_remove_segment=segment;
            }
            if(segment == null)
                return retval;

            long segment_id=next;
            long end=segment.getEndIndex();
            while(next < end && count < max) {
                Message msg=segment.remove(next);
                if(msg == null) {
                    looping=false;
                    break;
                }
                if(list == null) {
                    list=new LinkedList<Message>(); // we remove msgs.size() messages *max*
                    retval=new Tuple<List<Message>,Long>(list, 0L);
                }
                list.add(msg);
                count++;
                retval.setVal2(next);
                next_to_remove.compareAndSet(next, ++next);
                if(segment.allRemoved())
                    segments.remove(segment_id / segment_capacity);
            }
        }

        return retval;
    }