blob: 45b4d56fcc4c378bf9d89ece37faca51a532ceb2 [file] [log] [blame]
Aaron Kruglikova26f6542016-04-19 13:37:42 -07001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.store.primitives.resources.impl;
18
19import com.google.common.collect.Lists;
Aaron Kruglikova26f6542016-04-19 13:37:42 -070020import com.google.common.collect.Multiset;
21import io.atomix.copycat.client.CopycatClient;
22import io.atomix.resource.AbstractResource;
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -070023import io.atomix.resource.ResourceTypeInfo;
Aaron Kruglikova26f6542016-04-19 13:37:42 -070024import org.onosproject.store.service.AsyncConsistentMultimap;
25import org.onosproject.store.service.Versioned;
26
27import java.util.Collection;
28import java.util.ConcurrentModificationException;
29import java.util.Map;
30import java.util.Properties;
31import java.util.Set;
32import java.util.concurrent.CompletableFuture;
33
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -070034import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Clear;
35import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsEntry;
36import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsKey;
37import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.ContainsValue;
38import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Entries;
39import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Get;
40import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.IsEmpty;
41import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.KeySet;
42import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Keys;
43import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.MultiRemove;
44import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Put;
45import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.RemoveAll;
46import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Replace;
47import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Size;
48import static org.onosproject.store.primitives.resources.impl.AsyncConsistentMultimapCommands.Values;
Aaron Kruglikova26f6542016-04-19 13:37:42 -070049
50/**
51 * Set based implementation of the {@link AsyncConsistentMultimap}.
52 * <p>
53 * Note: this implementation does not allow null entries or duplicate entries.
54 */
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -070055@ResourceTypeInfo(id = -153, factory = AsyncConsistentSetMultimapFactory.class)
Aaron Kruglikova26f6542016-04-19 13:37:42 -070056public class AsyncConsistentSetMultimap
57 extends AbstractResource<AsyncConsistentSetMultimap>
58 implements AsyncConsistentMultimap<String, byte[]> {
59
60 public AsyncConsistentSetMultimap(CopycatClient client,
61 Properties properties) {
62 super(client, properties);
63 }
64
65 @Override
66 public CompletableFuture<AsyncConsistentSetMultimap> open() {
67 return super.open();
68 //TODO
69 }
70
71 @Override
72 public CompletableFuture<Integer> size() {
Madan Jampani630e7ac2016-05-31 11:34:05 -070073 return client.submit(new Size());
Aaron Kruglikova26f6542016-04-19 13:37:42 -070074 }
75
76 @Override
77 public CompletableFuture<Boolean> isEmpty() {
Madan Jampani630e7ac2016-05-31 11:34:05 -070078 return client.submit(new IsEmpty());
Aaron Kruglikova26f6542016-04-19 13:37:42 -070079 }
80
81 @Override
82 public CompletableFuture<Boolean> containsKey(String key) {
Madan Jampani630e7ac2016-05-31 11:34:05 -070083 return client.submit(new ContainsKey(key));
Aaron Kruglikova26f6542016-04-19 13:37:42 -070084 }
85
86 @Override
87 public CompletableFuture<Boolean> containsValue(byte[] value) {
Madan Jampani630e7ac2016-05-31 11:34:05 -070088 return client.submit(new ContainsValue(value));
Aaron Kruglikova26f6542016-04-19 13:37:42 -070089 }
90
91 @Override
92 public CompletableFuture<Boolean> containsEntry(String key, byte[] value) {
Madan Jampani630e7ac2016-05-31 11:34:05 -070093 return client.submit(new ContainsEntry(key, value));
Aaron Kruglikova26f6542016-04-19 13:37:42 -070094 }
95
96 @Override
97 public CompletableFuture<Boolean> put(String key, byte[] value) {
Madan Jampani630e7ac2016-05-31 11:34:05 -070098 return client.submit(new Put(key, Lists.newArrayList(value), null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -070099 }
100
101 @Override
102 public CompletableFuture<Boolean> remove(String key, byte[] value) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700103 return client.submit(new MultiRemove(key,
104 Lists.newArrayList(value),
105 null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700106 }
107
108 @Override
Madan Jampani630e7ac2016-05-31 11:34:05 -0700109 public CompletableFuture<Boolean> removeAll(String key, Collection<? extends byte[]> values) {
110 return client.submit(new MultiRemove(key, (Collection<byte[]>) values, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700111 }
112
113 @Override
Madan Jampani630e7ac2016-05-31 11:34:05 -0700114 public CompletableFuture<Versioned<Collection<? extends byte[]>>> removeAll(String key) {
115 return client.submit(new RemoveAll(key, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700116 }
117
118 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700119 public CompletableFuture<Boolean> putAll(
120 String key, Collection<? extends byte[]> values) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700121 return client.submit(new Put(key, values, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700122 }
123
124 @Override
Madan Jampani630e7ac2016-05-31 11:34:05 -0700125 public CompletableFuture<Versioned<Collection<? extends byte[]>>> replaceValues(
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700126 String key, Collection<byte[]> values) {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700127 return client.submit(new Replace(key, values, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700128 }
129
130 @Override
131 public CompletableFuture<Void> clear() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700132 return client.submit(new Clear());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700133 }
134
135 @Override
Madan Jampani630e7ac2016-05-31 11:34:05 -0700136 public CompletableFuture<Versioned<Collection<? extends byte[]>>> get(String key) {
137 return client.submit(new Get(key));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700138 }
139
140 @Override
141 public CompletableFuture<Set<String>> keySet() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700142 return client.submit(new KeySet());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700143 }
144
145 @Override
146 public CompletableFuture<Multiset<String>> keys() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700147 return client.submit(new Keys());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700148 }
149
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700150 public CompletableFuture<Multiset<byte[]>> values() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700151 return client.submit(new Values());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700152 }
153
154 @Override
155 public CompletableFuture<Collection<Map.Entry<String, byte[]>>> entries() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700156 return client.submit(new Entries());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700157 }
158
159 @Override
160 public CompletableFuture<Map<String, Collection<byte[]>>> asMap() {
161 //TODO
162 throw new UnsupportedOperationException("Expensive operation.");
163 }
164
165 @Override
166 public String name() {
167 return null;
168 }
169
170 /**
171 * Helper to check if there was a lock based issue.
172 * @param status the status of an update result
173 */
174 private void throwIfLocked(MapEntryUpdateResult.Status status) {
175 if (status == MapEntryUpdateResult.Status.WRITE_LOCK) {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700176 throw new ConcurrentModificationException("Cannot update map: " +
177 "Another transaction " +
178 "in progress");
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700179 }
180 }
181}