DataCommon.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.data.db;
00002
00003 import java.io.FileInputStream;
00004 import java.io.IOException;
00005 import java.io.PrintStream;
00006 import java.nio.ByteBuffer;
00007 import java.nio.channels.FileChannel;
00008
00009
00010
00011
00012
00013
00014
00015 abstract public class DataCommon
00016 {
00017 private final static int HEADER_COMMON_SIZE = 256;
00018 private final static int MESSAGE_SIZE = 32;
00019 private final static int MAGIC = 0x06870630;
00020
00021 protected long format;
00022 protected long num_threads;
00023 protected long num_cctid;
00024 protected long num_metric;
00025
00026 protected String filename;
00027
00028
00029
00030
00031
00032
00033
00034 public void open(final String file)
00035 throws IOException
00036 {
00037 filename = file;
00038
00039 final FileInputStream fis = new FileInputStream(filename);
00040 final FileChannel channel = fis.getChannel();
00041
00042 ByteBuffer buffer = ByteBuffer.allocate(HEADER_COMMON_SIZE);
00043 int numBytes = channel.read(buffer);
00044 if (numBytes > 0)
00045 {
00046
00047 readHeader(file, fis, buffer);
00048
00049
00050
00051
00052 if ( readNext(channel) )
00053 {
00054
00055 }
00056 }
00057
00058 channel.close();
00059 fis.close();
00060 }
00061
00062
00063
00064
00065
00066 public void printInfo( PrintStream out)
00067 {
00068 out.println("Format: " + format);
00069 out.println("Num threads: " + num_threads);
00070 out.println("Num cctid: " + num_cctid);
00071 out.println("Num metric: " + num_metric);
00072 }
00073
00074
00075
00076
00077
00078 public void dispose() throws IOException
00079 {
00080
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 protected void readHeader(String file, FileInputStream fis, ByteBuffer buffer)
00093 throws IOException
00094 {
00095
00096
00097
00098 final byte []bytes = new byte[MESSAGE_SIZE];
00099 buffer.flip();
00100 buffer.get(bytes, 0, MESSAGE_SIZE);
00101
00102
00103
00104
00105 long magic_number = buffer.getLong();
00106 if (magic_number != MAGIC)
00107 {
00108 throw_exception(fis, "Magic number is incorrect: " + magic_number);
00109 }
00110
00111
00112
00113
00114 final long version = buffer.getLong();
00115 if (version < 3)
00116 {
00117 throw_exception(fis, "Incorrect file version: " + version);
00118 }
00119
00120
00121
00122
00123 final long type = buffer.getLong();
00124 if (!isTypeFormatCorrect(type))
00125 {
00126 throw_exception(fis, file + " has inconsistent type " + type);
00127 }
00128
00129
00130
00131
00132
00133 format = buffer.getLong();
00134
00135
00136
00137
00138 num_cctid = buffer.getLong();
00139
00140
00141
00142
00143 num_metric = buffer.getLong();
00144
00145
00146
00147
00148 num_threads = buffer.getLong();
00149
00150 if (num_threads <= 0 || num_cctid <= 0 || num_metric <=0)
00151 {
00152
00153
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 private void throw_exception(FileInputStream input, String message)
00166 throws IOException
00167 {
00168 input.close();
00169 throw new IOException(message);
00170 }
00171
00172 protected abstract boolean isTypeFormatCorrect(long type);
00173 protected abstract boolean isFileHeaderCorrect(String header);
00174 protected abstract boolean readNext(FileChannel input) throws IOException;
00175 }