Skip to content

Commit ea31b9b

Browse files
committed
Add singleTopic in CommittableOffsetBatch
1 parent 7b8f807 commit ea31b9b

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

modules/core/src/main/scala/fs2/kafka/CommittableOffset.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,7 @@ object CommittableOffset {
110110
Map(_topicPartition -> _offsetAndMetadata)
111111

112112
override def batch: CommittableOffsetBatch[F] =
113-
CommittableOffsetBatch.one(
114-
_topicPartition,
115-
_offsetAndMetadata,
116-
consumerGroupId,
117-
_commit
118-
)
113+
CommittableOffsetBatch.one(this)
119114

120115
override def commit: F[Unit] =
121116
_commit(offsets)

modules/core/src/main/scala/fs2/kafka/CommittableOffsetBatch.scala

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ sealed abstract class CommittableOffsetBatch[F[_]] {
101101
}
102102

103103
object CommittableOffsetBatch {
104-
private[kafka] def apply[F[_]](
104+
105+
private[kafka] def ofMultiTopic[F[_]](
105106
offsets: Map[TopicPartition, OffsetAndMetadata],
106107
consumerGroupIds: Set[String],
107108
consumerGroupIdsMissing: Boolean,
@@ -114,15 +115,15 @@ object CommittableOffsetBatch {
114115

115116
new CommittableOffsetBatch[F] {
116117
override def updated(that: CommittableOffset[F]): CommittableOffsetBatch[F] =
117-
CommittableOffsetBatch(
118+
CommittableOffsetBatch.ofMultiTopic(
118119
_offsets.updated(that.topicPartition, that.offsetAndMetadata),
119120
that.consumerGroupId.fold(_consumerGroupIds)(_consumerGroupIds + _),
120121
_consumerGroupIdsMissing || that.consumerGroupId.isEmpty,
121122
_commitMap
122123
)
123124

124125
override def updated(that: CommittableOffsetBatch[F]): CommittableOffsetBatch[F] =
125-
CommittableOffsetBatch(
126+
CommittableOffsetBatch.ofMultiTopic(
126127
_offsets ++ that.offsets,
127128
_consumerGroupIds ++ that.consumerGroupIds,
128129
_consumerGroupIdsMissing || that.consumerGroupIdsMissing,
@@ -140,7 +141,7 @@ object CommittableOffsetBatch {
140141

141142
override def commit: F[Unit] =
142143
if (_consumerGroupIdsMissing)
143-
F.raiseError(ConsumerGroupException(consumerGroupIds))
144+
ApplicativeThrow[F].raiseError(ConsumerGroupException(consumerGroupIds))
144145
else {
145146
offsetsByTopic
146147
.map {
@@ -149,7 +150,7 @@ object CommittableOffsetBatch {
149150
.getOrElse[Map[TopicPartition, OffsetAndMetadata] => F[Unit]](
150151
topicName,
151152
_ =>
152-
F.raiseError(
153+
ApplicativeThrow[F].raiseError(
153154
new RuntimeException(s"Cannot perform commit for topic: $topicName")
154155
)
155156
)
@@ -164,17 +165,43 @@ object CommittableOffsetBatch {
164165
}
165166
}
166167

167-
def one[F[_]: ApplicativeThrow](
168-
topicPartition: TopicPartition,
169-
offsetAndMetadata: OffsetAndMetadata,
170-
consumerGroupId: Option[String],
168+
@deprecated("Use CommittableOffsetBatch.apply with commitMap instead.", since = "2.5.1")
169+
private[kafka] def apply[F[_]](
170+
offsets: Map[TopicPartition, OffsetAndMetadata],
171+
consumerGroupIds: Set[String],
172+
consumerGroupIdsMissing: Boolean,
171173
commit: Map[TopicPartition, OffsetAndMetadata] => F[Unit]
174+
)(implicit F: ApplicativeError[F, Throwable]): CommittableOffsetBatch[F] =
175+
ofMultiTopic[F](
176+
offsets,
177+
consumerGroupIds,
178+
consumerGroupIdsMissing,
179+
offsets.headOption
180+
.map(_._1.topic())
181+
.map(topicName => Map(topicName -> commit))
182+
.getOrElse(Map.empty)
183+
)
184+
185+
/**
186+
* A [[CommittableOffsetBatch]] which does include only one offset for a single topic.
187+
*
188+
* @tparam F effect type to use to perform the commit effect
189+
* @return A [[CommittableOffsetBatch]] which does include only one offset for a single topic.
190+
*
191+
* @see [[CommittableOffsetBatch#fromFoldable]]
192+
* @see [[CommittableOffsetBatch#fromFoldableOption]]
193+
*/
194+
def one[F[_]: ApplicativeThrow](
195+
committableOffset: CommittableOffset[F]
172196
): CommittableOffsetBatch[F] =
173-
CommittableOffsetBatch(
174-
Map(topicPartition -> offsetAndMetadata),
175-
consumerGroupId.toSet,
176-
consumerGroupId.isEmpty,
177-
Map(topicPartition.topic() -> commit)
197+
CommittableOffsetBatch.ofMultiTopic[F](
198+
Map(committableOffset.topicPartition -> committableOffset.offsetAndMetadata),
199+
committableOffset.consumerGroupId.toSet,
200+
committableOffset.consumerGroupId.isEmpty,
201+
Map(
202+
committableOffset.topicPartition
203+
.topic() -> Map(committableOffset.topicPartition.topic() -> committableOffset.commit)
204+
)
178205
)
179206

180207
/**
@@ -238,7 +265,7 @@ object CommittableOffsetBatch {
238265
}
239266
}
240267

241-
CommittableOffsetBatch(
268+
CommittableOffsetBatch.ofMultiTopic(
242269
offsetsMap,
243270
consumerGroupIds,
244271
consumerGroupIdsMissing,
@@ -290,7 +317,7 @@ object CommittableOffsetBatch {
290317
if (offsets.isEmpty || offsets.exists(_.isEmpty))
291318
CommittableOffsetBatch.empty[F]
292319
else
293-
CommittableOffsetBatch(
320+
CommittableOffsetBatch.ofMultiTopic(
294321
offsetsMap,
295322
consumerGroupIds,
296323
consumerGroupIdsMissing,

0 commit comments

Comments
 (0)