1 25 26 package org.jrobin.core; 27 28 import java.io.IOException ; 29 30 42 public class Robin implements RrdUpdater { 43 private Archive parentArc; 44 private RrdInt pointer; 45 private RrdDoubleArray values; 46 private int rows; 47 48 Robin(Archive parentArc, int rows, boolean shouldInitialize) throws IOException { 49 this.parentArc = parentArc; 50 this.pointer = new RrdInt(this); 51 this.values = new RrdDoubleArray(this, rows); 52 this.rows = rows; 53 if(shouldInitialize) { 54 pointer.set(0); 55 values.set(0, Double.NaN, rows); 56 } 57 } 58 59 65 public double[] getValues() throws IOException { 66 return getValues(0, rows); 67 } 68 69 void store(double newValue) throws IOException { 71 int position = pointer.get(); 72 values.set(position, newValue); 73 pointer.set((position + 1) % rows); 74 } 75 76 void bulkStore(double newValue, int bulkCount) throws IOException { 78 assert bulkCount <= rows: "Invalid number of bulk updates: " + bulkCount + 79 " rows=" + rows; 80 int position = pointer.get(); 81 int tailUpdateCount = Math.min(rows - position, bulkCount); 83 values.set(position, newValue, tailUpdateCount); 84 pointer.set((position + tailUpdateCount) % rows); 85 int headUpdateCount = bulkCount - tailUpdateCount; 87 if(headUpdateCount > 0) { 88 values.set(0, newValue, headUpdateCount); 89 pointer.set(headUpdateCount); 90 } 91 } 92 93 void update(double[] newValues) throws IOException { 95 assert rows == newValues.length: "Invalid number of values supplied: " + newValues.length + 96 " rows=" + rows; 97 pointer.set(0); 98 values.writeDouble(0, newValues); 99 } 100 101 String dump() throws IOException { 102 StringBuffer buffer = new StringBuffer ("Robin " + pointer.get() + "/" + rows + ": "); 103 double[] values = getValues(); 104 for(int i = 0; i < values.length; i++) { 105 buffer.append(Util.formatDouble(values[i], true) + " "); 106 } 107 buffer.append("\n"); 108 return buffer.toString(); 109 } 110 111 117 public double getValue(int index) throws IOException { 118 int arrayIndex = (pointer.get() + index) % rows; 119 return values.get(arrayIndex); 120 } 121 122 double[] getValues(int index, int count) throws IOException { 123 assert count <= rows: "Too many values requested: " + count + " rows=" + rows; 124 int startIndex = (pointer.get() + index) % rows; 125 int tailReadCount = Math.min(rows - startIndex, count); 126 double[] tailValues = values.get(startIndex, tailReadCount); 127 if(tailReadCount < count) { 128 int headReadCount = count - tailReadCount; 129 double[] headValues = values.get(0, headReadCount); 130 double[] values = new double[count]; 131 int k = 0; 132 for(int i = 0; i < tailValues.length; i++) { 133 values[k++] = tailValues[i]; 134 } 135 for(int i = 0; i < headValues.length; i++) { 136 values[k++] = headValues[i]; 137 } 138 return values; 139 } 140 else { 141 return tailValues; 142 } 143 } 144 145 150 public Archive getParent() { 151 return parentArc; 152 } 153 154 159 public int getSize() { 160 return rows; 161 } 162 163 169 public void copyStateTo(RrdUpdater other) throws IOException , RrdException { 170 if(!(other instanceof Robin)) { 171 throw new RrdException( 172 "Cannot copy Robin object to " + other.getClass().getName()); 173 } 174 Robin robin = (Robin) other; 175 int rowsDiff = rows - robin.rows; 176 if(rowsDiff == 0) { 177 robin.pointer.set(pointer.get()); 179 robin.values.writeBytes(values.readBytes()); 180 } 181 else { 182 for(int i = 0; i < robin.rows; i++) { 184 int j = i + rowsDiff; 185 robin.store(j >= 0? getValue(j): Double.NaN); 186 } 187 } 188 } 189 190 void filterValues(double minValue, double maxValue) throws IOException { 191 for(int i = 0; i < rows; i++) { 192 double value = values.get(i); 193 if(!Double.isNaN(minValue) && !Double.isNaN(value) && minValue > value) { 194 values.set(i, Double.NaN); 195 } 196 if(!Double.isNaN(maxValue) && !Double.isNaN(value) && maxValue < value) { 197 values.set(i, Double.NaN); 198 } 199 } 200 } 201 202 207 public RrdBackend getRrdBackend() { 208 return parentArc.getRrdBackend(); 209 } 210 211 215 public RrdAllocator getRrdAllocator() { 216 return parentArc.getRrdAllocator(); 217 } 218 } 219 | Popular Tags |