iamaleksey commented on code in PR #2256:
URL: https://github.com/apache/cassandra/pull/2256#discussion_r1160581492


##########
src/java/org/apache/cassandra/journal/StaticSegment.java:
##########
@@ -0,0 +1,342 @@
+/*
+ * 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.journal;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.NoSuchFileException;
+import java.nio.file.StandardOpenOption;
+import java.util.*;
+
+import org.agrona.collections.IntHashSet;
+import org.apache.cassandra.io.util.DataInputBuffer;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.Closeable;
+import org.apache.cassandra.utils.concurrent.Ref;
+
+/**
+ * An immutable data segment that is no longer written to.
+ * <p>
+ * Can be compacted with input from {@code PersistedInvalidations} into a new 
smaller segment,
+ * with invalidated entries removed.
+ */
+final class StaticSegment<K> extends Segment<K>
+{
+    final FileChannel channel;
+
+    private final Ref<Segment<K>> selfRef;
+
+    private StaticSegment(Descriptor descriptor,
+                          FileChannel channel,
+                          MappedByteBuffer buffer,
+                          SyncedOffsets syncedOffsets,
+                          Index<K> index,
+                          Metadata metadata,
+                          KeySupport<K> keySupport)
+    {
+        super(descriptor, syncedOffsets, index, metadata, keySupport);
+
+        this.channel = channel;
+        this.buffer = buffer;
+
+        selfRef = new Ref<>(this, new Tidier<>(descriptor, channel, buffer, 
index));
+    }
+
+    /**
+     * Loads all segments matching the supplied desctiptors
+     *
+     * @param descriptors descriptors of the segments to load
+     * @return list of the loaded segments
+     */
+    static <K> List<StaticSegment<K>> open(Collection<Descriptor> descriptors, 
KeySupport<K> keySupport)
+    {
+        List<StaticSegment<K>> segments = new ArrayList<>(descriptors.size());
+        for (Descriptor descriptor : descriptors)
+            segments.add(open(descriptor, keySupport));
+        return segments;
+    }
+
+    /**
+     * Load the segment corresponding to the provided desrciptor
+     *
+     * @param descriptor descriptor of the segment to load
+     * @return the loaded segment
+     */
+    @SuppressWarnings({ "resource", "RedundantSuppression" })
+    static <K> StaticSegment<K> open(Descriptor descriptor, KeySupport<K> 
keySupport)
+    {
+        if (!Component.DATA.existsFor(descriptor))
+            throw new IllegalArgumentException("Data file for segment " + 
descriptor + " doesn't exist");
+
+        SyncedOffsets syncedOffsets = 
Component.SYNCED_OFFSETS.existsFor(descriptor)
+                                    ? SyncedOffsets.load(descriptor)
+                                    : SyncedOffsets.absent();
+
+        Metadata metadata = Component.INDEX.existsFor(descriptor)
+                          ? Metadata.load(descriptor)
+                          : Metadata.rebuildAndPersist(descriptor, keySupport, 
syncedOffsets.syncedOffset());
+
+        OnDiskIndex<K> index = Component.METADATA.existsFor(descriptor)
+                             ? OnDiskIndex.open(descriptor, keySupport)
+                             : OnDiskIndex.rebuildAndPersist(descriptor, 
keySupport, syncedOffsets.syncedOffset());

Review Comment:
   Good catch. Only tested it manually with both components missing  and both 
components present at the same time, so in that scenario it just worked anyway. 
Will fix.



-- 
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]

Reply via email to