Skip to content

Commit

Permalink
Implemented get() and get() for lists, plus tests
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Dec 21, 2011
1 parent 02b593b commit 8b3f501
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
42 changes: 33 additions & 9 deletions src/org/jgroups/util/RingBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ public List<T> removeMany(boolean nullify, int max_results) {
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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -235,7 +260,6 @@ protected boolean block(long seqno) {
buffer_full.await();
}
catch(InterruptedException e) {
;
}
}
return running;
Expand Down
49 changes: 48 additions & 1 deletion tests/junit-functional/org/jgroups/tests/RingBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
}


Expand Down

0 comments on commit 8b3f501

Please sign in to comment.