Class HugeAtomicLongArray


  • public abstract class HugeAtomicLongArray
    extends java.lang.Object
    A long-indexable version of a AtomicLongArray that can contain more than 2 bn. elements.

    It is implemented by paging of smaller long-arrays (long[][]) to support approx. 32k bn. elements. If the the provided size is small enough, an optimized view of a single long[] might be used.

    • The array is of a fixed size and cannot grow or shrink dynamically.
    • The array is not optimized for sparseness and has a large memory overhead if the values written to it are very sparse.
    • The array does not support default values and returns the same default for unset values that a regular long[] does (0).
    • It only supports a minimal subset of the atomic operations that AtomicLongArray provides.

    Basic Usage

     
     AllocationTracker tracker = ...;
     long arraySize = 42L;
     HugeAtomicLongArray array = HugeAtomicLongArray.newArray(arraySize, tracker);
     array.set(13L, 37L);
     long value = array.get(13L);
     // value = 37L
     
     
    Implementation is similar to the AtomicLongArray, based on sun.misc.Unsafe https://hg.openjdk.java.net/jdk/jdk13/file/9e0c80381e32/jdk/src/java.base/share/classes/java/util/concurrent/atomic/AtomicLongArray.java
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract boolean compareAndSet​(long index, long expect, long update)
      Atomically sets the element at position index to the given updated value if the current value == the expected value.
      abstract long get​(long index)  
      static long memoryEstimation​(long size)  
      static HugeAtomicLongArray newArray​(long size, AllocationTracker tracker)
      Creates a new array of the given size, tracking the memory requirements into the given AllocationTracker.
      static HugeAtomicLongArray newArray​(long size, PageFiller pageFiller, AllocationTracker tracker)
      Creates a new array of the given size, tracking the memory requirements into the given AllocationTracker.
      abstract long release()
      Destroys the data, allowing the underlying storage arrays to be collected as garbage.
      abstract void set​(long index, long value)
      Sets the long value at the given index to the given value.
      abstract long size()
      Returns the length of this array.
      abstract long sizeOf()  
      abstract void update​(long index, java.util.function.LongUnaryOperator updateFunction)
      Atomically updates the element at index index with the results of applying the given function, returning the updated value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HugeAtomicLongArray

        public HugeAtomicLongArray()
    • Method Detail

      • get

        public abstract long get​(long index)
        Returns:
        the long value at the given index
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the index is not within size()
      • set

        public abstract void set​(long index,
                                 long value)
        Sets the long value at the given index to the given value.
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if the index is not within size()
      • compareAndSet

        public abstract boolean compareAndSet​(long index,
                                              long expect,
                                              long update)
        Atomically sets the element at position index to the given updated value if the current value == the expected value.
        Parameters:
        index - the index
        expect - the expected value
        update - the new value
        Returns:
        true if successful. False return indicates that the actual value was not equal to the expected value.
      • update

        public abstract void update​(long index,
                                    java.util.function.LongUnaryOperator updateFunction)
        Atomically updates the element at index index 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 index
        updateFunction - 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.
      • 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 throw NullPointerExceptions 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.

        The amount is not removed from the AllocationTracker that had been provided in the constructor.

        Returns:
        the amount of memory freed, in bytes.
      • newArray

        public static HugeAtomicLongArray newArray​(long size,
                                                   AllocationTracker tracker)
        Creates a new array of the given size, tracking the memory requirements into the given AllocationTracker. The tracker is no longer referenced, as the arrays do not dynamically change their size.
      • newArray

        public static HugeAtomicLongArray newArray​(long size,
                                                   PageFiller pageFiller,
                                                   AllocationTracker tracker)
        Creates a new array of the given size, tracking the memory requirements into the given AllocationTracker. The tracker is no longer referenced, as the arrays do not dynamically change their size. The values are pre-calculated according to the semantics of Arrays.setAll(long[], IntToLongFunction)
      • memoryEstimation

        public static long memoryEstimation​(long size)