Class MemoryOpenHashTable
- java.lang.Object
-
- org.apache.druid.query.groupby.epinephelinae.collection.MemoryOpenHashTable
-
public class MemoryOpenHashTable extends Object
An open-addressed hash table with linear probing backed byWritableMemory
. Does not offer a similar interface toMap
because this is meant to be useful to lower-level, high-performance callers. There is no copying or serde of keys and values: callers access the backing memory of the table directly. This table will not grow itself. Callers must handle growing if required; thecopyTo(org.apache.druid.query.groupby.epinephelinae.collection.MemoryOpenHashTable, org.apache.druid.query.groupby.epinephelinae.collection.MemoryOpenHashTable.BucketCopyHandler)
method is provided to assist.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
MemoryOpenHashTable.BucketCopyHandler
-
Constructor Summary
Constructors Constructor Description MemoryOpenHashTable(org.apache.datasketches.memory.WritableMemory tableMemory, int numBuckets, int maxSize, int keySize, int valueSize)
Create a new table.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description it.unimi.dsi.fastutil.ints.IntIterator
bucketIterator()
Iterates over all used buckets, returning bucket numbers for each one.int
bucketKeyOffset()
Returns the offset within each bucket where the key starts.int
bucketMemoryPosition(int bucket)
Returns the position withinmemory()
where a particular bucket starts.int
bucketSize()
Returns the size in bytes of each bucket.static int
bucketSize(int keySize, int valueSize)
Returns the size of each bucket in a table.int
bucketValueOffset()
Returns the offset within each bucket where the value starts.boolean
canInsertNewBucket()
Returns whether this table can accept a new bucket.void
clear()
Clear the table, resetting size to zero.void
copyTo(MemoryOpenHashTable other, MemoryOpenHashTable.BucketCopyHandler copyHandler)
Copy this table into another one.int
findBucket(int keyHash, org.apache.datasketches.memory.Memory keySpace, int keySpacePosition)
Finds the bucket for a particular key.void
initBucket(int bucket, org.apache.datasketches.memory.Memory keySpace, int keySpacePosition)
Initialize a bucket with a particular key.int
keySize()
Returns the size of keys, in bytes.org.apache.datasketches.memory.WritableMemory
memory()
Returns the memory backing this table.static int
memoryNeeded(int numBuckets, int bucketSize)
Returns the amount of memory needed for a table.int
numBuckets()
Returns the number of buckets in this table.int
size()
Returns the number of elements currently in the table.int
valueSize()
Returns the size of values, in bytes.
-
-
-
Constructor Detail
-
MemoryOpenHashTable
public MemoryOpenHashTable(org.apache.datasketches.memory.WritableMemory tableMemory, int numBuckets, int maxSize, int keySize, int valueSize)
Create a new table.- Parameters:
tableMemory
- backing memory for the table; must be exactly large enough to hold "numBuckets"numBuckets
- number of buckets for the tablemaxSize
- maximum number of elements for the table; must be less than numBucketskeySize
- key size in bytesvalueSize
- value size in bytes
-
-
Method Detail
-
memoryNeeded
public static int memoryNeeded(int numBuckets, int bucketSize)
Returns the amount of memory needed for a table. This is just a multiplication, which is easy enough to do on your own, but sometimes it's nice for clarity's sake to call a function with a name that indicates why the multiplication is happening.- Parameters:
numBuckets
- number of bucketsbucketSize
- size per bucket (in bytes)- Returns:
- size of table (in bytes)
-
bucketSize
public static int bucketSize(int keySize, int valueSize)
Returns the size of each bucket in a table.- Parameters:
keySize
- size of keys (in bytes)valueSize
- size of values (in bytes)- Returns:
- size of buckets (in bytes)
-
clear
public void clear()
Clear the table, resetting size to zero.
-
copyTo
public void copyTo(MemoryOpenHashTable other, @Nullable MemoryOpenHashTable.BucketCopyHandler copyHandler)
Copy this table into another one. The other table must be large enough to hold all the copied buckets. The other table will be cleared before the copy takes place.- Parameters:
other
- the other tablecopyHandler
- a callback that is notified for each copied bucket
-
findBucket
public int findBucket(int keyHash, org.apache.datasketches.memory.Memory keySpace, int keySpacePosition)
Finds the bucket for a particular key.- Parameters:
keyHash
- result of callingHashTableUtils.hashMemory(org.apache.datasketches.memory.Memory, long, int)
on this keykeySpace
- memory containing the keykeySpacePosition
- position of the key within keySpace- Returns:
- bucket number if currently occupied, or
-bucket - 1
if not occupied (yet)
-
canInsertNewBucket
public boolean canInsertNewBucket()
Returns whether this table can accept a new bucket.
-
initBucket
public void initBucket(int bucket, org.apache.datasketches.memory.Memory keySpace, int keySpacePosition)
Initialize a bucket with a particular key. Do not call this method unless the bucket is currently unused andcanInsertNewBucket()
returns true.- Parameters:
bucket
- bucket numberkeySpace
- memory containing the keykeySpacePosition
- position of the key within keySpace
-
size
public int size()
Returns the number of elements currently in the table.
-
numBuckets
public int numBuckets()
Returns the number of buckets in this table. Note that not all of these can actually be used. The amount that can be used depends on the "maxSize" parameter provided during construction.
-
keySize
public int keySize()
Returns the size of keys, in bytes.
-
valueSize
public int valueSize()
Returns the size of values, in bytes.
-
bucketKeyOffset
public int bucketKeyOffset()
Returns the offset within each bucket where the key starts.
-
bucketValueOffset
public int bucketValueOffset()
Returns the offset within each bucket where the value starts.
-
bucketSize
public int bucketSize()
Returns the size in bytes of each bucket.
-
bucketMemoryPosition
public int bucketMemoryPosition(int bucket)
Returns the position withinmemory()
where a particular bucket starts.
-
memory
public org.apache.datasketches.memory.WritableMemory memory()
Returns the memory backing this table.
-
bucketIterator
public it.unimi.dsi.fastutil.ints.IntIterator bucketIterator()
Iterates over all used buckets, returning bucket numbers for each one. The intent is that callers will pass the bucket numbers tobucketMemoryPosition(int)
and then usebucketKeyOffset()
andbucketValueOffset()
to extract keys and values from the buckets as needed.
-
-