Madan Jampani
Committed by Gerrit Code Review

Refactored code to consolidate functionality in Database* classes.

Renamed few methods and variables to align with local convention and also to match the description of functionality.

Change-Id: Ib17e73079534c76f76bcb01f14b6496e62275dbd
Showing 21 changed files with 287 additions and 422 deletions
......@@ -18,7 +18,6 @@ package org.onosproject.store.service;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
......@@ -166,17 +165,6 @@ public interface AsyncConsistentMap<K, V> {
CompletableFuture<Versioned<V>> putAndGet(K key, V value);
/**
* Associates the specified value with the specified key in this map (optional operation).
* If the map previously contained a mapping for the key, the old value is replaced by the
* specified value.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return optional updated value. Will be empty if update did not happen
*/
CompletableFuture<Optional<Versioned<V>>> putIfAbsentAndGet(K key, V value);
/**
* Removes the mapping for a key from this map if it is present (optional operation).
*
* @param key key whose value is to be removed from the map
......@@ -279,17 +267,6 @@ public interface AsyncConsistentMap<K, V> {
CompletableFuture<Boolean> replace(K key, long oldVersion, V newValue);
/**
* Replaces the entry for the specified key only if it is currently mapped to the
* specified version.
*
* @param key key key with which the specified value is associated
* @param oldVersion version expected to be associated with the specified key
* @param newValue value to be associated with the specified key
* @return optional updated value. Will be empty if update did not happen.
*/
CompletableFuture<Optional<Versioned<V>>> replaceAndGet(K key, long oldVersion, V newValue);
/**
* Registers the specified listener to be notified whenever the map is updated.
*
* @param listener listener to notify about map events
......
......@@ -18,7 +18,6 @@ package org.onosproject.store.service;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
......@@ -168,17 +167,6 @@ public interface ConsistentMap<K, V> {
Versioned<V> putAndGet(K key, V value);
/**
* Associates the specified value with the specified key in this map (optional operation).
* If the map previously contained a mapping for the key, the old value is replaced by the
* specified value.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return optional updated value. Will be empty if update did not happen
*/
Optional<Versioned<V>> putIfAbsentAndGet(K key, V value);
/**
* Removes the mapping for a key from this map if it is present (optional operation).
*
* @param key key whose value is to be removed from the map
......@@ -280,17 +268,6 @@ public interface ConsistentMap<K, V> {
boolean replace(K key, long oldVersion, V newValue);
/**
* Replaces the entry for the specified key only if it is currently mapped to the
* specified version.
*
* @param key key key with which the specified value is associated
* @param oldVersion version expected to be associated with the specified key
* @param newValue value to be associated with the specified key
* @return optional new value. Will be empty if replace did not happen
*/
Optional<Versioned<V>> replaceAndGet(K key, long oldVersion, V newValue);
/**
* Registers the specified listener to be notified whenever the map is updated.
*
* @param listener listener to notify about map events
......
......@@ -68,7 +68,7 @@ public final class DatabaseUpdate {
}
private Type type;
private String tableName;
private String mapName;
private String key;
private byte[] value;
private byte[] currentValue;
......@@ -83,11 +83,11 @@ public final class DatabaseUpdate {
}
/**
* Returns the tableName being updated.
* @return table name.
* Returns the name of map being updated.
* @return map name.
*/
public String tableName() {
return tableName;
public String mapName() {
return mapName;
}
/**
......@@ -126,7 +126,7 @@ public final class DatabaseUpdate {
public String toString() {
return MoreObjects.toStringHelper(this)
.add("type", type)
.add("tableName", tableName)
.add("mapName", mapName)
.add("key", key)
.add("value", value)
.add("currentValue", currentValue)
......@@ -161,8 +161,8 @@ public final class DatabaseUpdate {
return this;
}
public Builder withTableName(String tableName) {
update.tableName = checkNotNull(tableName, "tableName cannot be null");
public Builder withMapName(String mapName) {
update.mapName = checkNotNull(mapName, "mapName cannot be null");
return this;
}
......@@ -189,7 +189,7 @@ public final class DatabaseUpdate {
private void validateInputs() {
checkNotNull(update.type, "type must be specified");
checkNotNull(update.tableName, "table name must be specified");
checkNotNull(update.mapName, "map name must be specified");
checkNotNull(update.key, "key must be specified");
switch (update.type) {
case PUT:
......
......@@ -33,7 +33,7 @@ public class DatabaseUpdateTest extends TestCase {
.withValue("2".getBytes())
.withCurrentVersion(3)
.withKey("4")
.withTableName("5")
.withMapName("5")
.withType(DatabaseUpdate.Type.PUT)
.build();
......@@ -42,7 +42,7 @@ public class DatabaseUpdateTest extends TestCase {
.withValue("2".getBytes())
.withCurrentVersion(3)
.withKey("4")
.withTableName("5")
.withMapName("5")
.withType(DatabaseUpdate.Type.REMOVE)
.build();
......@@ -51,7 +51,7 @@ public class DatabaseUpdateTest extends TestCase {
.withValue("2".getBytes())
.withCurrentVersion(3)
.withKey("4")
.withTableName("5")
.withMapName("5")
.withType(DatabaseUpdate.Type.REMOVE_IF_VALUE_MATCH)
.build();
......@@ -60,7 +60,7 @@ public class DatabaseUpdateTest extends TestCase {
.withValue("2".getBytes())
.withCurrentVersion(3)
.withKey("4")
.withTableName("5")
.withMapName("5")
.withType(DatabaseUpdate.Type.REMOVE_IF_VERSION_MATCH)
.build();
......@@ -69,7 +69,7 @@ public class DatabaseUpdateTest extends TestCase {
.withValue("2".getBytes())
.withCurrentVersion(3)
.withKey("4")
.withTableName("5")
.withMapName("5")
.withType(DatabaseUpdate.Type.PUT_IF_VALUE_MATCH)
.build();
......@@ -78,7 +78,7 @@ public class DatabaseUpdateTest extends TestCase {
.withValue("2".getBytes())
.withCurrentVersion(3)
.withKey("4")
.withTableName("5")
.withMapName("5")
.withType(DatabaseUpdate.Type.PUT_IF_VERSION_MATCH)
.build();
......@@ -91,7 +91,7 @@ public class DatabaseUpdateTest extends TestCase {
assertThat(stats1.value(), is("2".getBytes()));
assertThat(stats1.currentVersion(), is(3L));
assertThat(stats1.key(), is("4"));
assertThat(stats1.tableName(), is("5"));
assertThat(stats1.mapName(), is("5"));
assertThat(stats1.type(), is(DatabaseUpdate.Type.PUT));
}
......
......@@ -391,9 +391,9 @@ public class DatabaseManager implements StorageService, StorageAdminService {
}
private List<MapInfo> getMapInfo(Database database) {
return complete(database.tableNames())
return complete(database.maps())
.stream()
.map(name -> new MapInfo(name, complete(database.size(name))))
.map(name -> new MapInfo(name, complete(database.mapSize(name))))
.filter(info -> info.size() > 0)
.collect(Collectors.toList());
}
......
......@@ -24,9 +24,9 @@ import com.google.common.collect.ImmutableList;
import com.google.common.hash.Hashing;
/**
* Partitioner for mapping table entries to individual database partitions.
* Partitioner for mapping map entries to individual database partitions.
* <p>
* By default a md5 hash of the hash key (key or table name) is used to pick a
* By default a md5 hash of the hash key (key or map name) is used to pick a
* partition.
*/
public abstract class DatabasePartitioner implements Partitioner<String> {
......
......@@ -31,11 +31,11 @@ import org.onosproject.store.service.Versioned;
public interface DatabaseProxy<K, V> {
/**
* Returns a set of all tables names.
* Returns a set of all map names.
*
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Set<String>> tableNames();
CompletableFuture<Set<String>> maps();
/**
* Returns a mapping from counter name to next value.
......@@ -45,183 +45,91 @@ public interface DatabaseProxy<K, V> {
CompletableFuture<Map<String, Long>> counters();
/**
* Gets the table size.
*
* @param tableName table name
* @param mapName map name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Integer> size(String tableName);
CompletableFuture<Integer> mapSize(String mapName);
/**
* Checks whether the table is empty.
* Checks whether the map is empty.
*
* @param tableName table name
* @param mapName map name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Boolean> isEmpty(String tableName);
CompletableFuture<Boolean> mapIsEmpty(String mapName);
/**
* Checks whether the table contains a key.
* Checks whether the map contains a key.
*
* @param tableName table name
* @param key The key to check.
* @param mapName map name
* @param key key to check.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Boolean> containsKey(String tableName, K key);
CompletableFuture<Boolean> mapContainsKey(String mapName, K key);
/**
* Checks whether the table contains a value.
* Checks whether the map contains a value.
*
* @param tableName table name
* @param mapName map name
* @param value The value to check.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Boolean> containsValue(String tableName, V value);
CompletableFuture<Boolean> mapContainsValue(String mapName, V value);
/**
* Gets a value from the table.
* Gets a value from the map.
*
* @param tableName table name
* @param mapName map name
* @param key The key to get.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Versioned<V>> get(String tableName, K key);
CompletableFuture<Versioned<V>> mapGet(String mapName, K key);
/**
* Puts a value in the table.
* Updates the map.
*
* @param tableName table name
* @param key The key to set.
* @param value The value to set.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<Versioned<V>>> put(String tableName, K key, V value);
/**
* Puts a value in the table.
*
* @param tableName table name
* @param key The key to set.
* @param value The value to set.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<UpdateResult<Versioned<V>>>> putAndGet(String tableName, K key, V value);
/**
* Puts a value in the table.
*
* @param tableName table name
* @param key The key to set.
* @param value The value to set.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<UpdateResult<Versioned<V>>>> putIfAbsentAndGet(String tableName, K key, V value);
/**
* Removes a value from the table.
*
* @param tableName table name
* @param key The key to remove.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<Versioned<V>>> remove(String tableName, K key);
/**
* Clears the table.
*
* @param tableName table name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<Void>> clear(String tableName);
/**
* Gets a set of keys in the table.
*
* @param tableName table name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Set<K>> keySet(String tableName);
/**
* Gets a collection of values in the table.
*
* @param tableName table name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Collection<Versioned<V>>> values(String tableName);
/**
* Gets a set of entries in the table.
*
* @param tableName table name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> entrySet(String tableName);
/**
* Puts a value in the table if the given key does not exist.
*
* @param tableName table name
* @param key The key to set.
* @param value The value to set if the given key does not exist.
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<Versioned<V>>> putIfAbsent(String tableName, K key, V value);
/**
* Removes a key and if the existing value for that key matches the specified value.
*
* @param tableName table name
* @param key The key to remove.
* @param value The value to remove.
* @return A completable future to be completed with the result once complete.
* @param mapName map name
* @param key The key to set
* @param valueMatch match for checking existing value
* @param versionMatch match for checking existing version
* @param value new value
* @return A completable future to be completed with the result once complete
*/
CompletableFuture<Result<Boolean>> remove(String tableName, K key, V value);
CompletableFuture<Result<UpdateResult<K, V>>> mapUpdate(
String mapName, K key, Match<V> valueMatch, Match<Long> versionMatch, V value);
/**
* Removes a key and if the existing version for that key matches the specified version.
* Clears the map.
*
* @param tableName table name
* @param key The key to remove.
* @param version The expected version.
* @param mapName map name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<Boolean>> remove(String tableName, K key, long version);
CompletableFuture<Result<Void>> mapClear(String mapName);
/**
* Replaces the entry for the specified key only if currently mapped to the specified value.
* Gets a set of keys in the map.
*
* @param tableName table name
* @param key The key to replace.
* @param oldValue The value to replace.
* @param newValue The value with which to replace the given key and value.
* @param mapName map name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<Boolean>> replace(String tableName, K key, V oldValue, V newValue);
CompletableFuture<Set<K>> mapKeySet(String mapName);
/**
* Replaces the entry for the specified key only if currently mapped to the specified version.
* Gets a collection of values in the map.
*
* @param tableName table name
* @param key The key to update
* @param oldVersion existing version in the map for this replace to succeed.
* @param newValue The value with which to replace the given key and version.
* @param mapName map name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<Boolean>> replace(String tableName, K key, long oldVersion, V newValue);
CompletableFuture<Collection<Versioned<V>>> mapValues(String mapName);
/**
* Replaces the entry for the specified key only if currently mapped to the specified version.
* Gets a set of entries in the map.
*
* @param tableName table name
* @param key The key to update
* @param oldVersion existing version in the map for this replace to succeed.
* @param newValue The value with which to replace the given key and version.
* @param mapName map name
* @return A completable future to be completed with the result once complete.
*/
CompletableFuture<Result<UpdateResult<Versioned<V>>>> replaceAndGet(String tableName,
K key, long oldVersion,
V newValue);
CompletableFuture<Set<Map.Entry<K, Versioned<V>>>> mapEntrySet(String mapName);
/**
* Atomically add the given value to current value of the specified counter.
......
......@@ -79,6 +79,7 @@ public class DatabaseSerializer extends SerializerConfig {
.register(Result.Status.class)
.register(DefaultTransaction.class)
.register(Transaction.State.class)
.register(Match.class)
.register(NodeId.class)
.build();
......
......@@ -45,67 +45,40 @@ public interface DatabaseState<K, V> {
void init(StateContext<DatabaseState<K, V>> context);
@Query
Set<String> tableNames();
Set<String> maps();
@Query
Map<String, Long> counters();
@Query
int size(String tableName);
int size(String mapName);
@Query
boolean isEmpty(String tableName);
boolean mapIsEmpty(String mapName);
@Query
boolean containsKey(String tableName, K key);
boolean mapContainsKey(String mapName, K key);
@Query
boolean containsValue(String tableName, V value);
boolean mapContainsValue(String mapName, V value);
@Query
Versioned<V> get(String tableName, K key);
Versioned<V> mapGet(String mapName, K key);
@Command
Result<Versioned<V>> put(String tableName, K key, V value);
Result<UpdateResult<K, V>> mapUpdate(String mapName, K key, Match<V> valueMatch, Match<Long> versionMatch, V value);
@Command
Result<UpdateResult<Versioned<V>>> putAndGet(String tableName, K key, V value);
@Command
Result<UpdateResult<Versioned<V>>> putIfAbsentAndGet(String tableName, K key, V value);
@Command
Result<Versioned<V>> remove(String tableName, K key);
@Command
Result<Void> clear(String tableName);
Result<Void> mapClear(String mapName);
@Query
Set<K> keySet(String tableName);
Set<K> mapKeySet(String mapName);
@Query
Collection<Versioned<V>> values(String tableName);
Collection<Versioned<V>> mapValues(String mapName);
@Query
Set<Entry<K, Versioned<V>>> entrySet(String tableName);
@Command
Result<Versioned<V>> putIfAbsent(String tableName, K key, V value);
@Command
Result<Boolean> remove(String tableName, K key, V value);
@Command
Result<Boolean> remove(String tableName, K key, long version);
@Command
Result<Boolean> replace(String tableName, K key, V oldValue, V newValue);
@Command
Result<Boolean> replace(String tableName, K key, long oldVersion, V newValue);
@Command
Result<UpdateResult<Versioned<V>>> replaceAndGet(String tableName, K key, long oldVersion, V newValue);
Set<Entry<K, Versioned<V>>> mapEntrySet(String mapName);
@Command
Long counterAddAndGet(String counterName, long delta);
......
......@@ -18,7 +18,6 @@ package org.onosproject.store.consistent.impl;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
......@@ -115,11 +114,6 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
}
@Override
public Optional<Versioned<V>> putIfAbsentAndGet(K key, V value) {
return complete(asyncMap.putIfAbsentAndGet(key, value));
}
@Override
public Versioned<V> remove(K key) {
return complete(asyncMap.remove(key));
}
......@@ -169,11 +163,6 @@ public class DefaultConsistentMap<K, V> implements ConsistentMap<K, V> {
return complete(asyncMap.replace(key, oldVersion, newValue));
}
@Override
public Optional<Versioned<V>> replaceAndGet(K key, long oldVersion, V newValue) {
return complete(asyncMap.replaceAndGet(key, oldVersion, newValue));
}
private static <T> T complete(CompletableFuture<T> future) {
try {
return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
......
......@@ -64,8 +64,8 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab
}
@Override
public CompletableFuture<Set<String>> tableNames() {
return checkOpen(() -> proxy.tableNames());
public CompletableFuture<Set<String>> maps() {
return checkOpen(() -> proxy.maps());
}
@Override
......@@ -74,105 +74,54 @@ public class DefaultDatabase extends AbstractResource<Database> implements Datab
}
@Override
public CompletableFuture<Integer> size(String tableName) {
return checkOpen(() -> proxy.size(tableName));
public CompletableFuture<Integer> mapSize(String mapName) {
return checkOpen(() -> proxy.mapSize(mapName));
}
@Override
public CompletableFuture<Boolean> isEmpty(String tableName) {
return checkOpen(() -> proxy.isEmpty(tableName));
public CompletableFuture<Boolean> mapIsEmpty(String mapName) {
return checkOpen(() -> proxy.mapIsEmpty(mapName));
}
@Override
public CompletableFuture<Boolean> containsKey(String tableName, String key) {
return checkOpen(() -> proxy.containsKey(tableName, key));
public CompletableFuture<Boolean> mapContainsKey(String mapName, String key) {
return checkOpen(() -> proxy.mapContainsKey(mapName, key));
}
@Override
public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) {
return checkOpen(() -> proxy.containsValue(tableName, value));
public CompletableFuture<Boolean> mapContainsValue(String mapName, byte[] value) {
return checkOpen(() -> proxy.mapContainsValue(mapName, value));
}
@Override
public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) {
return checkOpen(() -> proxy.get(tableName, key));
public CompletableFuture<Versioned<byte[]>> mapGet(String mapName, String key) {
return checkOpen(() -> proxy.mapGet(mapName, key));
}
@Override
public CompletableFuture<Result<Versioned<byte[]>>> put(String tableName, String key, byte[] value) {
return checkOpen(() -> proxy.put(tableName, key, value));
public CompletableFuture<Result<UpdateResult<String, byte[]>>> mapUpdate(
String mapName, String key, Match<byte[]> valueMatch, Match<Long> versionMatch, byte[] value) {
return checkOpen(() -> proxy.mapUpdate(mapName, key, valueMatch, versionMatch, value));
}
@Override
public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> putAndGet(String tableName,
String key,
byte[] value) {
return checkOpen(() -> proxy.putAndGet(tableName, key, value));
public CompletableFuture<Result<Void>> mapClear(String mapName) {
return checkOpen(() -> proxy.mapClear(mapName));
}
@Override
public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> putIfAbsentAndGet(String tableName,
String key,
byte[] value) {
return checkOpen(() -> proxy.putIfAbsentAndGet(tableName, key, value));
public CompletableFuture<Set<String>> mapKeySet(String mapName) {
return checkOpen(() -> proxy.mapKeySet(mapName));
}
@Override
public CompletableFuture<Result<Versioned<byte[]>>> remove(String tableName, String key) {
return checkOpen(() -> proxy.remove(tableName, key));
public CompletableFuture<Collection<Versioned<byte[]>>> mapValues(String mapName) {
return checkOpen(() -> proxy.mapValues(mapName));
}
@Override
public CompletableFuture<Result<Void>> clear(String tableName) {
return checkOpen(() -> proxy.clear(tableName));
}
@Override
public CompletableFuture<Set<String>> keySet(String tableName) {
return checkOpen(() -> proxy.keySet(tableName));
}
@Override
public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) {
return checkOpen(() -> proxy.values(tableName));
}
@Override
public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> entrySet(String tableName) {
return checkOpen(() -> proxy.entrySet(tableName));
}
@Override
public CompletableFuture<Result<Versioned<byte[]>>> putIfAbsent(String tableName, String key, byte[] value) {
return checkOpen(() -> proxy.putIfAbsent(tableName, key, value));
}
@Override
public CompletableFuture<Result<Boolean>> remove(String tableName, String key, byte[] value) {
return checkOpen(() -> proxy.remove(tableName, key, value));
}
@Override
public CompletableFuture<Result<Boolean>> remove(String tableName, String key, long version) {
return checkOpen(() -> proxy.remove(tableName, key, version));
}
@Override
public CompletableFuture<Result<Boolean>> replace(String tableName, String key, byte[] oldValue, byte[] newValue) {
return checkOpen(() -> proxy.replace(tableName, key, oldValue, newValue));
}
@Override
public CompletableFuture<Result<Boolean>> replace(String tableName, String key, long oldVersion, byte[] newValue) {
return checkOpen(() -> proxy.replace(tableName, key, oldVersion, newValue));
}
@Override
public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> replaceAndGet(String tableName,
String key,
long oldVersion,
byte[] newValue) {
return checkOpen(() -> proxy.replaceAndGet(tableName, key, oldVersion, newValue));
public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> mapEntrySet(String mapName) {
return checkOpen(() -> proxy.mapEntrySet(mapName));
}
@Override
......
......@@ -164,7 +164,7 @@ public class DefaultTransactionalMap<K, V> implements TransactionalMap<K, V> {
Versioned<V> original = readCache.get(key);
if (original != null) {
updates.add(DatabaseUpdate.newBuilder()
.withTableName(name)
.withMapName(name)
.withType(DatabaseUpdate.Type.REMOVE_IF_VERSION_MATCH)
.withKey(keyCache.getUnchecked(key))
.withCurrentVersion(original.version())
......@@ -175,14 +175,14 @@ public class DefaultTransactionalMap<K, V> implements TransactionalMap<K, V> {
Versioned<V> original = readCache.get(key);
if (original == null) {
updates.add(DatabaseUpdate.newBuilder()
.withTableName(name)
.withMapName(name)
.withType(DatabaseUpdate.Type.PUT_IF_ABSENT)
.withKey(keyCache.getUnchecked(key))
.withValue(serializer.encode(value))
.build());
} else {
updates.add(DatabaseUpdate.newBuilder()
.withTableName(name)
.withMapName(name)
.withType(DatabaseUpdate.Type.PUT_IF_VERSION_MATCH)
.withKey(keyCache.getUnchecked(key))
.withCurrentVersion(original.version())
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed 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.onosproject.store.consistent.impl;
import static com.google.common.base.MoreObjects.toStringHelper;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
/**
* Utility class for checking matching values.
*
* @param <T> type of value
*/
public final class Match<T> {
private final boolean matchAny;
private final T value;
/**
* Returns a Match that matches any value.
* @param <T> match type
* @return new instance
*/
public static <T> Match<T> any() {
return new Match<>();
}
/**
* Returns a Match that matches null values.
* @param <T> match type
* @return new instance
*/
public static <T> Match<T> ifNull() {
return ifValue(null);
}
/**
* Returns a Match that matches only specified value.
* @param value value to match
* @param <T> match type
* @return new instance
*/
public static <T> Match<T> ifValue(T value) {
return new Match<>(value);
}
private Match() {
matchAny = true;
value = null;
}
private Match(T value) {
matchAny = false;
this.value = value;
}
/**
* Maps this instance to a Match of another type.
* @param mapper transformation function
* @param <V> new match type
* @return new instance
*/
public <V> Match<V> map(Function<T, V> mapper) {
if (matchAny) {
return any();
} else if (value == null) {
return ifNull();
} else {
return ifValue(mapper.apply(value));
}
}
/**
* Checks if this instance matches specified value.
* @param other other value
* @return true if matches; false otherwise
*/
public boolean matches(T other) {
if (matchAny) {
return true;
} else if (other == null) {
return value == null;
} else {
if (value instanceof byte[]) {
return Arrays.equals((byte[]) value, (byte[]) other);
}
return Objects.equals(value, other);
}
}
@Override
public String toString() {
return toStringHelper(this)
.add("matchAny", matchAny)
.add("value", value)
.toString();
}
}
......@@ -82,14 +82,14 @@ public class PartitionedDatabase implements Database {
}
@Override
public CompletableFuture<Set<String>> tableNames() {
public CompletableFuture<Set<String>> maps() {
checkState(isOpen.get(), DB_NOT_OPEN);
Set<String> tableNames = Sets.newConcurrentHashSet();
Set<String> mapNames = Sets.newConcurrentHashSet();
return CompletableFuture.allOf(partitions
.stream()
.map(db -> db.tableNames().thenApply(tableNames::addAll))
.map(db -> db.maps().thenApply(mapNames::addAll))
.toArray(CompletableFuture[]::new))
.thenApply(v -> tableNames);
.thenApply(v -> mapNames);
}
@Override
......@@ -108,158 +108,100 @@ public class PartitionedDatabase implements Database {
}
@Override
public CompletableFuture<Integer> size(String tableName) {
public CompletableFuture<Integer> mapSize(String mapName) {
checkState(isOpen.get(), DB_NOT_OPEN);
AtomicInteger totalSize = new AtomicInteger(0);
return CompletableFuture.allOf(partitions
.stream()
.map(p -> p.size(tableName).thenApply(totalSize::addAndGet))
.map(p -> p.mapSize(mapName).thenApply(totalSize::addAndGet))
.toArray(CompletableFuture[]::new))
.thenApply(v -> totalSize.get());
}
@Override
public CompletableFuture<Boolean> isEmpty(String tableName) {
public CompletableFuture<Boolean> mapIsEmpty(String mapName) {
checkState(isOpen.get(), DB_NOT_OPEN);
return size(tableName).thenApply(size -> size == 0);
return mapSize(mapName).thenApply(size -> size == 0);
}
@Override
public CompletableFuture<Boolean> containsKey(String tableName, String key) {
public CompletableFuture<Boolean> mapContainsKey(String mapName, String key) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).containsKey(tableName, key);
return partitioner.getPartition(mapName, key).mapContainsKey(mapName, key);
}
@Override
public CompletableFuture<Boolean> containsValue(String tableName, byte[] value) {
public CompletableFuture<Boolean> mapContainsValue(String mapName, byte[] value) {
checkState(isOpen.get(), DB_NOT_OPEN);
AtomicBoolean containsValue = new AtomicBoolean(false);
return CompletableFuture.allOf(partitions
.stream()
.map(p -> p.containsValue(tableName, value).thenApply(v -> containsValue.compareAndSet(false, v)))
.map(p -> p.mapContainsValue(mapName, value)
.thenApply(v -> containsValue.compareAndSet(false, v)))
.toArray(CompletableFuture[]::new))
.thenApply(v -> containsValue.get());
}
@Override
public CompletableFuture<Versioned<byte[]>> get(String tableName, String key) {
public CompletableFuture<Versioned<byte[]>> mapGet(String mapName, String key) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).get(tableName, key);
return partitioner.getPartition(mapName, key).mapGet(mapName, key);
}
@Override
public CompletableFuture<Result<Versioned<byte[]>>> put(String tableName, String key, byte[] value) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).put(tableName, key, value);
}
@Override
public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> putAndGet(String tableName,
String key,
byte[] value) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).putAndGet(tableName, key, value);
}
@Override
public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> putIfAbsentAndGet(String tableName,
String key,
byte[] value) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).putIfAbsentAndGet(tableName, key, value);
}
public CompletableFuture<Result<UpdateResult<String, byte[]>>> mapUpdate(
String mapName, String key, Match<byte[]> valueMatch,
Match<Long> versionMatch, byte[] value) {
return partitioner.getPartition(mapName, key).mapUpdate(mapName, key, valueMatch, versionMatch, value);
@Override
public CompletableFuture<Result<Versioned<byte[]>>> remove(String tableName, String key) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).remove(tableName, key);
}
@Override
public CompletableFuture<Result<Void>> clear(String tableName) {
public CompletableFuture<Result<Void>> mapClear(String mapName) {
AtomicBoolean isLocked = new AtomicBoolean(false);
checkState(isOpen.get(), DB_NOT_OPEN);
return CompletableFuture.allOf(partitions
.stream()
.map(p -> p.clear(tableName)
.map(p -> p.mapClear(mapName)
.thenApply(v -> isLocked.compareAndSet(false, Result.Status.LOCKED == v.status())))
.toArray(CompletableFuture[]::new))
.thenApply(v -> isLocked.get() ? Result.locked() : Result.ok(null));
}
@Override
public CompletableFuture<Set<String>> keySet(String tableName) {
public CompletableFuture<Set<String>> mapKeySet(String mapName) {
checkState(isOpen.get(), DB_NOT_OPEN);
Set<String> keySet = Sets.newConcurrentHashSet();
return CompletableFuture.allOf(partitions
.stream()
.map(p -> p.keySet(tableName).thenApply(keySet::addAll))
.map(p -> p.mapKeySet(mapName).thenApply(keySet::addAll))
.toArray(CompletableFuture[]::new))
.thenApply(v -> keySet);
}
@Override
public CompletableFuture<Collection<Versioned<byte[]>>> values(String tableName) {
public CompletableFuture<Collection<Versioned<byte[]>>> mapValues(String mapName) {
checkState(isOpen.get(), DB_NOT_OPEN);
List<Versioned<byte[]>> values = new CopyOnWriteArrayList<>();
return CompletableFuture.allOf(partitions
.stream()
.map(p -> p.values(tableName).thenApply(values::addAll))
.map(p -> p.mapValues(mapName).thenApply(values::addAll))
.toArray(CompletableFuture[]::new))
.thenApply(v -> values);
}
@Override
public CompletableFuture<Set<Entry<String, Versioned<byte[]>>>> entrySet(String tableName) {
public CompletableFuture<Set<Entry<String, Versioned<byte[]>>>> mapEntrySet(String mapName) {
checkState(isOpen.get(), DB_NOT_OPEN);
Set<Entry<String, Versioned<byte[]>>> entrySet = Sets.newConcurrentHashSet();
return CompletableFuture.allOf(partitions
.stream()
.map(p -> p.entrySet(tableName).thenApply(entrySet::addAll))
.map(p -> p.mapEntrySet(mapName).thenApply(entrySet::addAll))
.toArray(CompletableFuture[]::new))
.thenApply(v -> entrySet);
}
@Override
public CompletableFuture<Result<Versioned<byte[]>>> putIfAbsent(String tableName, String key, byte[] value) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).putIfAbsent(tableName, key, value);
}
@Override
public CompletableFuture<Result<Boolean>> remove(String tableName, String key, byte[] value) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).remove(tableName, key, value);
}
@Override
public CompletableFuture<Result<Boolean>> remove(String tableName, String key, long version) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).remove(tableName, key, version);
}
@Override
public CompletableFuture<Result<Boolean>> replace(
String tableName, String key, byte[] oldValue, byte[] newValue) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).replace(tableName, key, oldValue, newValue);
}
@Override
public CompletableFuture<Result<Boolean>> replace(
String tableName, String key, long oldVersion, byte[] newValue) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).replace(tableName, key, oldVersion, newValue);
}
@Override
public CompletableFuture<Result<UpdateResult<Versioned<byte[]>>>> replaceAndGet(
String tableName, String key, long oldVersion, byte[] newValue) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(tableName, key).replaceAndGet(tableName, key, oldVersion, newValue);
}
@Override
public CompletableFuture<Long> counterGet(String counterName) {
checkState(isOpen.get(), DB_NOT_OPEN);
return partitioner.getPartition(counterName, counterName).counterGet(counterName);
......@@ -408,7 +350,7 @@ public class PartitionedDatabase implements Database {
Transaction transaction) {
Map<Database, List<DatabaseUpdate>> perPartitionUpdates = Maps.newHashMap();
for (DatabaseUpdate update : transaction.updates()) {
Database partition = partitioner.getPartition(update.tableName(), update.key());
Database partition = partitioner.getPartition(update.mapName(), update.key());
List<DatabaseUpdate> partitionUpdates =
perPartitionUpdates.computeIfAbsent(partition, k -> Lists.newLinkedList());
partitionUpdates.add(update);
......
......@@ -25,9 +25,9 @@ public interface Partitioner<K> {
/**
* Returns the database partition.
* @param tableName table name
* @param mapName map name
* @param key key
* @return Database partition
*/
Database getPartition(String tableName, K key);
Database getPartition(String mapName, K key);
}
......
......@@ -32,7 +32,7 @@ public class SimpleKeyHashPartitioner extends DatabasePartitioner {
}
@Override
public Database getPartition(String tableName, String key) {
public Database getPartition(String mapName, String key) {
return partitions.get(hash(key) % partitions.size());
}
}
\ No newline at end of file
......
......@@ -19,11 +19,11 @@ package org.onosproject.store.consistent.impl;
import java.util.List;
/**
* A simple Partitioner that uses the table name hash to
* A simple Partitioner that uses the map name hash to
* pick a partition.
* <p>
* This class uses a md5 hash based hashing scheme for hashing the table name to
* a partition. This partitioner maps all keys for a table to the same database
* This class uses a md5 hash based hashing scheme for hashing the map name to
* a partition. This partitioner maps all keys for a map to the same database
* partition.
*/
public class SimpleTableHashPartitioner extends DatabasePartitioner {
......@@ -33,7 +33,7 @@ public class SimpleTableHashPartitioner extends DatabasePartitioner {
}
@Override
public Database getPartition(String tableName, String key) {
return partitions.get(hash(tableName) % partitions.size());
public Database getPartition(String mapName, String key) {
return partitions.get(hash(mapName) % partitions.size());
}
}
......
......@@ -15,6 +15,11 @@
*/
package org.onosproject.store.consistent.impl;
import java.util.function.Function;
import org.onosproject.store.service.MapEvent;
import org.onosproject.store.service.Versioned;
/**
* Result of a update operation.
* <p>
......@@ -23,14 +28,18 @@ package org.onosproject.store.consistent.impl;
* point to the same unmodified value.
* @param <V> result type
*/
public class UpdateResult<V> {
public class UpdateResult<K, V> {
private final boolean updated;
private final V oldValue;
private final V newValue;
private final String mapName;
private final K key;
private final Versioned<V> oldValue;
private final Versioned<V> newValue;
public UpdateResult(boolean updated, V oldValue, V newValue) {
public UpdateResult(boolean updated, String mapName, K key, Versioned<V> oldValue, Versioned<V> newValue) {
this.updated = updated;
this.mapName = mapName;
this.key = key;
this.oldValue = oldValue;
this.newValue = newValue;
}
......@@ -39,11 +48,38 @@ public class UpdateResult<V> {
return updated;
}
public V oldValue() {
public String mapName() {
return mapName;
}
public K key() {
return key;
}
public Versioned<V> oldValue() {
return oldValue;
}
public V newValue() {
public Versioned<V> newValue() {
return newValue;
}
public <K1, V1> UpdateResult<K1, V1> map(Function<K, K1> keyTransform, Function<V, V1> valueMapper) {
return new UpdateResult<>(updated,
mapName,
keyTransform.apply(key),
oldValue == null ? null : oldValue.map(valueMapper),
newValue == null ? null : newValue.map(valueMapper));
}
public MapEvent<K, V> toMapEvent() {
if (!updated) {
return null;
} else {
MapEvent.Type eventType = oldValue == null ?
MapEvent.Type.INSERT : newValue == null ? MapEvent.Type.REMOVE : MapEvent.Type.UPDATE;
Versioned<V> eventValue = eventType == MapEvent.Type.REMOVE ? oldValue : newValue;
return new MapEvent<>(mapName(), eventType, key(), eventValue);
}
}
}
\ No newline at end of file
......