/** * 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; }
/** * 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; }