This is true about `put`, but we still need to choose a key to insert into 
`sortedMap`. If I don't declare the `nextKey` variable, I need to have a bunch 
of redundant code in the if and else blocks:
```java
final TimeKey<Bytes> previousKey = index.get(key);
        // non-resetting semantics:
        // if there was a previous version of the same record,
        // then insert the new record in the same place in the priority queue
        if (previousKey == null) {
            final TimeKey<Bytes> nextKey = new TimeKey<>(time, key);

            index.put(key, nextKey);
            sortedMap.put(nextKey, value);

            minTimestamp = Math.min(minTimestamp, nextKey.time());
            memBufferSize =
                memBufferSize
                    + computeRecordSize(key, value);
        } else {
            final ContextualRecord<byte[]> removedValue = 
sortedMap.remove(previousKey);
            sortedMap.put(previousKey, value);
            memBufferSize =
                memBufferSize
                    + computeRecordSize(key, value)
                    - (removedValue == null ? 0 : computeRecordSize(key, 
removedValue));
        }
```

IMHO, this is less readable than the linear version where we just reuse or 
construct the key in line 103.

[ Full content available at: https://github.com/apache/kafka/pull/5693 ]
This message was relayed via gitbox.apache.org for [email protected]

Reply via email to