Added support for a distributed set implementation that is backed by Consistent Map
Change-Id: Ic393d305d31abcdf1dd4c9afc3b9234f8d50da2d
Showing
5 changed files
with
264 additions
and
1 deletions
1 | +/* | ||
2 | + * Copyright 2015 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 | +package org.onosproject.store.service; | ||
17 | + | ||
18 | +import java.util.Set; | ||
19 | + | ||
20 | +/** | ||
21 | + * Builder for distributed set. | ||
22 | + * | ||
23 | + * @param <E> type set elements. | ||
24 | + */ | ||
25 | +public interface SetBuilder<E> { | ||
26 | + | ||
27 | + /** | ||
28 | + * Sets the name of the set. | ||
29 | + * <p> | ||
30 | + * Each set is identified by a unique name. | ||
31 | + * </p> | ||
32 | + * <p> | ||
33 | + * Note: This is a mandatory parameter. | ||
34 | + * </p> | ||
35 | + * | ||
36 | + * @param name name of the set | ||
37 | + * @return this SetBuilder | ||
38 | + */ | ||
39 | + public SetBuilder<E> withName(String name); | ||
40 | + | ||
41 | + /** | ||
42 | + * Sets a serializer that can be used to serialize | ||
43 | + * the elements add to the set. The serializer | ||
44 | + * builder should be pre-populated with any classes that will be | ||
45 | + * put into the set. | ||
46 | + * <p> | ||
47 | + * Note: This is a mandatory parameter. | ||
48 | + * </p> | ||
49 | + * | ||
50 | + * @param serializer serializer | ||
51 | + * @return this SetBuilder | ||
52 | + */ | ||
53 | + public SetBuilder<E> withSerializer(Serializer serializer); | ||
54 | + | ||
55 | + /** | ||
56 | + * Builds an set based on the configuration options | ||
57 | + * supplied to this builder. | ||
58 | + * | ||
59 | + * @return new set | ||
60 | + * @throws java.lang.RuntimeException if a mandatory parameter is missing | ||
61 | + */ | ||
62 | + public Set<E> build(); | ||
63 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -52,4 +52,12 @@ public interface StorageService { | ... | @@ -52,4 +52,12 @@ public interface StorageService { |
52 | * @return builder for an eventually consistent map | 52 | * @return builder for an eventually consistent map |
53 | */ | 53 | */ |
54 | <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder(); | 54 | <K, V> ConsistentMapBuilder<K, V> consistentMapBuilder(); |
55 | + | ||
56 | + /** | ||
57 | + * Creates a new distributed set builder. | ||
58 | + * | ||
59 | + * @param <E> set element type | ||
60 | + * @return builder for an distributed set | ||
61 | + */ | ||
62 | + <E> SetBuilder<E> setBuilder(); | ||
55 | } | 63 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -19,6 +19,7 @@ package org.onosproject.store.consistent.impl; | ... | @@ -19,6 +19,7 @@ package org.onosproject.store.consistent.impl; |
19 | import com.google.common.collect.ImmutableSet; | 19 | import com.google.common.collect.ImmutableSet; |
20 | import com.google.common.collect.Lists; | 20 | import com.google.common.collect.Lists; |
21 | import com.google.common.collect.Sets; | 21 | import com.google.common.collect.Sets; |
22 | + | ||
22 | import net.kuujo.copycat.CopycatConfig; | 23 | import net.kuujo.copycat.CopycatConfig; |
23 | import net.kuujo.copycat.cluster.ClusterConfig; | 24 | import net.kuujo.copycat.cluster.ClusterConfig; |
24 | import net.kuujo.copycat.cluster.Member; | 25 | import net.kuujo.copycat.cluster.Member; |
... | @@ -32,6 +33,7 @@ import net.kuujo.copycat.netty.NettyTcpProtocol; | ... | @@ -32,6 +33,7 @@ import net.kuujo.copycat.netty.NettyTcpProtocol; |
32 | import net.kuujo.copycat.protocol.Consistency; | 33 | import net.kuujo.copycat.protocol.Consistency; |
33 | import net.kuujo.copycat.protocol.Protocol; | 34 | import net.kuujo.copycat.protocol.Protocol; |
34 | import net.kuujo.copycat.util.concurrent.NamedThreadFactory; | 35 | import net.kuujo.copycat.util.concurrent.NamedThreadFactory; |
36 | + | ||
35 | import org.apache.felix.scr.annotations.Activate; | 37 | import org.apache.felix.scr.annotations.Activate; |
36 | import org.apache.felix.scr.annotations.Component; | 38 | import org.apache.felix.scr.annotations.Component; |
37 | import org.apache.felix.scr.annotations.Deactivate; | 39 | import org.apache.felix.scr.annotations.Deactivate; |
... | @@ -48,6 +50,7 @@ import org.onosproject.store.service.ConsistentMapException; | ... | @@ -48,6 +50,7 @@ import org.onosproject.store.service.ConsistentMapException; |
48 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; | 50 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
49 | import org.onosproject.store.service.MapInfo; | 51 | import org.onosproject.store.service.MapInfo; |
50 | import org.onosproject.store.service.PartitionInfo; | 52 | import org.onosproject.store.service.PartitionInfo; |
53 | +import org.onosproject.store.service.SetBuilder; | ||
51 | import org.onosproject.store.service.StorageAdminService; | 54 | import org.onosproject.store.service.StorageAdminService; |
52 | import org.onosproject.store.service.StorageService; | 55 | import org.onosproject.store.service.StorageService; |
53 | import org.onosproject.store.service.TransactionContext; | 56 | import org.onosproject.store.service.TransactionContext; |
... | @@ -301,6 +304,11 @@ public class DatabaseManager implements StorageService, StorageAdminService { | ... | @@ -301,6 +304,11 @@ public class DatabaseManager implements StorageService, StorageAdminService { |
301 | } | 304 | } |
302 | 305 | ||
303 | @Override | 306 | @Override |
307 | + public <E> SetBuilder<E> setBuilder() { | ||
308 | + return new DefaultSetBuilder<>(partitionedDatabase); | ||
309 | + } | ||
310 | + | ||
311 | + @Override | ||
304 | public List<MapInfo> getMapInfo() { | 312 | public List<MapInfo> getMapInfo() { |
305 | List<MapInfo> maps = Lists.newArrayList(); | 313 | List<MapInfo> maps = Lists.newArrayList(); |
306 | maps.addAll(getMapInfo(inMemoryDatabase)); | 314 | maps.addAll(getMapInfo(inMemoryDatabase)); |
... | @@ -328,4 +336,4 @@ public class DatabaseManager implements StorageService, StorageAdminService { | ... | @@ -328,4 +336,4 @@ public class DatabaseManager implements StorageService, StorageAdminService { |
328 | throw new ConsistentMapException(e.getCause()); | 336 | throw new ConsistentMapException(e.getCause()); |
329 | } | 337 | } |
330 | } | 338 | } |
331 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
339 | +} | ... | ... |
core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultDistributedSet.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2015 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 | +package org.onosproject.store.consistent.impl; | ||
17 | + | ||
18 | +import java.util.Collection; | ||
19 | +import java.util.Iterator; | ||
20 | +import java.util.Set; | ||
21 | +import org.onosproject.store.service.ConsistentMap; | ||
22 | +import org.onosproject.store.service.Serializer; | ||
23 | + | ||
24 | +import com.google.common.collect.Sets; | ||
25 | + | ||
26 | +/** | ||
27 | + * Implementation of distributed set that is backed by a ConsistentMap. | ||
28 | + | ||
29 | + * @param <E> set element type | ||
30 | + */ | ||
31 | +public class DefaultDistributedSet<E> implements Set<E> { | ||
32 | + | ||
33 | + private final ConsistentMap<E, Boolean> backingMap; | ||
34 | + | ||
35 | + public DefaultDistributedSet(String name, Database database, Serializer serializer) { | ||
36 | + backingMap = new DefaultConsistentMap<>(name, database, serializer); | ||
37 | + } | ||
38 | + | ||
39 | + @Override | ||
40 | + public int size() { | ||
41 | + return backingMap.size(); | ||
42 | + } | ||
43 | + | ||
44 | + @Override | ||
45 | + public boolean isEmpty() { | ||
46 | + return backingMap.isEmpty(); | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public boolean contains(Object o) { | ||
51 | + return backingMap.containsKey((E) o); | ||
52 | + } | ||
53 | + | ||
54 | + @Override | ||
55 | + public Iterator<E> iterator() { | ||
56 | + return backingMap.keySet().iterator(); | ||
57 | + } | ||
58 | + | ||
59 | + @Override | ||
60 | + public Object[] toArray() { | ||
61 | + return backingMap.keySet().stream().toArray(); | ||
62 | + } | ||
63 | + | ||
64 | + @Override | ||
65 | + public <T> T[] toArray(T[] a) { | ||
66 | + return backingMap.keySet().stream().toArray(size -> a); | ||
67 | + } | ||
68 | + | ||
69 | + @Override | ||
70 | + public boolean add(E e) { | ||
71 | + return backingMap.putIfAbsent(e, true) == null; | ||
72 | + } | ||
73 | + | ||
74 | + @Override | ||
75 | + public boolean remove(Object o) { | ||
76 | + return backingMap.remove((E) o, true); | ||
77 | + } | ||
78 | + | ||
79 | + @Override | ||
80 | + public boolean containsAll(Collection<?> c) { | ||
81 | + return c.stream() | ||
82 | + .allMatch(this::contains); | ||
83 | + } | ||
84 | + | ||
85 | + @Override | ||
86 | + public boolean addAll(Collection<? extends E> c) { | ||
87 | + return c.stream() | ||
88 | + .map(this::add) | ||
89 | + .reduce(Boolean::logicalOr) | ||
90 | + .orElse(false); | ||
91 | + } | ||
92 | + | ||
93 | + @Override | ||
94 | + public boolean retainAll(Collection<?> c) { | ||
95 | + Set<?> retainSet = Sets.newHashSet(c); | ||
96 | + return backingMap.keySet() | ||
97 | + .stream() | ||
98 | + .filter(k -> !retainSet.contains(k)) | ||
99 | + .map(this::remove) | ||
100 | + .reduce(Boolean::logicalOr) | ||
101 | + .orElse(false); | ||
102 | + } | ||
103 | + | ||
104 | + @Override | ||
105 | + public boolean removeAll(Collection<?> c) { | ||
106 | + Set<?> removeSet = Sets.newHashSet(c); | ||
107 | + return backingMap.keySet() | ||
108 | + .stream() | ||
109 | + .filter(removeSet::contains) | ||
110 | + .map(this::remove) | ||
111 | + .reduce(Boolean::logicalOr) | ||
112 | + .orElse(false); | ||
113 | + } | ||
114 | + | ||
115 | + @Override | ||
116 | + public void clear() { | ||
117 | + backingMap.clear(); | ||
118 | + } | ||
119 | +} |
core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultSetBuilder.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2015 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 | +package org.onosproject.store.consistent.impl; | ||
17 | + | ||
18 | +import static com.google.common.base.Preconditions.checkArgument; | ||
19 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
20 | +import static com.google.common.base.Preconditions.checkState; | ||
21 | + | ||
22 | +import java.util.Set; | ||
23 | + | ||
24 | +import org.onosproject.store.service.Serializer; | ||
25 | +import org.onosproject.store.service.SetBuilder; | ||
26 | + | ||
27 | +/** | ||
28 | + * Default Set builder. | ||
29 | + * | ||
30 | + * @param <E> type for set elements | ||
31 | + */ | ||
32 | +public class DefaultSetBuilder<E> implements SetBuilder<E> { | ||
33 | + | ||
34 | + private Serializer serializer; | ||
35 | + private String name; | ||
36 | + private final Database database; | ||
37 | + | ||
38 | + public DefaultSetBuilder(Database database) { | ||
39 | + this.database = checkNotNull(database); | ||
40 | + } | ||
41 | + | ||
42 | + @Override | ||
43 | + public SetBuilder<E> withName(String name) { | ||
44 | + checkArgument(name != null && !name.isEmpty()); | ||
45 | + this.name = name; | ||
46 | + return this; | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public SetBuilder<E> withSerializer(Serializer serializer) { | ||
51 | + checkArgument(serializer != null); | ||
52 | + this.serializer = serializer; | ||
53 | + return this; | ||
54 | + } | ||
55 | + | ||
56 | + private boolean validInputs() { | ||
57 | + return name != null && serializer != null; | ||
58 | + } | ||
59 | + | ||
60 | + @Override | ||
61 | + public Set<E> build() { | ||
62 | + checkState(validInputs()); | ||
63 | + return new DefaultDistributedSet<>(name, database, serializer); | ||
64 | + } | ||
65 | +} |
-
Please register or login to post a comment