nicktelford commented on code in PR #22652: URL: https://github.com/apache/kafka/pull/22652#discussion_r3473335616
########## streams/src/main/java/org/apache/kafka/streams/state/internals/InMemorySessionTransactionBuffer.java: ########## @@ -0,0 +1,392 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.state.internals; + +import org.apache.kafka.common.utils.Bytes; +import org.apache.kafka.streams.KeyValue; +import org.apache.kafka.streams.kstream.Windowed; + +import java.util.Iterator; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ConcurrentNavigableMap; +import java.util.concurrent.ConcurrentSkipListMap; + +/** + * A {@link TransactionBuffer} implementation for {@link InMemorySessionStore}. + * Uses a composite key of (endTime, key, startTime) to maintain correct sort order in the staging map. + */ +class InMemorySessionTransactionBuffer extends AbstractTransactionBuffer<InMemorySessionTransactionBuffer.SessionEntryKey> { + + private final ConcurrentNavigableMap<Long, ConcurrentNavigableMap<Bytes, ConcurrentNavigableMap<Long, byte[]>>> endTimeMap; + + InMemorySessionTransactionBuffer( + final ConcurrentNavigableMap<Long, ConcurrentNavigableMap<Bytes, ConcurrentNavigableMap<Long, byte[]>>> endTimeMap) { + this.endTimeMap = endTimeMap; + } + + /** + * Composite key for the session store staging map. Sorts by endTime, then key, then startTime. + */ + static final class SessionEntryKey implements Comparable<SessionEntryKey> { + private final long endTime; + private final Bytes key; + private final long startTime; + + SessionEntryKey(final long endTime, final Bytes key, final long startTime) { + this.endTime = endTime; + this.key = key; + this.startTime = startTime; + } + + long endTime() { + return endTime; + } + + Bytes key() { + return key; + } + + long startTime() { + return startTime; + } + + @Override + public int compareTo(final SessionEntryKey other) { + int cmp = Long.compare(this.endTime, other.endTime); + if (cmp != 0) { + return cmp; + } + cmp = this.key.compareTo(other.key); + if (cmp != 0) { + return cmp; + } + return Long.compare(this.startTime, other.startTime); + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof SessionEntryKey)) return false; + final SessionEntryKey that = (SessionEntryKey) o; + return endTime == that.endTime && startTime == that.startTime && Objects.equals(key, that.key); + } + + @Override + public int hashCode() { + return Objects.hash(endTime, key, startTime); + } + } + + // -- Convenience methods for the store -- + + void stage(final Windowed<Bytes> sessionKey, final byte[] value) { + super.stage(new SessionEntryKey(sessionKey.window().end(), sessionKey.key(), sessionKey.window().start()), value); + } + + Optional<byte[]> get(final Bytes key, final long startTime, final long endTime) { + return super.get(new SessionEntryKey(endTime, key, startTime)); + } + + // -- AbstractTransactionBuffer implementation -- + + @Override + int estimateKeySize(final SessionEntryKey key) { + return 2 * Long.BYTES + key.key().get().length; Review Comment: @bbejeck `SessionEntryKey` contains 3 fields: 1. `Bytes key` 2. `long startTime` 3. `long endTime` This expression reads as: `(2 * Long.BYTES) + key.length`, because there are 2 `Long` fields. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
