Package org.neo4j.gds.core.utils.paged
Class HugeAtomicDoubleArray
- java.lang.Object
-
- org.neo4j.gds.core.utils.paged.HugeAtomicDoubleArray
-
public abstract class HugeAtomicDoubleArray extends java.lang.Object
-
-
Constructor Summary
Constructors Constructor Description HugeAtomicDoubleArray()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description DoubleNodePropertyValues
asNodeProperties()
abstract double
compareAndExchange(long index, double expect, double update)
Atomically sets the element at positionindex
to the given updated value if the current value, referred to as the witness value,==
the expected value.abstract boolean
compareAndSet(long index, double expect, double update)
Atomically sets the element at positionindex
to the given updated value if the current value==
the expected value.abstract double
get(long index)
abstract double
getAndAdd(long index, double delta)
Atomically adds the given delta to the value at the given index.abstract double
getAndReplace(long index, double value)
Atomically returns the double value at the given index and replaces it with the given value.static long
memoryEstimation(long size)
static HugeAtomicDoubleArray
newArray(long size)
Creates a new array of the given size.static HugeAtomicDoubleArray
newArray(long size, DoublePageCreator pageFiller)
Creates a new array of the given size.abstract long
release()
Destroys the data, allowing the underlying storage arrays to be collected as garbage.abstract void
set(long index, double value)
Sets the double value at the given index to the given value.abstract void
setAll(double value)
abstract long
size()
Returns the length of this array.abstract long
sizeOf()
abstract void
update(long index, java.util.function.DoubleUnaryOperator updateFunction)
Atomically updates the element at indexindex
with the results of applying the given function, returning the updated value.
-
-
-
Method Detail
-
get
public abstract double get(long index)
- Returns:
- the double value at the given index
- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the index is not withinsize()
-
getAndAdd
public abstract double getAndAdd(long index, double delta)
Atomically adds the given delta to the value at the given index.- Parameters:
index
- the indexdelta
- the value to add- Returns:
- the previous value at index
-
set
public abstract void set(long index, double value)
Sets the double value at the given index to the given value.- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the index is not withinsize()
-
getAndReplace
public abstract double getAndReplace(long index, double value)
Atomically returns the double value at the given index and replaces it with the given value.- Throws:
java.lang.ArrayIndexOutOfBoundsException
- if the index is not withinsize()
-
compareAndSet
public abstract boolean compareAndSet(long index, double expect, double update)
Atomically sets the element at positionindex
to the given updated value if the current value==
the expected value.- Parameters:
index
- the indexexpect
- the expected valueupdate
- the new value- Returns:
true
if successful. False return indicates that the actual value was not equal to the expected value.
-
compareAndExchange
public abstract double compareAndExchange(long index, double expect, double update)
Atomically sets the element at positionindex
to the given updated value if the current value, referred to as the witness value,==
the expected value. This operation works as if implemented asif (this.compareAndSet(index, expect, update)) { return expect; } else { return this.get(index); }
The actual implementation is done with a single atomic operation so that the returned witness value is the value that was failing the update, not one that needs be read again after the failed update. This allows one to write CAS-loops in a different way, which removes one volatile read per loop iterationvar oldValue = this.get(index); while (true) { var newValue = updateFunction(oldValue); var witnessValue = this.compareAndExchange(index, oldValue, newValue); if (witnessValue == oldValue) { // update successful break; } // update unsuccessful set, loop and try again. // Here we already have the updated witness value and don't need to issue // a new read oldValue = witnessValue; }
- Parameters:
index
- the indexexpect
- the expected valueupdate
- the new value- Returns:
- the result that is the witness value, which will be the same as the expected value if successful or the new current value if unsuccessful.
-
update
public abstract void update(long index, java.util.function.DoubleUnaryOperator updateFunction)
Atomically updates the element at indexindex
with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.- Parameters:
index
- the indexupdateFunction
- a side-effect-free function
-
size
public abstract long size()
Returns the length of this array.If the size is greater than zero, the highest supported index is
size() - 1
The behavior is identical to calling
array.length
on primitive arrays.
-
sizeOf
public abstract long sizeOf()
- Returns:
- the amount of memory used by the instance of this array, in bytes.
This should be the same as returned from
release()
without actually releasing the array.
-
setAll
public abstract void setAll(double value)
-
release
public abstract long release()
Destroys the data, allowing the underlying storage arrays to be collected as garbage. The array is unusable after calling this method and will throwNullPointerException
s on virtually every method invocation.Note that the data might not immediately collectible if there are still cursors alive that reference this array. You have to
HugeCursor.close()
every cursor instance as well.- Returns:
- the amount of memory freed, in bytes.
-
asNodeProperties
public DoubleNodePropertyValues asNodeProperties()
-
newArray
public static HugeAtomicDoubleArray newArray(long size)
Creates a new array of the given size.
-
newArray
public static HugeAtomicDoubleArray newArray(long size, DoublePageCreator pageFiller)
Creates a new array of the given size. The values are pre-calculated according to the semantics ofArrays.setAll(double[], java.util.function.IntToDoubleFunction)
-
memoryEstimation
public static long memoryEstimation(long size)
-
-