belliottsmith commented on code in PR #256: URL: https://github.com/apache/cassandra-accord/pull/256#discussion_r2492078880
########## accord-core/src/main/java/accord/topology/PendingEpochs.java: ########## @@ -0,0 +1,156 @@ +/* + * 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 accord.topology; + +import accord.local.Node; +import accord.primitives.Ranges; +import accord.utils.Invariants; + +/** + * Thread safety is managed by a synchronized lock on this object, and extending classes can use the same lock when needed. + * <p> + * There is a special case when the last recieved/acknowleged epochs are needed, they can be accessed without a lock + * and provide a happens-before relationship (if lastReceived=42, epoch 42 exists and is set up) + */ +class PendingEpochs +{ + final TopologyManager manager; + private PendingEpoch[] epochs = new PendingEpoch[16]; + private int start, end; + + PendingEpochs(TopologyManager manager) + { + this.manager = manager; + } + + int size() + { + return end - start; + } + + boolean isEmpty() + { + return end == start; + } + + private void append(PendingEpoch append) + { + if (end == epochs.length) + { + int capacity = Math.max(epochs.length, size() * 2); + resize(capacity, 0); + } + epochs[end++] = append; + } + + private void prepend(PendingEpoch append) + { + if (start == 0) + { + int size = size(); + int capacity = Math.max(epochs.length, size * 2); + resize(capacity, Math.max(1, capacity / 4)); Review Comment: We may have to prepend several epochs in a row, and this guarantees we have constant time amortised complexity (rather than quadratic), and once we run out of space appending we will reclaim the space. -- 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]

