Skip to content

Commit f45addd

Browse files
committed
Add range support for bidirectional closedOpen (#7)
1 parent 51671c5 commit f45addd

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

src/main/java/org/lmdbjava/KeyRange.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ public static <T> KeyRange<T> closedBackward(final T start, final T stop) {
154154
return new KeyRange<>(KeyRangeType.BACKWARD_CLOSED, start, stop);
155155
}
156156

157+
/**
158+
* Create a {@link KeyRangeType#FORWARD_CLOSED_OPEN} range.
159+
*
160+
* @param <T> buffer type
161+
* @param start start key (required)
162+
* @param stop stop key (required)
163+
* @return a key range (never null)
164+
*/
165+
public static <T> KeyRange<T> closedOpen(final T start, final T stop) {
166+
return new KeyRange<>(KeyRangeType.FORWARD_CLOSED_OPEN, start, stop);
167+
}
168+
169+
/**
170+
* Create a {@link KeyRangeType#BACKWARD_CLOSED_OPEN} range.
171+
*
172+
* @param <T> buffer type
173+
* @param start start key (required)
174+
* @param stop stop key (required)
175+
* @return a key range (never null)
176+
*/
177+
public static <T> KeyRange<T> closedOpenBackward(final T start, final T stop) {
178+
return new KeyRange<>(KeyRangeType.BACKWARD_CLOSED_OPEN, start, stop);
179+
}
180+
157181
/**
158182
* Create a {@link KeyRangeType#FORWARD_GREATER_THAN} range.
159183
*

src/main/java/org/lmdbjava/KeyRangeType.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ public enum KeyRangeType {
9797
* would be 4 and 6. With a range of 2 - 6, the keys would be 2, 4 and 6.
9898
*/
9999
FORWARD_CLOSED(true, true, true),
100+
/**
101+
* Iterate forward between the passed keys, matching on the first keys
102+
* directly equal to the passed key (or immediately following it in the case
103+
* of the "start" key, or immediately preceding it in the case of the "stop"
104+
* key). Do not return the "stop" key.
105+
*
106+
* <p>
107+
* The "start" and "stop" values are both required.
108+
*
109+
* <p>
110+
* In our example and with a passed search range of 3 - 8, the returned keys
111+
* would be 4 and 6. With a range of 2 - 6, the keys would be 2 and 4.
112+
*/
113+
FORWARD_CLOSED_OPEN(true, true, true),
100114
/**
101115
* Start after the passed key (but not equal to it) and iterate forward until
102116
* no keys remain.
@@ -180,6 +194,20 @@ public enum KeyRangeType {
180194
* would be 6 and 4. With a range of 6 - 2, the keys would be 6, 4 and 2.
181195
*/
182196
BACKWARD_CLOSED(false, true, true),
197+
/**
198+
* Iterate backward between the passed keys, matching on the first keys
199+
* directly equal to the passed key (or immediately preceding it in the case
200+
* of the "start" key, or immediately following it in the case of the "stop"
201+
* key). Do not return the "stop" key.
202+
*
203+
* <p>
204+
* The "start" and "stop" values are both required.
205+
*
206+
* <p>
207+
* In our example and with a passed search range of 8 - 3, the returned keys
208+
* would be 8, 6 and 4. With a range of 7 - 2, the keys would be 6 and 4.
209+
*/
210+
BACKWARD_CLOSED_OPEN(false, true, true),
183211
/**
184212
* Start immediate prior to the passed key (but not equal to it) and iterate
185213
* backward until no keys remain.
@@ -275,6 +303,8 @@ CursorOp initialOp() {
275303
return FIRST;
276304
case FORWARD_CLOSED:
277305
return GET_START_KEY;
306+
case FORWARD_CLOSED_OPEN:
307+
return GET_START_KEY;
278308
case FORWARD_GREATER_THAN:
279309
return GET_START_KEY;
280310
case FORWARD_LESS_THAN:
@@ -289,6 +319,8 @@ CursorOp initialOp() {
289319
return LAST;
290320
case BACKWARD_CLOSED:
291321
return GET_START_KEY;
322+
case BACKWARD_CLOSED_OPEN:
323+
return GET_START_KEY;
292324
case BACKWARD_GREATER_THAN:
293325
return GET_START_KEY;
294326
case BACKWARD_LESS_THAN:
@@ -327,6 +359,8 @@ <T, C extends Comparator<T>> IteratorOp iteratorOp(final T start, final T stop,
327359
return c.compare(buffer, stop) > 0 ? TERMINATE : RELEASE;
328360
case FORWARD_CLOSED:
329361
return c.compare(buffer, stop) > 0 ? TERMINATE : RELEASE;
362+
case FORWARD_CLOSED_OPEN:
363+
return c.compare(buffer, stop) >= 0 ? TERMINATE : RELEASE;
330364
case FORWARD_GREATER_THAN:
331365
return c.compare(buffer, start) == 0 ? CALL_NEXT_OP : RELEASE;
332366
case FORWARD_LESS_THAN:
@@ -347,6 +381,11 @@ <T, C extends Comparator<T>> IteratorOp iteratorOp(final T start, final T stop,
347381
return CALL_NEXT_OP; // rewind
348382
}
349383
return c.compare(buffer, stop) >= 0 ? RELEASE : TERMINATE;
384+
case BACKWARD_CLOSED_OPEN:
385+
if (c.compare(buffer, start) > 0) {
386+
return CALL_NEXT_OP; // rewind
387+
}
388+
return c.compare(buffer, stop) > 0 ? RELEASE : TERMINATE;
350389
case BACKWARD_GREATER_THAN:
351390
return c.compare(buffer, start) >= 0 ? CALL_NEXT_OP : RELEASE;
352391
case BACKWARD_LESS_THAN:

src/test/java/org/lmdbjava/CursorIteratorTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import static org.lmdbjava.KeyRange.atMostBackward;
5151
import static org.lmdbjava.KeyRange.closed;
5252
import static org.lmdbjava.KeyRange.closedBackward;
53+
import static org.lmdbjava.KeyRange.closedOpen;
54+
import static org.lmdbjava.KeyRange.closedOpenBackward;
5355
import static org.lmdbjava.KeyRange.greaterThan;
5456
import static org.lmdbjava.KeyRange.greaterThanBackward;
5557
import static org.lmdbjava.KeyRange.lessThan;
@@ -159,6 +161,18 @@ public void closedBackwardTest() {
159161
verify(closedBackward(bb(6), bb(2)), 6, 4, 2);
160162
}
161163

164+
@Test
165+
public void closedOpenBackwardTest() {
166+
verify(closedOpenBackward(bb(8), bb(3)), 8, 6, 4);
167+
verify(closedOpenBackward(bb(7), bb(2)), 6, 4);
168+
}
169+
170+
@Test
171+
public void closedOpenTest() {
172+
verify(closedOpen(bb(3), bb(8)), 4, 6);
173+
verify(closedOpen(bb(2), bb(6)), 2, 4);
174+
}
175+
162176
@Test
163177
public void closedTest() {
164178
verify(closed(bb(3), bb(7)), 4, 6);

src/test/java/org/lmdbjava/KeyRangeTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import static org.lmdbjava.KeyRange.atMostBackward;
3636
import static org.lmdbjava.KeyRange.closed;
3737
import static org.lmdbjava.KeyRange.closedBackward;
38+
import static org.lmdbjava.KeyRange.closedOpen;
39+
import static org.lmdbjava.KeyRange.closedOpenBackward;
3840
import static org.lmdbjava.KeyRange.greaterThan;
3941
import static org.lmdbjava.KeyRange.greaterThanBackward;
4042
import static org.lmdbjava.KeyRange.lessThan;
@@ -103,6 +105,18 @@ public void closedBackwardTest() {
103105
verify(closedBackward(6, 2), 6, 4, 2);
104106
}
105107

108+
@Test
109+
public void closedOpenBackwardTest() {
110+
verify(closedOpenBackward(8, 3), 8, 6, 4);
111+
verify(closedOpenBackward(7, 2), 6, 4);
112+
}
113+
114+
@Test
115+
public void closedOpenTest() {
116+
verify(closedOpen(3, 8), 4, 6);
117+
verify(closedOpen(2, 6), 2, 4);
118+
}
119+
106120
@Test
107121
public void closedTest() {
108122
verify(closed(3, 7), 4, 6);

0 commit comments

Comments
 (0)