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
30 /**
31  * Class to represent internal RRD archive state for a single datasource. Objects of this
32  * class are never manipulated directly, it's up to JRobin framework to manage
33  * internal arcihve states.<p>
34  *
35  * @author <a href="mailto:saxon@jrobin.org">Sasa Markovic</a>
36  */

37 public class ArcState implements RrdUpdater {
38     private Archive parentArc;
39
40     private RrdDouble accumValue;
41     private RrdLong nanSteps;
42
43     ArcState(Archive parentArc, boolean shouldInitialize) throws IOException {
44         this.parentArc = parentArc;
45         accumValue = new RrdDouble(this);
46         nanSteps = new RrdLong(this);
47         if (shouldInitialize) {
48             Header header = parentArc.getParentDb().getHeader();
49             long step = header.getStep();
50             long lastUpdateTime = header.getLastUpdateTime();
51             long arcStep = parentArc.getArcStep();
52             long initNanSteps = (Util.normalize(lastUpdateTime, step) -
53                     Util.normalize(lastUpdateTime, arcStep)) / step;
54             accumValue.set(Double.NaN);
55             nanSteps.set(initNanSteps);
56         }
57     }
58
59     String dump() throws IOException {
60         return "accumValue:" + accumValue.get() + " nanSteps:" + nanSteps.get() + "\n";
61     }
62
63     void setNanSteps(long value) throws IOException {
64         nanSteps.set(value);
65     }
66
67     /**
68      * Returns the number of currently accumulated NaN steps.
69      *
70      * @return Number of currently accumulated NaN steps.
71      * @throws IOException Thrown in case of I/O error
72      */

73     public long getNanSteps() throws IOException {
74         return nanSteps.get();
75     }
76
77     void setAccumValue(double value) throws IOException {
78         accumValue.set(value);
79     }
80
81     /**
82      * Returns the value accumulated so far.
83      *
84      * @return Accumulated value
85      * @throws IOException Thrown in case of I/O error
86      */

87     public double getAccumValue() throws IOException {
88         return accumValue.get();
89     }
90
91     /**
92      * Returns the Archive object to which this ArcState object belongs.
93      *
94      * @return Parent Archive object.
95      */

96     public Archive getParent() {
97         return parentArc;
98     }
99
100     void appendXml(XmlWriter writer) throws IOException {
101         writer.startTag("ds");
102         writer.writeTag("value", accumValue.get());
103         writer.writeTag("unknown_datapoints", nanSteps.get());
104         writer.closeTag(); // ds
105     }
106
107     /**
108      * Copies object's internal state to another ArcState object.
109      *
110      * @param other New ArcState object to copy state to
111      * @throws IOException  Thrown in case of I/O error
112      * @throws RrdException Thrown if supplied argument is not an ArcState object
113      */

114     public void copyStateTo(RrdUpdater other) throws IOException, RrdException {
115         if (!(other instanceof ArcState)) {
116             throw new RrdException(
117                     "Cannot copy ArcState object to " + other.getClass().getName());
118         }
119         ArcState arcState = (ArcState) other;
120         arcState.accumValue.set(accumValue.get());
121         arcState.nanSteps.set(nanSteps.get());
122     }
123
124     /**
125      * Returns the underlying storage (backend) object which actually performs all
126      * I/O operations.
127      *
128      * @return I/O backend object
129      */

130     public RrdBackend getRrdBackend() {
131         return parentArc.getRrdBackend();
132     }
133
134     /**
135      * Required to implement RrdUpdater interface. You should never call this method directly.
136      *
137      * @return Allocator object
138      */

139     public RrdAllocator getRrdAllocator() {
140         return parentArc.getRrdAllocator();
141     }
142 }
143