Yoni Weisberg
Yoni Weisberg

Reputation: 1

In Kafka Streams KGroupedTable.aggregate, should the adder and subtractor return a new immutable aggregator on every call?

I want to aggregate KTable entries to a Map of Sets (e.g. Map<Set<String>>), so in the aggregate function I need to provide adder and subtractor, which look like this:

(key, value, map) -> {
  map.computeIfAbsent(key, k -> new HashSet<>()).add(value);
  return map;
},
(key, value, map) -> {
  map.computeIfAbsent(key, k -> new HashSet<>()).remove(value)
  return map;
}

My question is: Is it OK to return the map as is, or should the aggregator be an immutable object, which force me to deep clone the entire map of sets on every call?

Upvotes: 0

Views: 33

Answers (1)

Matthias J. Sax
Matthias J. Sax

Reputation: 62285

It should be ok to modify the passed in aggregate parameter (ie, your map). There should be no need to deep-copy it.

Upvotes: 0

Related Questions