Class RangeCursor

java.lang.Object
me.ahoo.cosid.internal.RangeCursor
Direct Known Subclasses:
ExpiringRangeCursor

public class RangeCursor extends Object
The range a segment generator issues from (04-segment.md ยง2.1): one atomic increment and one comparison per value.

The counter keeps counting past end: a value at or past the end is never issued. A cursor is only ever incremented by the threads that read it while it was the generator's current cursor, so it passes its end by at most the number of such threads; of(long, long) keeps every end HEADROOM below Long.MAX_VALUE so the counter never wraps around into issuable values.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    Values within this distance of Long.MAX_VALUE are never issued (a gap, which D19 allows).
  • Constructor Summary

    Constructors
    Constructor
    Description
    RangeCursor(long next, long end)
    A cursor on [next, end), unchecked: also the empty and closed sentinels.
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    cap(long end)
    end capped HEADROOM below Long.MAX_VALUE.
    long
    end()
     
    of(long start, long end)
    A cursor on [start, end), its end capped HEADROOM below Long.MAX_VALUE.
    long
    remaining(long nowMillis)
    The values not taken yet.
    long
    Takes the next value.
    long
    The values not taken yet, ignoring expiry.
    boolean
    usable(long nowMillis)
    Whether a value is left at nowMillis.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HEADROOM

      public static final long HEADROOM
      Values within this distance of Long.MAX_VALUE are never issued (a gap, which D19 allows).
      See Also:
  • Constructor Details

    • RangeCursor

      public RangeCursor(long next, long end)
      A cursor on [next, end), unchecked: also the empty and closed sentinels.
      Parameters:
      next - the first value
      end - the end, exclusive
  • Method Details

    • of

      public static RangeCursor of(long start, long end)
      A cursor on [start, end), its end capped HEADROOM below Long.MAX_VALUE.
      Parameters:
      start - the first value
      end - the end, exclusive
      Returns:
      the cursor, or null if nothing of the range is left after the cap
    • cap

      public static long cap(long end)
      end capped HEADROOM below Long.MAX_VALUE.
      Parameters:
      end - the end of a range
      Returns:
      the end a cursor may issue up to
    • take

      public long take()
      Takes the next value.
      Returns:
      the value, or -1 if the cursor is used up (or expired)
    • untaken

      public long untaken()
      The values not taken yet, ignoring expiry.
      Returns:
      end - next, at least 0
    • end

      public long end()
    • remaining

      public long remaining(long nowMillis)
      The values not taken yet.
      Parameters:
      nowMillis - the current time
      Returns:
      the values left, 0 if none or expired
    • usable

      public boolean usable(long nowMillis)
      Whether a value is left at nowMillis.
      Parameters:
      nowMillis - the current time
      Returns:
      true if take() may still return a value