Skip to content

Commit

Permalink
fixed test search
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Austin committed Apr 3, 2017
1 parent f543742 commit 10db7f9
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 39 deletions.
89 changes: 68 additions & 21 deletions src/test/java/net/openhft/chronicle/queue/BinarySearch.java
Expand Up @@ -7,10 +7,7 @@
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.*;
import java.util.Comparator;
import java.util.List;
import java.util.NavigableSet;


/** /**
* @author Rob Austin. * @author Rob Austin.
Expand All @@ -36,20 +33,63 @@ public <T> long search(@NotNull SingleChronicleQueue q,
final NavigableSet<Long> cycles = q.listCyclesBetween(startCycle, endCycle); final NavigableSet<Long> cycles = q.listCyclesBetween(startCycle, endCycle);




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


long highSeqNum = q.exceptsPerCycle(cycle) - 1;
tailer.moveToIndex(q.rollCycle().toIndex(cycle, 0));
try (final DocumentContext dc = tailer.readingDocument()) {
final TestSearch.MyData myData = new TestSearch.MyData();
dc.wire().getValueIn().marshallable(myData);
System.out.println("findCycle - low=" + myData.toString() + "high+=" + highSeqNum);
}



tailer.direction(TailerDirection.FORWARD); tailer.direction(TailerDirection.FORWARD);


return findWithinCycle(key, c, cycle, tailer, q); return findWithinCycle(key, c, cycle, tailer, q);
} finally { } finally {
key.bytes().readPosition(readPosition); key.bytes().readPosition(readPosition);
} }


} }




private long findCycleLinearSearch(NavigableSet<Long> cycles, Wire key, Comparator<Wire> c, ExcerptTailer tailer) {

final Iterator<Long> iterator = cycles.iterator();
if (!iterator.hasNext())
return -1;
final RollCycle rollCycle = ((SingleChronicleQueue) (tailer.queue())).rollCycle();
long prevIndex = iterator.next();

while (iterator.hasNext()) {

final Long current = iterator.next();

final boolean b = tailer.moveToIndex(rollCycle.toIndex((int) (long) current, 0));
if (!b)
return prevIndex;


try (final DocumentContext dc = tailer.readingDocument()) {
dc.wire().bytes().readSkip(-4);
final int compare = c.compare(dc.wire(), key);
if (compare == 0)
return current;
else if (compare == 1)
return prevIndex;

prevIndex = current;
}
}

return prevIndex;

}

/** /**
* @param rollCycle * @param rollCycle
* @param cycles * @param cycles
Expand All @@ -58,32 +98,33 @@ public <T> long search(@NotNull SingleChronicleQueue q,
* @param tailer * @param tailer
* @return -1 value if not found, otherwise the cycle number * @return -1 value if not found, otherwise the cycle number
*/ */
private long findCycle(RollCycle rollCycle, NavigableSet cycles, Wire key, Comparator<Wire> c, ExcerptTailer tailer) {
private long findCycle(RollCycle rollCycle, NavigableSet<Long> cycles, Wire key, Comparator<Wire> c, ExcerptTailer tailer) {


final long readPosition = key.bytes().readPosition(); final long readPosition = key.bytes().readPosition();
try { try {
final SingleChronicleQueue q = ((SingleChronicleQueue) tailer.queue()); final SingleChronicleQueue q = ((SingleChronicleQueue) tailer.queue());
int low = 0; int low = 0;
int high = cycles.size() - 1; int high = cycles.size() - 1;
final List<Long> arrayList = new ArrayList<>(cycles); final List<Long> list = new ArrayList<>(cycles);


int mid = -1; int mid = -1;
Long midCycle = 1L; Long midCycle = 1L;
while (low <= high) { while (low <= high) {
mid = (low + high) >>> 1; mid = (low + high) >>> 1;
midCycle = arrayList.get(mid); midCycle = list.get(mid);
final long index = rollCycle.toIndex((int) (long) midCycle, 0); final long index = rollCycle.toIndex((int) (long) midCycle, 0);




tailer.moveToIndex(rollCycle.toIndex((int) (long) arrayList.get(low), 0)); tailer.moveToIndex(rollCycle.toIndex((int) (long) list.get(low), 0));
try (final DocumentContext dc = tailer.readingDocument()) { try (final DocumentContext dc = tailer.readingDocument()) {
final TestSearch.MyData myData = new TestSearch.MyData(); final TestSearch.MyData myData = new TestSearch.MyData();
dc.wire().getValueIn().marshallable(myData); dc.wire().getValueIn().marshallable(myData);
System.out.println("findCycle - low=" + myData.toString()); System.out.println("findCycle - low=" + myData.toString());
} }




tailer.moveToIndex(rollCycle.toIndex((int) (long) arrayList.get(high), 0)); tailer.moveToIndex(rollCycle.toIndex((int) (long) list.get(high), 0));
try (final DocumentContext dc = tailer.readingDocument()) { try (final DocumentContext dc = tailer.readingDocument()) {
if (!dc.isPresent()) if (!dc.isPresent())
System.out.println(""); System.out.println("");
Expand All @@ -109,17 +150,18 @@ private long findCycle(RollCycle rollCycle, NavigableSet cycles, Wire key, Compa
} }


if (low == high) { if (low == high) {
return arrayList.get(high); return list.get(high);
} }


if (low + 1 == high) {


try (DocumentContext dc = moveTo(rollCycle.toIndex((int) (long) arrayList.get(high), 0), tailer)) { if (low + 1 == high) {
key.bytes().readPosition(readPosition);
try (DocumentContext dc = moveTo(rollCycle.toIndex((int) (long) list.get(mid), 0), tailer)) {
int cmp = c.compare(dc.wire(), key); int cmp = c.compare(dc.wire(), key);
if (cmp == 0 || cmp == -1) if (cmp == 0 || cmp == -1)
return arrayList.get(high); return list.get(high);


return arrayList.get(low); return list.get(low);
} }
} }


Expand All @@ -129,15 +171,20 @@ private long findCycle(RollCycle rollCycle, NavigableSet cycles, Wire key, Compa
int cmp = c.compare(dc.wire(), key); int cmp = c.compare(dc.wire(), key);


if (cmp < 0) { if (cmp < 0) {

if (mid - 1 == low) {
low = mid;
continue;
}
low = mid + 1; low = mid + 1;
} else if (cmp > 0) } else if (cmp > 0) {
if (mid + 1 == high) {
high = mid;
continue;
}
high = mid - 1; high = mid - 1;
else } else
return midCycle; return midCycle;

if (low == high)
return arrayList.get(low); // key not found

} }
} }
if (mid == -1 || low > high) if (mid == -1 || low > high)
Expand Down
60 changes: 42 additions & 18 deletions src/test/java/net/openhft/chronicle/queue/service/TestSearch.java
Expand Up @@ -14,14 +14,15 @@
import java.io.File; import java.io.File;
import java.text.ParseException; import java.text.ParseException;
import java.util.Comparator; import java.util.Comparator;
import java.util.Objects;


/** /**
* @author Rob Austin. * @author Rob Austin.
*/ */
public class TestSearch extends ChronicleQueueTestBase { public class TestSearch extends ChronicleQueueTestBase {




public static final int MAX_SIZE = 6; public static final int MAX_SIZE = 20;




@Test @Test
Expand Down Expand Up @@ -63,27 +64,36 @@ public void test() throws ParseException {


final Comparator<Wire> comparator = (o1, o2) -> { final Comparator<Wire> comparator = (o1, o2) -> {


MyData myDataO1 = new MyData(); final long readPositionO1 = o1.bytes().readPosition();
MyData myDataO2 = new MyData(); final long readPositionO2 = o2.bytes().readPosition();
try {
MyData myDataO1 = null;
MyData myDataO2 = null;


try (final DocumentContext dc = o1.readingDocument()) { try (final DocumentContext dc = o1.readingDocument()) {
myDataO1 = dc.wire().getValueIn().typedMarshallable();
assert myDataO1.value != null;
System.out.println("Comparator - low=" + myDataO1);
}


myDataO1 = dc.wire().getValueIn().typedMarshallable();
assert myDataO1.value != null;
System.out.println("Comparator - low=" + myDataO1);
}


try (final DocumentContext dc = o2.readingDocument()) {
myDataO2 = dc.wire().getValueIn().typedMarshallable(); try (final DocumentContext dc = o2.readingDocument()) {
if (myDataO2.value == null) myDataO2 = dc.wire().getValueIn().typedMarshallable();
System.out.println(""); System.out.println("Comparator - high=" + myDataO2);
assert myDataO2.value != null; assert myDataO2.value != null;
System.out.println("Comparator - high=" + myDataO2); }
}




final int compare = Integer.compare(myDataO1.key, myDataO2.key); final int compare = Integer.compare(myDataO1.key, myDataO2.key);
return compare; System.out.println("compare =" + compare);


return compare;
} finally {
o1.bytes().readPosition(readPositionO1);
o2.bytes().readPosition(readPositionO2);
}
}; };


if (i == 3) if (i == 3)
Expand Down Expand Up @@ -113,7 +123,7 @@ public void test() throws ParseException {
private Wire toWire(int key) { private Wire toWire(int key) {
final MyData myData = new MyData(); final MyData myData = new MyData();
myData.key = key; myData.key = key;
myData.value = "unknown"; myData.value = Integer.toString(key);


Wire result = WireType.BINARY.apply(Bytes.elasticByteBuffer()); Wire result = WireType.BINARY.apply(Bytes.elasticByteBuffer());


Expand Down Expand Up @@ -145,5 +155,19 @@ public void writeMarshallable(@NotNull WireOut wire) {
wire.write("key").int32(key); wire.write("key").int32(key);
wire.write("value").text(value); wire.write("value").text(value);
} }

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MyData)) return false;
if (!super.equals(o)) return false;
MyData myData = (MyData) o;
return key == myData.key;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), key);
}
} }
} }

0 comments on commit 10db7f9

Please sign in to comment.