1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info:  http://www.jrobin.org
6  * Project Lead:  Sasa Markovic (saxon@jrobin.org);
7  *
8  * (C) Copyright 2003-2005, by Sasa Markovic.
9  *
10  * Developers:    Sasa Markovic (saxon@jrobin.org)
11  *
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25
26 package org.jrobin.core;
27
28 import java.io.IOException;
29 import java.util.HashMap;
30
31 /**
32  * Factory class which creates actual {@link RrdMemoryBackend} objects. JRobin's support
33  * for in-memory RRDs is still experimental. You should know that all active RrdMemoryBackend
34  * objects are held in memory, each backend object stores RRD data in one big byte array. This
35  * implementation is therefore quite basic and memory hungry but runs very fast.<p>
36  * <p/>
37  * Calling {@link RrdDb#close() close()} on RrdDb objects does not release any memory at all
38  * (RRD data must be available for the next <code>new RrdDb(path)</code> call. To release allocated
39  * memory, you'll have to call {@link #delete(String) delete(path)} method of this class.<p>
40  */

41 public class RrdMemoryBackendFactory extends RrdBackendFactory {
42     /**
43      * factory name, "MEMORY"
44      */

45     public static final String NAME = "MEMORY";
46     private HashMap<String, RrdMemoryBackend> backends = new HashMap<String, RrdMemoryBackend>();
47
48     /**
49      * Creates RrdMemoryBackend object.
50      *
51      * @param id       Since this backend holds all data in memory, this argument is interpreted
52      *                 as an ID for this memory-based storage.
53      * @param readOnly This parameter is ignored
54      * @return RrdMemoryBackend object which handles all I/O operations
55      * @throws IOException Thrown in case of I/O error.
56      */

57     protected synchronized RrdBackend open(String id, boolean readOnly)
58             throws IOException {
59         RrdMemoryBackend backend;
60         if (backends.containsKey(id)) {
61             backend = backends.get(id);
62         }
63         else {
64             backend = new RrdMemoryBackend(id);
65             backends.put(id, backend);
66         }
67         return backend;
68     }
69
70     /**
71      * Method to determine if a memory storage with the given ID already exists.
72      *
73      * @param id Memory storage ID.
74      * @return True, if such storage exists, false otherwise.
75      */

76     protected synchronized boolean exists(String id) {
77         return backends.containsKey(id);
78     }
79
80     /**
81      * Removes the storage with the given ID from the memory.
82      *
83      * @param id Storage ID
84      * @return True, if the storage with the given ID is deleted, false otherwise.
85      */

86     public boolean delete(String id) {
87         if (backends.containsKey(id)) {
88             backends.remove(id);
89             return true;
90         }
91         else {
92             return false;
93         }
94     }
95
96     /**
97      * Returns the name of this factory.
98      *
99      * @return Factory name (equals to "MEMORY").
100      */

101     public String getFactoryName() {
102         return NAME;
103     }
104 }
105