Class BytesRefHash

java.lang.Object
org.elasticsearch.common.util.BytesRefHash
All Implemented Interfaces:
Closeable, AutoCloseable, org.apache.lucene.util.Accountable, Releasable

public final class BytesRefHash extends Object implements org.apache.lucene.util.Accountable
Specialized hash table implementation similar to Lucene's BytesRefHash that maps BytesRef values to ids. Collisions are resolved with open addressing and linear probing, growth is smooth thanks to BigArrays, hashes are cached for faster re-hashing and capacity is always a multiple of 2 for faster identification of buckets. This class is not thread-safe.
  • Field Summary

    Fields inherited from interface org.apache.lucene.util.Accountable

    NULL_ACCOUNTABLE
  • Constructor Summary

    Constructors
    Constructor
    Description
    BytesRefHash(long capacity, float maxLoadFactor, BigArrays bigArrays)
     
    BytesRefHash(long capacity, BigArrays bigArrays)
     
    BytesRefHash(BytesRefArray bytesRefs, float maxLoadFactor, BigArrays bigArrays)
    Construct a BytesRefHash given a BytesRefArray.
    BytesRefHash(BytesRefArray bytesRefArray, BigArrays bigArrays)
    Construct a BytesRefHash given a BytesRefArray with default maximum load factor.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    add(org.apache.lucene.util.BytesRef key)
    long
    add(org.apache.lucene.util.BytesRef key, int code)
    Try to add key.
    long
    Return the number of allocated slots to store this hash table.
    void
     
    long
    find(org.apache.lucene.util.BytesRef key)
    long
    find(org.apache.lucene.util.BytesRef key, int code)
    Get the id associated with key
    org.apache.lucene.util.BytesRef
    get(long id, org.apache.lucene.util.BytesRef dest)
    Return the key at 0 <= index <= capacity().
     
    protected final void
     
    long
    id(long index)
    Get the id associated with key at 0 <= index <= capacity() or -1 if this slot is unused.
    protected final long
    id(long index, long id)
     
    long
     
    protected void
    removeAndAdd(long index)
    Remove the entry at the given index and add it back
    protected void
    resize(long capacity)
    Resize to the given capacity.
    long
    Return the number of longs in this hash table.
     
    protected boolean
    used(long bucket)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.apache.lucene.util.Accountable

    getChildResources
  • Constructor Details

    • BytesRefHash

      public BytesRefHash(long capacity, BigArrays bigArrays)
    • BytesRefHash

      public BytesRefHash(long capacity, float maxLoadFactor, BigArrays bigArrays)
    • BytesRefHash

      public BytesRefHash(BytesRefArray bytesRefArray, BigArrays bigArrays)
      Construct a BytesRefHash given a BytesRefArray with default maximum load factor. Note the comments below regarding leakage protection of the given BytesRefArray.
    • BytesRefHash

      public BytesRefHash(BytesRefArray bytesRefs, float maxLoadFactor, BigArrays bigArrays)
      Construct a BytesRefHash given a BytesRefArray. Note: The ownership over BytesRefArray is taken over by the created BytesRefHash. Because BytesRefHash construction potentially throws a circuit breaker exception, the BytesRefArray must be protected against leakage, e.g. boolean success = false; BytesRefArray array = null; try { array = new BytesRefArray(...); hash = new BytesRefHash(array, bigArrays); success = true; } finally { if (false == success) { try (Releasable releasable = Releasables.wrap(array)) { close(); // assuming hash is a member and close() closes it } } } Iff the BytesRefHash instance got created successfully, it is managed by BytesRefHash and does not need to be closed.
  • Method Details

    • get

      public org.apache.lucene.util.BytesRef get(long id, org.apache.lucene.util.BytesRef dest)
      Return the key at 0 <= index <= capacity(). The result is undefined if the slot is unused.

      Beware that the content of the BytesRef may become invalid as soon as close() is called

    • find

      public long find(org.apache.lucene.util.BytesRef key, int code)
      Get the id associated with key
    • find

      public long find(org.apache.lucene.util.BytesRef key)
    • add

      public long add(org.apache.lucene.util.BytesRef key, int code)
      Try to add key. Return its newly allocated id if it wasn't in the hash table yet, or -1-id if it was already present in the hash table.
    • add

      public long add(org.apache.lucene.util.BytesRef key)
    • removeAndAdd

      protected void removeAndAdd(long index)
      Remove the entry at the given index and add it back
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Releasable
    • getBytesRefs

      public BytesRefArray getBytesRefs()
    • takeBytesRefsOwnership

      public BytesRefArray takeBytesRefsOwnership()
    • ramBytesUsed

      public long ramBytesUsed()
      Specified by:
      ramBytesUsed in interface org.apache.lucene.util.Accountable
    • id

      public long id(long index)
      Get the id associated with key at 0 <= index <= capacity() or -1 if this slot is unused.
    • id

      protected final long id(long index, long id)
    • resize

      protected void resize(long capacity)
      Resize to the given capacity.
    • used

      protected boolean used(long bucket)
    • capacity

      public long capacity()
      Return the number of allocated slots to store this hash table.
    • size

      public long size()
      Return the number of longs in this hash table.
    • grow

      protected final void grow()