blob: e446a674b6e0a39f12df4d4fa6b6c837f3c7f95c [file] [log] [blame]
Aaron Kruglikova26f6542016-04-19 13:37:42 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Aaron Kruglikova26f6542016-04-19 13:37:42 -07003 *
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 Kruglikova1801aa2016-07-12 15:17:30 -070034import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Clear;
35import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.ContainsEntry;
36import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.ContainsKey;
37import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.ContainsValue;
38import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Entries;
39import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Get;
40import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.IsEmpty;
41import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.KeySet;
42import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Keys;
43import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.MultiRemove;
44import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Put;
45import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.RemoveAll;
46import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Replace;
47import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.Size;
48import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMultimapCommands.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 Kruglikova1801aa2016-07-12 15:17:30 -070055@ResourceTypeInfo(id = -153, factory = AtomixConsistentSetMultimapFactory.class)
56public class AtomixConsistentSetMultimap
57 extends AbstractResource<AtomixConsistentSetMultimap>
Aaron Kruglikova26f6542016-04-19 13:37:42 -070058 implements AsyncConsistentMultimap<String, byte[]> {
59
Aaron Kruglikova1801aa2016-07-12 15:17:30 -070060 public AtomixConsistentSetMultimap(CopycatClient client,
61 Properties properties) {
Aaron Kruglikova26f6542016-04-19 13:37:42 -070062 super(client, properties);
63 }
64
65 @Override
Aaron Kruglikova1801aa2016-07-12 15:17:30 -070066 public CompletableFuture<AtomixConsistentSetMultimap> open() {
Aaron Kruglikova26f6542016-04-19 13:37:42 -070067 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
Jonathan Hartad0c3022017-02-22 14:06:01 -0800150 @Override
Aaron Kruglikov44a1fef2016-04-27 11:29:23 -0700151 public CompletableFuture<Multiset<byte[]>> values() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700152 return client.submit(new Values());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700153 }
154
155 @Override
156 public CompletableFuture<Collection<Map.Entry<String, byte[]>>> entries() {
Madan Jampani630e7ac2016-05-31 11:34:05 -0700157 return client.submit(new Entries());
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700158 }
159
160 @Override
161 public CompletableFuture<Map<String, Collection<byte[]>>> asMap() {
Aaron Kruglikova26f6542016-04-19 13:37:42 -0700162 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}