[Mulgara-svn] r2066 - trunk/src/jar/util/java/org/mulgara/util/io
pag at mulgara.org
pag at mulgara.org
Fri Oct 7 22:30:03 UTC 2011
Author: pag
Date: 2011-10-07 22:30:03 +0000 (Fri, 07 Oct 2011)
New Revision: 2066
Modified:
trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java
trunk/src/jar/util/java/org/mulgara/util/io/IOUtil.java
trunk/src/jar/util/java/org/mulgara/util/io/LLHashMapUnitTest.java
Log:
Fixed a test, and re-organized to allow for variable-length objects in the mapping
Modified: trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java 2011-10-07 16:56:03 UTC (rev 2065)
+++ trunk/src/jar/util/java/org/mulgara/util/io/FileHashMap.java 2011-10-07 22:30:03 UTC (rev 2066)
@@ -1,5 +1,5 @@
/*
- * Copyright 2010 Paul Gearon.
+ * Copyright 2011 Paul Gearon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,14 @@
/**
* A file-based hashmap.
+ * This class maps {@link java.nio.ByteBuffer}s of fixed length to {@link java.nio.ByteBuffer}s
+ * of fixed length. To map another data type, wrap this class in a class that converts
+ * keys and values into ByteBuffers.
+ * For an example of mapping a fixed data type, see {@link LLHashMap}, which maps Long to Long.
+ *
+ * Mapping non-fixed types can be done using a custom {@link #hashCode(Object)} function for hashing,
+ * and a custom {@link #equalsKey(ByteBuffer, ByteBuffer)} method. These are protected methods that
+ * can be overridden in a subclass.
*/
public class FileHashMap implements Map<ByteBuffer,ByteBuffer>, Closeable {
@@ -632,15 +640,17 @@
}
/**
- * Tests if a file buffer contains a given key
- * @param kb The byte buffer of the key.
+ * Tests if a file buffer contains a given key.
+ * Note that the file buffer contains the entire record (key+value).
+ * Override this to expand key equality test.
+ * @param filekb The byte buffer of the key.
* @param key The key to look for.
* @return <code>true</code> iff the buffer contains the key.
*/
- private final boolean equalsKey(ByteBuffer kb, ByteBuffer key) {
+ protected final boolean equalsKey(ByteBuffer filekb, ByteBuffer key) {
// compare remaining bytes, if any
for (int i = keySize - 1; i >= 0; i--) {
- if (kb.get(i) != key.get(i)) return false;
+ if (filekb.get(i) != key.get(i)) return false;
}
return true;
}
@@ -658,35 +668,20 @@
return fileRecords;
}
+ protected long hashCode(Object b) {
+ return IOUtil.longHash(b.hashCode());
+ }
+
/**
* Calculate the location for a record, based on its key.
* @param key The ByteBuffer for the key.
* @return The record number in the file that the key specifies.
*/
private final long recordPosition(ByteBuffer key) {
- return longHash(key.hashCode()) % (long)fileRecords;
+ return hashCode(key) % (long)fileRecords;
}
/**
- * Mix an int into a long in a reasonably cheap way.
- * @param The int to mix.
- * @return A long value with the integer mixed into it. Positive numbers only.
- */
- private static final long longHash(int h) {
- long v = (long)h;
- v ^= v << 5;
- v ^= (v << 11) + 1049;
- v ^= ((v >> 32) | (v << 32));
- v ^= (v << 17) + 131041;
- v ^= ((v >> 56) | (v << 56));
- v ^= (v << 23) + 8313581;
- v ^= (((v >> 8) & 0xFF000000L) | ((v << 8) & 0xFF00000000L));
- v ^= (v << 37) + 2147483659L;
- v ^= ((v >> 32) | (v << 32));
- return v & 0x7FFFFFFFFFFFFFFFL;
- }
-
- /**
* Returns the index of the first prime that is greater than or equal to the requested size.
* @param s The requested size.
* @return The index into PRIMES to use.
Modified: trunk/src/jar/util/java/org/mulgara/util/io/IOUtil.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/IOUtil.java 2011-10-07 16:56:03 UTC (rev 2065)
+++ trunk/src/jar/util/java/org/mulgara/util/io/IOUtil.java 2011-10-07 22:30:03 UTC (rev 2066)
@@ -139,4 +139,24 @@
(order == NATIVE_ORDER ? ByteBuffer.allocateDirect(size) : ByteBuffer.allocateDirect(size).order(order)) :
(order == NATIVE_ORDER ? ByteBuffer.allocate(size) : ByteBuffer.allocate(size).order(order));
}
+
+ /**
+ * Mix an int into a long in a reasonably cheap way.
+ * @param The int to mix.
+ * @return A long value with the integer mixed into it. Positive numbers only.
+ */
+ public static final long longHash(int h) {
+ long v = (long)h;
+ v ^= v << 5;
+ v ^= (v << 11) + 1049;
+ v ^= ((v >> 32) | (v << 32));
+ v ^= (v << 17) + 131041;
+ v ^= ((v >> 56) | (v << 56));
+ v ^= (v << 23) + 8313581;
+ v ^= (((v >> 8) & 0xFF000000L) | ((v << 8) & 0xFF00000000L));
+ v ^= (v << 37) + 2147483659L;
+ v ^= ((v >> 32) | (v << 32));
+ return v & 0x7FFFFFFFFFFFFFFFL;
+ }
+
}
Modified: trunk/src/jar/util/java/org/mulgara/util/io/LLHashMapUnitTest.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LLHashMapUnitTest.java 2011-10-07 16:56:03 UTC (rev 2065)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LLHashMapUnitTest.java 2011-10-07 22:30:03 UTC (rev 2066)
@@ -55,7 +55,7 @@
assertEquals(-1L, map.get(4));
assertEquals(null, map.get(Long.valueOf(4)));
map.close();
- assertEquals(131 * 16, f.length());
+ assertEquals(67 * 16, f.length());
clean(f);
}
More information about the Mulgara-svn
mailing list