Skip to content

Commit

Permalink
ns
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Apr 19, 2024
1 parent 0ac1233 commit ae76cff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
35 changes: 15 additions & 20 deletions src/org/jgroups/util/RingBufferSeqno.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,26 @@ public T remove() {
public List<T> removeMany(int max_results) {
List<T> list=null;
int num_results=0;
T element;

lock.lock();
try {
long start=low, end=high;
while(start+1 <= end && (element=buf[index(start+1)]) != null) {
long start=low;
while(start+1 <= high) {
int index=index(start+1);
T element=buf[index];
if(element == null)
break;
if(list == null)
list=new ArrayList<>(max_results > 0? max_results : 20);
list.add(element);
buf[index]=null;
start++;
if(max_results > 0 && ++num_results >= max_results)
break;
}

if(start > high) { // do we need to move high forward ?
high=start;

// null elements
int from=index(low+1), length=(int)(start - low), capacity=capacity();
for(int i=from; i < from+length; i++) {
int index=i & (capacity - 1);
buf[index]=null;
}
// Releases some of the blocked adders
if(start > low) {
low=start;
buffer_full.signalAll();
}
if(start > low) {
low=start;
buffer_full.signalAll();
}
return list;
}
Expand Down Expand Up @@ -261,12 +253,15 @@ public void close() {
}

public SeqnoList getMissing() {
SeqnoList missing=null;
lock.lock();
try {
final SeqnoList missing=new SeqnoList((int)(high - low), low);
for(long i=low + 1; i <= high; i++) { // <= high: correct as element at buf[high] will always be non-null!
if(buf[index(i)] == null)
if(buf[index(i)] == null) {
if(missing == null)
missing=new SeqnoList((int)(high - low), low);
missing.add(i);
}
}
return missing;
}
Expand Down
34 changes: 14 additions & 20 deletions tests/junit-functional/org/jgroups/tests/RingBufferSeqnoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,22 +332,16 @@ public void testGetList() {

public void testRemove() {
final RingBufferSeqno<Integer> buf=new RingBufferSeqno<>(10, 0);
for(int i: Arrays.asList(1,2,3,4,5))
buf.add(i, i);
IntStream.rangeClosed(1,5).forEach(n -> buf.add(n,n));
System.out.println("buf = " + buf);
assertIndices(buf, 0, 5);

Integer el=buf.remove();
System.out.println("el = " + el);
assert el.equals(1);

el=buf.remove(); // not encouraged ! nullify should always be true or false
System.out.println("el = " + el);
assert el.equals(2);

el=buf.remove();
System.out.println("el = " + el);
assert el.equals(3);
for(int i=1; i <= 5; i++) {
Integer el=buf.remove();
System.out.println("el = " + el);
assert el.equals(i);
}
assert buf.size() == 0;
}

public void testRemove2() {
Expand Down Expand Up @@ -393,26 +387,25 @@ public void testRemovedPastHighestReceived() {

public void testRemoveMany() {
RingBufferSeqno<Integer> buf=new RingBufferSeqno<>(10, 0);
for(int i: Arrays.asList(1,2,3,4,5,6,7,9,10))
buf.add(i, i);
IntStream.rangeClosed(1,10).forEach(n -> buf.add(n,n));
List<Integer> list=buf.removeMany(3);
System.out.println("list = " + list);
assert list != null && list.size() == 3;

list=buf.removeMany(0);
System.out.println("list = " + list);
assert list != null && list.size() == 4;
assert list != null && list.size() == 7;

list=buf.removeMany(10);
assert list == null;

buf.add(8, 8);
list=buf.removeMany(0);
System.out.println("list = " + list);
assert list != null && list.size() == 3;
assert list == null;
}

public void testRemoveManyWithNulling() {
public void testRemoveManyWithMissingElements() {
RingBufferSeqno<Integer> buf=new RingBufferSeqno<>(10, 0);
for(int i: Arrays.asList(1,2,3,4,5,6,7,9,10))
buf.add(i, i);
Expand Down Expand Up @@ -594,8 +587,9 @@ public void testIterator() {
}
}
System.out.println();
assert count == 9 : "count=" + count;
buf.add(8,8);
assert count == 10 : "count=" + count;
boolean rc=buf.add(8, 8);
assert rc == false;
count=0;
for(Integer num: buf) {
if(num != null) {
Expand Down

0 comments on commit ae76cff

Please sign in to comment.