Jian Li
Committed by Gerrit Code Review

[ONOS-2809] Support a TransactionalSet data structure

Change-Id: Ia99bc2285b3fea39ee3845f5f6613a45a6a61626
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 +/**
19 + * Transactional Set data structure.
20 + * <p>
21 + * A TransactionalSet is implemented with the help of TransactionalMap data structure.
22 + * All operations performed on this set within a transaction boundary are invisible externally
23 + * until the point when the transaction commits. A commit usually succeeds in the absence of conflicts.
24 + *
25 + * @param <E> type of element.
26 + */
27 +public interface TransactionalSet<E> {
28 +
29 + /**
30 + * Adds the specified element to this set if it is not already present
31 + * (optional operation). More formally, adds the specified element
32 + * <tt>e</tt> to this set if the set contains no element <tt>e2</tt>
33 + * such that
34 + * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
35 + * If this set already contains the element, the call leaves the set
36 + * unchanged and returns <tt>false</tt>. In combination with the
37 + * restriction on constructors, this ensures that sets never contain
38 + * duplicate elements.
39 + *
40 + * @param e element to be added to this set
41 + * @return <tt>true</tt> if this set did not already contain the specified
42 + * element
43 + */
44 + boolean add(E e);
45 +
46 + /**
47 + * Removes the specified element from this set if it is present
48 + * (optional operation). More formally, removes an element <tt>e</tt>
49 + * such that
50 + * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>, if
51 + * this set contains such an element. Returns <tt>true</tt> if this set
52 + * contained the element (or equivalently, if this set changed as a
53 + * result of the call). (This set will not contain the element once the
54 + * call returns.)
55 + *
56 + * @param e element to be removed to this set
57 + * @return <tt>true</tt> if this set contained the specified element
58 + */
59 + boolean remove(E e);
60 +
61 + /**
62 + * Returns <tt>true</tt> if this set contains the specified element.
63 + * More formally, returns <tt>true</tt> if and only if this set
64 + * contains an element <tt>e</tt> such that
65 + * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
66 + *
67 + * @param e element whose presence in this set is to be tested
68 + * @return <tt>true</tt> if this set contains the specified element
69 + * @throws ClassCastException if the type of the specified element
70 + * is incompatible with this set
71 + * @throws NullPointerException if the specified element is null and this
72 + * set does not permit null elements
73 + */
74 + boolean contains(E e);
75 +}
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 +
19 +import org.onosproject.store.service.Serializer;
20 +import org.onosproject.store.service.TransactionContext;
21 +import org.onosproject.store.service.TransactionalMap;
22 +import org.onosproject.store.service.TransactionalSet;
23 +
24 +/**
25 + * Default TransactionalSet implementation that provides a repeatable reads
26 + * transaction isolation level.
27 + *
28 + * @param <E> element type.
29 + */
30 +public class DefaultTransactionalSet<E> implements TransactionalSet<E> {
31 +
32 + private TransactionalMap<E, Boolean> map;
33 +
34 + // dummy value to associate with an Object in the backing map
35 + private static final Boolean PRESENT = new Boolean(true);
36 +
37 + public DefaultTransactionalSet(
38 + String name,
39 + TransactionContext txContext,
40 + Serializer serializer) {
41 + map = txContext.getTransactionalMap(name, serializer);
42 + }
43 +
44 + @Override
45 + public boolean add(E e) {
46 + return map.put(e, PRESENT) == null;
47 + }
48 +
49 + @Override
50 + public boolean remove(E e) {
51 + return map.remove(e) != null;
52 + }
53 +
54 + @Override
55 + public boolean contains(E e) {
56 + return map.get(e) != null;
57 + }
58 +}