nitsanw commented on code in PR #4402: URL: https://github.com/apache/cassandra/pull/4402#discussion_r2517648796
########## src/java/org/apache/cassandra/io/sstable/ClusteringDescriptor.java: ########## @@ -0,0 +1,184 @@ +/* + * 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; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; + +import org.apache.cassandra.io.util.ResizableByteBuffer; +import org.apache.cassandra.db.Clustering; +import org.apache.cassandra.db.ClusteringBound; +import org.apache.cassandra.db.ClusteringPrefix; +import org.apache.cassandra.db.marshal.AbstractType; +import org.apache.cassandra.db.marshal.ByteArrayAccessor; +import org.apache.cassandra.io.util.DataInputBuffer; +import org.apache.cassandra.io.util.RandomAccessReader; + +import static org.apache.cassandra.io.sstable.SSTableCursorReader.readUnfilteredClustering; + +public class ClusteringDescriptor extends ResizableByteBuffer +{ + public static final byte EXCL_END_BOUND_CLUSTERING_TYPE = (byte) ClusteringPrefix.Kind.EXCL_END_BOUND.ordinal(); + public static final byte INCL_START_BOUND_CLUSTERING_TYPE = (byte) ClusteringPrefix.Kind.INCL_START_BOUND.ordinal(); + public static final byte INCL_END_EXCL_START_BOUNDARY_CLUSTERING_TYPE = (byte) ClusteringPrefix.Kind.INCL_END_EXCL_START_BOUNDARY.ordinal(); + + public static final byte STATIC_CLUSTERING_TYPE = (byte)ClusteringPrefix.Kind.STATIC_CLUSTERING.ordinal(); + public static final byte ROW_CLUSTERING_TYPE = (byte) ClusteringPrefix.Kind.CLUSTERING.ordinal(); + + public static final byte EXCL_END_INCL_START_BOUNDARY_CLUSTERING_TYPE = (byte) ClusteringPrefix.Kind.EXCL_END_INCL_START_BOUNDARY.ordinal(); + public static final byte INCL_END_BOUND_CLUSTERING_TYPE = (byte) ClusteringPrefix.Kind.INCL_END_BOUND.ordinal(); + public static final byte EXCL_START_BOUND_CLUSTERING_TYPE = (byte) ClusteringPrefix.Kind.EXCL_START_BOUND.ordinal(); + + protected AbstractType<?>[] clusteringTypes; Review Comment: This is a good point, it puts the clustering into the context of the compaction and will require a bit of untangling. ########## src/java/org/apache/cassandra/io/sstable/PartitionDescriptor.java: ########## @@ -0,0 +1,91 @@ +/* + * 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; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import org.apache.cassandra.io.util.ResizableByteBuffer; +import org.apache.cassandra.db.DeletionTime; +import org.apache.cassandra.io.util.RandomAccessReader; + +public class PartitionDescriptor extends ResizableByteBuffer Review Comment: This is semi-related to the detachment of keys from descriptors that we have in order to compare with prev key during compaction. Is this required? ########## src/java/org/apache/cassandra/io/sstable/SSTableCursorWriter.java: ########## @@ -0,0 +1,681 @@ +/* + * 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; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.List; + +import com.google.common.primitives.Ints; + +import org.agrona.collections.IntArrayList; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.ClusteringBoundOrBoundary; +import org.apache.cassandra.db.ClusteringPrefix; +import org.apache.cassandra.db.DecoratedKey; +import org.apache.cassandra.db.DeletionTime; +import org.apache.cassandra.db.LivenessInfo; +import org.apache.cassandra.db.SerializationHeader; +import org.apache.cassandra.db.partitions.PartitionStatisticsCollector; +import org.apache.cassandra.db.rows.Cell; +import org.apache.cassandra.db.rows.Cells; +import org.apache.cassandra.db.rows.RangeTombstoneMarker; +import org.apache.cassandra.db.rows.Row; +import org.apache.cassandra.db.rows.Rows; +import org.apache.cassandra.db.rows.SerializationHelper; +import org.apache.cassandra.db.rows.Unfiltered; +import org.apache.cassandra.db.rows.UnfilteredSerializer; +import org.apache.cassandra.dht.IPartitioner; +import org.apache.cassandra.io.FSWriteError; +import org.apache.cassandra.io.sstable.format.SSTableReader; +import org.apache.cassandra.io.sstable.format.SSTableWriter; +import org.apache.cassandra.io.sstable.format.SortedTableWriter; +import org.apache.cassandra.io.sstable.format.big.BigFormatPartitionWriter; +import org.apache.cassandra.io.sstable.format.big.BigTableWriter; +import org.apache.cassandra.io.sstable.format.big.RowIndexEntry; +import org.apache.cassandra.io.sstable.metadata.MetadataCollector; +import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.io.util.SequentialWriter; +import org.apache.cassandra.schema.ColumnMetadata; +import org.apache.cassandra.utils.BloomFilter; +import org.apache.cassandra.utils.ByteArrayUtil; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.concurrent.Ref; + +import static org.apache.cassandra.db.rows.UnfilteredSerializer.*; + +public class SSTableCursorWriter implements AutoCloseable +{ + private static final UnfilteredSerializer SERIALIZER = UnfilteredSerializer.serializer; + private static final ColumnMetadata[] EMPTY_COL_META = new ColumnMetadata[0]; + private final SortedTableWriter<?,?> ssTableWriter; + private final SequentialWriter dataWriter; + private final SortedTableWriter.AbstractIndexWriter indexWriter; + private final DeletionTime.Serializer deletionTimeSerializer; + private final MetadataCollector metadataCollector; + private final SerializationHeader serializationHeader; + /** + * See: {@link BloomFilter#reusableIndexes} + */ + private final long[] reusableIndexes = new long[21]; + private final boolean hasStaticColumns; + + private long partitionStart; + // ROW contents, needed because of the order of writing and the var int fields + private int rowFlags; // discovered as we go along + private int rowExtendedFlags; + private final byte[] copyColumnValueBuffer = new byte[4096]; // used to copy cell contents (maybe piecemiel if very large, since we don't have a direct read option) + private final DataOutputBuffer rowHeaderBuffer = new DataOutputBuffer(); // holds the contents between FLAGS and SIZE + private final DataOutputBuffer rowBuffer = new DataOutputBuffer(); + private final DeletionTime openMarker = DeletionTime.build(0,0); + + private final ColumnMetadata[] staticColumns; + private final ColumnMetadata[] regularColumns; + private final IntArrayList missingColumns = new IntArrayList(); + private ColumnMetadata[] columns; // points to static/regular + private int columnsWrittenCount = 0; + private int nextCellIndex = 0; + // Index info + private final DataOutputBuffer rowIndexEntries = new DataOutputBuffer(); + private final IntArrayList rowIndexEntriesOffsets = new IntArrayList(); + private final ClusteringDescriptor rowIndexEntryLastClustering = new ClusteringDescriptor(); + private int indexBlockStartOffset; + private int rowIndexEntryOffset; + private final int indexBlockThreshold; + + + private SSTableCursorWriter( + Descriptor desc, + SortedTableWriter<?,?> ssTableWriter, + SequentialWriter dataWriter, + SortedTableWriter.AbstractIndexWriter indexWriter, + MetadataCollector metadataCollector, + SerializationHeader serializationHeader) + { + this.ssTableWriter = ssTableWriter; + this.dataWriter = dataWriter; + this.indexWriter = indexWriter; + this.deletionTimeSerializer = DeletionTime.getSerializer(desc.version); + this.metadataCollector = metadataCollector; + this.serializationHeader = serializationHeader; + hasStaticColumns = serializationHeader.hasStatic(); + staticColumns = hasStaticColumns ? serializationHeader.columns(true).toArray(EMPTY_COL_META) : EMPTY_COL_META; + regularColumns = serializationHeader.columns(false).toArray(EMPTY_COL_META); + this.indexBlockThreshold = DatabaseDescriptor.getColumnIndexSize(BigFormatPartitionWriter.DEFAULT_GRANULARITY); + } + + public SSTableCursorWriter(SortedTableWriter<?,?> ssTableWriter) + { + this(ssTableWriter.descriptor, + ssTableWriter, + ssTableWriter.dataWriter, + ssTableWriter.indexWriter, + ssTableWriter.metadataCollector, + ssTableWriter.partitionWriter.getHeader()); + } + + @Override + public void close() + { + SSTableReader finish = ssTableWriter.finish(false); + if (finish != null) { + Ref<SSTableReader> ref = finish.ref(); + if (ref != null) ref.close(); + } + ssTableWriter.close(); + } + + public long getPartitionStart() + { + return partitionStart; + } + + public long getPosition() + { + return dataWriter.position(); + } + +// public int writePartitionStart(PartitionHeader pHeader) throws IOException +// { +// return writePartitionStart(pHeader.keyBytes(), pHeader.keyLength(), pHeader.deletionTime()); +// } +// + public int writePartitionStart(byte[] partitionKey, int partitionKeyLength, DeletionTime partitionDeletionTime) throws IOException + { + rowIndexEntries.clear(); + rowIndexEntriesOffsets.clear(); + rowIndexEntryOffset = 0; + openMarker.resetLive(); + + partitionStart = dataWriter.position(); + writePartitionHeader(partitionKey, partitionKeyLength, partitionDeletionTime); + updateIndexBlockStartOffset(dataWriter.position()); + return indexBlockStartOffset; + } + +// public void writePartitionEnd(PartitionHeader pHeader, int headerLength) throws IOException +// { +// writePartitionEnd(pHeader.keyBytes(), pHeader.keyLength(), pHeader.deletionTime(), headerLength); +// } + + public void writePartitionEnd(byte[] partitionKey, int partitionKeyLength, DeletionTime partitionDeletionTime, int headerLength) throws IOException + { + SERIALIZER.writeEndOfPartition(dataWriter); + long partitionEnd = dataWriter.position(); + long partitionSize = partitionEnd - partitionStart; + addPartitionMetadata(partitionKey, partitionKeyLength, partitionSize, partitionDeletionTime); + + /** {@link SortedTableWriter#endPartition(DecoratedKey, DeletionTime)} + lastWrittenKey = key; // tracked for verification, see {@link SortedTableWriter#verifyPartition(DecoratedKey)}, checking the key size and sorting + // first/last are retained for metadata {@link SSTableWriter#finalizeMetadata()}. They are also exposed via + // getters from the writer, but usage is unclear. + last = lastWrittenKey; + if (first == null) + first = lastWrittenKey; + // this is implemented differently for BIG/BTI + createRowIndexEntry(key, partitionLevelDeletion, partitionEnd - 1); + */ + appendBIGIndex(partitionKey, partitionKeyLength, partitionStart, headerLength, partitionDeletionTime, partitionEnd); + } + + private void appendBIGIndex(byte[] key, int keyLength, long partitionStart, int headerLength, DeletionTime partitionDeletionTime, long partitionEnd) throws IOException Review Comment: I can't see a great way to avoid duplication. The `BigFormatPartitionWriter` is taking in `Unfiltered` elements with all the assumptions that flow from there. I would like to maybe revisit this patch in future to more clearly split the index writing (potentially different formats, and secondary indexes) and sstable writing (common), but that is future work IMO. -- 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]

