Class AChristmasDictionary<K>
- java.lang.Object
-
- com.qfs.dic.impl.AChristmasDictionary<K>
-
- Type Parameters:
K- The type of Objects stored in this dictionary
- All Implemented Interfaces:
IConcurrentDictionary<K>,IDictionary<K>,IWritableDictionary<K>,ICompressible,IMemoryMonitored,Externalizable,Serializable
- Direct Known Subclasses:
ChristmasDictionary,ChristmasDictionaryDouble,ChristmasDictionaryFloat,ChristmasDictionaryInteger,ChristmasDictionaryLong
public abstract class AChristmasDictionary<K> extends Object implements IConcurrentDictionary<K>, Externalizable
AnIConcurrentDictionarywhere most of the regular operations are non-blocking.The main exceptions to this rule are:
- the operations happening concurrently with a resize of the dictionary
- a read or write operation on a given key happening concurrently with another write operation whose key's hash value is the same as the first key's hash value.
Its name comes from the fact that it has been designed on Christmas eve.
- Author:
- ActiveViam
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.qfs.dic.IConcurrentDictionary
IConcurrentDictionary.IDictionaryAction<K>
-
Nested classes/interfaces inherited from interface com.qfs.dic.IWritableDictionary
IWritableDictionary.IDoubleDictionary, IWritableDictionary.IFloatDictionary, IWritableDictionary.IIntegerDictionary, IWritableDictionary.ILongDictionary
-
-
Field Summary
-
Fields inherited from interface com.qfs.dic.IConcurrentDictionary
NEW_MAPPING
-
Fields inherited from interface com.qfs.dic.IDictionary
DEFAULT_INITIAL_CAPACITY, ID_GENERATOR
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears the whole content of the dictionary.ATasksPerNumaNodecompress(boolean destroyOnly)Collects all the useless objects stored off-heap and free that memory space without changing logical content of existing versions.voiddestroy()Frees all resources held by this data structure.longgetDictionaryId()Gets the unique ID of this dictionary useful for monitoring).intgetDoublePosition(double key)Searches for a double in the dictionary.intgetFloatPosition(float key)Searches for a float in the dictionary.intgetIntPosition(int key)Searches for an integer in the dictionary.intgetLongPosition(long key)Searches for a long in the dictionary.IMemoryStatisticgetMemoryStatistic()Gets theIMemoryStatisticof the component being monitored.KgetNullValue()Gets the value representing a"null"for a dictionarized value.intgetOrder()Returns the order of the dictionary.intgetPosition(K key)Searches for a key in the dictionary.booleanisNullable()Returns whether this dictionary is nullable (it is capable to map thenullkey).intmap(K key)Indexes a new object in the dictionary.longmap(K key, IConcurrentDictionary.IDictionaryAction<K> mappingAction, IConcurrentDictionary.IDictionaryAction<K> existingKeyAction)Maps a new object in the dictionary the same way asIWritableDictionary.map(Object).intmapDouble(double key)Indexes a double as a new key in the dictionary.intmapFloat(float key)Indexes a float as a new key in the dictionary.intmapInt(int key)Indexes an integer as a new key in the dictionary.intmapLong(long key)Indexes a long as a new key in the dictionary.Kread(int position)Reads the object stored at a given dictionary position.doublereadDouble(int position)Reads the double stored at a given dictionary position.voidreadExternal(ObjectInput in, K nullValue)Reads thesize()and keys in order and rebuilds the innerdictionary.floatreadFloat(int position)Reads the float stored at a given dictionary position.intreadInt(int position)Reads the integer stored at a given dictionary position.longreadLong(int position)Reads the long stored at a given dictionary position.static intregularizeHash(int h)Regularizes the given hash value so that it can be efficiently used in this dictionary.intsize()Gets the size (number of entries) of this dictionary.StringtoString()voidwriteExternal(ObjectOutput out)Simply writes thesize()and keys in order usingInnerChristmasDictionary.writeSerializedKeys(ObjectOutput).-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.io.Externalizable
readExternal
-
Methods inherited from interface com.qfs.memory.ICompressible
compress
-
Methods inherited from interface com.qfs.dic.IConcurrentDictionary
supportConcurrentReads, supportConcurrentWrites
-
Methods inherited from interface com.qfs.dic.IDictionary
copyInto, getFirstElements, getSize, getType
-
Methods inherited from interface com.qfs.dic.IWritableDictionary
mapFromCell
-
-
-
-
Method Detail
-
getNullValue
public K getNullValue()
Gets the value representing a"null"for a dictionarized value.- Returns:
- the chosen value for null
-
size
public int size()
Description copied from interface:IDictionaryGets the size (number of entries) of this dictionary.- Specified by:
sizein interfaceIDictionary<K>- Returns:
- the size
-
clear
public void clear()
Description copied from interface:IWritableDictionaryClears the whole content of the dictionary.This operation will not recreate or resize the internal data structures of the dictionary, but erase them.
- Specified by:
clearin interfaceIWritableDictionary<K>
-
isNullable
public boolean isNullable()
Description copied from interface:IDictionaryReturns whether this dictionary is nullable (it is capable to map thenullkey).Trying to map or search the
nullelement on a dictionary that is non-nullable will at some point throw aNullPointerException.- Specified by:
isNullablein interfaceIDictionary<K>- Returns:
trueif this dictionary is nullable,falseotherwise
-
destroy
public void destroy()
Description copied from interface:IWritableDictionaryFrees all resources held by this data structure.- Specified by:
destroyin interfaceIWritableDictionary<K>
-
compress
public ATasksPerNumaNode compress(boolean destroyOnly)
Description copied from interface:ICompressibleCollects all the useless objects stored off-heap and free that memory space without changing logical content of existing versions.Basically, this method tries to remove all the chunks without valid rows or converts all chunks into sparse chunks whether or not the ratio of valid rows and total rows reaches its critical value.
- Specified by:
compressin interfaceICompressible- Parameters:
destroyOnly- iftrue, the compression actions should only try to destroy useless chunks, and not to compress the sparse ones, this is to avoid asking for more (transient) direct memory- Returns:
- all the actions to run to compress this component, mapped by thread pool ID
-
readExternal
public void readExternal(ObjectInput in, K nullValue) throws IOException, ClassNotFoundException
Reads thesize()and keys in order and rebuilds the innerdictionary.- Parameters:
in- The input streamnullValue- The value representing anullvalue for a value.- Throws:
IOException- if it fails to read the inputClassNotFoundException- if one of the serialized data class does not exist
-
writeExternal
public void writeExternal(ObjectOutput out) throws IOException
Simply writes thesize()and keys in order usingInnerChristmasDictionary.writeSerializedKeys(ObjectOutput).- Specified by:
writeExternalin interfaceExternalizable- Parameters:
out- The output stream- Throws:
IOException
-
regularizeHash
public static final int regularizeHash(int h)
Regularizes the given hash value so that it can be efficiently used in this dictionary.- Parameters:
h- The hash value to regularize- Returns:
- The regularized hash value.
-
map
public long map(K key, IConcurrentDictionary.IDictionaryAction<K> mappingAction, IConcurrentDictionary.IDictionaryAction<K> existingKeyAction)
Description copied from interface:IConcurrentDictionaryMaps a new object in the dictionary the same way asIWritableDictionary.map(Object).If a new mapping is created because of this call (i.e. the key did not already exist in the dictionary), the associated mapping function is invoked before the mapping is made visible to the other threads. It will only become visible after the callback has returned.
Other calls toIWritableDictionary.map(Object)should block until this new mapping is made visible and return the newly mapped position afterwards. Other calls toIDictionary.getPosition(K)might either block or return-1if the new mapping is not visible yet.If a mapping already existed for this key, the existing key action is invoked before returning.
The returned value is a
longwhose lower 4 bytes represent the mapped object's position, while its upper 4 bytes are used for various flags. The returned value can therefore simply be casted as anintto get the key's address.- Specified by:
mapin interfaceIConcurrentDictionary<K>- Parameters:
key- the object to map in this dictionarymappingAction- the action to execute if a new mapping is created for this object, can benullexistingKeyAction- the action to execute if a mapping already existed for this key, can benull- Returns:
- a long that packs the key's dictionary address in its lower 4 bytes and various flags in the remaining upper 4 bytes
-
map
public int map(K key)
Description copied from interface:IWritableDictionaryIndexes a new object in the dictionary.If the object already existed in the dictionary (with respect to the equals / hashCode contract) the previous copy of the object is left in the dictionary. The new object copy can then be safely reused or cleared without any impact to the dictionary consistency.
- Specified by:
mapin interfaceIWritableDictionary<K>- Parameters:
key- the object- Returns:
- the address of the object in the dictionary buffer
-
mapDouble
public int mapDouble(double key)
Description copied from interface:IWritableDictionaryIndexes a double as a new key in the dictionary.If the key already existed in the dictionary (with respect to the equals / hashCode contract) the previous copy of the object is left in the dictionary. The new object copy can then be safely reused or cleared without any impact to the dictionary consistency.
This will throw UnsupportedOperationException if the type Double is not compatible with the type of the dictionary.
- Specified by:
mapDoublein interfaceIWritableDictionary<K>- Parameters:
key- the double to map- Returns:
- the address of the object in the dictionary buffer
-
mapFloat
public int mapFloat(float key)
Description copied from interface:IWritableDictionaryIndexes a float as a new key in the dictionary.If the key already existed in the dictionary (with respect to the equals / hashCode contract) the previous copy of the object is left in the dictionary. The new object copy can then be safely reused or cleared without any impact to the dictionary consistency.
This will throw UnsupportedOperationException if the type Float is not compatible with the type of the dictionary.
- Specified by:
mapFloatin interfaceIWritableDictionary<K>- Parameters:
key- the float to map- Returns:
- the address of the object in the dictionary buffer
-
mapInt
public int mapInt(int key)
Description copied from interface:IWritableDictionaryIndexes an integer as a new key in the dictionary.If the key already existed in the dictionary (with respect to the equals / hashCode contract) the previous copy of the object is left in the dictionary. The new object copy can then be safely reused or cleared without any impact to the dictionary consistency.
This will throw UnsupportedOperationException if the type Integer is not compatible with the type of the dictionary.
- Specified by:
mapIntin interfaceIWritableDictionary<K>- Parameters:
key- the integer to map- Returns:
- the address of the object in the dictionary buffer
-
mapLong
public int mapLong(long key)
Description copied from interface:IWritableDictionaryIndexes a long as a new key in the dictionary.If the key already existed in the dictionary (with respect to the equals / hashCode contract) the previous copy of the object is left in the dictionary. The new object copy can then be safely reused or cleared without any impact to the dictionary consistency.
This will throw UnsupportedOperationException if the type Long is not compatible with the type of the dictionary.
- Specified by:
mapLongin interfaceIWritableDictionary<K>- Parameters:
key- the long to map- Returns:
- the address of the object in the dictionary buffer
-
read
public K read(int position)
Description copied from interface:IDictionaryReads the object stored at a given dictionary position.- Specified by:
readin interfaceIDictionary<K>- Parameters:
position- the position in the dictionary where to read the object- Returns:
- the Object at the given
position
-
readDouble
public double readDouble(int position)
Description copied from interface:IDictionaryReads the double stored at a given dictionary position.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Double.
- Specified by:
readDoublein interfaceIDictionary<K>- Parameters:
position- the position in the dictionary- Returns:
- the double stored at the given position
-
readFloat
public float readFloat(int position)
Description copied from interface:IDictionaryReads the float stored at a given dictionary position.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Float.
- Specified by:
readFloatin interfaceIDictionary<K>- Parameters:
position- the position in the dictionary- Returns:
- the float stored at the given position
-
readInt
public int readInt(int position)
Description copied from interface:IDictionaryReads the integer stored at a given dictionary position.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Int.
- Specified by:
readIntin interfaceIDictionary<K>- Parameters:
position- the position in the dictionary- Returns:
- the integer stored at the given position
-
readLong
public long readLong(int position)
Description copied from interface:IDictionaryReads the long stored at a given dictionary position.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Long.
- Specified by:
readLongin interfaceIDictionary<K>- Parameters:
position- the position in the dictionary- Returns:
- the long stored at the given position
-
getPosition
public int getPosition(K key)
Description copied from interface:IDictionarySearches for a key in the dictionary.- Specified by:
getPositionin interfaceIDictionary<K>- Parameters:
key- the object we are looking for- Returns:
- position of the key in the dictionary, or
-1if the key is not in the dictionary
-
getDoublePosition
public int getDoublePosition(double key)
Description copied from interface:IDictionarySearches for a double in the dictionary.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Double.
- Specified by:
getDoublePositionin interfaceIDictionary<K>- Parameters:
key- the double- Returns:
- position of the key in the dictionary, or
-1if the key is not in the dictionary
-
getFloatPosition
public int getFloatPosition(float key)
Description copied from interface:IDictionarySearches for a float in the dictionary.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Float.
- Specified by:
getFloatPositionin interfaceIDictionary<K>- Parameters:
key- the float- Returns:
- position of the key in the dictionary, or
-1if the key is not in the dictionary
-
getIntPosition
public int getIntPosition(int key)
Description copied from interface:IDictionarySearches for an integer in the dictionary.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Int.
- Specified by:
getIntPositionin interfaceIDictionary<K>- Parameters:
key- the integer- Returns:
- position of the key in the dictionary, or
-1if the key is not in the dictionary
-
getLongPosition
public int getLongPosition(long key)
Description copied from interface:IDictionarySearches for a long in the dictionary.Will throw UnsupportedOperationException if the stored object in the dictionary is not of type Long.
- Specified by:
getLongPositionin interfaceIDictionary<K>- Parameters:
key- the long- Returns:
- position of the key in the dictionary, or
-1if the key is not in the dictionary
-
getMemoryStatistic
public IMemoryStatistic getMemoryStatistic()
Description copied from interface:IMemoryMonitoredGets theIMemoryStatisticof the component being monitored.- Specified by:
getMemoryStatisticin interfaceIMemoryMonitored- Returns:
- the
IMemoryStatistic
-
getOrder
public int getOrder()
Returns the order of the dictionary.- Specified by:
getOrderin interfaceIDictionary<K>- Returns:
- the order
-
getDictionaryId
public long getDictionaryId()
Description copied from interface:IDictionaryGets the unique ID of this dictionary useful for monitoring).- Specified by:
getDictionaryIdin interfaceIDictionary<K>- Returns:
- the ID of this dictionary (unique amongst all dictionaries used in the application)
-
-