Skip to content

Commit 7253786

Browse files
committed
Add more kdoc
1 parent 5d9868d commit 7253786

File tree

7 files changed

+100
-0
lines changed

7 files changed

+100
-0
lines changed

util/api/datasourcex-util.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ public final class com/caplin/integration/datasourcex/util/flow/FlowMapKt {
191191
public abstract interface class com/caplin/integration/datasourcex/util/flow/FlowMapStreamEvent {
192192
}
193193

194+
public final class com/caplin/integration/datasourcex/util/flow/FlowMapStreamEvent$Cleared : com/caplin/integration/datasourcex/util/flow/FlowMapStreamEvent {
195+
public static final field INSTANCE Lcom/caplin/integration/datasourcex/util/flow/FlowMapStreamEvent$Cleared;
196+
public fun toString ()Ljava/lang/String;
197+
}
198+
194199
public final class com/caplin/integration/datasourcex/util/flow/FlowMapStreamEvent$EventUpdate : com/caplin/integration/datasourcex/util/flow/FlowMapStreamEvent {
195200
public static final synthetic fun box-impl (Lcom/caplin/integration/datasourcex/util/flow/MapEvent$EntryEvent;)Lcom/caplin/integration/datasourcex/util/flow/FlowMapStreamEvent$EventUpdate;
196201
public static fun constructor-impl (Lcom/caplin/integration/datasourcex/util/flow/MapEvent$EntryEvent;)Lcom/caplin/integration/datasourcex/util/flow/MapEvent$EntryEvent;

util/src/main/kotlin/com/caplin/integration/datasourcex/util/AntPatternNamespace.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class AntPatternNamespace(pattern: String) : Namespace {
3131
}
3232
}
3333

34+
/**
35+
* Represents a mapping between a from-pattern and a to-pattern, used for injecting user-specific
36+
* information into subjects requested by Liberator.
37+
*
38+
* @property fromPattern The pattern used to match the incoming subject.
39+
* @property toPattern The pattern used to map to the destination subject.
40+
*/
3441
class ObjectMap(val fromPattern: String, val toPattern: String) {
3542
operator fun component1(): String = fromPattern
3643

@@ -59,12 +66,20 @@ class AntPatternNamespace(pattern: String) : Namespace {
5966
}
6067
}
6168

69+
/** The Ant-style path pattern used by this namespace. */
6270
val pattern = pattern.removeSuffix("/")
6371

6472
private val matcher = AntRegexPathMatcher(pattern)
6573

6674
override fun match(subject: String): Boolean = matcher.regex.matchEntire(subject) != null
6775

76+
/**
77+
* Extracts path variables from a matching subject.
78+
*
79+
* @param subject The subject to extract variables from. Must match the [pattern].
80+
* @return A map of path variable names to their extracted values.
81+
* @throws IllegalStateException If the subject does not match the pattern.
82+
*/
6883
fun extractPathVariables(subject: String): Map<String, String> {
6984
val groups =
7085
checkNotNull(matcher.regex.matchEntire(subject)) {

util/src/main/kotlin/com/caplin/integration/datasourcex/util/KLogger.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,71 @@ import org.slf4j.LoggerFactory
1010
@JvmInline
1111
value class KLogger(private val logger: Logger) {
1212

13+
/** Logs an error message lazily evaluated from [message] if error logging is enabled. */
1314
fun error(message: () -> Any?) {
1415
if (logger.isErrorEnabled) logger.error(message().toString())
1516
}
1617

18+
/** Logs a warning message lazily evaluated from [message] if warn logging is enabled. */
1719
fun warn(message: () -> Any?) {
1820
if (logger.isWarnEnabled) logger.warn(message().toString())
1921
}
2022

23+
/** Logs an info message lazily evaluated from [message] if info logging is enabled. */
2124
fun info(message: () -> Any?) {
2225
if (logger.isInfoEnabled) logger.info(message().toString())
2326
}
2427

28+
/** Logs a debug message lazily evaluated from [message] if debug logging is enabled. */
2529
fun debug(message: () -> Any?) {
2630
if (logger.isDebugEnabled) logger.debug(message().toString())
2731
}
2832

33+
/** Logs a trace message lazily evaluated from [message] if trace logging is enabled. */
2934
fun trace(message: () -> Any?) {
3035
if (logger.isTraceEnabled) logger.trace(message().toString())
3136
}
3237

38+
/**
39+
* Logs an error message with a [throwable], lazily evaluated from [message], if error logging is
40+
* enabled.
41+
*/
3342
fun error(throwable: Throwable?, message: () -> Any?) {
3443
if (logger.isErrorEnabled) logger.error(message().toString(), throwable)
3544
}
3645

46+
/**
47+
* Logs a warning message with a [throwable], lazily evaluated from [message], if warn logging is
48+
* enabled.
49+
*/
3750
fun warn(throwable: Throwable?, message: () -> Any?) {
3851
if (logger.isWarnEnabled) logger.warn(message().toString(), throwable)
3952
}
4053

54+
/**
55+
* Logs an info message with a [throwable], lazily evaluated from [message], if info logging is
56+
* enabled.
57+
*/
4158
fun info(throwable: Throwable?, message: () -> Any?) {
4259
if (logger.isInfoEnabled) logger.info(message().toString(), throwable)
4360
}
4461

62+
/**
63+
* Logs a debug message with a [throwable], lazily evaluated from [message], if debug logging is
64+
* enabled.
65+
*/
4566
fun debug(throwable: Throwable?, message: () -> Any?) {
4667
if (logger.isDebugEnabled) logger.debug(message().toString(), throwable)
4768
}
4869

70+
/**
71+
* Logs a trace message with a [throwable], lazily evaluated from [message], if trace logging is
72+
* enabled.
73+
*/
4974
fun trace(throwable: Throwable?, message: () -> Any?) {
5075
if (logger.isTraceEnabled) logger.trace(message().toString(), throwable)
5176
}
5277
}
5378

79+
/** Returns a [KLogger] instance for the specified class [T]. */
5480
inline fun <reified T : Any> getLogger(): KLogger = KLogger(LoggerFactory.getLogger(T::class.java))

util/src/main/kotlin/com/caplin/integration/datasourcex/util/ReadWriteLock.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ class ReadWriteLock {
1111
private val readerMutex = Mutex()
1212
private val writerMutex = Mutex()
1313

14+
/**
15+
* Executes the given [block] of code within a write lock. Suspends until the write lock can be
16+
* acquired.
17+
*/
1418
suspend fun <R> withWriteLock(block: suspend () -> R): R = writerMutex.withLock(null) { block() }
1519

20+
/**
21+
* Executes the given [block] of code within a read lock. Suspends until the read lock can be
22+
* acquired.
23+
*/
1624
suspend fun <R> withReadLock(block: suspend () -> R): R =
1725
withContext(NonCancellable) {
1826
readLock()
@@ -23,6 +31,7 @@ class ReadWriteLock {
2331
}
2432
}
2533

34+
/** Acquires a read lock. Suspends if a write lock is currently held. */
2635
suspend fun readLock() =
2736
withContext(NonCancellable) {
2837
readerMutex.withLock {
@@ -31,6 +40,7 @@ class ReadWriteLock {
3140
}
3241
}
3342

43+
/** Releases a previously acquired read lock. */
3444
suspend fun readUnlock() =
3545
withContext(NonCancellable) {
3646
readerMutex.withLock {
@@ -39,7 +49,9 @@ class ReadWriteLock {
3949
}
4050
}
4151

52+
/** Acquires a write lock. Suspends if any read or write locks are currently held. */
4253
suspend fun writeLock() = writerMutex.lock()
4354

55+
/** Releases a previously acquired write lock. */
4456
fun writeUnlock() = writerMutex.unlock()
4557
}

util/src/main/kotlin/com/caplin/integration/datasourcex/util/SimpleDataSourceConfig.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ package com.caplin.integration.datasourcex.util
33
import java.nio.file.Path
44
import java.util.UUID
55

6+
/** Configuration for creating a DataSource via [SimpleDataSourceFactory]. */
67
sealed interface SimpleDataSourceConfig {
8+
/** The directory where log files will be written. */
79
val logDirectory: Path?
10+
/** The name of the DataSource. */
811
val name: String
12+
/** The local label for the DataSource. */
913
val localLabel: String
14+
/** Any extra configuration to append to the configuration string. */
1015
val extraConfig: String?
1116

17+
/** Configuration for a DataSource that connects to a discovery service. */
1218
class Discovery(
19+
/** The hostname of the discovery service. */
1320
val hostname: String,
21+
/** The cluster name to join. */
1422
val clusterName: String = "caplin",
1523
override val name: String,
1624
override val logDirectory: Path?,
@@ -23,14 +31,19 @@ sealed interface SimpleDataSourceConfig {
2331
}
2432
}
2533

34+
/** Configuration for a DataSource that connects to specific peers. */
2635
class Peer(
2736
override val name: String,
2837
override val logDirectory: Path? = null,
2938
override val localLabel: String = "$name-${UUID.randomUUID()}",
3039
override val extraConfig: String? = null,
40+
/** Optional configuration for accepting incoming connections. */
3141
val incoming: Incoming? = null,
42+
/** List of outgoing peer connections. */
3243
val outgoing: List<Outgoing> = emptyList(),
44+
/** List of services required before this DataSource becomes active. */
3345
val requiredServices: List<String> = emptyList(),
46+
/** Whether to override development mode checks. */
3447
val devOverride: Boolean = false,
3548
) : SimpleDataSourceConfig {
3649

@@ -40,12 +53,14 @@ sealed interface SimpleDataSourceConfig {
4053
}
4154
}
4255

56+
/** Configuration for an outgoing peer connection. */
4357
class Outgoing(val hostname: String, val port: Int, val isWebsocket: Boolean) {
4458
override fun toString(): String {
4559
return "Outgoing(hostname='$hostname', port=$port, isWebsocket=$isWebsocket)"
4660
}
4761
}
4862

63+
/** Configuration for accepting incoming connections. */
4964
class Incoming(val port: Int, val isWebsocket: Boolean) {
5065
override fun toString(): String {
5166
return "Incoming(port=$port, isWebsocket=$isWebsocket)"

util/src/main/kotlin/com/caplin/integration/datasourcex/util/SimpleDataSourceFactory.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
1010
import java.nio.file.Files
1111
import java.util.logging.Logger
1212

13+
/**
14+
* A factory for creating [DataSource] instances from simplified configurations. Allows easy setup
15+
* for tests and examples.
16+
*/
1317
object SimpleDataSourceFactory {
1418

1519
private const val MAX_PATH_LENGTH = 32
1620

1721
private val logger = getLogger<SimpleDataSourceFactory>()
1822

23+
/**
24+
* The default [ObjectMapper] used for serializing and deserializing JSON payloads. It is
25+
* pre-configured with the JavaTime module and DataSource serialization extensions.
26+
*/
1927
val defaultObjectMapper: ObjectMapper =
2028
jacksonObjectMapper()
2129
.configure(WRITE_DATES_AS_TIMESTAMPS, false)

util/src/main/kotlin/com/caplin/integration/datasourcex/util/flow/SetEvent.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@ import kotlinx.coroutines.flow.launchIn
1515
import kotlinx.coroutines.flow.onCompletion
1616
import kotlinx.coroutines.flow.onEach
1717

18+
/** Events representing a mutation to a [Set]. */
1819
sealed interface SetEvent<out V : Any> {
1920

21+
/**
22+
* Indicates that a consistent view of the set has been emitted and only updates will be seen from
23+
* now on.
24+
*/
2025
object Populated : SetEvent<Nothing> {
2126
override fun toString(): String {
2227
return "Populated()"
2328
}
2429
}
2530

31+
/** Mutation event for a specific entry. */
2632
sealed interface EntryEvent<out V : Any> : SetEvent<V> {
2733
val value: V
2834

2935
operator fun component1(): V = value
3036

37+
/** An event indicating a value has been inserted into the set. */
3138
class Insert<out V : Any>(override val value: V) : EntryEvent<V> {
3239

3340
override fun equals(other: Any?): Boolean {
@@ -48,6 +55,7 @@ sealed interface SetEvent<out V : Any> {
4855
}
4956
}
5057

58+
/** An event indicating a value has been removed from the set. */
5159
class Removed<out V : Any>(override val value: V) : EntryEvent<V> {
5260

5361
override fun equals(other: Any?): Boolean {
@@ -148,11 +156,22 @@ fun <V : Any> Flow<SetEvent<V>>.runningFoldToSet(
148156
}
149157
}
150158

159+
/**
160+
* Transforms a flow of sets into a merged flow by applying [entryEventTransformer] to each entry
161+
* event (insert or remove). When a value is inserted, a new flow is created and merged. When a
162+
* value is removed, the corresponding flow is cancelled.
163+
*/
151164
@JvmName("flatMapLatestAndMergeSet")
152165
fun <V : Any, R> Flow<Set<V>>.flatMapLatestAndMerge(
153166
entryEventTransformer: (EntryEvent<V>) -> Flow<R>
154167
): Flow<R> = toEvents().flatMapLatestAndMerge(entryEventTransformer)
155168

169+
/**
170+
* Transforms a flow of [SetEvent] into a merged flow by applying [entryEventTransformer] to each
171+
* entry event. When an [Insert] event is received, a new flow is created and its emissions are
172+
* merged into the resulting flow. When a [Removed] event is received, the previously created flow
173+
* for that value is cancelled.
174+
*/
156175
fun <V : Any, R> Flow<SetEvent<V>>.flatMapLatestAndMerge(
157176
entryEventTransformer: (EntryEvent<V>) -> Flow<R>
158177
) = channelFlow {

0 commit comments

Comments
 (0)