Skip to content

Commit

Permalink
added binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Austin committed Mar 31, 2017
1 parent 285f541 commit 19b021e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
Expand Up @@ -306,7 +306,7 @@ public int nextCycle(int cycle, @NotNull TailerDirection direction) throws Parse
return pool.nextCycle(cycle, direction); return pool.nextCycle(cycle, direction);
} }


long exceptsPerCycle(int cycle) { public long exceptsPerCycle(int cycle) {
StoreTailer tailer = acquireTailer(); StoreTailer tailer = acquireTailer();
try { try {
long index = rollCycle.toIndex(cycle, 0); long index = rollCycle.toIndex(cycle, 0);
Expand Down
43 changes: 24 additions & 19 deletions src/test/java/net/openhft/chronicle/queue/BinarySearch.java
Expand Up @@ -6,8 +6,8 @@
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;


import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.NavigableSet; import java.util.NavigableSet;


/** /**
Expand All @@ -31,45 +31,50 @@ public <T> long search(@NotNull SingleChronicleQueue q,
return findWithinCycle(key, c, start, end, tailer, q.rollCycle()); return findWithinCycle(key, c, start, end, tailer, q.rollCycle());
} }


final NavigableSet<Long> longs = q.listCyclesBetween(startCycle, endCycle); final NavigableSet<Long> cycles = q.listCyclesBetween(startCycle, endCycle);


final int cycle = findCycle(q.rollCycle(), longs, key, c, tailer); final int cycle = (int) findCycle(q.rollCycle(), cycles, key, c, tailer);
if (cycle == -1) if (cycle == -1)
return -1; return -1;


final long count = q.exceptsPerCycle(cycle);

final long startIndex = q.rollCycle().toIndex(cycle, 0); final long startIndex = q.rollCycle().toIndex(cycle, 0);
final long cycleEnd = q.rollCycle().toIndex(cycle + 1, 0); final long endIndex = q.rollCycle().toIndex(cycle, count - 1);
tailer.direction(TailerDirection.BACKWARD);
tailer.moveToIndex(cycleEnd);
tailer.readingDocument().close(); System.out.print(q.rollCycle().toCycle(endIndex));
final long endIndex = tailer.index();
tailer.direction(TailerDirection.FORWARD); tailer.direction(TailerDirection.FORWARD);


return findWithinCycle(key, c, startIndex, endIndex, tailer, q.rollCycle()); return findWithinCycle(key, c, startIndex, endIndex, tailer, q.rollCycle());


} }




private int findCycle(RollCycle rollCycle, NavigableSet l, Wire key, Comparator<Wire> c, ExcerptTailer tailer) { private long findCycle(RollCycle rollCycle, NavigableSet cycles, Wire key, Comparator<Wire> c, ExcerptTailer tailer) {
int low = 0; int low = 0;
int high = l.size() - 1; int high = cycles.size() - 1;
Iterator i = l.iterator(); final ArrayList<Long> arrayList = new ArrayList<>(cycles);
final long readPosition = key.bytes().readPosition(); final long readPosition = key.bytes().readPosition();
while (low <= high) { while (low <= high) {
int mid = (low + high) >>> 1; int mid = (low + high) >>> 1;

final Long midCycle = arrayList.get(mid);
final long index = rollCycle.toIndex(mid, 0); final int midCycle1 = (int) (long) midCycle;
final long index = rollCycle.toIndex(midCycle1, 0);
try (DocumentContext midVal = get(index, tailer)) { try (DocumentContext midVal = get(index, tailer)) {


int cmp = c.compare(midVal.wire(), key); int cmp = c.compare(midVal.wire(), key);
if (cmp == 0) if (cmp == 0 && mid == high)
return low; return arrayList.get(high);

if (cmp < 0) if (cmp < 0)
low = mid + 1; low = mid + 1;
else if (cmp > 0) else if (cmp > 0)
high = mid - 1; high = mid - 1;
if (low == high) else if (low == high - 1)
return low; return arrayList.get(low);

} finally { } finally {
key.bytes().readPosition(readPosition); key.bytes().readPosition(readPosition);
} }
Expand All @@ -83,7 +88,7 @@ private <T> long findWithinCycle(Wire key, Comparator<Wire> c, long low, long hi
final long readPosition = key.bytes().readPosition(); final long readPosition = key.bytes().readPosition();
while (low <= high) { while (low <= high) {
long mid = (low + high) >>> 1L; long mid = (low + high) >>> 1L;

System.out.println("low" + rollCycle.toSequenceNumber(low) + ",high" + rollCycle.toSequenceNumber(high) + ",mid" + rollCycle.toSequenceNumber(mid));
try (DocumentContext dc = get(mid, tailer)) { try (DocumentContext dc = get(mid, tailer)) {
if (!dc.isPresent()) if (!dc.isPresent())
return -1; return -1;
Expand All @@ -101,7 +106,7 @@ else if (cmp > 0)
} }


} }
return -(low + 1); // key not found return -1; // key not found
} }




Expand Down
15 changes: 13 additions & 2 deletions src/test/java/net/openhft/chronicle/queue/service/TestSearch.java
@@ -1,6 +1,7 @@
package net.openhft.chronicle.queue.service; package net.openhft.chronicle.queue.service;


import net.openhft.chronicle.bytes.Bytes; import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.time.SetTimeProvider;
import net.openhft.chronicle.queue.BinarySearch; import net.openhft.chronicle.queue.BinarySearch;
import net.openhft.chronicle.queue.ChronicleQueueTestBase; import net.openhft.chronicle.queue.ChronicleQueueTestBase;
import net.openhft.chronicle.queue.ExcerptAppender; import net.openhft.chronicle.queue.ExcerptAppender;
Expand All @@ -15,6 +16,8 @@
import java.text.ParseException; import java.text.ParseException;
import java.util.Comparator; import java.util.Comparator;


import static net.openhft.chronicle.queue.RollCycles.TEST_SECONDLY;

/** /**
* @author Rob Austin. * @author Rob Austin.
*/ */
Expand All @@ -26,9 +29,15 @@ public class TestSearch extends ChronicleQueueTestBase {


@Test @Test
public void test() throws ParseException { public void test() throws ParseException {

SetTimeProvider stp = new SetTimeProvider();
long time = System.currentTimeMillis();
stp.currentTimeMillis(time);

try (SingleChronicleQueue queue = SingleChronicleQueueBuilder try (SingleChronicleQueue queue = SingleChronicleQueueBuilder
.fieldlessBinary(getTmpDir()) .binary(getTmpDir())
.blockSize(128 << 20) .rollCycle(TEST_SECONDLY)
.timeProvider(stp)
.build()) { .build()) {


final ExcerptAppender appender = queue.acquireAppender(); final ExcerptAppender appender = queue.acquireAppender();
Expand All @@ -39,6 +48,8 @@ public void test() throws ParseException {
myData.value = "some value where the key=" + String.valueOf(i); myData.value = "some value where the key=" + String.valueOf(i);
try (final DocumentContext dc = appender.writingDocument()) { try (final DocumentContext dc = appender.writingDocument()) {
dc.wire().getValueOut().marshallable(myData); dc.wire().getValueOut().marshallable(myData);
time += 300;
stp.currentTimeMillis(time);
} }


} }
Expand Down

0 comments on commit 19b021e

Please sign in to comment.