View Javadoc

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  /**
29   * Class to represent single archive definition within the RRD.
30   * Archive definition consists of the following four elements:
31   * <p/>
32   * <ul>
33   * <li>consolidation function
34   * <li>X-files factor
35   * <li>number of steps
36   * <li>number of rows.
37   * </ul>
38   * <p>For the complete explanation of all archive definition parameters, see RRDTool's
39   * <a href="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a>
40   * </p>
41   *
42   * @author <a href="mailto:saxon@jrobin.org">Sasa Markovic</a>
43   */
44  
45  public class ArcDef implements ConsolFuns {
46  	/**
47  	 * array of valid consolidation function names
48  	 */
49  	public static final String CONSOL_FUNS[] = {CF_AVERAGE, CF_MAX, CF_MIN, CF_LAST};
50  
51  	private String consolFun;
52  	private double xff;
53  	private int steps, rows;
54  
55  	/**
56  	 * <p>Creates new archive definition object. This object should be passed as argument to
57  	 * {@link RrdDef#addArchive(ArcDef) addArchive()} method of
58  	 * {@link RrdDb RrdDb} object.</p>
59  	 * <p/>
60  	 * <p>For the complete explanation of all archive definition parameters, see RRDTool's
61  	 * <a href="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a></p>
62  	 *
63  	 * @param consolFun Consolidation function. Allowed values are "AVERAGE", "MIN",
64  	 *                  "MAX" and "LAST" (these string constants are conveniently defined in the
65  	 *                  {@link ConsolFuns} class).
66  	 * @param xff	   X-files factor, between 0 and 1.
67  	 * @param steps	 Number of archive steps.
68  	 * @param rows	  Number of archive rows.
69  	 * @throws RrdException Thrown if any parameter has illegal value.
70  	 */
71  	public ArcDef(String consolFun, double xff, int steps, int rows) throws RrdException {
72  		this.consolFun = consolFun;
73  		this.xff = xff;
74  		this.steps = steps;
75  		this.rows = rows;
76  		validate();
77  	}
78  
79  	/**
80  	 * Returns consolidation function.
81  	 *
82  	 * @return Consolidation function.
83  	 */
84  	public String getConsolFun() {
85  		return consolFun;
86  	}
87  
88  	/**
89  	 * Returns the X-files factor.
90  	 *
91  	 * @return X-files factor value.
92  	 */
93  	public double getXff() {
94  		return xff;
95  	}
96  
97  	/**
98  	 * Returns the number of primary RRD steps which complete a single archive step.
99  	 *
100 	 * @return Number of steps.
101 	 */
102 	public int getSteps() {
103 		return steps;
104 	}
105 
106 	/**
107 	 * Returns the number of rows (aggregated values) stored in the archive.
108 	 *
109 	 * @return Number of rows.
110 	 */
111 	public int getRows() {
112 		return rows;
113 	}
114 
115 	private void validate() throws RrdException {
116 		if (!isValidConsolFun(consolFun)) {
117 			throw new RrdException("Invalid consolidation function specified: " + consolFun);
118 		}
119 		if (Double.isNaN(xff) || xff < 0.0 || xff >= 1.0) {
120 			throw new RrdException("Invalid xff, must be >= 0 and < 1: " + xff);
121 		}
122 		if (steps < 1 || rows < 2) {
123 			throw new RrdException("Invalid steps/rows settings: " + steps + "/" + rows +
124 					". Minimal values allowed are steps=1, rows=2");
125 		}
126 	}
127 
128 	/**
129 	 * Returns string representing archive definition (RRDTool format).
130 	 *
131 	 * @return String containing all archive definition parameters.
132 	 */
133 	public String dump() {
134 		return "RRA:" + consolFun + ":" + xff + ":" + steps + ":" + rows;
135 	}
136 
137 	/**
138 	 * Checks if two archive definitions are equal.
139 	 * Archive definitions are considered equal if they have the same number of steps
140 	 * and the same consolidation function. It is not possible to create RRD with two
141 	 * equal archive definitions.
142 	 *
143 	 * @param obj Archive definition to compare with.
144 	 * @return <code>true</code> if archive definitions are equal,
145 	 *         <code>false</code> otherwise.
146 	 */
147 	public boolean equals(Object obj) {
148 		if (obj instanceof ArcDef) {
149 			ArcDef arcObj = (ArcDef) obj;
150 			return consolFun.equals(arcObj.consolFun) && steps == arcObj.steps;
151 		}
152 		return false;
153 	}
154 
155 	/**
156 	 * Checks if function argument represents valid consolidation function name.
157 	 *
158 	 * @param consolFun Consolidation function to be checked
159 	 * @return <code>true</code> if <code>consolFun</code> is valid consolidation function,
160 	 *         <code>false</code> otherwise.
161 	 */
162 	public static boolean isValidConsolFun(String consolFun) {
163 		for (String cFun : CONSOL_FUNS) {
164 			if (cFun.equals(consolFun)) {
165 				return true;
166 			}
167 		}
168 		return false;
169 	}
170 
171 	void setRows(int rows) {
172 		this.rows = rows;
173 	}
174 
175 	boolean exactlyEqual(ArcDef def) {
176 		return consolFun.equals(def.consolFun) && xff == def.xff &&
177 				steps == def.steps && rows == def.rows;
178 	}
179 }