SammyVimes commented on a change in pull request #203: URL: https://github.com/apache/ignite-3/pull/203#discussion_r670745481
########## File path: modules/metastorage-server/src/main/java/org/apache/ignite/internal/metastorage/server/persistence/WatchCursor.java ########## @@ -0,0 +1,234 @@ +/* + * 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.metastorage.server.persistence; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.function.Predicate; +import org.apache.ignite.internal.metastorage.server.Entry; +import org.apache.ignite.internal.metastorage.server.EntryEvent; +import org.apache.ignite.internal.metastorage.server.Value; +import org.apache.ignite.internal.metastorage.server.WatchEvent; +import org.apache.ignite.internal.util.ByteUtils; +import org.apache.ignite.internal.util.Cursor; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.lang.IgniteInternalException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.rocksdb.ReadOptions; +import org.rocksdb.RocksDBException; +import org.rocksdb.RocksIterator; + +/** + * Subscription on updates of entries corresponding to the given keys range (where an upper bound is unlimited) + * and starting from the given revision number. + */ +class WatchCursor implements Cursor<WatchEvent> { + /** Storage. */ + private final RocksDBKeyValueStorage storage; + + /** Key predicate. */ + private final Predicate<byte[]> p; + + /** Iterator for this cursor. */ + private final Iterator<WatchEvent> it; + + /** Options for {@link #nativeIterator}. */ + private final ReadOptions options = new ReadOptions().setPrefixSameAsStart(true); + + /** RocksDB iterator. */ + @Nullable + private final RocksIterator nativeIterator; + + /** + * Last matching revision. + */ + private long lastRetRev; + + /** + * Next matching revision. {@code -1} means it's not found yet or does not exist. + */ + private long nextRetRev = -1; + + /** + * Constructor. + * + * @param storage Storage. + * @param rev Starting revision. + * @param p Key predicate. + */ + WatchCursor(RocksDBKeyValueStorage storage, long rev, Predicate<byte[]> p) { + this.storage = storage; + this.p = p; + this.lastRetRev = rev - 1; + this.nativeIterator = storage.db().newIterator(options); + this.it = createIterator(); + } + + /** + * {@inheritDoc} + */ + @Override public boolean hasNext() { + return it.hasNext(); + } + + /** + * {@inheritDoc} + */ + @Nullable + @Override public WatchEvent next() { + return it.next(); + } + + /** + * {@inheritDoc} + */ + @Override public void close() throws Exception { + IgniteUtils.closeAll(options, nativeIterator); + } + + /** + * {@inheritDoc} + */ + @NotNull + @Override public Iterator<WatchEvent> iterator() { + return it; + } + + /** + * Creates an iterator for this cursor. + * + * @return Iterator. + */ + @NotNull + private Iterator<WatchEvent> createIterator() { + return new Iterator<>() { + /** {@inheritDoc} */ + @Override public boolean hasNext() { + storage.lock().readLock().lock(); + + try { + if (nextRetRev != -1) + // Next revision is already calculated and is not -1, meaning that there is set of keys + // matching the revision and the predicate. + return true; + + while (true) { + long curRev = lastRetRev + 1; + + byte[] revisionPrefix = ByteUtils.longToBytes(curRev); + + boolean empty = true; + + if (!nativeIterator.isValid()) Review comment: I've removed `@Nullable`, so it should no complain anymore -- 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]
