ibessonov commented on code in PR #5005:
URL: https://github.com/apache/ignite-3/pull/5005#discussion_r1906988633


##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/storage/TermCache.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.ignite.internal.raft.storage;
+
+import static org.apache.ignite.internal.util.IgniteUtils.isPow2;
+
+import org.apache.ignite.raft.jraft.entity.LogId;
+
+/**
+ * Cyclic buffer to cache several last term values for log storage.
+ */
+public class TermCache {
+    private final int mask;
+    private final long[] indexes;
+    private final long[] terms;
+
+    // Head position. -1 means the cache is empty.
+    private int head = -1;
+
+    // Tail position. Might be equal to head if the cache only has a single 
term.
+    private int tail;
+
+    /**
+     * Constructor.
+     *
+     * @param capacity Cache capacity. Must be a power of 2. Should be a small 
value, term update is a rare operation.
+     */
+    public TermCache(int capacity) {
+        assert isPow2(capacity) : "Capacity must be a power of 2";
+
+        this.mask = capacity - 1;
+        this.indexes = new long[capacity];
+        this.terms = new long[capacity];
+    }
+
+    /**
+     * Should be called when appending a new log entry.
+     */
+    public void append(LogId id) {
+        // Cache is empty.
+        if (head == -1) {
+            head = 0;
+            indexes[tail] = id.getIndex();
+            terms[tail] = id.getTerm();
+
+            return;
+        }
+
+        // Terms has not changed, nothing to update.
+        if (terms[tail] == id.getTerm()) {
+            return;
+        }
+
+        tail = (tail + 1) & mask;
+        indexes[tail] = id.getIndex();
+        terms[tail] = id.getTerm();
+
+        // Handle buffer overflow by moving head to the next position.
+        if (tail == head) {
+            head = (head + 1) & mask;

Review Comment:
   Ok, I'll call them `prev` and `next` :)



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to