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