forked from lmdbjava/lmdbjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCursorIterator.java
More file actions
197 lines (176 loc) · 4.12 KB
/
Copy pathCursorIterator.java
File metadata and controls
197 lines (176 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*-
* #%L
* LmdbJava
* %%
* Copyright (C) 2016 - 2017 The LmdbJava Open Source Project
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.lmdbjava;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static org.lmdbjava.CursorIterator.IteratorType.FORWARD;
import static org.lmdbjava.CursorIterator.State.DONE;
import static org.lmdbjava.CursorIterator.State.NOT_READY;
import static org.lmdbjava.CursorIterator.State.READY;
import static org.lmdbjava.GetOp.MDB_SET_RANGE;
/**
* Iterator for entries that follow the same semantics as Cursors with regards
* to read and write transactions and how they are closed.
*
* @param <T> buffer type
*/
public final class CursorIterator<T> implements
Iterator<CursorIterator.KeyVal<T>>,
AutoCloseable {
private final Cursor<T> cursor;
private KeyVal<T> entry;
private boolean first = true;
private final T key;
private State state = NOT_READY;
private final IteratorType type;
CursorIterator(final Cursor<T> cursor, final T key, final IteratorType type) {
this.cursor = cursor;
this.type = type;
this.key = key;
}
@Override
public void close() {
cursor.close();
}
@Override
@SuppressWarnings("checkstyle:returncount")
public boolean hasNext() {
switch (state) {
case DONE:
return false;
case READY:
return true;
default:
}
return tryToComputeNext();
}
/**
* Obtain an iterator.
*
* @return an iterator
*/
public Iterable<KeyVal<T>> iterable() {
return () -> CursorIterator.this;
}
@Override
public KeyVal<T> next() throws NoSuchElementException {
if (!hasNext()) {
throw new NoSuchElementException();
}
state = NOT_READY;
final KeyVal<T> result = entry;
entry = null;
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
private void setEntry(final boolean success) {
if (success) {
this.entry = new KeyVal<>(cursor.key(), cursor.val());
} else {
this.entry = null;
}
}
@SuppressWarnings("checkstyle:returncount")
private boolean tryToComputeNext() {
if (first) {
if (key != null) { // NOPMD
setEntry(cursor.get(key, MDB_SET_RANGE));
} else if (type == FORWARD) {
setEntry(cursor.first());
} else {
setEntry(cursor.last());
}
first = false;
if (entry == null) {
state = DONE;
return false;
}
} else {
if (type == FORWARD) {
setEntry(cursor.next());
} else {
setEntry(cursor.prev());
}
if (entry == null) {
state = DONE;
return false;
}
}
state = READY;
return true;
}
/**
* Holder for a key and value pair.
*
* @param <T> buffer type
*/
public static final class KeyVal<T> {
private final T k;
private final T v;
/**
* Obtain a key-value holder.
*
* @param key the key
* @param val the value
*/
public KeyVal(final T key, final T val) {
this.k = key;
this.v = val;
}
/**
* The key.
*
* @return key
*/
public T key() {
return k;
}
/**
* The value.
*
* @return value
*/
public T val() {
return v;
}
}
/**
* Direction in terms of key ordering for CursorIterator.
*/
public enum IteratorType {
/**
* Move forward.
*/
FORWARD,
/**
* Move backward.
*/
BACKWARD
}
/**
* Represents the internal {@link CursorIterator} state.
*/
enum State {
READY, NOT_READY, DONE, FAILED,
}
}