Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete on non-exsistent keys returns false instead of throwing exception #45

Merged
merged 2 commits into from
Feb 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 20 additions & 15 deletions src/main/java/org/lmdbjava/Dbi.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ public void close() {
* Starts a new read-write transaction and deletes the key.
*
* @param key key to delete from the database (not null)
* @return true if the key/data pair was found, false otherwise
*
* @see #delete(org.lmdbjava.Txn, java.lang.Object, java.lang.Object)
*/
public void delete(final T key) {
public boolean delete(final T key) {
try (Txn<T> txn = env.txnWrite()) {
delete(txn, key);
final boolean ret = delete(txn, key);
txn.commit();
return ret;
}
}

Expand All @@ -95,10 +98,12 @@ public void delete(final T key) {
*
* @param txn transaction handle (not null; not committed; must be R-W)
* @param key key to delete from the database (not null)
* @return true if the key/data pair was found, false otherwise
*
* @see #delete(org.lmdbjava.Txn, java.lang.Object, java.lang.Object)
*/
public void delete(final Txn<T> txn, final T key) {
delete(txn, key, null);
public boolean delete(final Txn<T> txn, final T key) {
return delete(txn, key, null);
}

/**
Expand All @@ -111,15 +116,12 @@ public void delete(final Txn<T> txn, final T key) {
* the duplicate data items for the key will be deleted. Otherwise, if the
* data parameter is non-null only the matching data item will be deleted.
*
* <p>
* This function will throw {@link KeyNotFoundException} if the key/data pair
* is not found.
*
* @param txn transaction handle (not null; not committed; must be R-W)
* @param key key to delete from the database (not null)
* @param val value to delete from the database (null permitted)
* @return true if the key/data pair was found, false otherwise
*/
public void delete(final Txn<T> txn, final T key, final T val) {
public boolean delete(final Txn<T> txn, final T key, final T val) {
if (SHOULD_CHECK) {
requireNonNull(txn);
requireNonNull(key);
Expand All @@ -129,14 +131,17 @@ public void delete(final Txn<T> txn, final T key, final T val) {

txn.kv().keyIn(key);

if (val == null) {
checkRc(LIB.mdb_del(txn.pointer(), ptr, txn.kv().pointerKey(), null));
} else {
Pointer data = null;
if (val != null) {
txn.kv().valIn(val);
checkRc(LIB
.mdb_del(txn.pointer(), ptr, txn.kv().pointerKey(), txn.kv()
.pointerVal()));
data = txn.kv().pointerVal();
}
final int rc = LIB.mdb_del(txn.pointer(), ptr, txn.kv().pointerKey(), data);
if (rc == MDB_NOTFOUND) {
return false;
}
checkRc(rc);
return true;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/org/lmdbjava/DbiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public void putAndGetAndDeleteWithInternalTx() {
assertNotNull(found);
assertThat(txn.val().getInt(), is(5));
}
db.delete(bb(5));
assertThat(db.delete(bb(5)), is(true));
assertThat(db.delete(bb(5)), is(false));

try (Txn<ByteBuffer> txn = env.txnRead()) {
assertNull(db.get(txn, bb(5)));
Expand Down Expand Up @@ -215,7 +216,7 @@ public void putDelete() {

try (Txn<ByteBuffer> txn = env.txnWrite()) {
db.put(txn, bb(5), bb(5));
db.delete(txn, bb(5));
assertThat(db.delete(txn, bb(5)), is(true));

assertNull(db.get(txn, bb(5)));
txn.abort();
Expand All @@ -230,12 +231,15 @@ public void putDuplicateDelete() {
db.put(txn, bb(5), bb(5));
db.put(txn, bb(5), bb(6));
db.put(txn, bb(5), bb(7));
db.delete(txn, bb(5), bb(6));
assertThat(db.delete(txn, bb(5), bb(6)), is(true));
assertThat(db.delete(txn, bb(5), bb(6)), is(false));
assertThat(db.delete(txn, bb(5), bb(5)), is(true));
assertThat(db.delete(txn, bb(5), bb(5)), is(false));

try (Cursor<ByteBuffer> cursor = db.openCursor(txn)) {
final ByteBuffer key = bb(5);
cursor.get(key, MDB_SET_KEY);
assertThat(cursor.count(), is(2L));
assertThat(cursor.count(), is(1L));
}
txn.abort();
}
Expand Down