InMemoryTopics

class InMemoryTopics(partitions: Int = DEFAULT_PARTITIONS) : TopicProducer, PartitionedSource, TopicAdmin

Topics in memory, a TopicProducer and a PartitionedSource at once, for tests and samples, as the other in-memory backends: a topic is created, or created with partitions partitions when it is first written or read; a message goes to the partition it names, else to the one its key's hash picks (partitionOf), and every partition keeps what it was sent, from offset 0, up to its last retention messages, until retain drops its oldest messages as a broker's retention would.

failure, when set, makes every send throw it without writing anything, as a broker that does not acknowledge.

As a TopicAdmin, it describes the topics created so far and creates the ones asked for; grow adds partitions to a topic, as an operator does to a Kafka topic.

Constructors

Link copied to clipboard
constructor(partitions: Int = DEFAULT_PARTITIONS)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Thrown by every send while set.

Link copied to clipboard

The most messages a partition keeps, at least one (DEFAULT_RETENTION unless set): once a write takes a partition past it, the partition drops its oldest messages, as a broker's size retention drops them, and the topic says so once, the first time. A read from an offset no longer kept starts at the oldest one kept, the offset gap a reader handles as it does on a broker (PartitionedSource.read). So the topics stay bounded however long the node runs: they cannot keep a message until its readers have read it, since a source knows no reader's position, which each keeps in its own checkpoint.

Functions

Link copied to clipboard
open suspend override fun create(topics: Map<String, Int>)

fun create(topic: String, partitions: Int = this.partitions)

Creates topic with partitions partitions, unless it exists.

Link copied to clipboard
fun grow(topic: String, partitions: Int)

Gives topic partitions partitions, the new ones empty; a topic never has fewer than it had.

Link copied to clipboard

Every message topic holds, partition by partition, each in offset order.

Link copied to clipboard
open suspend override fun offsets(topic: String, partition: Int): PartitionOffsets
Link copied to clipboard
open suspend override fun partitionOf(topic: String, key: String): Int
Link copied to clipboard
open suspend override fun partitions(topic: String): List<Int>
open suspend override fun partitions(topics: Set<String>): Map<String, Int>
Link copied to clipboard
open override fun read(topic: String, partition: Int, from: Long): Flow<SourceMessage>
Link copied to clipboard
fun retain(topic: String, partition: Int, from: Long)

Drops the messages of partition of topic before from, as a broker's retention does: they are no longer read, and the partition's offsets start at from. Offsets do not move.

Link copied to clipboard
open suspend override fun send(topic: String, messages: List<TopicMessage>)