java.lang.Object
net.i2p.data.SimpleDataStructure
net.i2p.data.Hash
net.i2p.router.networkdb.kademlia.LocalHash
- All Implemented Interfaces:
DataStructure
Pull the caching used only by KBucketImpl out of Hash and put it here.
Field Summary
FieldsModifier and TypeFieldDescriptionDistances already computed, by peer hash.private static final intDistances kept before the cache is dropped.Fields inherited from class Hash
FAKE_HASH, HASH_LENGTHModifier and TypeFieldDescriptionstatic final HashPlaceholder all-zero hash, used as a stand-in when no real router hash is known.static final intLength of SHA-256 hash in bytes.Fields inherited from class SimpleDataStructure
_dataConstructor Summary
Method Summary
All MethodsModifier and TypeMethodDescriptionbyte[]Calculate the xor with the current object and the specified hash, caching values where possible.voidClear the XOR distance cache.voidPrepare 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.Methods inherited from class Hash
clearCache, create, create, create, hashCode, length, readBytes, setData, toBase32, toBase64Modifier and TypeMethodDescriptionstatic voidDrop the shared cache of Hash instances, so cached instances become eligible for collection.static Hashcreate(byte[] data) Pull from cache or return newstatic Hashcreate(byte[] data, int off) Pull from cache or return new.static Hashcreate(InputStream in) Pull from cache or return new.inthashCode()A Hash is a hash, so just use the first 4 bytes for speed.intlength()Hash length in bytes.voidreadBytes(InputStream in) Reads the hash from an input stream.voidsetData(byte[] data) IllegalArgumentException if data is not 32 bytes (null is ok).toBase32()The .b32.i2p form of the hash.toBase64()Returns the hash as a Base64 string.Methods inherited from class SimpleDataStructure
calculateHash, equals, fromBase64, fromByteArray, getData, read, toByteArray, toString, writeBytesModifier and TypeMethodDescriptionSHA256 hash of the byte array, or null if data is null.booleanCompares the raw data only: two different SimpleDataStructure subclasses of the same length holding the same bytes are equal, e.g.voidfromBase64(String data) Data from the base64 string.voidfromByteArray(byte[] data) Does the same thing asSimpleDataStructure.setData(byte[])but null not allowed.byte[]getData()Data reference (not a copy).protected intread(InputStream in, byte[] target) Repeated reads until the buffer is full or IOException is thrown.byte[]Same thing as getData().toString()Returns a string representation of this data structure.voidwriteBytes(OutputStream out) Writes the data to the output stream.
Field Details
_xorCache
MAX_CACHED_XOR
private static final int MAX_CACHED_XORDistances kept before the cache is dropped.- See Also:
Constructor Details
LocalHash
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
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.