Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.persistence.impl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
| 23 | import org.mapdb.DB; |
| 24 | import org.mapdb.DBMaker; |
| 25 | import org.onosproject.persistence.PersistenceService; |
| 26 | import org.onosproject.persistence.PersistentMapBuilder; |
| 27 | import org.onosproject.persistence.PersistentSetBuilder; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.nio.file.Files; |
| 32 | import java.nio.file.Path; |
| 33 | import java.nio.file.Paths; |
| 34 | import java.util.Map; |
| 35 | import java.util.Set; |
| 36 | import java.util.Timer; |
| 37 | import java.util.TimerTask; |
| 38 | |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 39 | import static org.onosproject.security.AppGuard.checkPermission; |
| 40 | import static org.onosproject.security.AppPermission.Type.PERSISTENCE_WRITE; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
| 44 | * Service that maintains local disk backed maps and sets. This implementation automatically deletes empty structures |
| 45 | * on shutdown. |
| 46 | */ |
| 47 | @Component(immediate = true) |
| 48 | @Service |
| 49 | public class PersistenceManager implements PersistenceService { |
| 50 | |
Madan Jampani | 44d8fbb | 2015-12-04 12:54:39 -0800 | [diff] [blame] | 51 | private static final String DATABASE_PATH = "../data/localDB"; |
| 52 | private static final String ENCLOSING_FOLDER = "../data"; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 53 | |
| 54 | static final String MAP_PREFIX = "map:"; |
| 55 | |
| 56 | static final String SET_PREFIX = "set:"; |
| 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | private DB localDB = null; |
| 61 | |
| 62 | private static final int FLUSH_FREQUENCY_MILLIS = 3000; |
| 63 | |
Aaron Kruglikov | dfb325a | 2015-11-23 14:16:55 -0800 | [diff] [blame] | 64 | private Timer timer; |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 65 | |
| 66 | private final CommitTask commitTask = new CommitTask(); |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
Aaron Kruglikov | dfb325a | 2015-11-23 14:16:55 -0800 | [diff] [blame] | 70 | timer = new Timer(); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 71 | Path dbPath = Paths.get(DATABASE_PATH); |
| 72 | Path dbFolderPath = Paths.get(ENCLOSING_FOLDER); |
| 73 | //Make sure the directory exists, if it does not, make it. |
| 74 | if (!dbFolderPath.toFile().isDirectory()) { |
| 75 | log.info("The specified folder location for the database did not exist and will be created."); |
| 76 | try { |
| 77 | Files.createDirectories(dbFolderPath); |
| 78 | } catch (IOException e) { |
| 79 | log.error("Could not create the required folder for the database."); |
| 80 | throw new PersistenceException("Database folder could not be created."); |
| 81 | } |
| 82 | } |
| 83 | //Notify if the database file does not exist. |
| 84 | boolean dbFound = Files.exists(dbPath); |
| 85 | if (!dbFound) { |
| 86 | log.info("The database file could not be located, a new database will be constructed."); |
| 87 | |
| 88 | } else { |
| 89 | log.info("A previous database file has been found."); |
| 90 | } |
| 91 | localDB = DBMaker.newFileDB(dbPath.toFile()) |
| 92 | .asyncWriteEnable() |
| 93 | .closeOnJvmShutdown() |
| 94 | .make(); |
| 95 | timer.schedule(commitTask, FLUSH_FREQUENCY_MILLIS, FLUSH_FREQUENCY_MILLIS); |
| 96 | log.info("Started"); |
| 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | public void deactivate() { |
Aaron Kruglikov | dfb325a | 2015-11-23 14:16:55 -0800 | [diff] [blame] | 101 | timer.cancel(); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 102 | for (Map.Entry<String, Object> entry : localDB.getAll().entrySet()) { |
| 103 | String key = entry.getKey(); |
| 104 | Object value = entry.getValue(); |
| 105 | //This is a map implementation to be handled as such |
| 106 | if (value instanceof Map) { |
| 107 | Map asMap = (Map) value; |
| 108 | if (asMap.isEmpty()) { |
| 109 | //the map is empty and may be deleted |
| 110 | localDB.delete(key); |
| 111 | } |
| 112 | //This is a set implementation and can be handled as such |
| 113 | } else if (value instanceof Set) { |
| 114 | Set asSet = (Set) value; |
| 115 | if (asSet.isEmpty()) { |
| 116 | //the set is empty and may be deleted |
| 117 | localDB.delete(key); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | localDB.commit(); |
| 122 | localDB.close(); |
| 123 | log.info("Stopped"); |
| 124 | } |
| 125 | |
| 126 | public <K, V> PersistentMapBuilder<K, V> persistentMapBuilder() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 127 | checkPermission(PERSISTENCE_WRITE); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 128 | return new DefaultPersistentMapBuilder<>(localDB); |
| 129 | } |
| 130 | |
| 131 | public <E> PersistentSetBuilder<E> persistentSetBuilder() { |
Heedo Kang | 4a47a30 | 2016-02-29 17:40:23 +0900 | [diff] [blame] | 132 | checkPermission(PERSISTENCE_WRITE); |
Aaron Kruglikov | 92511f2 | 2015-10-12 14:39:04 -0700 | [diff] [blame] | 133 | return new DefaultPersistentSetBuilder<>(localDB); |
| 134 | } |
| 135 | |
| 136 | private class CommitTask extends TimerTask { |
| 137 | |
| 138 | @Override |
| 139 | public void run() { |
| 140 | localDB.commit(); |
| 141 | } |
| 142 | } |
| 143 | } |