Skip to content

Commit

Permalink
fix ringbuffer expand() code
Browse files Browse the repository at this point in the history
  • Loading branch information
arnej27959 committed Sep 26, 2024
1 parent dda7caa commit 00eeec2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,11 @@ private void expand() {
int offset = (int) (tail % size);
int newOffset = (int) (tail % newSize);
int toWrite = size - offset;
// exactly doubling the buffer size ensures there is room in the new buffer
System.arraycopy(data, offset, newData, newOffset, toWrite);
if (toWrite < size)
System.arraycopy(data, 0, newData, newOffset + toWrite, size - toWrite);
// the wrapped data ends up either after the first part, or at the start of the new buffer
newOffset = (newOffset + toWrite) % newSize;
System.arraycopy(data, 0, newData, newOffset, offset);
size = newSize;
data = newData;
lock.notify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,25 @@

class JsonFeederTest {

static String lulVal(int i) {
var buf = new StringBuilder();
while (i-- > 0) {
buf.append("lal");
i = (int) (i/1.005);
}
return buf.toString();
}

@Test
void test() throws IOException {
int docs = 1 << 14;
int docs = 1 << 12;
String json = "[\n" +

IntStream.range(0, docs).mapToObj(i ->
" {\n" +
" \"id\": \"id:ns:type::abc" + i + "\",\n" +
" \"fields\": {\n" +
" \"lul\":\"lal\"\n" +
" \"lul\":\"" + lulVal(i) + "\"\n" +
" }\n" +
" },\n"
).collect(joining()) +
Expand All @@ -64,7 +73,7 @@ void test() throws IOException {
long startNanos = System.nanoTime();
MockClient feedClient = new MockClient();
JsonFeeder.builder(feedClient).build()
.feedMany(in, 1 << 10,
.feedMany(in, 1 << 7,
new JsonFeeder.ResultCallback() {
@Override
public void onNextResult(Result result, FeedException error) { resultsReceived.incrementAndGet(); }
Expand Down

0 comments on commit 00eeec2

Please sign in to comment.