-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ketan Verma <ketan9495@gmail.com>
- Loading branch information
Showing
7 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
server/src/test/java/org/opensearch/common/util/CompactBytesRefHashTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.util; | ||
|
||
import net.openhft.hashing.LongHashFunction; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
public class CompactBytesRefHashTests extends OpenSearchTestCase { | ||
|
||
public void testFuzzy() { | ||
LongHashFunction hasher = LongHashFunction.xx3(randomLong()); | ||
Map<BytesRef, Long> reference = new HashMap<>(); | ||
BytesRef[] keys = Stream.generate(() -> new BytesRef(randomAlphaOfLength(20))).limit(1000).toArray(BytesRef[]::new); | ||
|
||
try ( | ||
CompactBytesRefHash h = new CompactBytesRefHash( | ||
randomIntBetween(1, 100), // random capacity | ||
0.6f + randomFloat() * 0.39f, // random load factor to verify collision resolution | ||
key -> hasher.hashBytes(key.bytes, key.offset, key.length), | ||
BigArrays.NON_RECYCLING_INSTANCE | ||
) | ||
) { | ||
// Verify the behaviour of "add" and "find". | ||
for (int i = 0; i < keys.length * 10; i++) { | ||
BytesRef key = keys[i % keys.length]; | ||
if (reference.containsKey(key)) { | ||
long expectedOrdinal = reference.get(key); | ||
assertEquals(-1 - expectedOrdinal, h.add(key)); | ||
assertEquals(expectedOrdinal, h.find(key)); | ||
} else { | ||
assertEquals(-1, h.find(key)); | ||
reference.put(key, (long) reference.size()); | ||
assertEquals((long) reference.get(key), h.add(key)); | ||
} | ||
} | ||
|
||
// Verify the behaviour of "get". | ||
BytesRef scratch = new BytesRef(); | ||
for (Map.Entry<BytesRef, Long> entry : reference.entrySet()) { | ||
assertEquals(entry.getKey(), h.get(entry.getValue(), scratch)); | ||
} | ||
|
||
// Verify the behaviour of "size". | ||
assertEquals(reference.size(), h.size()); | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
server/src/test/java/org/opensearch/common/util/ReorganizingBytesRefHashTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.common.util; | ||
|
||
import net.openhft.hashing.LongHashFunction; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
public class ReorganizingBytesRefHashTests extends OpenSearchTestCase { | ||
|
||
public void testFuzzy() { | ||
LongHashFunction hasher = LongHashFunction.xx3(randomLong()); | ||
Map<BytesRef, Long> reference = new HashMap<>(); | ||
BytesRef[] keys = Stream.generate(() -> new BytesRef(randomAlphaOfLength(20))).limit(1000).toArray(BytesRef[]::new); | ||
|
||
try ( | ||
ReorganizingBytesRefHash h = new ReorganizingBytesRefHash( | ||
randomIntBetween(1, 100), // random capacity | ||
0.6f + randomFloat() * 0.39f, // random load factor to verify collision resolution | ||
key -> hasher.hashBytes(key.bytes, key.offset, key.length), | ||
BigArrays.NON_RECYCLING_INSTANCE | ||
) | ||
) { | ||
// Verify the behaviour of "add" and "find". | ||
for (int i = 0; i < keys.length * 10; i++) { | ||
BytesRef key = keys[i % keys.length]; | ||
if (reference.containsKey(key)) { | ||
long expectedOrdinal = reference.get(key); | ||
assertEquals(-1 - expectedOrdinal, h.add(key)); | ||
assertEquals(expectedOrdinal, h.find(key)); | ||
} else { | ||
assertEquals(-1, h.find(key)); | ||
reference.put(key, (long) reference.size()); | ||
assertEquals((long) reference.get(key), h.add(key)); | ||
} | ||
} | ||
|
||
// Verify the behaviour of "get". | ||
BytesRef scratch = new BytesRef(); | ||
for (Map.Entry<BytesRef, Long> entry : reference.entrySet()) { | ||
assertEquals(entry.getKey(), h.get(entry.getValue(), scratch)); | ||
} | ||
|
||
// Verify the behaviour of "size". | ||
assertEquals(reference.size(), h.size()); | ||
|
||
// Verify the calculation of PSLs. | ||
long capacity = h.getTable().size(); | ||
long mask = capacity - 1; | ||
for (long idx = 0; idx < h.getTable().size(); idx++) { | ||
long value = h.getTable().get(idx); | ||
if (value != -1) { | ||
BytesRef key = h.get((int) value, scratch); | ||
long homeIdx = hasher.hashBytes(key.bytes, key.offset, key.length) & mask; | ||
assertEquals((capacity + idx - homeIdx) & mask, value >>> 48); | ||
} | ||
} | ||
} | ||
} | ||
} |