Fix Java test for uint64add merge operator (#7243)

Summary:
The PR fixes a Java test for Merge operator `uint64add`.

The current implementation uses wrong byte order for long serialization, but fails to catch this error because the merge sum is lower than `256`.

The PR makes this test case more representative (i.e. it fails with wrong byte order) and changes the byte order to little endian.

Some background: RocksDB uses LittleEndian byte order for integer serialization across all platforms. `MergeTest` uses `ByteBuffer` that defaults to BigEndian byte order.

This test case might probably be used as a sample of `MergeOperator` usage in Java.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/7243

Reviewed By: ajkr

Differential Revision: D23079593

Pulled By: pdillinger

fbshipit-source-id: 82e8e166901d66733e96a0116f88d0ec4761ddf1
main
Arkady Dyakonov 4 years ago committed by Facebook GitHub Bot
parent b578ca2e4d
commit 2bc63e3aba
  1. 19
      java/src/test/java/org/rocksdb/MergeTest.java

@ -5,18 +5,18 @@
package org.rocksdb; package org.rocksdb;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
public class MergeTest { public class MergeTest {
@ClassRule @ClassRule
@ -46,13 +46,13 @@ public class MergeTest {
} }
private byte[] longToByteArray(long l) { private byte[] longToByteArray(long l) {
ByteBuffer buf = ByteBuffer.allocate(Long.SIZE / Byte.SIZE); ByteBuffer buf = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).order(ByteOrder.LITTLE_ENDIAN);
buf.putLong(l); buf.putLong(l);
return buf.array(); return buf.array();
} }
private long longFromByteArray(byte[] a) { private long longFromByteArray(byte[] a) {
ByteBuffer buf = ByteBuffer.allocate(Long.SIZE / Byte.SIZE); ByteBuffer buf = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).order(ByteOrder.LITTLE_ENDIAN);
buf.put(a); buf.put(a);
buf.flip(); buf.flip();
return buf.getLong(); return buf.getLong();
@ -144,14 +144,13 @@ public class MergeTest {
// writing (long)100 under key // writing (long)100 under key
db.put(columnFamilyHandleList.get(1), db.put(columnFamilyHandleList.get(1),
"cfkey".getBytes(), longToByteArray(100)); "cfkey".getBytes(), longToByteArray(100));
// merge (long)1 under key // merge (long)157 under key
db.merge(columnFamilyHandleList.get(1), db.merge(columnFamilyHandleList.get(1), "cfkey".getBytes(), longToByteArray(157));
"cfkey".getBytes(), longToByteArray(1));
byte[] value = db.get(columnFamilyHandleList.get(1), byte[] value = db.get(columnFamilyHandleList.get(1),
"cfkey".getBytes()); "cfkey".getBytes());
long longValue = longFromByteArray(value); long longValue = longFromByteArray(value);
assertThat(longValue).isEqualTo(101); assertThat(longValue).isEqualTo(257);
} finally { } finally {
for (final ColumnFamilyHandle handle : columnFamilyHandleList) { for (final ColumnFamilyHandle handle : columnFamilyHandleList) {
handle.close(); handle.close();

Loading…
Cancel
Save