maedhroz commented on code in PR #2267: URL: https://github.com/apache/cassandra/pull/2267#discussion_r1180769892
########## src/java/org/apache/cassandra/io/sstable/format/bti/PartitionIndex.java: ########## @@ -0,0 +1,452 @@ +/* + * 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.cassandra.io.sstable.format.bti; + +import java.io.IOException; +import java.io.PrintStream; +import java.nio.ByteBuffer; + +import com.google.common.annotations.VisibleForTesting; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.apache.cassandra.db.DecoratedKey; +import org.apache.cassandra.db.PartitionPosition; +import org.apache.cassandra.dht.IPartitioner; +import org.apache.cassandra.io.tries.SerializationNode; +import org.apache.cassandra.io.tries.TrieNode; +import org.apache.cassandra.io.tries.TrieSerializer; +import org.apache.cassandra.io.tries.ValueIterator; +import org.apache.cassandra.io.tries.Walker; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.io.util.File; +import org.apache.cassandra.io.util.FileDataInput; +import org.apache.cassandra.io.util.FileHandle; +import org.apache.cassandra.io.util.PageAware; +import org.apache.cassandra.io.util.Rebufferer; +import org.apache.cassandra.io.util.SizedInts; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.Pair; +import org.apache.cassandra.utils.bytecomparable.ByteSource; +import org.apache.cassandra.utils.concurrent.Ref; +import org.apache.cassandra.utils.concurrent.SharedCloseable; + +/** + * This class holds the partition index as an on-disk trie mapping unique prefixes of decorated keys to: + * <ul> + * <li>data file position if the partition is small enough to not need an index + * <li>row index file position if the partition has a row index + * </ul>plus<ul> + * <li>the last 8 bits of the key's filter hash which is used to filter out mismatched keys without reading the key + * </ul> + * To avoid having to create an object to carry the result, the two are distinguished by sign. Direct-to-dfile entries + * are recorded as ~position (~ instead of - to differentiate 0 in ifile from 0 in dfile). + * <p> + * In either case the contents of the file at this position start with a serialization of the key which can be used + * to verify the correct key is found. + * <p> + * The indexes are created by {@link PartitionIndexBuilder}. To read the index one must obtain a thread-unsafe + * {@link Reader} or {@link IndexPosIterator}. + */ +@VisibleForTesting +public class PartitionIndex implements SharedCloseable +{ + private static final Logger logger = LoggerFactory.getLogger(PartitionIndex.class); + + private final FileHandle fh; + private final long keyCount; + private final DecoratedKey first; + private final DecoratedKey last; + private final long root; + + public static final long NOT_FOUND = Long.MIN_VALUE; + public static final int FOOTER_LENGTH = 3 * 8; + + @VisibleForTesting + public PartitionIndex(FileHandle fh, long trieRoot, long keyCount, DecoratedKey first, DecoratedKey last) + { + this.keyCount = keyCount; + this.fh = fh.sharedCopy(); + this.first = first; + this.last = last; + this.root = trieRoot; + } + + private PartitionIndex(PartitionIndex src) + { + this(src.fh, src.root, src.keyCount, src.first, src.last); + } + + static class Payload + { + final long position; + final short hashBits; + + public Payload(long position, short hashBits) + { + this.position = position; + assert this.position != NOT_FOUND; + this.hashBits = hashBits; + } + } + + static final PartitionIndexSerializer TRIE_SERIALIZER = new PartitionIndexSerializer(); + + private static class PartitionIndexSerializer implements TrieSerializer<Payload, DataOutputPlus> + { + public int sizeofNode(SerializationNode<Payload> node, long nodePosition) + { + return TrieNode.typeFor(node, nodePosition).sizeofNode(node) + + (node.payload() != null ? 1 + SizedInts.nonZeroSize(node.payload().position) : 0); + } + + @Override + public void write(DataOutputPlus dest, SerializationNode<Payload> node, long nodePosition) throws IOException + { + write(dest, TrieNode.typeFor(node, nodePosition), node, nodePosition); + } + + public void write(DataOutputPlus dest, TrieNode type, SerializationNode<Payload> node, long nodePosition) throws IOException + { + Payload payload = node.payload(); + if (payload != null) + { + int payloadBits; + int size = SizedInts.nonZeroSize(payload.position); + payloadBits = 7 + size; Review Comment: Aside: So `size` is the number of bytes in the payload, but where does the `7` come from? (The hash is just one byte?) -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

