All Implemented Interfaces:
DataStructure

class LocalHashextends Hash
Pull the caching used only by KBucketImpl out of Hash and put it here.
  • Field Details

    • _xorCache

      private Map<Hash,byte[]> _xorCache
      Distances already computed, by peer hash.
    • MAX_CACHED_XOR

      private static final int MAX_CACHED_XOR
      Distances kept before the cache is dropped.
      See Also:
  • Constructor Details

    • LocalHash

      public LocalHash(Hash h)
      Builds a local hash from the data of an existing hash.
      Parameters:
      h - the hash to copy
    • LocalHash

      public LocalHash(byte[] b)
      Builds a local hash around raw hash bytes.
      Parameters:
      b - the raw hash bytes
  • Method Details

    • prepareCache

      public void prepareCache()
      Prepare this hash's cache for xor values - very few hashes will need it, so we don't want to waste the memory, and lazy initialization would incur online overhead to verify the initialization.
    • cachedXor

      public byte[] cachedXor(Hash key) throws IllegalStateException
      Calculate the xor with the current object and the specified hash, caching values where possible. Currently this keeps up to MAX_CACHED_XOR (1024) entries, and uses an essentially random ejection policy. Later perhaps go for an LRU or FIFO?
      Parameters:
      key - the hash to measure this object's own hash against
      Returns:
      the XOR distance between the two hashes, never null
      Throws:
      IllegalStateException - if you try to use the cache without first preparing this object's cache via .prepareCache()
    • clearXorCache

      public void clearXorCache()
      Clear the XOR distance cache.