Aaron Kruglikov
Committed by Madan Jampani

Updating multimap API and commands and providing implementation.

Change-Id: Iff49b429cfc7c0142f3ab2e1dde1a32e85f20e87
(cherry picked from commit 44a1fef9)
......@@ -16,7 +16,6 @@
package org.onosproject.store.service;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import java.util.Collection;
......@@ -92,7 +91,7 @@ public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
* and others ignoring put requests for existing entries.
* @param key the key to add
* @param value the value to add
* @return a future whose value will betrue if the map has changed because
* @return a future whose value will be true if the map has changed because
* of this call, false otherwise
*/
CompletableFuture<Boolean> put(K key, V value);
......@@ -119,16 +118,18 @@ public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
* @return a future whose value will be true if the map changes because of
* this call, false otherwise.
*/
CompletableFuture<Boolean> removeAll(K key, Iterable<? extends V> values);
CompletableFuture<Boolean> removeAll(K key,
Collection<? extends V> values);
/**
* Removes all values associated with the specified key as well as the key
* itself.
* @param key the key whose key-value pairs will be removed
* @return a future whose value is the set of values that were removed,
* which may be empty
* which may be empty, if the values did not exist the version will be
* less than one.
*/
CompletableFuture<Versioned<Collection<byte[]>>> removeAll(K key);
CompletableFuture<Versioned<Collection<? extends V>>> removeAll(K key);
/**
* Adds the set of key-value pairs of the specified key with each of the
......@@ -140,17 +141,8 @@ public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
* @return a future whose value will be true if any change in the map
* results from this call, false otherwise
*/
CompletableFuture<Boolean> putAll(K key, Iterable<? extends V> values);
/**
* Adds all entries from this multimap that are not already present, and
* may or may not add duplicate entries depending on the implementation.
* @param multiMap the map whose entries should be added
* @return a future whose value will be true if any change results from
* this call, false otherwise
*/
CompletableFuture<Boolean> putAll(
Multimap<? extends K, ? extends V> multiMap);
CompletableFuture<Boolean> putAll(K key,
Collection<? extends V> values);
/**
* Stores all the values in values associated with the key specified,
......@@ -161,7 +153,8 @@ public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
* @return a future whose value will be the collection of removed values,
* which may be empty
*/
CompletableFuture<Collection<V>> replaceValues(K key, Iterable<V> values);
CompletableFuture<Versioned<Collection<? extends V>>> replaceValues(
K key, Collection<V> values);
/**
* Removes all key-value pairs, after which it will be empty.
......@@ -177,7 +170,7 @@ public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
* @return a future whose value will be the collection of the values
* associated with the specified key, the collection may be empty
*/
CompletableFuture<Collection<V>> get(K key);
CompletableFuture<Versioned<Collection<? extends V>>> get(K key);
/**
* Returns a set of the keys contained in this multimap with one or more
......@@ -203,7 +196,7 @@ public interface AsyncConsistentMultimap<K, V> extends DistributedPrimitive {
* @return a future whose value will be a collection of values, this may be
* empty
*/
CompletableFuture<Collection<V>> values();
CompletableFuture<Multiset<V>> values();
/**
* Returns a collection of each key-value pair in this map.
......
......@@ -25,6 +25,7 @@ COMPILE_DEPS = [
TEST_DEPS = [
'//lib:TEST',
'//core/api:onos-api-tests',
'//lib:onos-atomix',
]
osgi_jar_with_tests (
......
......@@ -17,11 +17,10 @@
package org.onosproject.store.primitives.resources.impl;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;
import io.atomix.copycat.client.CopycatClient;
import io.atomix.resource.AbstractResource;
import org.onlab.util.Match;
import io.atomix.resource.ResourceTypeInfo;
import org.onosproject.store.service.AsyncConsistentMultimap;
import org.onosproject.store.service.Versioned;
......@@ -32,13 +31,28 @@ import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.*;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Clear;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsEntry;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsKey;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsValue;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Entries;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Get;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.IsEmpty;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.KeySet;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Keys;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.MultiRemove;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Put;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.RemoveAll;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Replace;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Size;
import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Values;
/**
* Set based implementation of the {@link AsyncConsistentMultimap}.
* <p>
* Note: this implementation does not allow null entries or duplicate entries.
*/
@ResourceTypeInfo(id = -153, factory = AsyncConsistentSetMultimapFactory.class)
public class AsyncConsistentSetMultimap
extends AbstractResource<AsyncConsistentSetMultimap>
implements AsyncConsistentMultimap<String, byte[]> {
......@@ -81,68 +95,50 @@ public class AsyncConsistentSetMultimap
@Override
public CompletableFuture<Boolean> put(String key, byte[] value) {
return submit(new UpdateAndGet(key, Lists.newArrayList(value),
Lists.newArrayList(Match.NULL),
Lists.newArrayList(Match.NULL)))
.whenComplete((result, e) -> throwIfLocked(result.status()))
.thenApply(result ->
result.status() == MapEntryUpdateResult.Status.OK);
return submit(new Put(key, Lists.newArrayList(value), null));
}
@Override
public CompletableFuture<Boolean> remove(String key, byte[] value) {
return submit(new UpdateAndGet(key, Lists.newArrayList(value),
Lists.newArrayList(Match.ifValue(value)),
Lists.newArrayList(Match.NULL)))
.whenComplete((result, e) -> throwIfLocked(result.status()))
.thenApply(result ->
result.status() == MapEntryUpdateResult.Status.OK);
return submit(new MultiRemove(key,
Lists.newArrayList(value),
null));
}
@Override
public CompletableFuture<Boolean> removeAll(String key, Iterable<? extends byte[]> values) {
throw new UnsupportedOperationException("This operation cannot be " +
"used without support for " +
"transactions.");
}
@Override
public CompletableFuture<Versioned<Collection<byte[]>>> removeAll(String key) {
return submit(new UpdateAndGet(key, null, null, null))
.whenComplete((result, e) -> throwIfLocked(result.status()))
.thenApply(result -> result.oldValue());
public CompletableFuture<Boolean> removeAll(
String key, Collection<? extends byte[]> values) {
return submit(new MultiRemove(key, (Collection<byte[]>) values, null));
}
@Override
public CompletableFuture<Boolean> putAll(String key, Iterable<? extends byte[]> values) {
throw new UnsupportedOperationException("This operation cannot be " +
"used without support for " +
"transactions.");
public CompletableFuture<
Versioned<Collection<? extends byte[]>>> removeAll(String key) {
return submit(new RemoveAll(key, null));
}
@Override
public CompletableFuture<Boolean> putAll(Multimap<? extends String, ? extends byte[]> multiMap) {
throw new UnsupportedOperationException("This operation cannot be " +
"used without support for " +
"transactions.");
public CompletableFuture<Boolean> putAll(
String key, Collection<? extends byte[]> values) {
return submit(new Put(key, values, null));
}
@Override
public CompletableFuture<Collection<byte[]>> replaceValues(String key, Iterable<byte[]> values) {
throw new UnsupportedOperationException("This operation cannot be " +
"used without support for " +
"transactions.");
public CompletableFuture<
Versioned<Collection<? extends byte[]>>> replaceValues(
String key, Collection<byte[]> values) {
return submit(new Replace(key, values, null));
}
@Override
public CompletableFuture<Void> clear() {
return submit(new AsyncConsistentMultimapCommands.Clear());
return submit(new Clear());
}
@Override
public CompletableFuture<Collection<byte[]>> get(String key) {
return submit(new Get());
public CompletableFuture<
Versioned<Collection<? extends byte[]>>> get(String key) {
return submit(new Get(key));
}
@Override
......@@ -156,7 +152,7 @@ public class AsyncConsistentSetMultimap
}
@Override
public CompletableFuture<Collection<byte[]>> values() {
public CompletableFuture<Multiset<byte[]>> values() {
return submit(new Values());
}
......@@ -182,7 +178,9 @@ public class AsyncConsistentSetMultimap
*/
private void throwIfLocked(MapEntryUpdateResult.Status status) {
if (status == MapEntryUpdateResult.Status.WRITE_LOCK) {
throw new ConcurrentModificationException("Cannot update map: Another transaction in progress");
throw new ConcurrentModificationException("Cannot update map: " +
"Another transaction " +
"in progress");
}
}
}
......
/*
* Copyright 2016 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.primitives.resources.impl;
import io.atomix.catalyst.serializer.SerializableTypeResolver;
import io.atomix.copycat.client.CopycatClient;
import io.atomix.resource.ResourceFactory;
import io.atomix.resource.ResourceStateMachine;
import java.util.Properties;
/**
* {@link AsyncConsistentSetMultimap} resource factory.
*/
public class AsyncConsistentSetMultimapFactory implements
ResourceFactory<AsyncConsistentSetMultimap> {
@Override
public SerializableTypeResolver createSerializableTypeResolver() {
return new AsyncConsistentMultimapCommands.TypeResolver();
}
@Override
public ResourceStateMachine createStateMachine(Properties config) {
return new AsyncConsistentSetMultimapState(config);
}
@Override
public AsyncConsistentSetMultimap createInstance(CopycatClient client,
Properties properties) {
return new AsyncConsistentSetMultimap(client, properties);
}
}