Skip to content

Commit 388e15a

Browse files
weidarubp-alex
authored andcommitted
Added delete flag to Dbi.drop which will enable deleteing a database. (#67)
1 parent e7916e0 commit 388e15a

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/main/java/org/lmdbjava/Dbi.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public final class Dbi<T> {
6262
private final BufferProxy<T> proxy;
6363
private final Pointer ptr;
6464

65+
private boolean isClean;
66+
6567
Dbi(final Env<T> env, final Txn<T> txn, final byte[] name,
6668
final Comparator<T> comparator, final DbiFlags... flags) {
6769
this.env = env;
@@ -101,11 +103,19 @@ public final class Dbi<T> {
101103
* state accordingly.
102104
*/
103105
public void close() {
106+
clean();
107+
LIB.mdb_dbi_close(env.pointer(), ptr);
108+
}
109+
110+
private void clean() {
111+
if (isClean) {
112+
return;
113+
}
114+
isClean = true;
104115
if (compKeyA != null) {
105116
proxy.deallocate(compKeyA);
106117
proxy.deallocate(compKeyB);
107118
}
108-
LIB.mdb_dbi_close(env.pointer(), ptr);
109119
}
110120

111121
/**
@@ -186,12 +196,28 @@ public boolean delete(final Txn<T> txn, final T key, final T val) {
186196
* @param txn transaction handle (not null; not committed; must be R-W)
187197
*/
188198
public void drop(final Txn<T> txn) {
199+
drop(txn, false);
200+
}
201+
202+
/**
203+
* Drops the database. If delete is set to true, the database will be deleted
204+
* and handle will be closed. See {@link #close()} for implication of handle close.
205+
* Otherwise, only the data in this database will be dropped.
206+
*
207+
* @param txn transaction handle (not null; not committed; must be R-W)
208+
* @param delete whether database should be deleted.
209+
*/
210+
public void drop(final Txn<T> txn, final boolean delete) {
189211
if (SHOULD_CHECK) {
190212
requireNonNull(txn);
191213
txn.checkReady();
192214
txn.checkWritesAllowed();
193215
}
194-
checkRc(LIB.mdb_drop(txn.pointer(), ptr, 0));
216+
if (delete) {
217+
clean();
218+
}
219+
final int del = delete ? 1 : 0;
220+
checkRc(LIB.mdb_drop(txn.pointer(), ptr, del));
195221
}
196222

197223
/**

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

Lines changed: 33 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+
3233
import java.util.Comparator;
3334
import java.util.List;
3435
import java.util.Random;
@@ -145,6 +146,38 @@ public void drop() {
145146
}
146147
}
147148

149+
@Test
150+
public void dropAndDelete() {
151+
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);
152+
final Dbi<ByteBuffer> nameDb = env.openDbi((byte[]) null);
153+
final byte[] dbNameBytes = DB_1.getBytes(UTF_8);
154+
final ByteBuffer dbNameBuffer = allocateDirect(dbNameBytes.length);
155+
dbNameBuffer.put(dbNameBytes).flip();
156+
157+
try (Txn<ByteBuffer> txn = env.txnWrite()) {
158+
assertThat(nameDb.get(txn, dbNameBuffer), not(nullValue()));
159+
db.drop(txn, true);
160+
assertThat(nameDb.get(txn, dbNameBuffer), is(nullValue()));
161+
txn.commit();
162+
}
163+
}
164+
165+
@Test
166+
public void dropAndDeleteAnonymousDb() {
167+
env.openDbi(DB_1, MDB_CREATE);
168+
final Dbi<ByteBuffer> nameDb = env.openDbi((byte[]) null);
169+
final byte[] dbNameBytes = DB_1.getBytes(UTF_8);
170+
final ByteBuffer dbNameBuffer = allocateDirect(dbNameBytes.length);
171+
dbNameBuffer.put(dbNameBytes).flip();
172+
173+
try (Txn<ByteBuffer> txn = env.txnWrite()) {
174+
assertThat(nameDb.get(txn, dbNameBuffer), not(nullValue()));
175+
nameDb.drop(txn, true);
176+
assertThat(nameDb.get(txn, dbNameBuffer), is(nullValue()));
177+
txn.commit();
178+
}
179+
}
180+
148181
@Test
149182
public void getName() {
150183
final Dbi<ByteBuffer> db = env.openDbi(DB_1, MDB_CREATE);

0 commit comments

Comments
 (0)