Skip to content

ByteBuffer is re-used even when the byte order is different, leading to wrong results #76

@jubax

Description

@jubax

In LMDB it is possible to use DbiFlags.MDB_INTEGERKEY when opening a database. When this flag is used then it is assumed that the keys are stored in native byte order.

When I have one database with MDB_INTEGERKEY then I use ByteBuffer.order(ByteOrder.nativeOrder()). Otherwise I keep the order.

When I retrieve values from the MDB_INTEGERKEY store I need to change the byte order of the returned ByteBuffer. Note that "db1" uses MDB_INTEGERKEY and "db2" not. The ByteBuffer which is returned is shared between both databases.

			try (Txn<ByteBuffer> txn = env.txnRead();) {
				ByteBuffer result = db1.get(txn, key); // <-- This buffer is re-used
				result.order(ByteOrder.nativeOrder());
				assertEquals(100, result.getInt());
			}
			try (Txn<ByteBuffer> txn = env.txnRead();) {
				ByteBuffer result = db2.get(txn, key);
				assertEquals(333, result.getInt()); // FAIL because "result" has native order
			}

The API does not mention that I cannot change e.g. the byte order of the returned ByteBuffer object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions