blob: 3d735be45f5db2a18aba313e572733982e69c410 [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() {
73 return submit(new Size());
74 }
75
76 @Override
77 public CompletableFuture<Boolean> isEmpty() {
78 return submit(new IsEmpty());
79 }
80
81 @Override
82 public CompletableFuture<Boolean> containsKey(String key) {
83 return submit(new ContainsKey(key));
84 }
85
86 @Override
87 public CompletableFuture<Boolean> containsValue(byte[] value) {
88 return submit(new ContainsValue(value));
89 }
90
91 @Override
92 public CompletableFuture<Boolean> containsEntry(String key, byte[] value) {
93 return submit(new ContainsEntry(key, value));
94 }
95
96 @Override
97 public CompletableFuture<Boolean> put(String key, byte[] value) {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -070098 return 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) {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700103 return submit(new MultiRemove(key,
104 Lists.newArrayList(value),
105 null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700106 }
107
108 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700109 public CompletableFuture<Boolean> removeAll(
110 String key, Collection<? extends byte[]> values) {
111 return submit(new MultiRemove(key, (Collection<byte[]>) values, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700112 }
113
114 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700115 public CompletableFuture<
116 Versioned<Collection<? extends byte[]>>> removeAll(String key) {
117 return submit(new RemoveAll(key, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700118 }
119
120 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700121 public CompletableFuture<Boolean> putAll(
122 String key, Collection<? extends byte[]> values) {
123 return submit(new Put(key, values, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700124 }
125
126 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700127 public CompletableFuture<
128 Versioned<Collection<? extends byte[]>>> replaceValues(
129 String key, Collection<byte[]> values) {
130 return submit(new Replace(key, values, null));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700131 }
132
133 @Override
134 public CompletableFuture<Void> clear() {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700135 return submit(new Clear());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700136 }
137
138 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700139 public CompletableFuture<
140 Versioned<Collection<? extends byte[]>>> get(String key) {
141 return submit(new Get(key));
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700142 }
143
144 @Override
145 public CompletableFuture<Set<String>> keySet() {
146 return submit(new KeySet());
147 }
148
149 @Override
150 public CompletableFuture<Multiset<String>> keys() {
151 return submit(new Keys());
152 }
153
154 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700155 public CompletableFuture<Multiset<byte[]>> values() {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700156 return submit(new Values());
157 }
158
159 @Override
160 public CompletableFuture<Collection<Map.Entry<String, byte[]>>> entries() {
161 return submit(new Entries());
162 }
163
164 @Override
165 public CompletableFuture<Map<String, Collection<byte[]>>> asMap() {
166 //TODO
167 throw new UnsupportedOperationException("Expensive operation.");
168 }
169
170 @Override
171 public String name() {
172 return null;
173 }
174
175 /**
176 * Helper to check if there was a lock based issue.
177 * @param status the status of an update result
178 */
179 private void throwIfLocked(MapEntryUpdateResult.Status status) {
180 if (status == MapEntryUpdateResult.Status.WRITE_LOCK) {
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700181 throw new ConcurrentModificationException("Cannot update map: " +
182 "Another transaction " +
183 "in progress");
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700184 }
185 }
186}