From: Arne V. <cob...@us...> - 2004-06-09 07:43:25
|
Update of /cvsroot/jrobin/src/org/jrobin/graph In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11018/org/jrobin/graph Added Files: FetchSourceList.java RrdOpener.java Log Message: JRobin 1.4.0 - Added setLazy() option for lazy graph regeneration - Added setDatasources() option for GraphDef def management - Fxed 0 as end timestamp option - Fixed GraphDef reuse issue with gprint() --- NEW FILE: FetchSourceList.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...) * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * (C) Copyright 2003, by Sasa Markovic. * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.graph; import java.util.HashMap; import java.util.ArrayList; import java.io.IOException; import org.jrobin.core.RrdException; import org.jrobin.core.RrdDb; /** * <p>A FetchSourceList represents a number of RRD datasources, * to be used with RrdGraphDef for Graph generation.</p> * * @author Arne Vandamme (cob...@jr...) */ public class FetchSourceList { // ================================================================ // -- Members // ================================================================ private HashMap map; private ArrayList list; private int defCount; private boolean persistent; private boolean opened; private RrdOpener rrdOpener; // ================================================================ // -- Constructors // ================================================================ /** * Creates a new FetchSourceList with the specified default size. * The size of the actual list is not limited to this number, and * the list will expand automatically if necessary. The default size * should be a ballpark figure for the number of different RrdDb * that will be used (usually a RrdDb corresponds with a single RRD file). * * @param defaultSize Default size of the FetchSourceList. */ public FetchSourceList( int defaultSize ) { this( defaultSize, false ); } /** * Creates a new FetchSourceList with the specified default size. * The size of the actual list is not limited to this number, and * the list will expand automatically if necessary. The default size * should be a ballpark figure for the number of different RrdDb * that will be used (usually a RrdDb corresponds with a single RRD file). * * @param defaultSize Default size of the FetchSourceList. * @param persistent True if the list is persistent, false if not. */ public FetchSourceList( int defaultSize, boolean persistent ) { map = new HashMap( defaultSize ); list = new ArrayList( defaultSize ); opened = false; this.persistent = persistent; } /** * Creates a new FetchSourceList with the specified default size. * The size of the actual list is not limited to this number, and * the list will expand automatically if necessary. The default size * should be a ballpark figure for the number of different RrdDb * that will be used (usually a RrdDb corresponds with a single RRD file). * * @param defaultSize Default size of the FetchSourceList. * @param persistent True if the list is persistent, false if not. * @param rrdOpener Reference to the RrdOpener object that will be used * for RrdDb retrieval. */ public FetchSourceList( int defaultSize, boolean persistent, RrdOpener rrdOpener ) { this( defaultSize, persistent ); this.rrdOpener = rrdOpener; } // ================================================================ // -- Public methods // ================================================================ /** * Sets the internal RrdOpener object to use for RrdDb retrieval. * * @param rrdOpener Reference to the corresponding RrdOpener instance. */ public void setRrdOpener( RrdOpener rrdOpener ) { // Only allow RrdOpener change if not persistent if ( !persistent ) this.rrdOpener = rrdOpener; } public RrdOpener getRrdOpener() { return rrdOpener; } /** * Sets the persistency state of the FetchSourceList. * If the list is set as persistent, RrdDb's can be opened * and retrieved, but not released, and the RrdOpener reference * can not be changed. This is useful to avoid premature closing * and reopening of datasources for performance reasons. * * Setting a FetchSourceList as persistent requires you to manually * control releasing all datasources (all calls to openAll() will * still succeed). * * @param persistent True if the list should behave as persistent. */ public void setPersistent( boolean persistent ) { this.persistent = persistent; } /** * Returns the number of FetchSources hold in the list. * @return Number of different FetchSources. */ public int size() { return list.size(); } /** * Returns the number of Defs represented by the * different FetchSources. * @return Number of Def definitions. */ public int defCount() { return defCount; } /** * Retrieves (opens) all RrdDb instances related to the * different FetchSources. * It is safe to call this method multiple times in a row. * * @throws IOException Thrown in case of fetching I/O error. * @throws RrdException Thrown in case of a JRobin specific error. */ public void openAll() throws RrdException, IOException { if ( opened ) return; for ( int i = 0; i < size(); i++ ) get(i).openRrd(); opened = true; } /** * Releases all RrdDb instances for the FetchSources. * It is safe to call this method multiple times in a row. * In case of a persistent list, this method does nothing * until persistency is removed. * * @throws IOException Thrown in case of fetching I/O error. * @throws RrdException Thrown in case of a JRobin specific error. */ public void releaseAll() throws RrdException, IOException { if ( persistent ) return; // Do not allow release if this FSList is persistent for ( int i = 0; i < size(); i++ ) get(i).release(); opened = false; } /** * Clears up the FetchSourceList for new use. * This removes persistency, releases all RrdDb instances, and * clears the internal list of FetchSources. After clear() * the list is empty. * * @throws IOException Thrown in case of fetching I/O error. * @throws RrdException Thrown in case of a JRobin specific error. */ public void clear() throws RrdException, IOException { persistent = false; releaseAll(); map.clear(); list.clear(); } /** * Returns the highest last update time in seconds of the datasources * represented by the list. If the update time differs for different * datasources, the highest overall timestamp will be returned. * * @return Last update time in seconds. * @throws IOException Thrown in case of fetching I/O error. * @throws RrdException Thrown in case of a JRobin specific error. */ public long getLastUpdateTime() throws RrdException, IOException { RrdDb rrd; long maxUpdateTime = 0; long lastUpdateTime = 0; for ( int i = 0; i < size(); i++ ) { rrd = get(i).getRrd(); lastUpdateTime = rrd.getLastUpdateTime(); if ( lastUpdateTime > maxUpdateTime ) maxUpdateTime = lastUpdateTime; } return maxUpdateTime; } /** * Adds a datasource for graphing purposes to the list, * {@see RrdGraphDef#datasource( java.lang.String, java.lang.String, * java.lang.String, java.lang.String, java.lang.String )}. * @param name Internal datasource name, to be used in GraphDefs. * @param file Path to RRD file. * @param dsName Data source name defined in the RRD file. * @param consolFunc Consolidation function that will be used to extract data from the RRD * file ("AVERAGE", "MIN", "MAX" or "LAST"). * @param backend Name of the RrdBackendFactory that should be used for this RrdDb. * @throws RrdException Thrown in case of a JRobin specific error. */ public void add( String name, String file, String dsName, String consolFunc, String backend ) throws RrdException { if ( map.containsKey(file) ) { FetchSource rf = (FetchSource) map.get(file); rf.addSource( consolFunc, dsName, name ); } else { FetchSource fs = new FetchSource( file, consolFunc, dsName, name, backend, this ); map.put( file, fs ); list.add( fs ); } defCount++; } /** * Adds a datasource for graphing purposes to the list, * {@see RrdGraphDef#datasource( java.lang.String, java.lang.String, * java.lang.String, java.lang.String )}. * * @param name Internal datasource name, to be used in GraphDefs. * @param file Path to RRD file. * @param dsName Data source name defined in the RRD file. * @param consolFunc Consolidation function that will be used to extract data from the RRD * file ("AVERAGE", "MIN", "MAX" or "LAST"). * @throws RrdException Thrown in case of a JRobin specific error. */ public void add( String name, String file, String dsName, String consolFunc ) throws RrdException { if ( map.containsKey(file) ) { FetchSource rf = (FetchSource) map.get(file); rf.addSource( consolFunc, dsName, name ); } else { FetchSource fs = new FetchSource( file, consolFunc, dsName, name, this ); map.put( file, fs ); list.add( fs ); } defCount++; } // ================================================================ // -- Protected (package) methods // ================================================================ /** * Returns the FetchSource for the given index. * * @param index Index of the FetchSource in the list. * @return FetchSource instance. */ protected FetchSource get( int index ) { return (FetchSource) list.get(index); } } --- NEW FILE: RrdOpener.java --- /* ============================================================ * JRobin : Pure java implementation of RRDTool's functionality * ============================================================ * * Project Info: http://www.jrobin.org * Project Lead: Sasa Markovic (sa...@jr...) * * Developers: Sasa Markovic (sa...@jr...) * Arne Vandamme (cob...@jr...) * * (C) Copyright 2003, by Sasa Markovic. * * This library is free software; you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Foundation; * either version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */ package org.jrobin.graph; import java.io.IOException; import org.jrobin.core.RrdDb; import org.jrobin.core.RrdBackendFactory; import org.jrobin.core.RrdException; import org.jrobin.core.RrdDbPool; /** * <p>Class that represents an object that can be used to perform the actual * opening and closing of RRD files, using different methods. Other objects * like the FetchSourceList representing the Graph datasources * {@see FetchSourceLink} use a RrdOpener to retrieve the RrdDb instances of * RRD datasources.</p> * <p>Overriding the RrdOpener class allows finetuned access on the level * of RrdDb retrieval and release. An child class could for example add * log or debug statements, gather statistics on RRD access, or provide * a transparent way to to access RRD datasources in an alternative way * like through a DBMS. </p> * * @author Arne Vandamme (cob...@jr...) */ public class RrdOpener { // ================================================================ // -- Members // ================================================================ protected RrdDbPool pool; protected boolean readOnly = false; protected boolean usePool = false; // ================================================================ // -- Constructors // ================================================================ /** * Creates a new RrdOpener that will open RrdDb objects with read/write * access. If the usePool flag is set, the RrdOpener will use the RrdDbPool * to retrieve RrdDb instances. * @param usePool True if the RrdOpener should use the RrdDbPool. */ public RrdOpener( boolean usePool ) { this.usePool = usePool; if ( usePool ) pool = RrdDbPool.getInstance(); } /** * Creates a new RrdOpener that will open RrdDb objects with read/write * or read-only access, depending on the readOnly flag.. If the usePool * flag is set, the RrdOpener will use the RrdDbPool to retrieve RrdDb * instances. * @param usePool True if the RrdOpener should use the RrdDbPool. * @param readOnly True if the RrdOpener should open RrdDb objects as read-only. */ public RrdOpener( boolean usePool, boolean readOnly ) { this( usePool ); this.readOnly = readOnly; } // ================================================================ // -- Public methods // ================================================================ /** * Retrieves the RrdDb instance matching a specific RRD datasource name * (usually a file name) and using a specified RrdBackendFactory. * * @param rrdFile Name of the RRD datasource. * @param backendFactory BackendFactory to use for retrieval. * @return RrdDb instance of the datasource. * @throws IOException Thrown in case of I/O error. * @throws RrdException Thrown in case of a JRobin specific error. */ public RrdDb getRrd( String rrdFile, RrdBackendFactory backendFactory ) throws IOException, RrdException { if ( pool != null ) return pool.requestRrdDb( rrdFile ); else return new RrdDb( rrdFile, readOnly, backendFactory ); } /** * Releases an RrdDb instance. Depending on the settings of the RrdOpener, * this either closes the RrdDb, or releases it back into the RrdDbPool. * @param rrdDb RrdDb object that should be released. * @throws IOException Thrown in case of I/O error. * @throws RrdException Thrown in case of a JRobin specific error. */ public void releaseRrd(RrdDb rrdDb) throws IOException, RrdException { if ( pool != null ) pool.release(rrdDb); else rrdDb.close(); } } |