diff options
Diffstat (limited to 'framework/src/onos/core/store/persistence/src/main')
9 files changed, 0 insertions, 1216 deletions
diff --git a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/DefaultPersistentMapBuilder.java b/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/DefaultPersistentMapBuilder.java deleted file mode 100644 index 88c7d148..00000000 --- a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/DefaultPersistentMapBuilder.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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.persistence.impl; - -import org.mapdb.DB; -import org.onosproject.persistence.PersistentMapBuilder; -import org.onosproject.store.service.Serializer; - -import java.util.Map; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Default builder for persistent maps stored in the mapDB local database via the persistence service. - */ -public class DefaultPersistentMapBuilder<K, V> implements PersistentMapBuilder<K, V> { - - private final DB localDB; - - private String name = null; - - private Serializer serializer = null; - - - public DefaultPersistentMapBuilder(DB localDB) { - checkNotNull(localDB, "The local database cannot be null."); - this.localDB = localDB; - } - - public PersistentMapBuilder<K, V> withName(String name) { - this.name = PersistenceManager.MAP_PREFIX + checkNotNull(name); - return this; - } - - public PersistentMapBuilder<K, V> withSerializer(Serializer serializer) { - checkArgument(this.serializer == null); - checkNotNull(serializer); - this.serializer = serializer; - return this; - } - - public Map<K, V> build() { - checkNotNull(name, "The name must be assigned."); - checkNotNull(serializer, "The key serializer must be assigned."); - - return new PersistentMap<K, V>(serializer, localDB, name); - } -}
\ No newline at end of file diff --git a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/DefaultPersistentSetBuilder.java b/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/DefaultPersistentSetBuilder.java deleted file mode 100644 index e1544fb4..00000000 --- a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/DefaultPersistentSetBuilder.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.persistence.impl; - -import org.mapdb.DB; -import org.onosproject.persistence.PersistentSetBuilder; -import org.onosproject.store.service.Serializer; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Default builder for persistent sets stored in the mapDB local database via the persistence service.. - */ -public class DefaultPersistentSetBuilder<E> implements PersistentSetBuilder<E> { - - private final DB localDB; - - private String name = null; - - private Serializer serializer = null; - - public DefaultPersistentSetBuilder(DB localDB) { - this.localDB = checkNotNull(localDB, "The local database cannot be null."); - } - - public PersistentSetBuilder<E> withName(String name) { - this.name = PersistenceManager.SET_PREFIX + checkNotNull(name); - return this; - } - - public PersistentSetBuilder<E> withSerializer(Serializer serializer) { - checkArgument(this.serializer == null); - checkNotNull(serializer); - this.serializer = serializer; - return this; - } - - public PersistentSet<E> build() { - checkNotNull(name, "The name must be assigned."); - checkNotNull(serializer, "The serializer must be assigned."); - - return new PersistentSet<E>(serializer, localDB, name); - } -}
\ No newline at end of file diff --git a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceException.java b/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceException.java deleted file mode 100644 index f2ba20c2..00000000 --- a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceException.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.persistence.impl; - -/** - * An exception defined for failures of the local persistent store system. - */ - -/** - * Throws an exception with the specified message. - */ -public class PersistenceException extends RuntimeException { - public PersistenceException(String s) { - super(s); - } -} diff --git a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceManager.java b/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceManager.java deleted file mode 100644 index 05c577c0..00000000 --- a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistenceManager.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.persistence.impl; - -import org.apache.felix.scr.annotations.Activate; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Deactivate; -import org.apache.felix.scr.annotations.Service; -import org.mapdb.DB; -import org.mapdb.DBMaker; -import org.onosproject.persistence.PersistenceService; -import org.onosproject.persistence.PersistentMapBuilder; -import org.onosproject.persistence.PersistentSetBuilder; -import org.slf4j.Logger; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Map; -import java.util.Set; -import java.util.Timer; -import java.util.TimerTask; - -import static org.slf4j.LoggerFactory.getLogger; - -/** - * Service that maintains local disk backed maps and sets. This implementation automatically deletes empty structures - * on shutdown. - */ -@Component(immediate = true) -@Service -public class PersistenceManager implements PersistenceService { - - private static final String DATABASE_PATH = "../data/localDB"; - private static final String ENCLOSING_FOLDER = "../data"; - - static final String MAP_PREFIX = "map:"; - - static final String SET_PREFIX = "set:"; - - private final Logger log = getLogger(getClass()); - - private DB localDB = null; - - private static final int FLUSH_FREQUENCY_MILLIS = 3000; - - private Timer timer; - - private final CommitTask commitTask = new CommitTask(); - - @Activate - public void activate() { - timer = new Timer(); - Path dbPath = Paths.get(DATABASE_PATH); - Path dbFolderPath = Paths.get(ENCLOSING_FOLDER); - //Make sure the directory exists, if it does not, make it. - if (!dbFolderPath.toFile().isDirectory()) { - log.info("The specified folder location for the database did not exist and will be created."); - try { - Files.createDirectories(dbFolderPath); - } catch (IOException e) { - log.error("Could not create the required folder for the database."); - throw new PersistenceException("Database folder could not be created."); - } - } - //Notify if the database file does not exist. - boolean dbFound = Files.exists(dbPath); - if (!dbFound) { - log.info("The database file could not be located, a new database will be constructed."); - - } else { - log.info("A previous database file has been found."); - } - localDB = DBMaker.newFileDB(dbPath.toFile()) - .asyncWriteEnable() - .closeOnJvmShutdown() - .make(); - timer.schedule(commitTask, FLUSH_FREQUENCY_MILLIS, FLUSH_FREQUENCY_MILLIS); - log.info("Started"); - } - - @Deactivate - public void deactivate() { - timer.cancel(); - for (Map.Entry<String, Object> entry : localDB.getAll().entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue(); - //This is a map implementation to be handled as such - if (value instanceof Map) { - Map asMap = (Map) value; - if (asMap.isEmpty()) { - //the map is empty and may be deleted - localDB.delete(key); - } - //This is a set implementation and can be handled as such - } else if (value instanceof Set) { - Set asSet = (Set) value; - if (asSet.isEmpty()) { - //the set is empty and may be deleted - localDB.delete(key); - } - } - } - localDB.commit(); - localDB.close(); - log.info("Stopped"); - } - - public <K, V> PersistentMapBuilder<K, V> persistentMapBuilder() { - return new DefaultPersistentMapBuilder<>(localDB); - } - - public <E> PersistentSetBuilder<E> persistentSetBuilder() { - return new DefaultPersistentSetBuilder<>(localDB); - } - - private class CommitTask extends TimerTask { - - @Override - public void run() { - localDB.commit(); - } - } -}
\ No newline at end of file diff --git a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistentMap.java b/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistentMap.java deleted file mode 100644 index 4506bbda..00000000 --- a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistentMap.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * 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.persistence.impl; - -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.mapdb.DB; -import org.mapdb.Hasher; -import org.onosproject.store.service.Serializer; - -import java.util.Collection; -import java.util.Map; -import java.util.Set; - -import static com.google.common.base.Preconditions.checkNotNull; - - -/** - * A map implementation that stores and receives all data from a serialized internal map. - */ -public class PersistentMap<K, V> implements Map<K, V> { - - private final Serializer serializer; - - private final org.mapdb.DB database; - - private final Map<byte[], byte[]> items; - - private final String name; - - public PersistentMap(Serializer serializer, DB database, String name) { - this.serializer = checkNotNull(serializer); - this.database = checkNotNull(database); - this.name = checkNotNull(name); - - items = database - .createHashMap(name) - .keySerializer(org.mapdb.Serializer.BYTE_ARRAY) - .valueSerializer(org.mapdb.Serializer.BYTE_ARRAY) - .hasher(Hasher.BYTE_ARRAY) - .makeOrGet(); - } - - /** - * Reads this set in deserialized form into the provided map. - * - * @param items the map to be populated - */ - public void readInto(Map<K, V> items) { - this.items.forEach((keyBytes, valueBytes) -> - items.put(serializer.decode(keyBytes), - serializer.decode(valueBytes))); - } - - @Override - public V remove(Object key) { - checkNotNull(key, "Key can not be null."); - V removed = get(key); - items.remove(serializer.encode(key)); - return removed; - } - - @Override - public int size() { - return items.size(); - } - - @Override - public boolean isEmpty() { - return items.isEmpty(); - } - - @Override - public boolean containsKey(Object key) { - checkNotNull(key, "Key cannot be null."); - return items.containsKey(serializer.encode(key)); - } - - @Override - public boolean containsValue(Object value) { - checkNotNull(value, "Value cannot be null."); - byte[] serialized = serializer.encode(value); - for (byte[] compareValue : items.values()) { - boolean same = true; - if (compareValue == null) { - same = false; - } else if (compareValue.length != serialized.length) { - same = false; - } else { - for (int i = 0; i < serialized.length; i++) { - if (serialized[i] != compareValue[i]) { - same = false; - break; - } - } - } - if (same) { - return true; - } - } - return false; - } - - @Override - public V get(Object key) { - checkNotNull(key, "Key cannot be null."); - return serializer.decode(items.get(serializer.encode(key))); - } - - @Override - public V put(K key, V value) { - checkNotNull(key, "Key cannot be null."); - checkNotNull(value, "Value cannot be null."); - byte[] prevVal = items.put(serializer.encode(key), serializer.encode(value)); - if (prevVal == null) { - return null; - } - return serializer.decode(prevVal); - } - - @Override - public void putAll(Map<? extends K, ? extends V> m) { - checkNotNull(m, "The passed in map cannot be null."); - m.forEach((k, v) -> items.put(serializer.encode(k), serializer.encode(v))); - } - - @Override - public void clear() { - items.clear(); - } - - @Override - public Set<K> keySet() { - Set<K> keys = Sets.newHashSet(); - items.keySet().forEach(k -> keys.add(serializer.decode(k))); - return keys; - } - - @Override - public Collection<V> values() { - Collection<V> values = Sets.newHashSet(); - items.values().forEach(v -> values.add(serializer.decode(v))); - return values; - } - - @Override - public Set<Entry<K, V>> entrySet() { - Set<Entry<K, V>> entries = Sets.newHashSet(); - items.entrySet(). - forEach(e -> entries.add(Maps.immutableEntry(serializer.decode(e.getKey()), - serializer.decode(e.getValue())))); - return entries; - } - - @Override - public boolean equals(Object map) { - //This is not threadsafe and on larger maps incurs a significant processing cost - if (!(map instanceof Map)) { - return false; - } - Map asMap = (Map) map; - if (this.size() != asMap.size()) { - return false; - } - for (Entry entry : this.entrySet()) { - Object key = entry.getKey(); - if (!asMap.containsKey(key) || !asMap.get(key).equals(entry.getValue())) { - return false; - } - } - return true; - } - - @Override - public int hashCode() { - return super.hashCode(); - } -}
\ No newline at end of file diff --git a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistentSet.java b/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistentSet.java deleted file mode 100644 index 26118cf6..00000000 --- a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/PersistentSet.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * 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.persistence.impl; - -import com.google.common.collect.Iterators; -import org.mapdb.DB; -import org.mapdb.Hasher; -import org.mapdb.Serializer; - -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * A set implementation that gets and receives all data from a serialized internal set. - */ -//TODO add locking for reads and writes -public class PersistentSet<E> implements Set<E> { - - private final org.onosproject.store.service.Serializer serializer; - - private final org.mapdb.DB database; - - private final Set<byte[]> items; - - private final String name; - - public PersistentSet(org.onosproject.store.service.Serializer serializer, DB database, String name) { - this.serializer = checkNotNull(serializer); - this.database = checkNotNull(database); - this.name = checkNotNull(name); - - items = database - .createHashSet(name) - .serializer(Serializer.BYTE_ARRAY) - .hasher(Hasher.BYTE_ARRAY) - .makeOrGet(); - } - - public void readInto(Set<E> items) { - this.items.forEach(item -> items.add(serializer.decode(item))); - } - - @Override - public int size() { - return items.size(); - } - - @Override - public boolean isEmpty() { - return items.isEmpty(); - } - - @Override - public boolean contains(Object o) { - checkNotNull(o, "The argument cannot be null"); - return items.contains(serializer.encode(o)); - } - - @Override - public Iterator<E> iterator() { - return Iterators.transform(items.iterator(), serializer::decode); - } - - @Override - public Object[] toArray() { - Object[] retArray = new Object[items.size()]; - int index = 0; - Iterator<byte[]> iterator = items.iterator(); - while (iterator.hasNext()) { - retArray[index] = serializer.decode(iterator.next()); - index++; - } - return retArray; - } - - @Override - public <T> T[] toArray(T[] a) { - checkNotNull(a, "The passed in array cannot be null."); - int index = 0; - Iterator<byte[]> iterator = items.iterator(); - T[] retArray; - if (a.length >= items.size()) { - retArray = a; - } else { - retArray = (T[]) new Object[items.size()]; - } - while (iterator.hasNext()) { - retArray[index++] = serializer.decode(iterator.next()); - } - if (retArray.length > items.size()) { - retArray[index] = null; - } - return retArray; - } - - @Override - public boolean add(E item) { - checkNotNull("Item to be added cannot be null."); - return items.add(serializer.encode(item)); - } - - @Override - public boolean remove(Object o) { - checkNotNull(o, "Item to be removed cannot be null."); - return items.remove(serializer.encode(o)); - } - - @Override - public boolean containsAll(Collection<?> c) { - checkNotNull(c, "Collection cannot be internal."); - for (Object item : c) { - if (!items.contains(serializer.encode(item))) { - return false; - } - } - return true; - } - - @Override - public boolean addAll(Collection<? extends E> c) { - checkNotNull(c, "The collection to be added cannot be null."); - boolean changed = false; - for (Object item : c) { - changed = items.add(serializer.encode(item)) || changed; - } - return changed; - } - - @Override - public boolean retainAll(Collection<?> c) { - boolean changed = false; - for (byte[] item : items) { - E deserialized = serializer.decode(item); - if (!c.contains(deserialized)) { - changed = items.remove(item) || changed; - } - } - return changed; - } - - @Override - public boolean removeAll(Collection<?> c) { - boolean changed = false; - for (Object item : c) { - changed = items.remove(serializer.encode(item)) || changed; - } - return changed; - } - - @Override - public void clear() { - items.clear(); - } - - @Override - public boolean equals(Object set) { - //This is not threadsafe and on larger sets incurs a significant processing cost - if (!(set instanceof Set)) { - return false; - } - Set asSet = (Set) set; - if (asSet.size() != this.size()) { - return false; - } - for (Object item : this) { - if (!asSet.contains(item)) { - return false; - } - } - return true; - } - - @Override - public int hashCode() { - return super.hashCode(); - } -}
\ No newline at end of file diff --git a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/package-info.java b/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/package-info.java deleted file mode 100644 index 968a5046..00000000 --- a/framework/src/onos/core/store/persistence/src/main/java/org/onosproject/persistence/impl/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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. - */ - -/** - * Implementations of core persistence classes. - */ -package org.onosproject.persistence.impl; diff --git a/framework/src/onos/core/store/persistence/src/main/test/test/PersistentMapTest.java b/framework/src/onos/core/store/persistence/src/main/test/test/PersistentMapTest.java deleted file mode 100644 index b059f18e..00000000 --- a/framework/src/onos/core/store/persistence/src/main/test/test/PersistentMapTest.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * 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 test; - -import com.google.common.collect.Maps; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mapdb.DB; -import org.mapdb.DBMaker; -import org.onosproject.persistence.impl.PersistentMap; -import org.onosproject.store.service.Serializer; - -import java.nio.file.Paths; -import java.util.Map; -import java.util.Set; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -/** - * Test suite for Persistent Map. - */ -public class PersistentMapTest { - - private Map<Integer, Integer> map = null; - private DB fakeDB = null; - - - /** - * Set up the database, create a map and a direct executor to handle it. - * - * @throws Exception if instantiation fails - */ - @Before - public void setUp() throws Exception { - //Creates a db, a map within it and a basic integer serializer (async writing is off) - fakeDB = DBMaker - .newFileDB(Paths.get("../testDb").toFile()) - .asyncWriteEnable() - .closeOnJvmShutdown() - .make(); - map = new PersistentMap<Integer, Integer>(new Serializer() { - @Override - public <T> byte[] encode(T object) { - if (object == null) { - return null; - } - int num = (Integer) object; - byte[] result = new byte[4]; - - result[0] = (byte) (num >> 24); - result[1] = (byte) (num >> 16); - result[2] = (byte) (num >> 8); - result[3] = (byte) num; - return result; - } - - @Override - public <T> T decode(byte[] bytes) { - if (bytes == null) { - return null; - } - int num = 0x00000000; - - num = num | bytes[0] << 24; - num = num | bytes[1] << 16; - num = num | bytes[2] << 8; - num = num | bytes[3]; - - return (T) new java.lang.Integer(num); - } - }, fakeDB, "map"); - } - - /** - * Clears and deletes the map, closes the datbase and deletes the file. - * - * @throws Exception if shutdown fails - */ - @After - public void tearDown() throws Exception { - map.clear(); - fakeDB.delete("map:map"); - fakeDB.commit(); - fakeDB.close(); - //This is key to prevent artifacts persisting between tests. - Paths.get("../testDB").toFile().delete(); - - - } - - @Test - public void testRemove() throws Exception { - //Checks removal and return values - fillMap(10); - assertEquals(10, map.size()); - for (int i = 0; i < 10; i++) { - assertEquals("The previous value was wrong.", new Integer(i), map.remove(i)); - assertNull("The previous value was wrong.", map.remove(i)); - //(i+1) compensates for base zero. - assertEquals("The size was wrong.", 10 - (i + 1), map.size()); - } - } - - @Test - public void testSize() throws Exception { - //Checks size values throughout addition and removal - for (int i = 0; i < 10; i++) { - map.put(i, i); - assertEquals("The map size is wrong.", i + 1, map.size()); - } - for (int i = 0; i < 10; i++) { - map.remove(i); - assertEquals("The map size is wrong.", 9 - i, map.size()); - } - } - - @Test - public void testIsEmpty() throws Exception { - //Checks empty condition - //asserts that the map starts out empty - assertTrue("Map should be empty", map.isEmpty()); - map.put(1, 1); - assertFalse("Map shouldn't be empty.", map.isEmpty()); - map.remove(1); - assertTrue("Map should be empty", map.isEmpty()); - } - - @Test - public void testContains() throws Exception { - //Checks both containsKey and containsValue be aware the implementations vary widely (value does not use mapDB - //due to object '=='being an insufficient check) - for (int i = 0; i < 10; i++) { - assertFalse("Map should not contain the key", map.containsKey(i)); - assertFalse("Map should not contain the value", map.containsValue(i)); - map.put(i, i); - assertTrue("Map should contain the key", map.containsKey(i)); - assertTrue("Map should contain the value", map.containsValue(i)); - } - } - - @Test - public void testGet() throws Exception { - //Tests value retrieval and nonexistent key return values - for (int i = 0; i < 10; i++) { - map.put(i, i); - for (int j = 0; j <= i; j++) { - assertEquals("The value was wrong.", new Integer(j), map.get(j)); - } - } - assertNull("Null return value for nonexistent keys.", map.get(10)); - } - - @Test - public void testPutAll() throws Exception { - //Tests adding of an outside map - Map<Integer, Integer> testMap = Maps.newHashMap(); - fillMap(10); - map.putAll(testMap); - for (int i = 0; i < 10; i++) { - assertTrue("The map should contain the current 'i' value.", map.containsKey(i)); - assertTrue("The map should contain the current 'i' value.", map.containsValue(i)); - } - } - - @Test - public void testClear() throws Exception { - //Tests clearing the map - assertTrue("Map was initialized incorrectly, should be empty.", map.isEmpty()); - fillMap(10); - assertFalse("Map should contain entries now.", map.isEmpty()); - map.clear(); - assertTrue("Map should have been cleared of entries.", map.isEmpty()); - - } - - @Test - public void testKeySet() throws Exception { - //Tests key set generation - fillMap(10); - Set<Integer> keys = map.keySet(); - for (int i = 0; i < 10; i++) { - assertTrue("The key set doesn't contain all keys 0-9", keys.contains(i)); - } - assertEquals("The key set has an incorrect number of entries", 10, keys.size()); - } - - @Test - public void testValues() throws Exception { - //Tests value set generation - fillMap(10); - Set<Integer> values = (Set<Integer>) map.values(); - for (int i = 0; i < 10; i++) { - assertTrue("The key set doesn't contain all keys 0-9", values.contains(i)); - } - assertEquals("The key set has an incorrect number of entries", 10, values.size()); - } - - @Test - public void testEntrySet() throws Exception { - //Test entry set generation (violates abstraction by knowing the type of the returned entries) - fillMap(10); - Set<Map.Entry<Integer, Integer>> entries = map.entrySet(); - for (int i = 0; i < 10; i++) { - assertTrue("The key set doesn't contain all keys 0-9", entries.contains(Maps.immutableEntry(i, i))); - } - assertEquals("The key set has an incorrect number of entries", 10, entries.size()); - } - - @Test public void testPut() throws Exception { - //Tests insertion behavior (particularly the returning of previous value) - fillMap(10); - for (int i = 0; i < 10; i++) { - assertEquals("Put should return the previous value", new Integer(i), map.put(i, i + 1)); - } - assertNull(map.put(11, 11)); - } - - /** - * Populated the map with pairs of integers from (0, 0) up to (numEntries - 1, numEntries -1). - * @param numEntries number of entries to add - */ - private void fillMap(int numEntries) { - for (int i = 0; i < numEntries; i++) { - map.put(i, i); - } - } -} diff --git a/framework/src/onos/core/store/persistence/src/main/test/test/PersistentSetTest.java b/framework/src/onos/core/store/persistence/src/main/test/test/PersistentSetTest.java deleted file mode 100644 index 3107ab30..00000000 --- a/framework/src/onos/core/store/persistence/src/main/test/test/PersistentSetTest.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * 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 test; - -import com.google.common.collect.Sets; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mapdb.DB; -import org.mapdb.DBMaker; -import org.onosproject.persistence.impl.PersistentSet; -import org.onosproject.store.service.Serializer; - -import java.nio.file.Paths; -import java.util.HashSet; -import java.util.Set; - -import static com.google.common.base.Preconditions.checkNotNull; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -/** - * Test suite for Persistent Set. - */ -public class PersistentSetTest { - - private Set<Integer> set = null; - private DB fakeDB = null; - - @Before - public void setUp() throws Exception { - //Creates a db, a set within it and a basic integer serializer (async writing is off) - fakeDB = DBMaker - .newFileDB(Paths.get("../testDb").toFile()) - .asyncWriteEnable() - .closeOnJvmShutdown() - .make(); - set = new PersistentSet<Integer>(new Serializer() { - @Override - public <T> byte[] encode(T object) { - if (object == null) { - return null; - } - int num = (Integer) object; - byte[] result = new byte[4]; - - result[0] = (byte) (num >> 24); - result[1] = (byte) (num >> 16); - result[2] = (byte) (num >> 8); - result[3] = (byte) num; - return result; - } - - @Override - public <T> T decode(byte[] bytes) { - if (bytes == null) { - return null; - } - int num = 0x00000000; - - num = num | bytes[0] << 24; - num = num | bytes[1] << 16; - num = num | bytes[2] << 8; - num = num | bytes[3]; - - return (T) new java.lang.Integer(num); - } - }, fakeDB, "set"); - - } - - @After - public void tearDown() throws Exception { - set.clear(); - fakeDB.delete("map:map"); - fakeDB.commit(); - fakeDB.close(); - //This is key to prevent artifacts persisting between tests. - Paths.get("../testDB").toFile().delete(); - } - - @Test - public void testSize() throws Exception { - //Check correct sizing throughout population - for (int i = 0; i < 10; i++) { - set.add(i); - assertEquals("The set should have i + 1 entries.", i + 1, set.size()); - } - } - - @Test - public void testIsEmpty() throws Exception { - //test empty condition - assertTrue("The set should be initialized empty.", set.isEmpty()); - fillSet(5, this.set); - assertFalse("The set should no longer be empty.", set.isEmpty()); - set.clear(); - assertTrue("The set should have been cleared.", set.isEmpty()); - } - - @Test - public void testContains() throws Exception { - //Test contains - assertFalse("The set should not contain anything", set.contains(1)); - fillSet(10, this.set); - for (int i = 0; i < 10; i++) { - assertTrue("The set should contain all values 0-9.", set.contains(i)); - } - } - - @Test - public void testIterator() throws Exception { - //Test iterator behavior (no order guarantees are made) - Set<Integer> validationSet = Sets.newHashSet(); - fillSet(10, this.set); - fillSet(10, validationSet); - set.iterator().forEachRemaining(item -> assertTrue("Items were mismatched.", validationSet.remove(item))); - //All values should have been seen and removed - assertTrue("All entries in the validation set should have been removed.", validationSet.isEmpty()); - } - - @Test - public void testToArray() throws Exception { - //Test creation of a new array of the values - fillSet(10, set); - Object[] arr = set.toArray(); - assertEquals("The array should be of length 10.", 10, arr.length); - for (int i = 0; i < 10; i++) { - assertTrue("All elements of the array should be in the set.", set.contains((Integer) arr[i])); - } - } - - @Test - public void testToArray1() throws Exception { - //Test creation of a new array with the possibility of populating passed array if space allows - Integer[] originalArray = new Integer[9]; - fillSet(9, set); - //Test the case where the array and set match in size - Object[] retArray = set.toArray(originalArray); - assertEquals("If the set can fit the array should be the one passed in.", originalArray, retArray); - //Test the case where the passe array is too small to fit the set - set.add(9); - assertNotEquals("A new set should be generated if the contents will not fit in the passed set", - set.toArray(originalArray), originalArray); - //Now test the case where there should be a null terminator - set.clear(); - fillSet(5, set); - assertNull("The character one after last should be null if the array is larger than the set.", - set.toArray(originalArray)[5]); - } - - @Test - public void testAdd() throws Exception { - //Test of add - for (int i = 0; i < 10; i++) { - assertEquals("The size of the set is wrong.", i, set.size()); - assertTrue("The first add of an element should be true.", set.add(i)); - assertFalse("The second add of an element should be false.", set.add(i)); - } - } - - @Test - public void testRemove() throws Exception { - //Test removal - fillSet(10, set); - for (int i = 0; i < 10; i++) { - assertEquals("The size of the set is wrong.", 10 - i, set.size()); - assertTrue("The first removal should be true.", set.remove(i)); - assertFalse("The second removal should be false (item no longer contained).", set.remove(i)); - } - assertTrue("All elements should have been removed.", set.isEmpty()); - } - - @Test - public void testContainsAll() throws Exception { - //Test contains with short circuiting - Set<Integer> integersToCheck = Sets.newHashSet(); - fillSet(10, integersToCheck); - fillSet(10, set); - assertTrue("The sets should be identical so mutual subsets.", set.containsAll(integersToCheck)); - set.remove(9); - assertFalse("The set should contain one fewer value.", set.containsAll(integersToCheck)); - } - - @Test - public void testAddAll() throws Exception { - //Test multi-adds with change checking - Set<Integer> integersToCheck = Sets.newHashSet(); - fillSet(10, integersToCheck); - assertFalse("Set should be empty and so integers to check should not be a subset.", - set.containsAll(integersToCheck)); - assertTrue("The set should have changed as a result of add all.", set.addAll(integersToCheck)); - assertFalse("The set should not have changed as a result of add all a second time.", - set.addAll(integersToCheck)); - assertTrue("The sets should now be equivalent.", set.containsAll(integersToCheck)); - assertTrue("The sets should now be equivalent.", integersToCheck.containsAll(set)); - } - - @Test - public void testRetainAll() throws Exception { - //Test ability to generate the intersection set - Set<Integer> retainSet = Sets.newHashSet(); - fillSet(10, set); - assertTrue("The set should have changed.", set.retainAll(retainSet)); - assertTrue("The set should have been emptied.", set.isEmpty()); - fillSet(10, set); - fillSet(10, retainSet); - Set<Integer> duplicateSet = new HashSet<>(set); - assertFalse("The set should not have changed.", set.retainAll(retainSet)); - assertEquals("The set should be the same as the duplicate.", duplicateSet, set); - retainSet.remove(9); - assertTrue("The set should have changed.", set.retainAll(retainSet)); - duplicateSet.remove(9); - assertEquals("The set should have had the nine element removed.", duplicateSet, set); - } - - @Test - public void testRemoveAll() throws Exception { - //Test for mass removal and change checking - Set<Integer> removeSet = Sets.newHashSet(); - fillSet(10, set); - Set<Integer> duplicateSet = Sets.newHashSet(set); - assertFalse("No elements should change.", set.removeAll(removeSet)); - assertEquals("Set should not have diverged from the duplicate.", duplicateSet, set); - fillSet(5, removeSet); - assertTrue("Elements should have been removed.", set.removeAll(removeSet)); - assertNotEquals("Duplicate set should no longer be equivalent.", duplicateSet, set); - assertEquals("Five elements should have been removed from set.", 5, set.size()); - for (Integer item : removeSet) { - assertFalse("No element of remove set should remain.", set.contains(item)); - } - } - - @Test - public void testClear() throws Exception { - //Test set emptying - assertTrue("The set should be initialized empty.", set.isEmpty()); - set.clear(); - assertTrue("Clear should have no effect on an empty set.", set.isEmpty()); - fillSet(10, set); - assertFalse("The set should no longer be empty.", set.isEmpty()); - set.clear(); - assertTrue("The set should be empty after clear.", set.isEmpty()); - } - - /** - * Populated the map with integers from (0) up to (numEntries - 1). - * - * @param numEntries number of entries to add - */ - private void fillSet(int numEntries, Set<Integer> set) { - checkNotNull(set); - for (int i = 0; i < numEntries; i++) { - set.add(i); - } - } -} |