KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > RrdMemoryBackendFactory


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, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
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 JavaDoc;
29 import java.util.HashMap JavaDoc;
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  *
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(java.lang.String) delete(path)} method of this class.<p>
40  */

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

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

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

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

96     public String JavaDoc getFactoryName() {
97         return NAME;
98     }
99 }
100
Popular Tags