Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
480e984
gh-249 Remove MDB_UNSIGNEDKEY, let CursorIterable call mdb_cmp
at055612 Mar 6, 2025
46f8d08
gh-249 Remove non-J8 features, use indent size 2
at055612 Mar 6, 2025
f92012e
gh-249 Fix indents
at055612 Mar 6, 2025
e1756d6
gh-249 Tidy code and refactor RangeComparator impls
at055612 Mar 6, 2025
9509f6b
gh-249 Remove commented code
at055612 Mar 6, 2025
67e2df1
gh-249 Add CursorIterableIntegerKeyTest
at055612 Mar 7, 2025
620a89f
gh-249 Remove MaskedFlag.isPropagatedToLmdb, add DbiBuilder WIP
at055612 Jun 5, 2025
75d87d0
Merge branch 'master' into mdb_cmp
at055612 Oct 26, 2025
47b4c4f
Merge branch 'master' into mdb_cmp
at055612 Oct 26, 2025
bfbf223
Add FlagSet, DbiFlagSet, PutFlagSet
at055612 Oct 27, 2025
0f66aaf
Rename FlagSet to AbstractFlagSet
at055612 Oct 27, 2025
aa000a1
Add remaining FlagSet impls
at055612 Oct 27, 2025
667dab3
Fix Javadoc
at055612 Oct 27, 2025
0234d32
Replace get(Uns|S)ignedComparator with getComparator(DbiFlagSet)
at055612 Oct 28, 2025
ef0c852
Add missing txn commit in DbiBuilder
at055612 Oct 28, 2025
1b3f94d
Change CursorIterableTest to use Parameterized
at055612 Oct 28, 2025
c0bbe73
Deprecate methods using varargs flags
at055612 Oct 28, 2025
4fd89ff
Add int key compare method to (Direct|Byte)BufferProxy
at055612 Oct 29, 2025
58dcc6e
Tidy code
at055612 Oct 29, 2025
26665ba
Fix byte order issues with compareAsIntegerKeys
at055612 Oct 29, 2025
c427801
Add/refactor tests
at055612 Nov 4, 2025
f606e7e
Merge branch 'master' into mdb_cmp
at055612 Nov 4, 2025
e2be6bf
Add integer key comparator tests
at055612 Nov 5, 2025
1acd971
Add ComparatorFactory
at055612 Nov 5, 2025
dea7975
Merge branch 'master' into mdb_cmp
at055612 Nov 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add missing txn commit in DbiBuilder
  • Loading branch information
at055612 committed Oct 28, 2025
commit ef0c852ad9a631ae0f194f4d63dce62e3d1b549f
15 changes: 15 additions & 0 deletions src/main/java/org/lmdbjava/AbstractFlagSet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 java.util.Collection;
Expand Down Expand Up @@ -62,7 +77,7 @@

/**
* @return True if this set is empty.
*/
@Override
public boolean isEmpty() {
return flags.isEmpty();
Expand Down Expand Up @@ -136,7 +151,7 @@
return false;
}

@Override
public Iterator<T> iterator() {
if (enumSet == null) {
return initSet().iterator();
Expand Down Expand Up @@ -196,7 +211,7 @@
}

@Override
public boolean isEmpty() {
return true;
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/lmdbjava/CopyFlagSet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 java.util.Collection;
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/lmdbjava/Dbi.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
*/
public final class Dbi<T> {

private final ComparatorCallback ccb;
private final ComparatorCallback callbackComparator;
private boolean cleaned;
// Used for CursorIterable KeyRange testing and/or native callbacks
private final Comparator<T> comparator;
Expand Down Expand Up @@ -82,7 +82,7 @@ public final class Dbi<T> {
if (nativeCb) {
requireNonNull(comparator, "comparator cannot be null if nativeCb is set");
// LMDB will call back to this comparator for insertion/iteration order
this.ccb =
this.callbackComparator =
(keyA, keyB) -> {
final T compKeyA = proxy.out(proxy.allocate(), keyA);
final T compKeyB = proxy.out(proxy.allocate(), keyB);
Expand All @@ -91,9 +91,9 @@ public final class Dbi<T> {
proxy.deallocate(compKeyB);
return result;
};
LIB.mdb_set_compare(txn.pointer(), ptr, ccb);
LIB.mdb_set_compare(txn.pointer(), ptr, callbackComparator);
} else {
ccb = null;
callbackComparator = null;
}
}

Expand Down Expand Up @@ -380,8 +380,7 @@ public boolean put(final Txn<T> txn, final T key, final T val, final PutFlags...
final Pointer transientKey = txn.kv().keyIn(key);
final Pointer transientVal = txn.kv().valIn(val);
final int mask = mask(flags);
final int rc =
LIB.mdb_put(txn.pointer(), ptr, txn.kv().pointerKey(), txn.kv().pointerVal(), mask);
final int rc = LIB.mdb_put(txn.pointer(), ptr, txn.kv().pointerKey(), txn.kv().pointerVal(), mask);
if (rc == MDB_KEYEXIST) {
if (isSet(mask, MDB_NOOVERWRITE)) {
txn.kv().valOut(); // marked as in,out in LMDB C docs
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/lmdbjava/DbiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,23 @@
/**
* Construct and open the {@link Dbi}.
* <p>
* If a {@link Txn} was supplied to the builder, it should be committed upon return from
* this method.
* If a {@link Txn} was supplied to the builder, it is the callers responsibility to
* commit and close the txn upon return from this method, else the created DB won't be retained.
* </p>
*
* @return A newly constructed and opened {@link Dbi}.
*/
public Dbi<T> open() {
final DbiBuilder<T> dbiBuilder = dbiBuilderStage2.dbiBuilder;
if (txn == null) {
if (txn != null) {
return open(txn, dbiBuilder);
} else {
try (final Txn<T> txn = getTxn(dbiBuilder)) {

Check notice

Code scanning / CodeQL

Possible confusion of local and field Note

Confusing name: method
open
also refers to field
txn
(without qualifying it with 'this').
return open(txn, dbiBuilder);
final Dbi<T> dbi = open(txn, dbiBuilder);
// even RO Txns require a commit to retain Dbi in Env
txn.commit();
return dbi;
}
} else {
return open(txn, dbiBuilder);
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/lmdbjava/DbiFlagSet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 java.util.Collection;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/lmdbjava/EnvFlagSet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 java.util.Collection;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/lmdbjava/FlagSet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 java.util.Comparator;
Expand All @@ -12,7 +27,7 @@
* @param <T>
*/
public interface FlagSet<T extends MaskedFlag> extends Iterable<T> {

int getMask();

Set<T> getFlags();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/lmdbjava/PutFlagSet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 java.util.Collection;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/lmdbjava/ReferenceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private ReferenceUtil() {}
*/
public static void reachabilityFence0(final Object ref) {
if (ref != null) {
//noinspection EmptySynchronizedStatement
synchronized (ref) {
// Empty synchronized is ok: https://stackoverflow.com/a/31933260/1151521
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/lmdbjava/Txn.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ public final class Txn<T> implements AutoCloseable {
private final Pointer ptr;
private final boolean readOnly;
private final Env<T> env;
private final TxnFlagSet flags;
private State state;

Txn(final Env<T> env, final Txn<T> parent, final BufferProxy<T> proxy, final TxnFlagSet flags) {
final TxnFlagSet flagSet = flags != null
this.flags = flags != null
? flags
: TxnFlagSet.EMPTY;
this.proxy = proxy;
this.keyVal = proxy.keyVal();
this.readOnly = flagSet.isSet(MDB_RDONLY_TXN);
this.readOnly = this.flags.isSet(MDB_RDONLY_TXN);
if (env.isReadOnly() && !this.readOnly) {
throw new EnvIsReadOnly();
}
Expand All @@ -61,7 +62,7 @@ public final class Txn<T> implements AutoCloseable {
}
final Pointer txnPtr = allocateDirect(RUNTIME, ADDRESS);
final Pointer txnParentPtr = parent == null ? null : parent.ptr;
checkRc(LIB.mdb_txn_begin(env.pointer(), txnParentPtr, flagSet.getMask(), txnPtr));
checkRc(LIB.mdb_txn_begin(env.pointer(), txnParentPtr, this.flags.getMask(), txnPtr));
ptr = txnPtr.getPointer(0);

state = READY;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/lmdbjava/TxnFlagSet.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 java.util.EnumSet;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/lmdbjava/CopyFlagSetTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 org.hamcrest.CoreMatchers.is;
Expand Down
97 changes: 97 additions & 0 deletions src/test/java/org/lmdbjava/DbiBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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 com.jakewharton.byteunits.BinaryByteUnit.MEBIBYTES;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.lmdbjava.Env.create;
import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR;
import static org.lmdbjava.TestUtils.bb;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class DbiBuilderTest {

@Rule
public final TemporaryFolder tmp = new TemporaryFolder();
private Env<ByteBuffer> env;

@After
public void after() {
env.close();
}

@Before
public void before() throws IOException {
System.out.println("before");
final File path = tmp.newFile();
env = create()
.setMapSize(MEBIBYTES.toBytes(64))
.setMaxReaders(2)
.setMaxDbs(2)
.open(path, MDB_NOSUBDIR);
}

@Test
public void unnamed() {
final Dbi<ByteBuffer> dbi = env.buildDbi()
.withoutDbName()
.withDefaultComparator()
.withDbiFlags(DbiFlags.MDB_CREATE)
.open();

assertThat(env.getDbiNames().size(), Matchers.is(0));

assertPutAndGet(dbi);
}


@Test
public void named() {
final Dbi<ByteBuffer> dbi = env.buildDbi()
.withDbName("foo")
.withDefaultComparator()
.withDbiFlags(DbiFlags.MDB_CREATE)
.open();

assertPutAndGet(dbi);

assertThat(env.getDbiNames().size(), Matchers.is(1));
assertThat(new String(env.getDbiNames().get(0), StandardCharsets.UTF_8), Matchers.is("foo"));
}

private void assertPutAndGet(Dbi<ByteBuffer> dbi) {
try (Txn<ByteBuffer> writeTxn = env.txnWrite()) {
dbi.put(writeTxn, bb(123), bb(123_000));
writeTxn.commit();
}

try (Txn<ByteBuffer> readTxn = env.txnRead()) {
final ByteBuffer byteBuffer = dbi.get(readTxn, bb(123));
final int val = byteBuffer.getInt();
assertThat(val, Matchers.is(123_000));
}
}
}
15 changes: 15 additions & 0 deletions src/test/java/org/lmdbjava/DbiFlagSetTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 org.hamcrest.CoreMatchers.is;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/lmdbjava/EnvFlagSetTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* 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 org.hamcrest.CoreMatchers.is;
Expand Down
Loading
Loading