Options
All
  • Public
  • Public/Protected
  • All
Menu

Class RecoveryBuffer

Port of the Java RecoveryBuffer implementation, with a bounded number of entries.

Timestamps are recorded in a timestamp index. The timestamp index also has a fixed upper size bound.

This implementation expects timestamp parameters in milliseconds. It rounds timestamps to reduce the required index size and the number of index updates.

The recovery buffer is implemented as a bounded circular buffer of (timestamp, messages index) pairs. Each entry states that messages with earlier indexes than its index were added at its timestamp or earlier. This serves to timestamp ranges of .messages, allowing us to flush stale entries.

Each call to markTime adds an entry, possibly evicting messages referred to by an older entry for which there is no longer room.

Some invariants:

  • Valid entries have a pair of values at the same index in the timestamps & indicies arrays. The index is in the range [timesHead, timesTail).
  • indices[timesHead] == -1 <=> the timestamp index is empty.
  • timesHead == timesTail <=> either the timestamp index is empty, or indices[timesHead] >= 0 and the timestamp index isfull.
  • size == 0 => the timestamp index is empty.
  • For each valid entry at index i, indices[i] >= 0 and is either equal to tail or points to a non-null entry in messages.
  • No entry points to the head of messages, i.e. the message at tail - size.
  • Later entries have a messages index that is at least as great (modulo messages.length) as earlier entries.
author

Peter Hughes

since

5.8

Hierarchy

  • RecoveryBuffer

Index

Constructors

constructor

  • new RecoveryBuffer(minimumMessages: number, minimumTimeIndex: number): RecoveryBuffer
  • Create a RecoveryBuffer

    Parameters

    • minimumMessages: number

      the minimum number of messages

    • minimumTimeIndex: number

      the minimum number of time indices

    Returns RecoveryBuffer

Properties

Private indexCapacity

indexCapacity: number

The size of the indices and times arrays

Private indexMask

indexMask: (p: number) => number

A utility function that limits a position to an index in the circular indices array.

Type declaration

    • (p: number): number
    • Parameters

      • p: number

      Returns number

Private indices

indices: any[]

An array containing indices

Private messages

messages: Message[]

An array containing messages

Private messagesLength

messagesLength: number

The size of the messages array

Private messagesMask

messagesMask: (p: number) => number

A utility function that limits a position to an index in the circular messages array.

Type declaration

    • (p: number): number
    • Parameters

      • p: number

      Returns number

Private messagesSize

messagesSize: number

Index of the head message in the messages circular buffer array.

Private messagesTail

messagesTail: number

Index of the tail message in the messages circular buffer array.

Private times

times: any[]

An array containing time stamps

Private timesHead

timesHead: number

Index of the head timestamp in the times circular buffer array.

Private timesTail

timesTail: number

Index of the tail timestamp in the times circular buffer array.

Methods

clear

  • clear(): void
  • Clears this buffer

    Returns void

flush

  • flush(timestamp: number): void
  • Remove all elements added at or before the timestamp.

    Parameters

    • timestamp: number

      the timestamp. Follows the same properties as used in RecoveryBuffer#markTime

    Returns void

markTime

  • markTime(timestamp: number): void
  • Inform this recovery buffer of the current timestamp. Subsequent messages put in the buffer are recorded as older than this timestamp.

    Parameters

    • timestamp: number

    Returns void

put

  • Add a message to the buffer. If the buffer is full, the oldest message is discarded

    Parameters

    Returns void

recover

  • recover(n: number, consumer: (m: Message) => void): boolean
  • If there are at least N messages in the buffer, pass the N most recent messages to the consumer function and return true. Otherwise return false.

    This is a non-destructive operation.

    Parameters

    • n: number

      the number of messages to recover

    • consumer: (m: Message) => void

      the consumer function to be called with recovered messages (in order)

    Returns boolean

    false if there are not N messages, in which case consumer will not be called.

removeElements

  • removeElements(newElementsHead: number): void
  • Remove elements from the buffer

    Parameters

    • newElementsHead: number

    Returns void

size

  • size(): number
  • Get the number of messages

    Returns number

    the number of messages that can be recovered

Copyright © 2025 DiffusionData Ltd. All Rights Reserved.