FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lmdbjava/src/test/java/org/lmdbjava/EnvDeprecatedTest.java at master · lmdbjava/lmdbjava · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
lmdbjava
/
lmdbjava
Public
Notifications
You must be signed in to change notification settings
Fork
126
Star
873
Code
Issues
8
Pull requests
6
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
lmdbjava
/
src
/
test
/
java
/
org
/
lmdbjava
/
EnvDeprecatedTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
383 lines (354 loc) · 12.3 KB
Breadcrumbs
lmdbjava
/
src
/
test
/
java
/
org
/
lmdbjava
/
EnvDeprecatedTest.java
Copy path
File metadata and controls
383 lines (354 loc) · 12.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright © 2016-2025 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.
*/
package
org
.
lmdbjava
;
import
static
java
.
nio
.
ByteBuffer
.
allocateDirect
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatThrownBy
;
import
static
org
.
lmdbjava
.
ByteUnit
.
KIBIBYTES
;
import
static
org
.
lmdbjava
.
ByteUnit
.
MEBIBYTES
;
import
static
org
.
lmdbjava
.
CopyFlags
.
MDB_CP_COMPACT
;
import
static
org
.
lmdbjava
.
DbiFlags
.
MDB_CREATE
;
import
static
org
.
lmdbjava
.
Env
.
Builder
.
MAX_READERS_DEFAULT
;
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
.
bb
;
import
java
.
io
.
IOException
;
import
java
.
io
.
UncheckedIOException
;
import
java
.
nio
.
ByteBuffer
;
import
java
.
nio
.
file
.
Files
;
import
java
.
nio
.
file
.
Path
;
import
java
.
util
.
Random
;
import
org
.
junit
.
jupiter
.
api
.
AfterEach
;
import
org
.
junit
.
jupiter
.
api
.
BeforeEach
;
import
org
.
junit
.
jupiter
.
api
.
Test
;
import
org
.
lmdbjava
.
Env
.
AlreadyClosedException
;
import
org
.
lmdbjava
.
Env
.
AlreadyOpenException
;
import
org
.
lmdbjava
.
Env
.
Builder
;
import
org
.
lmdbjava
.
Env
.
InvalidCopyDestination
;
import
org
.
lmdbjava
.
Env
.
MapFullException
;
/**
* Tests all the deprecated methods in {@link Env}. Essentially a duplicate of {@link EnvTest}. When
* all the deprecated methods are deleted we can delete this test class.
*
* @deprecated Tests all the deprecated methods in {@link Env}.
*/
@
Deprecated
public
class
EnvDeprecatedTest
{
private
TempDir
tempDir
;
@
BeforeEach
void
beforeEach
() {
tempDir
=
new
TempDir
();
}
@
AfterEach
void
afterEach
() {
tempDir
.
cleanup
();
}
@
Test
void
byteUnit
() {
final
Path
file
=
tempDir
.
createTempFile
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
()
.
setMaxReaders
(
1
)
.
setMapSize
(
MEBIBYTES
.
toBytes
(
1
))
.
open
(
file
.
toFile
(),
MDB_NOSUBDIR
)) {
final
EnvInfo
info
=
env
.
info
();
assertThat
(
info
.
mapSize
).
isEqualTo
(
MEBIBYTES
.
toBytes
(
1
));
}
}
@
Test
void
cannotChangeMapSizeAfterOpen
() {
assertThatThrownBy
(
() -> {
final
Path
file
=
tempDir
.
createTempFile
();
final
Builder
<
ByteBuffer
>
builder
=
Env
.
create
().
setMaxReaders
(
1
);
try
(
Env
<
ByteBuffer
>
env
=
builder
.
open
(
file
.
toFile
(),
MDB_NOSUBDIR
)) {
builder
.
setMapSize
(
1
);
}
})
.
isInstanceOf
(
AlreadyOpenException
.
class
);
}
@
Test
void
cannotChangeMaxDbsAfterOpen
() {
assertThatThrownBy
(
() -> {
final
Path
file
=
tempDir
.
createTempFile
();
final
Builder
<
ByteBuffer
>
builder
=
Env
.
create
().
setMaxReaders
(
1
);
try
(
Env
<
ByteBuffer
>
env
=
builder
.
open
(
file
.
toFile
(),
MDB_NOSUBDIR
)) {
builder
.
setMaxDbs
(
1
);
}
})
.
isInstanceOf
(
AlreadyOpenException
.
class
);
}
@
Test
void
cannotChangeMaxReadersAfterOpen
() {
assertThatThrownBy
(
() -> {
final
Path
file
=
tempDir
.
createTempFile
();
final
Builder
<
ByteBuffer
>
builder
=
Env
.
create
().
setMaxReaders
(
1
);
try
(
Env
<
ByteBuffer
>
env
=
builder
.
open
(
file
.
toFile
(),
MDB_NOSUBDIR
)) {
builder
.
setMaxReaders
(
1
);
}
})
.
isInstanceOf
(
AlreadyOpenException
.
class
);
}
@
Test
void
cannotInfoOnceClosed
() {
assertThatThrownBy
(
() -> {
final
Path
file
=
tempDir
.
createTempFile
();
final
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
file
.
toFile
(),
MDB_NOSUBDIR
);
env
.
close
();
env
.
info
();
})
.
isInstanceOf
(
AlreadyClosedException
.
class
);
}
@
Test
void
cannotOpenTwice
() {
assertThatThrownBy
(
() -> {
final
Path
file
=
tempDir
.
createTempFile
();
final
Builder
<
ByteBuffer
>
builder
=
Env
.
create
().
setMaxReaders
(
1
);
builder
.
open
(
file
.
toFile
(),
MDB_NOSUBDIR
).
close
();
builder
.
open
(
file
.
toFile
(),
MDB_NOSUBDIR
);
})
.
isInstanceOf
(
AlreadyOpenException
.
class
);
}
@
Test
void
cannotStatOnceClosed
() {
assertThatThrownBy
(
() -> {
final
Path
file
=
tempDir
.
createTempFile
();
final
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
file
.
toFile
(),
MDB_NOSUBDIR
);
env
.
close
();
env
.
stat
();
})
.
isInstanceOf
(
AlreadyClosedException
.
class
);
}
@
Test
void
cannotSyncOnceClosed
() {
assertThatThrownBy
(
() -> {
final
Path
file
=
tempDir
.
createTempFile
();
final
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
file
.
toFile
(),
MDB_NOSUBDIR
);
env
.
close
();
env
.
sync
(
false
);
})
.
isInstanceOf
(
AlreadyClosedException
.
class
);
}
@
Test
void
copyDirectoryBased
() {
final
Path
dest
=
tempDir
.
createTempDir
();
final
Path
src
=
tempDir
.
createTempDir
();
assertThat
(
Files
.
exists
(
dest
)).
isTrue
();
assertThat
(
Files
.
isDirectory
(
dest
)).
isTrue
();
assertThat
(
FileUtil
.
count
(
dest
)).
isEqualTo
(
0
);
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
src
.
toFile
())) {
env
.
copy
(
dest
.
toFile
(),
MDB_CP_COMPACT
);
assertThat
(
FileUtil
.
count
(
dest
)).
isEqualTo
(
1
);
}
}
@
Test
void
copyDirectoryRejectsFileDestination
() {
assertThatThrownBy
(
() -> {
final
Path
dest
=
tempDir
.
createTempDir
();
final
Path
src
=
tempDir
.
createTempDir
();
FileUtil
.
deleteDir
(
dest
);
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
src
.
toFile
())) {
env
.
copy
(
dest
.
toFile
(),
MDB_CP_COMPACT
);
}
})
.
isInstanceOf
(
InvalidCopyDestination
.
class
);
}
@
Test
void
copyDirectoryRejectsMissingDestination
() {
final
Path
dest
=
tempDir
.
createTempDir
();
final
Path
src
=
tempDir
.
createTempDir
();
assertThatThrownBy
(
() -> {
try
{
Files
.
delete
(
dest
);
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
src
.
toFile
())) {
env
.
copy
(
dest
.
toFile
(),
MDB_CP_COMPACT
);
}
}
catch
(
final
IOException
e
) {
throw
new
UncheckedIOException
(
e
);
}
})
.
isInstanceOf
(
InvalidCopyDestination
.
class
);
}
@
Test
void
copyDirectoryRejectsNonEmptyDestination
() {
final
Path
dest
=
tempDir
.
createTempDir
();
final
Path
src
=
tempDir
.
createTempDir
();
assertThatThrownBy
(
() -> {
try
{
final
Path
subDir
=
dest
.
resolve
(
"hello"
);
Files
.
createDirectory
(
subDir
);
assertThat
(
Files
.
isDirectory
(
subDir
)).
isTrue
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
src
.
toFile
())) {
env
.
copy
(
dest
.
toFile
(),
MDB_CP_COMPACT
);
}
}
catch
(
final
IOException
e
) {
throw
new
UncheckedIOException
(
e
);
}
})
.
isInstanceOf
(
InvalidCopyDestination
.
class
);
}
@
Test
void
copyFileBased
() {
final
Path
dest
=
tempDir
.
createTempFile
();
final
Path
src
=
tempDir
.
createTempFile
();
assertThat
(
Files
.
exists
(
dest
)).
isFalse
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
src
.
toFile
(),
MDB_NOSUBDIR
)) {
env
.
copy
(
dest
.
toFile
(),
MDB_CP_COMPACT
);
}
assertThat
(
FileUtil
.
size
(
dest
)).
isGreaterThan
(
0L
);
}
@
Test
void
copyFileRejectsExistingDestination
() {
final
Path
dest
=
tempDir
.
createTempFile
();
final
Path
src
=
tempDir
.
createTempFile
();
assertThatThrownBy
(
() -> {
Files
.
createFile
(
dest
);
assertThat
(
Files
.
exists
(
dest
)).
isTrue
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
src
.
toFile
(),
MDB_NOSUBDIR
)) {
env
.
copy
(
dest
.
toFile
(),
MDB_CP_COMPACT
);
}
})
.
isInstanceOf
(
InvalidCopyDestination
.
class
);
}
@
Test
void
createAsFile
() {
final
Path
file
=
tempDir
.
createTempFile
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
()
.
setMapSize
(
MEBIBYTES
.
toBytes
(
1
))
.
setMaxDbs
(
1
)
.
setMaxReaders
(
1
)
.
open
(
file
.
toFile
(),
MDB_NOSUBDIR
)) {
env
.
sync
(
true
);
assertThat
(
Files
.
isRegularFile
(
file
)).
isTrue
();
}
}
@
Test
void
mapFull
() {
final
Path
dir
=
tempDir
.
createTempDir
();
assertThatThrownBy
(
() -> {
final
byte
[]
k
=
new
byte
[
500
];
final
ByteBuffer
key
=
allocateDirect
(
500
);
final
ByteBuffer
val
=
allocateDirect
(
1_024
);
final
Random
rnd
=
new
Random
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
()
.
setMaxReaders
(
1
)
.
setMapSize
(
MEBIBYTES
.
toBytes
(
8
))
.
setMaxDbs
(
1
)
.
open
(
dir
.
toFile
())) {
final
Dbi
<
ByteBuffer
>
db
=
env
.
openDbi
(
DB_1
,
MDB_CREATE
);
for
(; ; ) {
rnd
.
nextBytes
(
k
);
key
.
clear
();
key
.
put
(
k
).
flip
();
val
.
clear
();
db
.
put
(
key
,
val
);
}
}
})
.
isInstanceOf
(
MapFullException
.
class
);
}
@
Test
void
readOnlySupported
() {
final
Path
dir
=
tempDir
.
createTempDir
();
try
(
Env
<
ByteBuffer
>
rwEnv
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
dir
.
toFile
())) {
final
Dbi
<
ByteBuffer
>
rwDb
=
rwEnv
.
openDbi
(
DB_1
,
MDB_CREATE
);
rwDb
.
put
(
bb
(
1
),
bb
(
42
));
}
try
(
Env
<
ByteBuffer
>
roEnv
=
Env
.
create
().
setMaxReaders
(
1
).
open
(
dir
.
toFile
(),
MDB_RDONLY_ENV
)) {
final
Dbi
<
ByteBuffer
>
roDb
=
roEnv
.
openDbi
(
DB_1
);
try
(
Txn
<
ByteBuffer
>
roTxn
=
roEnv
.
txnRead
()) {
assertThat
(
roDb
.
get
(
roTxn
,
bb
(
1
))).
isNotNull
();
}
}
}
@
Test
void
setMapSize
() {
final
Path
dir
=
tempDir
.
createTempDir
();
final
byte
[]
k
=
new
byte
[
500
];
final
ByteBuffer
key
=
allocateDirect
(
500
);
final
ByteBuffer
val
=
allocateDirect
(
1_024
);
final
Random
rnd
=
new
Random
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
create
()
.
setMaxReaders
(
1
)
.
setMapSize
(
KIBIBYTES
.
toBytes
(
256
))
.
setMaxDbs
(
1
)
.
open
(
dir
.
toFile
())) {
final
Dbi
<
ByteBuffer
>
db
=
env
.
openDbi
(
DB_1
,
MDB_CREATE
);
db
.
put
(
bb
(
1
),
bb
(
42
));
boolean
mapFullExThrown
=
false
;
try
{
for
(
int
i
=
0
;
i
<
70
;
i
++) {
rnd
.
nextBytes
(
k
);
key
.
clear
();
key
.
put
(
k
).
flip
();
val
.
clear
();
db
.
put
(
key
,
val
);
}
}
catch
(
final
MapFullException
mfE
) {
mapFullExThrown
=
true
;
}
assertThat
(
mapFullExThrown
).
isTrue
();
env
.
setMapSize
(
KIBIBYTES
.
toBytes
(
1024
));
try
(
Txn
<
ByteBuffer
>
roTxn
=
env
.
txnRead
()) {
final
ByteBuffer
byteBuffer
=
db
.
get
(
roTxn
,
bb
(
1
));
assertThat
(
byteBuffer
).
isNotNull
();
assertThat
(
byteBuffer
.
getInt
()).
isEqualTo
(
42
);
}
mapFullExThrown
=
false
;
try
{
for
(
int
i
=
0
;
i
<
70
;
i
++) {
rnd
.
nextBytes
(
k
);
key
.
clear
();
key
.
put
(
k
).
flip
();
val
.
clear
();
db
.
put
(
key
,
val
);
}
}
catch
(
final
MapFullException
mfE
) {
mapFullExThrown
=
true
;
}
assertThat
(
mapFullExThrown
).
isFalse
();
}
}
@
Test
void
testDefaultOpen
() {
final
Path
dir
=
tempDir
.
createTempDir
();
try
(
Env
<
ByteBuffer
>
env
=
Env
.
open
(
dir
.
toFile
(),
10
)) {
final
EnvInfo
info
=
env
.
info
();
assertThat
(
info
.
maxReaders
).
isEqualTo
(
MAX_READERS_DEFAULT
);
final
Dbi
<
ByteBuffer
>
db
=
env
.
openDbi
(
"test"
,
MDB_CREATE
);
db
.
put
(
allocateDirect
(
1
),
allocateDirect
(
1
));
}
}
}
Back
|
FazBrowse Home
|
New Git URL