agura commented on a change in pull request #105: URL: https://github.com/apache/ignite-3/pull/105#discussion_r619339451
########## File path: modules/vault/src/main/java/org/apache/ignite/internal/vault/impl/VaultServiceImpl.java ########## @@ -0,0 +1,130 @@ +/* + * 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.vault.impl; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.vault.common.*; +import org.apache.ignite.internal.vault.service.VaultService; +import org.apache.ignite.lang.ByteArray; +import org.apache.ignite.lang.IgniteUuid; +import org.jetbrains.annotations.NotNull; + +/** + * Simple in-memory representation of vault. Only for test purposes. + */ +public class VaultServiceImpl implements VaultService { + /** Map to store values. */ + private TreeMap<ByteArray, byte[]> storage = new TreeMap<>(); + + /** + * Special key for vault where applied revision for {@code putAll} operation is stored. + */ + private static ByteArray APPLIED_REV = ByteArray.fromString("applied_revision"); + + /** Mutex. */ + private final Object mux = new Object(); + + private final WatcherImpl watcher; + + public VaultServiceImpl() { + this.watcher = new WatcherImpl(); + } + + /** {@inheritDoc} */ + @Override public CompletableFuture<VaultEntry> get(ByteArray key) { + synchronized (mux) { + return CompletableFuture.completedFuture(new VaultEntry(key, storage.get(key))); + } + } + + /** {@inheritDoc} */ + @NotNull @Override public CompletableFuture<Long> appliedRevision() { + synchronized (mux) { + return CompletableFuture.completedFuture(IgniteUtils.bytesToLong(storage.get(APPLIED_REV), 0)); + } + } + + /** {@inheritDoc} */ + @Override public CompletableFuture<Void> put(ByteArray key, byte[] val) { + synchronized (mux) { + storage.put(key, val); + + watcher.notify(new VaultEntry(key, val)); + + return CompletableFuture.allOf(); + } + } + + /** {@inheritDoc} */ + @Override public CompletableFuture<Void> remove(ByteArray key) { + synchronized (mux) { + storage.remove(key); + + return CompletableFuture.allOf(); + } + } + + /** {@inheritDoc} */ + @Override public Iterator<VaultEntry> range(ByteArray fromKey, ByteArray toKey) { Review comment: It should be cursor, not iterator. -- 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. For queries about this service, please contact Infrastructure at: [email protected]
