guozhangwang commented on a change in pull request #11802: URL: https://github.com/apache/kafka/pull/11802#discussion_r823215870
########## File path: streams/src/main/java/org/apache/kafka/streams/state/internals/PrefixedWindowKeySchemas.java ########## @@ -0,0 +1,405 @@ +/* + * 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 java.util.Arrays; +import org.apache.kafka.common.serialization.Deserializer; +import org.apache.kafka.common.utils.Bytes; +import org.apache.kafka.streams.kstream.Window; +import org.apache.kafka.streams.kstream.Windowed; + +import java.nio.ByteBuffer; +import java.util.List; +import org.apache.kafka.streams.state.StateSerdes; + +import static org.apache.kafka.streams.state.StateSerdes.TIMESTAMP_SIZE; +import static org.apache.kafka.streams.state.internals.WindowKeySchema.timeWindowForSize; + +public class PrefixedWindowKeySchemas { + + private static final int PREFIX_SIZE = 1; + private static final byte TIME_FIRST_PREFIX = 0; + private static final byte KEY_FIRST_PREFIX = 1; + private static final int SEQNUM_SIZE = 4; + private static final int SUFFIX_SIZE = TIMESTAMP_SIZE + SEQNUM_SIZE; + + private static byte extractPrefix(final byte[] binaryBytes) { + return binaryBytes[0]; + } + + public static class TimeFirstWindowKeySchema implements RocksDBSegmentedBytesStore.KeySchema { + + @Override + public Bytes upperRange(final Bytes key, final long to) { + if (key == null) { + // Put next prefix instead of null so that we can start from right prefix + // when scanning backwards + final byte nextPrefix = TIME_FIRST_PREFIX + 1; + return Bytes.wrap(ByteBuffer.allocate(PREFIX_SIZE).put(nextPrefix).array()); + } + byte[] maxKey = new byte[key.get().length]; + Arrays.fill(maxKey, (byte) 0xFF); + return Bytes.wrap(ByteBuffer.allocate(PREFIX_SIZE + TIMESTAMP_SIZE + maxKey.length + SEQNUM_SIZE) + .put(TIME_FIRST_PREFIX) + .putLong(to) + .put(maxKey).putInt(Integer.MAX_VALUE) + .array()); + } + + @Override + public Bytes lowerRange(final Bytes key, final long from) { + if (key == null) { + return Bytes.wrap(ByteBuffer.allocate(PREFIX_SIZE + TIMESTAMP_SIZE) + .put(TIME_FIRST_PREFIX) + .putLong(from) + .array()); + } + + /* + * Larger timestamp or key's byte order can't be smaller than this lower range. Reason: + * 1. Timestamp is fixed length (with big endian byte order). Since we put timestamp + * first, larger timestamp will have larger byte order. + * 2. If timestamp is the same but key (k1) is larger than this lower range key (k2): + * a. If k2 is not a prefix of k1, then k1 will always have larger byte order no + * matter what seqnum k2 has + * b. If k2 is a prefix of k1, since k2's seqnum is 0, after k1 appends seqnum, + * it will always be larger than (k1 + seqnum). + */ + return Bytes.wrap(ByteBuffer.allocate(PREFIX_SIZE + TIMESTAMP_SIZE + key.get().length) + .put(TIME_FIRST_PREFIX) + .putLong(from) + .put(key.get()) + .array()); + } + + @Override + public Bytes lowerRangeFixedSize(final Bytes key, final long from) { + return TimeFirstWindowKeySchema.toStoreKeyBinary(key, Math.max(0, from), + 0); + } + + @Override + public Bytes upperRangeFixedSize(final Bytes key, final long to) { + return TimeFirstWindowKeySchema.toStoreKeyBinary(key, to, Integer.MAX_VALUE); + } + + @Override + public long segmentTimestamp(final Bytes key) { + return TimeFirstWindowKeySchema.extractStoreTimestamp(key.get()); + } + + @Override + public HasNextCondition hasNextCondition(final Bytes binaryKeyFrom, + final Bytes binaryKeyTo, final long from, final long to, boolean forward) { + return iterator -> { + while (iterator.hasNext()) { + final Bytes bytes = iterator.peekNextKey(); + final byte prefix = extractPrefix(bytes.get()); + + if (prefix != TIME_FIRST_PREFIX) { + return false; + } + + final long time = TimeFirstWindowKeySchema.extractStoreTimestamp(bytes.get()); + + // We can return false directly here since keys are sorted by time and if + // we get time larger than `to`, there won't be time within range. + if (forward && time > to) { + return false; + } + if (!forward && time < from) { + return false; + } + + final Bytes keyBytes = Bytes.wrap( + TimeFirstWindowKeySchema.extractStoreKeyBytes(bytes.get())); + if ((binaryKeyFrom == null || keyBytes.compareTo(binaryKeyFrom) >= 0) + && (binaryKeyTo == null || keyBytes.compareTo(binaryKeyTo) <= 0) + && time >= from && time <= to) { + return true; + } + iterator.next(); + } + return false; + }; + } + + @Override + public HasNextCondition hasNextCondition(final Bytes binaryKeyFrom, Review comment: I see; so it seems to me that the `forward` overloaded function is only different with the other one that its implementation has an early stop optimization at https://github.com/apache/kafka/pull/11802/files/496517aa8140a7125da117159cf28f181fb1ad34#diff-1efce76341bb9f0793d5a3fc1fdd05a016fc31db788da8d0597142fa551c4babR121-R126 while all others are the same, is that right? -- 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: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org