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    
030    /**
031     * Class to represent internal RRD archive state for a single datasource. Objects of this
032     * class are never manipulated directly, it's up to JRobin framework to manage
033     * internal arcihve states.<p>
034     *
035     * @author <a href="mailto:saxon@jrobin.org">Sasa Markovic</a>
036     */
037    public class ArcState implements RrdUpdater {
038            private Archive parentArc;
039    
040            private RrdDouble accumValue;
041            private RrdLong nanSteps;
042    
043            ArcState(Archive parentArc, boolean shouldInitialize) throws IOException {
044                    this.parentArc = parentArc;
045                    accumValue = new RrdDouble(this);
046                    nanSteps = new RrdLong(this);
047                    if (shouldInitialize) {
048                            Header header = parentArc.getParentDb().getHeader();
049                            long step = header.getStep();
050                            long lastUpdateTime = header.getLastUpdateTime();
051                            long arcStep = parentArc.getArcStep();
052                            long initNanSteps = (Util.normalize(lastUpdateTime, step) -
053                                            Util.normalize(lastUpdateTime, arcStep)) / step;
054                            accumValue.set(Double.NaN);
055                            nanSteps.set(initNanSteps);
056                    }
057            }
058    
059            String dump() throws IOException {
060                    return "accumValue:" + accumValue.get() + " nanSteps:" + nanSteps.get() + "\n";
061            }
062    
063            void setNanSteps(long value) throws IOException {
064                    nanSteps.set(value);
065            }
066    
067            /**
068             * Returns the number of currently accumulated NaN steps.
069             *
070             * @return Number of currently accumulated NaN steps.
071             * @throws IOException Thrown in case of I/O error
072             */
073            public long getNanSteps() throws IOException {
074                    return nanSteps.get();
075            }
076    
077            void setAccumValue(double value) throws IOException {
078                    accumValue.set(value);
079            }
080    
081            /**
082             * Returns the value accumulated so far.
083             *
084             * @return Accumulated value
085             * @throws IOException Thrown in case of I/O error
086             */
087            public double getAccumValue() throws IOException {
088                    return accumValue.get();
089            }
090    
091            /**
092             * Returns the Archive object to which this ArcState object belongs.
093             *
094             * @return Parent Archive object.
095             */
096            public Archive getParent() {
097                    return parentArc;
098            }
099    
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    }