Skip to content

Commit

Permalink
Merge pull request #26 from xchrdw/master
Browse files Browse the repository at this point in the history
fix concurrent database access with multiple instances
  • Loading branch information
mbastian authored Jun 25, 2016
2 parents c0ce95e + a65bfed commit 31f9829
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public class StorageReader implements Iterable<Map.Entry<byte[], byte[]>> {
private final DataInputOutput sizeBuffer = new DataInputOutput(new byte[5]);
private final byte[] slotBuffer;

private final HashUtils hashUtils;

StorageReader(Configuration configuration, File file)
throws IOException {
path = file;
Expand All @@ -99,6 +101,8 @@ public class StorageReader implements Iterable<Map.Entry<byte[], byte[]>> {
//Config
segmentSize = config.getLong(Configuration.MMAP_SEGMENT_SIZE);

hashUtils = new HashUtils();

// Check valid segmentSize
if (segmentSize > Integer.MAX_VALUE) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -242,7 +246,7 @@ public byte[] get(byte[] key)
if (keyLength >= slots.length || keyCounts[keyLength] == 0) {
return null;
}
long hash = (long) HashUtils.hash(key);
long hash = (long) hashUtils.hash(key);
int numSlots = slots[keyLength];
int slotSize = slotSizes[keyLength];
int indexOffset = indexOffsets[keyLength];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public class StorageWriter {
// Number of collisions
private int collisions;

private HashUtils hashUtils;

StorageWriter(Configuration configuration, OutputStream stream) {
config = configuration;
loadFactor = config.getDouble(Configuration.LOAD_FACTOR);
Expand All @@ -94,6 +96,7 @@ public class StorageWriter {
dataLengths = new long[0];
maxOffsetLengths = new int[0];
keyCounts = new int[0];
hashUtils = new HashUtils();
}

public void put(byte[] key, byte[] value)
Expand Down Expand Up @@ -300,7 +303,7 @@ private File buildIndex(int keyLength)
long offset = LongPacker.unpackLong(tempIndexStream);

// Hash
long hash = (long) HashUtils.hash(keyBuffer);
long hash = (long) hashUtils.hash(keyBuffer);

boolean collision = false;
for (int probe = 0; probe < count; probe++) {
Expand Down
9 changes: 2 additions & 7 deletions paldb/src/main/java/com/linkedin/paldb/utils/HashUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@
public class HashUtils {

// Hash implementation
private static final Murmur3A hash = new Murmur3A(42);

// Default constructor
private HashUtils() {

}
private final Murmur3A hash = new Murmur3A(42);

/**
* Returns the positive hash for the given <code>bytes</code>.
*
* @param bytes bytes to hash
* @return hash
*/
public static int hash(byte[] bytes) {
public int hash(byte[] bytes) {
hash.reset();
hash.update(bytes);
return hash.getIntValue() & 0x7fffffff;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@


public class TestHashUtils {
HashUtils hashUtils = new HashUtils();

@Test
public void testHashEquals() {
Assert.assertEquals(HashUtils.hash("foo".getBytes()), HashUtils.hash("foo".getBytes()));
Assert.assertEquals(hashUtils.hash("foo".getBytes()), hashUtils.hash("foo".getBytes()));
}

@Test
public void testEmpty() {
Assert.assertTrue(HashUtils.hash(new byte[0]) > 0);
Assert.assertTrue(hashUtils.hash(new byte[0]) > 0);
}
}

0 comments on commit 31f9829

Please sign in to comment.