Skip to content

Commit 05725cb

Browse files
committed
Add Env.getDbiNames():List<byte[]> method (#37)
1 parent a6c02e3 commit 05725cb

3 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/main/java/org/lmdbjava/Env.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import static java.lang.Boolean.getBoolean;
2525
import java.nio.ByteBuffer;
2626
import static java.nio.charset.StandardCharsets.UTF_8;
27+
import java.util.ArrayList;
28+
import java.util.List;
2729
import static java.util.Objects.requireNonNull;
2830
import jnr.ffi.Pointer;
2931
import jnr.ffi.byref.PointerByReference;
@@ -155,6 +157,28 @@ public void copy(final File path, final CopyFlags... flags) {
155157
checkRc(LIB.mdb_env_copy2(ptr, path.getAbsolutePath(), flagsMask));
156158
}
157159

160+
/**
161+
* Obtain the DBI names.
162+
*
163+
* @return a list of DBI names or null if none were found
164+
*/
165+
public List<byte[]> getDbiNames() {
166+
final List<byte[]> result = new ArrayList<>();
167+
final Dbi<T> names = openDbi((byte[]) null);
168+
try (Txn<T> txn = txnRead();
169+
Cursor<T> cursor = names.openCursor(txn)) {
170+
if (!cursor.first()) {
171+
return null;
172+
}
173+
do {
174+
final byte[] name = proxy.getBytes(cursor.key());
175+
result.add(name);
176+
} while (cursor.next());
177+
}
178+
179+
return result;
180+
}
181+
158182
/**
159183
* Get the maximum size of keys and MDB_DUPSORT data we can write.
160184
*

src/test/java/org/lmdbjava/CursorParamTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import java.io.IOException;
2727
import static java.lang.Long.BYTES;
2828
import java.nio.ByteBuffer;
29+
import static java.nio.charset.StandardCharsets.UTF_8;
2930
import org.agrona.DirectBuffer;
3031
import org.agrona.MutableDirectBuffer;
3132
import static org.hamcrest.CoreMatchers.is;
33+
import static org.hamcrest.CoreMatchers.nullValue;
3234
import static org.hamcrest.MatcherAssert.assertThat;
3335
import org.junit.Rule;
3436
import org.junit.Test;
@@ -106,7 +108,9 @@ protected AbstractBufferRunner(final BufferProxy<T> proxy) {
106108
@Override
107109
public final void execute(final TemporaryFolder tmp) {
108110
try (Env<T> env = env(tmp)) {
111+
assertThat(env.getDbiNames(), nullValue());
109112
final Dbi<T> db = env.openDbi(DB_1, MDB_CREATE, MDB_DUPSORT);
113+
assertThat(env.getDbiNames().get(0), is(DB_1.getBytes(UTF_8)));
110114
try (Txn<T> txn = env.txnWrite()) {
111115
// populate data
112116
final Cursor<T> c = db.openCursor(txn);

src/test/java/org/lmdbjava/DbiTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static java.nio.ByteBuffer.allocateDirect;
3030
import static java.nio.charset.StandardCharsets.UTF_8;
3131
import static java.util.Collections.nCopies;
32+
import java.util.List;
3233
import java.util.Random;
3334
import org.agrona.concurrent.UnsafeBuffer;
3435
import static org.hamcrest.CoreMatchers.is;
@@ -121,6 +122,24 @@ public void getName() {
121122
assertThat(db.getName(), is(DB_1.getBytes(UTF_8)));
122123
}
123124

125+
@Test
126+
public void getNamesWhenDbisPresent() {
127+
final byte[] dbHello = new byte[]{'h', 'e', 'l', 'l', 'o'};
128+
final byte[] dbWorld = new byte[]{'w', 'o', 'r', 'l', 'd'};
129+
env.openDbi(dbHello, MDB_CREATE);
130+
env.openDbi(dbWorld, MDB_CREATE);
131+
final List<byte[]> dbiNames = env.getDbiNames();
132+
assertThat(dbiNames.size(), is(2));
133+
assertThat(dbiNames.get(0), is(dbHello));
134+
assertThat(dbiNames.get(1), is(dbWorld));
135+
}
136+
137+
@Test
138+
public void getNamesWhenEmpty() {
139+
final List<byte[]> dbiNames = env.getDbiNames();
140+
assertThat(dbiNames, nullValue());
141+
}
142+
124143
@Test
125144
public void putAbortGet() {
126145
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);

0 commit comments

Comments
 (0)