public final class BytesRefHash extends Object
BytesRefHash
is a special purpose hash-map like data-structure
optimized for BytesRef
instances. BytesRefHash maintains mappings of
byte arrays to ordinal (MapBytesRefHash
and is guaranteed to be increased
for each added BytesRef
.
Note: The maximum capacity BytesRef
instance passed to
add(BytesRef)
must not be longer than ByteBlockPool.BYTE_BLOCK_SIZE
-2.
The internal storage is limited to 2GB total byte storage.
Modifier and Type | Class and Description |
---|---|
static class |
BytesRefHash.BytesStartArray
Manages allocation of the per-term addresses.
|
static class |
BytesRefHash.DirectBytesStartArray
A simple
BytesRefHash.BytesStartArray that tracks
memory allocation using a private AtomicLong
instance. |
static class |
BytesRefHash.MaxBytesLengthExceededException
|
static class |
BytesRefHash.TrackingDirectBytesStartArray
A simple
BytesRefHash.BytesStartArray that tracks all
memory allocation using a shared AtomicLong
instance. |
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_CAPACITY |
Constructor and Description |
---|
BytesRefHash()
|
BytesRefHash(ByteBlockPool pool)
Creates a new
BytesRefHash |
BytesRefHash(ByteBlockPool pool,
int capacity,
BytesRefHash.BytesStartArray bytesStartArray)
Creates a new
BytesRefHash |
Modifier and Type | Method and Description |
---|---|
int |
add(BytesRef bytes)
Adds a new
BytesRef |
int |
add(BytesRef bytes,
int code)
Adds a new
BytesRef with a pre-calculated hash code. |
int |
addByPoolOffset(int offset) |
int |
byteStart(int ord)
Returns the bytesStart offset into the internally used
ByteBlockPool for the given ord |
void |
clear() |
void |
clear(boolean resetPool)
|
void |
close()
Closes the BytesRefHash and releases all internally used memory
|
int[] |
compact()
Returns the ords array in arbitrary order.
|
BytesRef |
get(int ord,
BytesRef ref)
Populates and returns a
BytesRef with the bytes for the given ord. |
void |
reinit()
reinitializes the
BytesRefHash after a previous clear()
call. |
int |
size()
Returns the number of
BytesRef values in this BytesRefHash . |
int[] |
sort(Comparator<BytesRef> comp)
Returns the values array sorted by the referenced byte values.
|
public static final int DEFAULT_CAPACITY
public BytesRefHash()
public BytesRefHash(ByteBlockPool pool)
BytesRefHash
public BytesRefHash(ByteBlockPool pool, int capacity, BytesRefHash.BytesStartArray bytesStartArray)
BytesRefHash
public int size()
BytesRef
values in this BytesRefHash
.BytesRef
values in this BytesRefHash
.public BytesRef get(int ord, BytesRef ref)
BytesRef
with the bytes for the given ord.
Note: the given ord must be a positive integer less that the current size (
size()
)
ord
- the ordref
- the BytesRef
to populatepublic int[] compact()
size()
- 1
Note: This is a destructive operation. clear()
must be called in
order to reuse this BytesRefHash
instance.
public int[] sort(Comparator<BytesRef> comp)
Note: This is a destructive operation. clear()
must be called in
order to reuse this BytesRefHash
instance.
comp
- the Comparator
used for sortingpublic void clear(boolean resetPool)
public void clear()
public void close()
public int add(BytesRef bytes)
BytesRef
bytes
- the bytes to hash(-(ord)-1)
. This guarantees
that the return value will always be >= 0 if the given bytes
haven't been hashed before.BytesRefHash.MaxBytesLengthExceededException
- if the given bytes are > 2 +
ByteBlockPool.BYTE_BLOCK_SIZE
public int add(BytesRef bytes, int code)
BytesRef
with a pre-calculated hash code.bytes
- the bytes to hashcode
- the bytes hash code
Hashcode is defined as:
int hash = 0; for (int i = offset; i < offset + length; i++) { hash = 31 * hash + bytes[i]; }
(-(ord)-1)
. This guarantees
that the return value will always be >= 0 if the given bytes
haven't been hashed before.BytesRefHash.MaxBytesLengthExceededException
- if the given bytes are >
ByteBlockPool.BYTE_BLOCK_SIZE
- 2public int addByPoolOffset(int offset)
public void reinit()
BytesRefHash
after a previous clear()
call. If clear()
has not been called previously this method has no
effect.public int byteStart(int ord)
ByteBlockPool
for the given ordord
- the ord to look upByteBlockPool
for the given ord