forked from lmdbjava/lmdbjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTxnTest.java
More file actions
308 lines (268 loc) · 8.98 KB
/
TxnTest.java
File metadata and controls
308 lines (268 loc) · 8.98 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*-
* #%L
* LmdbJava
* %%
* Copyright (C) 2016 - 2019 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 static com.jakewharton.byteunits.BinaryByteUnit.KIBIBYTES;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import static java.nio.ByteBuffer.allocateDirect;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.After;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.lmdbjava.Dbi.BadValueSizeException;
import static org.lmdbjava.DbiFlags.MDB_CREATE;
import org.lmdbjava.Env.AlreadyClosedException;
import static org.lmdbjava.Env.create;
import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR;
import static org.lmdbjava.EnvFlags.MDB_RDONLY_ENV;
import static org.lmdbjava.TestUtils.DB_1;
import static org.lmdbjava.TestUtils.POSIX_MODE;
import static org.lmdbjava.TestUtils.bb;
import org.lmdbjava.Txn.EnvIsReadOnly;
import org.lmdbjava.Txn.IncompatibleParent;
import org.lmdbjava.Txn.NotReadyException;
import org.lmdbjava.Txn.NotResetException;
import org.lmdbjava.Txn.ReadOnlyRequiredException;
import org.lmdbjava.Txn.ReadWriteRequiredException;
import org.lmdbjava.Txn.ResetException;
import static org.lmdbjava.Txn.State.DONE;
import static org.lmdbjava.Txn.State.READY;
import static org.lmdbjava.Txn.State.RELEASED;
import static org.lmdbjava.Txn.State.RESET;
import static org.lmdbjava.TxnFlags.MDB_RDONLY_TXN;
/**
* Test {@link Txn}.
*/
public final class TxnTest {
@Rule
public final TemporaryFolder tmp = new TemporaryFolder();
private Env<ByteBuffer> env;
private File path;
@After
public void after() {
env.close();
}
@Before
public void before() throws IOException {
path = tmp.newFile();
env = create()
.setMapSize(KIBIBYTES.toBytes(100))
.setMaxReaders(1)
.setMaxDbs(2)
.open(path, POSIX_MODE, MDB_NOSUBDIR);
}
@Test(expected = BadValueSizeException.class)
public void largeKeysRejected() throws IOException {
final Dbi<ByteBuffer> dbi = env.openDbi(DB_1, MDB_CREATE);
final ByteBuffer key = allocateDirect(env.getMaxKeySize() + 1);
key.limit(key.capacity());
dbi.put(key, bb(2));
}
@Test
public void rangeSearch() {
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
final ByteBuffer key = allocateDirect(env.getMaxKeySize());
key.put("cherry".getBytes(UTF_8)).flip();
db.put(key, bb(1));
key.clear();
key.put("strawberry".getBytes(UTF_8)).flip();
db.put(key, bb(3));
key.clear();
key.put("pineapple".getBytes(UTF_8)).flip();
db.put(key, bb(2));
try (Txn<ByteBuffer> txn = env.txnRead()) {
final ByteBuffer start = allocateDirect(env.getMaxKeySize());
start.put("a".getBytes(UTF_8)).flip();
final ByteBuffer end = allocateDirect(env.getMaxKeySize());
end.put("z".getBytes(UTF_8)).flip();
final List<String> keysFound = new ArrayList<>();
final CursorIterator<ByteBuffer> ckr = db.iterate(txn, KeyRange.closed(
start, end));
for (final CursorIterator.KeyVal<ByteBuffer> kv : ckr.iterable()) {
keysFound.add(UTF_8.decode(kv.key()).toString());
}
assertEquals(3, keysFound.size());
}
}
@Test
public void readOnlyTxnAllowedInReadOnlyEnv() {
env.openDbi(DB_1, MDB_CREATE);
try (Env<ByteBuffer> roEnv = create()
.setMaxReaders(1)
.open(path, MDB_NOSUBDIR, MDB_RDONLY_ENV)) {
assertThat(roEnv.txnRead(), is(notNullValue()));
}
}
@Test(expected = EnvIsReadOnly.class)
public void readWriteTxnDeniedInReadOnlyEnv() {
env.openDbi(DB_1, MDB_CREATE);
env.close();
try (Env<ByteBuffer> roEnv = create()
.setMaxReaders(1)
.open(path, MDB_NOSUBDIR, MDB_RDONLY_ENV)) {
roEnv.txnWrite(); // error
}
}
@Test(expected = NotReadyException.class)
public void testCheckNotCommitted() {
final Txn<ByteBuffer> txn = env.txnRead();
txn.commit();
txn.checkReady();
}
@Test(expected = ReadOnlyRequiredException.class)
public void testCheckReadOnly() {
final Txn<ByteBuffer> txn = env.txnWrite();
txn.checkReadOnly();
}
@Test(expected = ReadWriteRequiredException.class)
public void testCheckWritesAllowed() {
final Txn<ByteBuffer> txn = env.txnRead();
txn.checkWritesAllowed();
}
@Test
public void testGetId() {
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
final AtomicLong txId1 = new AtomicLong();
final AtomicLong txId2 = new AtomicLong();
try (Txn<ByteBuffer> tx1 = env.txnRead()) {
txId1.set(tx1.getId());
}
db.put(bb(1), bb(2));
try (Txn<ByteBuffer> tx2 = env.txnRead()) {
txId2.set(tx2.getId());
}
// should not see the same snapshot
assertThat(txId1.get(), is(not(txId2.get())));
}
@Test
public void txCanCommitThenCloseWithoutError() {
try (Txn<ByteBuffer> txn = env.txnRead()) {
assertThat(txn.getState(), is(READY));
txn.commit();
assertThat(txn.getState(), is(DONE));
}
}
@Test(expected = NotReadyException.class)
public void txCannotAbortIfAlreadyCommitted() {
try (Txn<ByteBuffer> txn = env.txnRead()) {
assertThat(txn.getState(), is(READY));
txn.commit();
assertThat(txn.getState(), is(DONE));
txn.abort();
}
}
@Test(expected = NotReadyException.class)
public void txCannotCommitTwice() {
final Txn<ByteBuffer> txn = env.txnRead();
txn.commit();
txn.commit(); // error
}
@Test(expected = AlreadyClosedException.class)
@SuppressWarnings("ResultOfObjectAllocationIgnored")
public void txConstructionDeniedIfEnvClosed() {
env.close();
env.txnRead();
}
@Test
public void txParent() {
final Txn<ByteBuffer> txRoot = env.txnWrite();
final Txn<ByteBuffer> txChild = env.txn(txRoot);
assertThat(txRoot.getParent(), is(nullValue()));
assertThat(txChild.getParent(), is(txRoot));
}
@Test(expected = IncompatibleParent.class)
public void txParentROChildRWIncompatible() {
final Txn<ByteBuffer> txRoot = env.txnRead();
env.txn(txRoot); // error
}
@Test(expected = IncompatibleParent.class)
public void txParentRWChildROIncompatible() {
final Txn<ByteBuffer> txRoot = env.txnWrite();
env.txn(txRoot, MDB_RDONLY_TXN); // error
}
@Test
public void txReadOnly() {
final Txn<ByteBuffer> txn = env.txnRead();
assertThat(txn.getParent(), is(nullValue()));
assertThat(txn.getState(), is(READY));
assertThat(txn.isReadOnly(), is(true));
txn.checkReady();
txn.checkReadOnly();
txn.reset();
assertThat(txn.getState(), is(RESET));
txn.renew();
assertThat(txn.getState(), is(READY));
txn.commit();
assertThat(txn.getState(), is(DONE));
txn.close();
assertThat(txn.getState(), is(RELEASED));
}
@Test
public void txReadWrite() {
final Txn<ByteBuffer> txn = env.txnWrite();
assertThat(txn.getParent(), is(nullValue()));
assertThat(txn.getState(), is(READY));
assertThat(txn.isReadOnly(), is(false));
txn.checkReady();
txn.checkWritesAllowed();
txn.commit();
assertThat(txn.getState(), is(DONE));
txn.close();
assertThat(txn.getState(), is(RELEASED));
}
@Test(expected = NotResetException.class)
public void txRenewDeniedWithoutPriorReset() {
final Txn<ByteBuffer> txn = env.txnRead();
txn.renew();
}
@Test(expected = ResetException.class)
public void txResetDeniedForAlreadyResetTransaction() {
final Txn<ByteBuffer> txn = env.txnRead();
txn.reset();
txn.renew();
txn.reset();
txn.reset();
}
@Test(expected = ReadOnlyRequiredException.class)
public void txResetDeniedForReadWriteTransaction() {
final Txn<ByteBuffer> txn = env.txnWrite();
txn.reset();
}
@Test(expected = BadValueSizeException.class)
public void zeroByteKeysRejected() throws IOException {
final Dbi<ByteBuffer> dbi = env.openDbi(DB_1, MDB_CREATE);
final ByteBuffer key = allocateDirect(4);
key.putInt(1);
assertThat(key.remaining(), is(0)); // because key.flip() skipped
dbi.put(key, bb(2));
}
}