Permalink
Browse files

Implemented get() and get() for lists, plus tests

  • Loading branch information...
1 parent 02b593b commit 8b3f501c6632266944eeafc8e04d0a6084e65304 Bela Ban committed Dec 21, 2011
Showing with 81 additions and 10 deletions.
  1. +33 −9 src/org/jgroups/util/RingBuffer.java
  2. +48 −1 tests/junit-functional/org/jgroups/tests/RingBufferTest.java
@@ -146,6 +146,36 @@ public T remove() {
return list;
}
+ public T get(long seqno) {
+ validate(seqno);
+ if(seqno <= low || seqno > hr.get())
+ return null;
+ int index=index(seqno);
+ return buf.get(index);
+ }
+
+ /**
+ * Returns a list of messages in the range [from .. to], including from and to
+ * @param from
+ * @param to
+ * @return A list of messages, or null if none in range [from .. to] was found
+ */
+ public List<T> get(long from, long to) {
+ if(from > to)
+ throw new IllegalArgumentException("from (" + from + ") has to be <= to (" + to + ")");
+ validate(from);
+ List<T> retval=null;
+ for(long i=from; i <= to; i++) {
+ T element=get(i);
+ if(element != null) {
+ if(retval == null)
+ retval=new ArrayList<T>();
+ retval.add(element);
+ }
+ }
+ return retval;
+ }
+
/** Nulls elements between low and seqno and forwards low */
public void stable(long seqno) {
@@ -179,14 +209,8 @@ public void destroy() {
}
public final int capacity() {return buf.length();}
-
- public int size() {
- return count(false);
- }
-
- public int missing() {
- return count(true);
- }
+ public int size() {return count(false);}
+ public int missing() {return count(true);}
public SeqnoList getMissing() {
SeqnoList missing=null;
@@ -218,6 +242,7 @@ public String toString() {
}
+
protected static final void validate(long seqno) {
if(seqno < 0)
throw new IllegalArgumentException("seqno " + seqno + " cannot be negative");
@@ -235,7 +260,6 @@ protected boolean block(long seqno) {
buffer_full.await();
}
catch(InterruptedException e) {
- ;
}
}
return running;
@@ -7,6 +7,7 @@
import org.testng.annotations.Test;
import java.util.Arrays;
+import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
@@ -167,6 +168,35 @@ public void run() {
assert buf.missing() == 0;
}
+ public void testGet() {
+ final RingBuffer<Integer> buf=new RingBuffer<Integer>(10, 0);
+ for(int i: Arrays.asList(1,2,3,4,5))
+ buf.add(i, i);
+ assert buf.get(0) == null;
+ assert buf.get(1) == 1;
+ assert buf.get(10) == null;
+ assert buf.get(5) == 5;
+ assert buf.get(6) == null;
+ }
+
+ public void testGetList() {
+ final RingBuffer<Integer> buf=new RingBuffer<Integer>(10, 0);
+ for(int i: Arrays.asList(1,2,3,4,5))
+ buf.add(i, i);
+ List<Integer> elements=buf.get(3,5);
+ System.out.println("elements = " + elements);
+ assert elements != null && elements.size() == 3;
+ assert elements.contains(3) && elements.contains(4) && elements.contains(5);
+
+ elements=buf.get(4, 10);
+ System.out.println("elements = " + elements);
+ assert elements != null && elements.size() == 2;
+ assert elements.contains(4) && elements.contains(5);
+
+ elements=buf.get(10, 20);
+ assert elements == null;
+ }
+
public void testRemovedPastHighestReceived() {
RingBuffer<Integer> buf=new RingBuffer<Integer>(10, 0);
for(int i=1; i <= 15; i++) {
@@ -187,7 +217,24 @@ public void testRemovedPastHighestReceived() {
}
public void testRemoveMany() {
-
+ RingBuffer<Integer> buf=new RingBuffer<Integer>(10, 0);
+ for(int i: Arrays.asList(1,2,3,4,5,6,7,9,10))
+ buf.add(i, i);
+ List<Integer> list=buf.removeMany(false,3);
+ System.out.println("list = " + list);
+ assert list != null && list.size() == 3;
+
+ list=buf.removeMany(false, 20);
+ System.out.println("list = " + list);
+ assert list != null && list.size() == 4;
+
+ list=buf.removeMany(false, 10);
+ assert list == null;
+
+ buf.add(8, 8);
+ list=buf.removeMany(false, 10);
+ System.out.println("list = " + list);
+ assert list != null && list.size() == 3;
}

0 comments on commit 8b3f501

Please sign in to comment.