|
45 | 45 | import java.util.concurrent.atomic.AtomicBoolean; |
46 | 46 | import java.util.concurrent.atomic.AtomicLong; |
47 | 47 | import java.util.concurrent.atomic.LongAdder; |
| 48 | +import java.util.stream.Stream; |
48 | 49 | import org.assertj.core.api.Assertions; |
49 | 50 | import org.junit.jupiter.api.AfterEach; |
50 | 51 | import org.junit.jupiter.api.BeforeEach; |
@@ -285,10 +286,58 @@ void copyFileBased() { |
285 | 286 | final Path dest = tempDir.createTempFile(); |
286 | 287 | assertThat(Files.exists(dest)).isFalse(); |
287 | 288 | final Path src = tempDir.createTempFile(); |
| 289 | + |
| 290 | + // Create the source env and put an entry |
288 | 291 | try (Env<ByteBuffer> env = |
289 | 292 | Env.create().setSafeClose().setMaxReaders(1).setEnvFlags(MDB_NOSUBDIR).open(src)) { |
| 293 | + final Dbi<ByteBuffer> rwDb = |
| 294 | + env.createDbi().setDbName(DB_1).withDefaultComparator().setDbiFlags(MDB_CREATE).open(); |
| 295 | + rwDb.put(bb(1), bb(42)); |
| 296 | + |
| 297 | + env.copy(dest, MDB_CP_COMPACT); |
| 298 | + } |
| 299 | + |
| 300 | + // Check the destination env and get the entry |
| 301 | + try (Env<ByteBuffer> env = |
| 302 | + Env.create().setSafeClose().setMaxReaders(1).setEnvFlags(MDB_NOSUBDIR).open(dest)) { |
| 303 | + final Dbi<ByteBuffer> dbi = |
| 304 | + env.createDbi().setDbName(DB_1).withDefaultComparator().setDbiFlags(MDB_CREATE).open(); |
| 305 | + try (Txn<ByteBuffer> txn = env.txnRead()) { |
| 306 | + final ByteBuffer byteBuffer = dbi.get(txn, bb(1)); |
| 307 | + assertThat(byteBuffer).isNotNull(); |
| 308 | + assertThat(byteBuffer.getInt()).isEqualTo(42); |
| 309 | + } |
| 310 | + } |
| 311 | + assertThat(FileUtil.size(dest)).isGreaterThan(0L); |
| 312 | + } |
| 313 | + |
| 314 | + @Test |
| 315 | + void copyDirBased() { |
| 316 | + final Path dest = tempDir.createTempDir(); |
| 317 | + assertThat(isEmptyDir(dest)).isTrue(); |
| 318 | + final Path src = tempDir.createTempDir(); |
| 319 | + assertThat(isEmptyDir(src)).isTrue(); |
| 320 | + // Create the source env and put an entry |
| 321 | + try (Env<ByteBuffer> env = Env.create().setSafeClose().setMaxReaders(1).open(src)) { |
| 322 | + final Dbi<ByteBuffer> rwDb = |
| 323 | + env.createDbi().setDbName(DB_1).withDefaultComparator().setDbiFlags(MDB_CREATE).open(); |
| 324 | + rwDb.put(bb(1), bb(42)); |
| 325 | + |
290 | 326 | env.copy(dest, MDB_CP_COMPACT); |
291 | 327 | } |
| 328 | + |
| 329 | + // Check the destination env and get the entry |
| 330 | + try (Env<ByteBuffer> env = Env.create().setSafeClose().setMaxReaders(1).open(dest)) { |
| 331 | + final Dbi<ByteBuffer> dbi = |
| 332 | + env.createDbi().setDbName(DB_1).withDefaultComparator().setDbiFlags(MDB_CREATE).open(); |
| 333 | + try (Txn<ByteBuffer> txn = env.txnRead()) { |
| 334 | + final ByteBuffer byteBuffer = dbi.get(txn, bb(1)); |
| 335 | + assertThat(byteBuffer).isNotNull(); |
| 336 | + assertThat(byteBuffer.getInt()).isEqualTo(42); |
| 337 | + } |
| 338 | + } |
| 339 | + assertThat(isEmptyDir(dest)).isFalse(); |
| 340 | + assertThat(isEmptyDir(src)).isFalse(); |
292 | 341 | assertThat(FileUtil.size(dest)).isGreaterThan(0L); |
293 | 342 | } |
294 | 343 |
|
@@ -1110,6 +1159,14 @@ void testToString() { |
1110 | 1159 | } |
1111 | 1160 | } |
1112 | 1161 |
|
| 1162 | + private boolean isEmptyDir(final Path dir) { |
| 1163 | + try (Stream<Path> pathStream = Files.list(dir)) { |
| 1164 | + return !pathStream.findAny().isPresent(); |
| 1165 | + } catch (IOException e) { |
| 1166 | + throw new RuntimeException(e); |
| 1167 | + } |
| 1168 | + } |
| 1169 | + |
1113 | 1170 | private void testEnvUse(final BooleanArg safeClose, final BooleanArg singleThreaded) { |
1114 | 1171 | final Path file = tempDir.createTempFile(); |
1115 | 1172 |
|
|
0 commit comments