001 /* ============================================================
002 * JRobin : Pure java implementation of RRDTool's functionality
003 * ============================================================
004 *
005 * Project Info: http://www.jrobin.org
006 * Project Lead: Sasa Markovic (saxon@jrobin.org);
007 *
008 * (C) Copyright 2003-2005, by Sasa Markovic.
009 *
010 * Developers: Sasa Markovic (saxon@jrobin.org)
011 *
012 *
013 * This library is free software; you can redistribute it and/or modify it under the terms
014 * of the GNU Lesser General Public License as published by the Free Software Foundation;
015 * either version 2.1 of the License, or (at your option) any later version.
016 *
017 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
018 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
019 * See the GNU Lesser General Public License for more details.
020 *
021 * You should have received a copy of the GNU Lesser General Public License along with this
022 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
023 * Boston, MA 02111-1307, USA.
024 */
025
026 package org.jrobin.core;
027
028 import java.io.IOException;
029 import java.util.HashMap;
030
031 /**
032 * Factory class which creates actual {@link RrdMemoryBackend} objects. JRobin's support
033 * for in-memory RRDs is still experimental. You should know that all active RrdMemoryBackend
034 * objects are held in memory, each backend object stores RRD data in one big byte array. This
035 * implementation is therefore quite basic and memory hungry but runs very fast.<p>
036 * <p/>
037 * Calling {@link RrdDb#close() close()} on RrdDb objects does not release any memory at all
038 * (RRD data must be available for the next <code>new RrdDb(path)</code> call. To release allocated
039 * memory, you'll have to call {@link #delete(String) delete(path)} method of this class.<p>
040 */
041 public class RrdMemoryBackendFactory extends RrdBackendFactory {
042 /**
043 * factory name, "MEMORY"
044 */
045 public static final String NAME = "MEMORY";
046 private HashMap<String, RrdMemoryBackend> backends = new HashMap<String, RrdMemoryBackend>();
047
048 /**
049 * Creates RrdMemoryBackend object.
050 *
051 * @param id Since this backend holds all data in memory, this argument is interpreted
052 * as an ID for this memory-based storage.
053 * @param readOnly This parameter is ignored
054 * @return RrdMemoryBackend object which handles all I/O operations
055 * @throws IOException Thrown in case of I/O error.
056 */
057 protected synchronized RrdBackend open(String id, boolean readOnly)
058 throws IOException {
059 RrdMemoryBackend backend;
060 if (backends.containsKey(id)) {
061 backend = backends.get(id);
062 }
063 else {
064 backend = new RrdMemoryBackend(id);
065 backends.put(id, backend);
066 }
067 return backend;
068 }
069
070 /**
071 * Method to determine if a memory storage with the given ID already exists.
072 *
073 * @param id Memory storage ID.
074 * @return True, if such storage exists, false otherwise.
075 */
076 protected synchronized boolean exists(String id) {
077 return backends.containsKey(id);
078 }
079
080 /**
081 * Removes the storage with the given ID from the memory.
082 *
083 * @param id Storage ID
084 * @return True, if the storage with the given ID is deleted, false otherwise.
085 */
086 public boolean delete(String id) {
087 if (backends.containsKey(id)) {
088 backends.remove(id);
089 return true;
090 }
091 else {
092 return false;
093 }
094 }
095
096 /**
097 * Returns the name of this factory.
098 *
099 * @return Factory name (equals to "MEMORY").
100 */
101 public String getFactoryName() {
102 return NAME;
103 }
104 }