From: Sasa M. <sa...@us...> - 2004-07-12 13:35:25
|
Update of /cvsroot/jrobin/src/org/jrobin/core In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17030/org/jrobin/core Modified Files: FetchData.java RrdFileBackend.java RrdNioBackend.java Util.java Log Message: RRDTool like command parser added. So far, supported RRDTool commands are: - create - dump - update - fetch - last Also, some minor improvements to core FILE and NIO backends. Index: FetchData.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/FetchData.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** FetchData.java 10 Jul 2004 00:06:29 -0000 1.7 --- FetchData.java 12 Jul 2004 13:35:16 -0000 1.8 *************** *** 30,33 **** --- 30,34 ---- import java.io.FileOutputStream; import java.io.ByteArrayOutputStream; + import java.text.DecimalFormat; /** *************** *** 235,238 **** --- 236,276 ---- /** + * Returns string representing fetched data in a RRDTool-like form. + * @return Fetched data as a string in a rrdfetch-like output form. + */ + public String toString() { + final DecimalFormat df = new DecimalFormat("+0.0000000000E00"); + // print header row + StringBuffer buff = new StringBuffer(); + buff.append(padWithBlanks("", 10)); + buff.append(" "); + for(int i = 0; i < dsNames.length; i++) { + buff.append(padWithBlanks(dsNames[i], 18)); + } + buff.append("\n \n"); + for(int i = 0; i < timestamps.length; i++) { + buff.append(padWithBlanks("" + timestamps[i], 10)); + buff.append(":"); + for(int j = 0; j < dsNames.length; j++) { + double value = values[j][i]; + String valueStr = Double.isNaN(value)? "nan": df.format(value); + buff.append(padWithBlanks(valueStr, 18)); + } + buff.append("\n"); + } + return buff.toString(); + } + + private static String padWithBlanks(String input, int width) { + StringBuffer buff = new StringBuffer(""); + int diff = width - input.length(); + while(diff-- > 0) { + buff.append(' '); + } + buff.append(input); + return buff.toString(); + } + + /** * Returns aggregated value from the fetched data for a single datasource. * @param dsName Datasource name Index: RrdNioBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdNioBackend.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** RrdNioBackend.java 29 Jun 2004 09:02:22 -0000 1.8 --- RrdNioBackend.java 12 Jul 2004 13:35:16 -0000 1.9 *************** *** 49,53 **** FileChannel.MapMode mapMode = readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE; ! this.byteBuffer = file.getChannel().map(mapMode, 0, getLength()); if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) { createSyncTask(syncPeriod); --- 49,53 ---- FileChannel.MapMode mapMode = readOnly? FileChannel.MapMode.READ_ONLY: FileChannel.MapMode.READ_WRITE; ! this.byteBuffer = channel.map(mapMode, 0, getLength()); if(syncMode == RrdNioBackendFactory.SYNC_BACKGROUND && !readOnly) { createSyncTask(syncPeriod); *************** *** 108,111 **** --- 108,112 ---- } super.close(); // calls sync() + byteBuffer = null; } Index: Util.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/Util.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** Util.java 9 Jul 2004 12:20:11 -0000 1.17 --- Util.java 12 Jul 2004 13:35:16 -0000 1.18 *************** *** 67,71 **** /** * Returns current timestamp in seconds (without milliseconds). Returned timestamp ! * is obtained with the following expression: * * <code>(System.currentTimeMillis() + 500L) / 1000L</code> --- 67,71 ---- /** * Returns current timestamp in seconds (without milliseconds). Returned timestamp ! * is obtained with the following expression: <p> * * <code>(System.currentTimeMillis() + 500L) / 1000L</code> *************** *** 76,79 **** --- 76,95 ---- } + /** + * Just an alias for {@link #getTime()} method. + * @return Current timestamp (without milliseconds) + */ + public static long getTimestamp() { + return getTime(); + } + + /** + * Rounds the given timestamp to the nearest whole "step" by evaluating + * the following expression:<p> + * <code>timestamp - timestamp % step;</code> + * @param timestamp Timestamp in seconds + * @param step Step in seconds + * @return "Rounded" timestamp + */ public static long normalize(long timestamp, long step) { return timestamp - timestamp % step; Index: RrdFileBackend.java =================================================================== RCS file: /cvsroot/jrobin/src/org/jrobin/core/RrdFileBackend.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** RrdFileBackend.java 29 Jun 2004 12:20:55 -0000 1.6 --- RrdFileBackend.java 12 Jul 2004 13:35:16 -0000 1.7 *************** *** 45,60 **** protected RandomAccessFile file; protected FileLock fileLock; protected RrdFileBackend(String path, boolean readOnly, int lockMode) throws IOException { super(path); - if(!readOnly) { - registerWriter(path); - } file = new RandomAccessFile(path, readOnly? "r": "rw"); ! // We'll try to lock the file only in "rw" mode ! // locks are meaningless for read-only access if(!readOnly) { lockFile(lockMode); } } --- 45,59 ---- protected RandomAccessFile file; + protected FileChannel channel; protected FileLock fileLock; protected RrdFileBackend(String path, boolean readOnly, int lockMode) throws IOException { super(path); file = new RandomAccessFile(path, readOnly? "r": "rw"); ! channel = file.getChannel(); if(!readOnly) { lockFile(lockMode); + // We'll try to lock the file only in "rw" mode + registerWriter(path); } } *************** *** 73,79 **** private void lockFile(int lockMode) throws IOException { if(lockMode == RrdDb.WAIT_IF_LOCKED || lockMode == RrdDb.EXCEPTION_IF_LOCKED) { - FileChannel fileChannel = file.getChannel(); do { ! fileLock = fileChannel.tryLock(); if(fileLock == null) { // could not obtain lock --- 72,77 ---- private void lockFile(int lockMode) throws IOException { if(lockMode == RrdDb.WAIT_IF_LOCKED || lockMode == RrdDb.EXCEPTION_IF_LOCKED) { do { ! fileLock = channel.tryLock(); if(fileLock == null) { // could not obtain lock *************** *** 101,107 **** public void close() throws IOException { super.close(); // calls sync() unlockFile(); file.close(); - unregisterWriter(getPath()); } --- 99,106 ---- public void close() throws IOException { super.close(); // calls sync() + unregisterWriter(getPath()); unlockFile(); + channel.close(); file.close(); } |