From fc5ca93765a4b055a24fa8793771e94e3253d975 Mon Sep 17 00:00:00 2001 From: Jian Li Date: Thu, 10 Dec 2015 12:11:57 -0800 Subject: [PATCH] [ONOS-2809] Support a TransactionalSet data structure Change-Id: Ia99bc2285b3fea39ee3845f5f6613a45a6a61626 --- .../store/service/TransactionalSet.java | 75 +++++++++++++++++++ .../impl/DefaultTransactionalSet.java | 58 ++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 core/api/src/main/java/org/onosproject/store/service/TransactionalSet.java create mode 100644 core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultTransactionalSet.java diff --git a/core/api/src/main/java/org/onosproject/store/service/TransactionalSet.java b/core/api/src/main/java/org/onosproject/store/service/TransactionalSet.java new file mode 100644 index 0000000000..97d9fe7346 --- /dev/null +++ b/core/api/src/main/java/org/onosproject/store/service/TransactionalSet.java @@ -0,0 +1,75 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.service; + +/** + * Transactional Set data structure. + *

+ * A TransactionalSet is implemented with the help of TransactionalMap data structure. + * All operations performed on this set within a transaction boundary are invisible externally + * until the point when the transaction commits. A commit usually succeeds in the absence of conflicts. + * + * @param type of element. + */ +public interface TransactionalSet { + + /** + * Adds the specified element to this set if it is not already present + * (optional operation). More formally, adds the specified element + * e to this set if the set contains no element e2 + * such that + * (e==null ? e2==null : e.equals(e2)). + * If this set already contains the element, the call leaves the set + * unchanged and returns false. In combination with the + * restriction on constructors, this ensures that sets never contain + * duplicate elements. + * + * @param e element to be added to this set + * @return true if this set did not already contain the specified + * element + */ + boolean add(E e); + + /** + * Removes the specified element from this set if it is present + * (optional operation). More formally, removes an element e + * such that + * (o==null ? e==null : o.equals(e)), if + * this set contains such an element. Returns true if this set + * contained the element (or equivalently, if this set changed as a + * result of the call). (This set will not contain the element once the + * call returns.) + * + * @param e element to be removed to this set + * @return true if this set contained the specified element + */ + boolean remove(E e); + + /** + * Returns true if this set contains the specified element. + * More formally, returns true if and only if this set + * contains an element e such that + * (o==null ? e==null : o.equals(e)). + * + * @param e element whose presence in this set is to be tested + * @return true if this set contains the specified element + * @throws ClassCastException if the type of the specified element + * is incompatible with this set + * @throws NullPointerException if the specified element is null and this + * set does not permit null elements + */ + boolean contains(E e); +} diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultTransactionalSet.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultTransactionalSet.java new file mode 100644 index 0000000000..11fcb80881 --- /dev/null +++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DefaultTransactionalSet.java @@ -0,0 +1,58 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.store.consistent.impl; + + +import org.onosproject.store.service.Serializer; +import org.onosproject.store.service.TransactionContext; +import org.onosproject.store.service.TransactionalMap; +import org.onosproject.store.service.TransactionalSet; + +/** + * Default TransactionalSet implementation that provides a repeatable reads + * transaction isolation level. + * + * @param element type. + */ +public class DefaultTransactionalSet implements TransactionalSet { + + private TransactionalMap map; + + // dummy value to associate with an Object in the backing map + private static final Boolean PRESENT = new Boolean(true); + + public DefaultTransactionalSet( + String name, + TransactionContext txContext, + Serializer serializer) { + map = txContext.getTransactionalMap(name, serializer); + } + + @Override + public boolean add(E e) { + return map.put(e, PRESENT) == null; + } + + @Override + public boolean remove(E e) { + return map.remove(e) != null; + } + + @Override + public boolean contains(E e) { + return map.get(e) != null; + } +}