|
| 1 | +### Migration off `Fn` within `lib` |
| 2 | + |
| 3 | +This document tracks the progress of migrating away from the `Fn` class and its related infrastructure within the `lib` module. The goal is to replace `Fn` with more standard or efficient functional representations where applicable. |
| 4 | + |
| 5 | +#### Migration Instructions |
| 6 | + |
| 7 | +The goal of this migration is to replace the use of the `Fn` class with standard Kotlin functional interfaces (lambdas) in the `lib` module while maintaining serialization compatibility in the `exe` module. |
| 8 | + |
| 9 | +##### Step 1: Update the `lib` module |
| 10 | + |
| 11 | +Modify the classes in the `lib` module to use standard Kotlin functional interfaces instead of `Fn`. |
| 12 | + |
| 13 | +- Change constructor parameters and properties from `Fn<T, R>` to `(T) -> R` (or appropriate functional type). |
| 14 | +- Update the implementation to call the lambda directly instead of using `.apply()`. |
| 15 | +- Keep the `BeanParams` classes and other structures, but update their properties to use lambdas. |
| 16 | +- If the component needs to access parameters from the environment (e.g., multiplier in `changeAmplitude`), it should use `ExecutionScope`. |
| 17 | +- `ExecutionScope` should be added to the `BeanParams` class and passed to the lambda as a receiver: `ExecutionScope.(T) -> R`. |
| 18 | +- If the `BeanParams` had a custom serializer within the `lib` module, it should be moved or replaced by a more general approach, as lambdas cannot be directly serialized by `kotlinx.serialization` without extra help. |
| 19 | + |
| 20 | +Example with `ExecutionScope` (`MapStreamParams` in `io.wavebeans.lib.stream.MapStream`): |
| 21 | +```kotlin |
| 22 | +class MapStreamParams<T : Any, R : Any>( |
| 23 | + val scope: ExecutionScope, |
| 24 | + val transform: ExecutionScope.(T) -> R |
| 25 | +) : BeanParams |
| 26 | +``` |
| 27 | + |
| 28 | +Example (`InputParams` in `io.wavebeans.lib.io.FunctionInput`): |
| 29 | +```kotlin |
| 30 | +// Before |
| 31 | +class InputParams<T : Any>( |
| 32 | + val generator: Fn<Pair<Long, Float>, T?>, |
| 33 | + val sampleRate: Float? = null |
| 34 | +) : BeanParams |
| 35 | + |
| 36 | +// After |
| 37 | +class InputParams<T : Any>( |
| 38 | + val generator: (Long, Float) -> T?, |
| 39 | + val sampleRate: Float? = null |
| 40 | +) : BeanParams |
| 41 | +``` |
| 42 | + |
| 43 | +##### Step 2: Create a custom serializer in the `exe` module |
| 44 | + |
| 45 | +Since lambdas are not serializable, create a custom `KSerializer` in the `exe` module (typically under `io.wavebeans.execution.serializer`) that wraps the lambda into an `Fn` during serialization and unwraps it during deserialization. |
| 46 | + |
| 47 | +- The `serialize` method should use `io.wavebeans.lib.wrap()` to convert the lambda to an `Fn`. |
| 48 | +- If using `ExecutionScope`, ensure it is also serialized (using `ExecutionScope.serializer()`) and passed to `wrap()` if necessary, or handled in the lambda returned by `deserialize`. |
| 49 | +- The `deserialize` method should decode the `Fn` and then return a lambda that calls `fn.apply()`. |
| 50 | +- Use `FnSerializer` to handle the actual serialization/deserialization of the wrapped `Fn`. |
| 51 | + |
| 52 | +Example with `ExecutionScope` (`MapStreamParamsSerializer` in `io.wavebeans.execution.serializer`): |
| 53 | +```kotlin |
| 54 | +object MapStreamParamsSerializer : KSerializer<MapStreamParams<*, *>> { |
| 55 | + |
| 56 | + override val descriptor: SerialDescriptor = buildClassSerialDescriptor(MapStreamParams::class.className()) { |
| 57 | + element("scope", ExecutionScope.serializer().descriptor) |
| 58 | + element("transformFn", FnSerializer.descriptor) |
| 59 | + } |
| 60 | + |
| 61 | + override fun deserialize(decoder: Decoder): MapStreamParams<*, *> { |
| 62 | + return decoder.decodeStructure(descriptor) { |
| 63 | + lateinit var fn: Fn<Any, Any> |
| 64 | + lateinit var scope: ExecutionScope |
| 65 | + loop@ while (true) { |
| 66 | + when (val i = decodeElementIndex(descriptor)) { |
| 67 | + CompositeDecoder.DECODE_DONE -> break@loop |
| 68 | + 0 -> scope = decodeSerializableElement(descriptor, i, ExecutionScope.serializer()) |
| 69 | + 1 -> fn = decodeSerializableElement(descriptor, i, FnSerializer) as Fn<Any, Any> |
| 70 | + else -> throw SerializationException("Unknown index $i") |
| 71 | + } |
| 72 | + } |
| 73 | + MapStreamParams<Any, Any>(scope) { fn.apply(it) } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + override fun serialize(encoder: Encoder, value: MapStreamParams<*, *>) { |
| 78 | + encoder.encodeStructure(descriptor) { |
| 79 | + encodeSerializableElement(descriptor, 0, ExecutionScope.serializer(), value.scope) |
| 80 | + encodeSerializableElement(descriptor, 1, FnSerializer, wrap(value.transform)) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +Example (`InputParamsSerializer` in `io.wavebeans.execution.serializer`): |
| 87 | +```kotlin |
| 88 | +object InputParamsSerializer : KSerializer<InputParams<*>> { |
| 89 | + override val descriptor: SerialDescriptor = buildClassSerialDescriptor(InputParams::class.className()) { |
| 90 | + element("generateFn", FnSerializer.descriptor) |
| 91 | + element("sampleRate", Float.serializer().nullable.descriptor) |
| 92 | + } |
| 93 | + |
| 94 | + override fun deserialize(decoder: Decoder): InputParams<*> { |
| 95 | + return decoder.decodeStructure(descriptor) { |
| 96 | + var sampleRate: Float? = null |
| 97 | + lateinit var func: Fn<Pair<Long, Float>, Any?> |
| 98 | + loop@ while (true) { |
| 99 | + when (val i = decodeElementIndex(descriptor)) { |
| 100 | + CompositeDecoder.DECODE_DONE -> break@loop |
| 101 | + 0 -> func = decodeSerializableElement(descriptor, i, FnSerializer) as Fn<Pair<Long, Float>, Any?> |
| 102 | + 1 -> sampleRate = decodeNullableSerializableElement(descriptor, i, Float.serializer().nullable) |
| 103 | + else -> throw SerializationException("Unknown index $i") |
| 104 | + } |
| 105 | + } |
| 106 | + InputParams({ a, b -> func.apply(a to b) }, sampleRate) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + override fun serialize(encoder: Encoder, value: InputParams<*>) { |
| 111 | + encoder.encodeStructure(descriptor) { |
| 112 | + encodeSerializableElement(descriptor, 0, FnSerializer, wrap(value.generator)) |
| 113 | + encodeNullableSerializableElement(descriptor, 1, Float.serializer().nullable, value.sampleRate) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +##### Step 3: Register the serializer in `SerializationUtils.kt` |
| 120 | + |
| 121 | +Update `io.wavebeans.execution.SerializationUtils.kt` to register the new serializer in the `beanParams()` method. This ensures that when a `BeanParams` is encountered during topology serialization, it uses your custom serializer. |
| 122 | + |
| 123 | +```kotlin |
| 124 | +fun SerializersModuleBuilder.beanParams() { |
| 125 | + polymorphic(BeanParams::class) { |
| 126 | + // ... |
| 127 | + subclass(InputParams::class, InputParamsSerializer) |
| 128 | + // ... |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +#### Technical Debt |
| 134 | + |
| 135 | +The following items are temporary measures introduced during the migration and should be resolved once the migration is complete: |
| 136 | + |
| 137 | +- [ ] Migrate `sincResampleFunc` and `SincResampleFn` to use lambdas instead of `Fn`. |
| 138 | +- [ ] Migrate `SimpleResampleFn` to use lambdas instead of `Fn`. |
| 139 | + |
| 140 | +#### Classes to Migrate |
| 141 | + |
| 142 | +- [ ] `io.wavebeans.lib.stream.SincResampleFn` |
| 143 | +- [x] `io.wavebeans.lib.io.CsvStreamOutput` |
| 144 | +- [x] `io.wavebeans.lib.io.CsvStreamOutputParams` |
| 145 | +- [x] `io.wavebeans.lib.io.CsvPartialStreamOutput` |
| 146 | +- [x] `io.wavebeans.lib.stream.window.MapWindowFn` |
| 147 | +- [x] `io.wavebeans.lib.stream.ResampleStreamParams` |
| 148 | +- [x] `io.wavebeans.lib.stream.ResampleBeanStream` |
| 149 | +- [x] `io.wavebeans.lib.stream.ResampleFiniteStream` |
| 150 | +- [x] `io.wavebeans.lib.stream.AbstractResampleStream` |
| 151 | +- [x] `io.wavebeans.lib.io.InputParams` (in `io.wavebeans.lib.io.FunctionInput`) |
| 152 | +- [x] `io.wavebeans.lib.io.Input` (in `io.wavebeans.lib.io.FunctionInput`) |
| 153 | +- [x] `io.wavebeans.lib.io.FunctionStreamOutput` |
| 154 | +- [x] `io.wavebeans.lib.io.FunctionStreamOutputParams` |
| 155 | +- [x] `io.wavebeans.lib.stream.FlattenStreamsParams` (in `io.wavebeans.lib.stream.FlattenStream`) |
| 156 | +- [x] `io.wavebeans.lib.stream.FlattenStream` |
| 157 | +- [x] `io.wavebeans.lib.stream.FlattenWindowStreamsParams` (in `io.wavebeans.lib.stream.FlattenWindowStream`) |
| 158 | +- [x] `io.wavebeans.lib.stream.FlattenWindowStream` |
| 159 | +- [x] `io.wavebeans.lib.stream.FunctionMergedStreamParams` |
| 160 | +- [x] `io.wavebeans.lib.stream.FunctionMergedStream` |
| 161 | +- [x] Identify areas for `ExecutionScope` documentation. |
| 162 | +- [x] Update `docs/user/api/functions.md` with `ExecutionScope` and `ScopeParameters`. |
| 163 | +- [x] Update operation-specific docs (`map`, `merge`, `input`, `out`) with `ExecutionScope` examples. |
| 164 | +- [x] Update `distributed-execution.md` with technical details of `ExecutionScope` serialization. |
| 165 | +- [x] Update `docs/user/api/readme.md` with `ExecutionScope` as a key concept. |
0 commit comments