Methods Summary |
---|
protected synchronized void | buildLogFileIndex()This method builds the file index in the beginning. The index is for
the beginning of every record after the size specified by '_indexSize'
variable.
int cnt, idx;
// NOTE: This should be -1 for indexing at the right intervals
long recordCount = -1;
char recordBeginMarker[] = RECORD_BEGIN_MARKER.toCharArray();
int recordBeginMarkerLen = recordBeginMarker.length;
// Open the file and skip to the where we left off
long charPos = ((Long)_recordIdx.get(_recordIdx.size()-1)).longValue();
BufferedReader reader = getLogFileReader(charPos);
long localIndexSize = getIndexSize();
try {
while (true) {
try {
cnt = reader.read();
if (cnt == -1) {
break;
}
charPos++;
// Compare to RECORD_BEGIN_MARKER
for (idx=0; idx<recordBeginMarkerLen; idx++) {
if (cnt != recordBeginMarker[idx]) {
break;
}
cnt = reader.read();
charPos++;
}
if (idx == recordBeginMarkerLen) {
// Begining of a new record
recordCount++;
if (recordCount == localIndexSize) {
// Now we have traversed the records equal
// to index size. Time to add a new entry
// into the index.
recordCount = 0;
_recordIdx.add(
new Long(charPos - (recordBeginMarkerLen+1)));
}
}
} catch (EOFException ex) {
break;
} catch (Exception ex) {
ex.printStackTrace();
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
}
}
}
|
protected java.io.BufferedReader | getFilePosition(long recordNumber)This method returns the file position given the record number.
// The index is stored from the second slot. i.e., if there
// are 100 records and the index will be on 20, 40, 60, 80, 100
// if the _indexSize is 20. We don't store '0' hence we subtract
// from 1 to get the right index
int index = (int)(recordNumber / getIndexSize());
if (_recordIdx.size() <= index) {
// We have not indexed enough
buildLogFileIndex();
if (_recordIdx.size() <= index) {
// Hmm... something's not right
throw new IllegalArgumentException(
"Attempting to access Log entries that don't exist! ");
}
}
return getRecordPosition(index,
(int) (recordNumber % getIndexSize()));
|
public long | getIndexSize()
return _indexSize;
|
public long | getLastIndexNumber()The log records are indexed, this method returns the last index. It
will ensure that the indexes are up-to-date.
// Ensure the file is fully indexed for this call
buildLogFileIndex();
return _recordIdx.size()-1;
|
public java.util.List | getLogEntries(long startingRecord)This method returns up to _indexSize records starting with the given
record number.
return getLogEntries(startingRecord, getIndexSize());
|
public java.util.List | getLogEntries(long startingRecord, long maxRecords)This method returns up to _indexSize records starting with the given
record number. It will return up to "maxRecords" records.
if (startingRecord < 0) {
return null;
}
// Open the file at the desired starting Record
BufferedReader reader = getFilePosition(startingRecord);
List results = new ArrayList();
try {
while (results.size()<maxRecords) {
// Get a line from the log file
String line = reader.readLine();
if (line == null) {
break;
}
if (!line.startsWith(RECORD_BEGIN_MARKER)) {
continue;
}
// Read the whole record
while (!line.endsWith(RECORD_END_MARKER)) {
line += "\n" + reader.readLine();
}
// Read the LogEntry
try {
results.add(new LogEntry(line,
startingRecord+results.size()));
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ex) {
}
}
}
// Return the results
return results;
|
public java.lang.String | getLogFileName()
return _logFileName;
|
protected java.io.BufferedReader | getLogFileReader(long fromFilePosition)This method opens the server.log file and moves the stream to
the specified filePosition.
try {
FileInputStream file = new FileInputStream(getLogFileName());
file.skip(fromFilePosition);
BufferedReader reader =
new BufferedReader(new InputStreamReader(file));
return reader;
} catch (Exception ex) {
System.err.println("Exception in openFile..." + ex);
ex.printStackTrace();
}
return null;
|
private java.io.BufferedReader | getRecordPosition(int index, int recordsToAdvance)Gets the precise record position from the specified FilePosition.
// Get the indexed file position
long filePosition = ((Long)_recordIdx.get(index)).longValue();
BufferedReader reader = getLogFileReader(filePosition) ;
char recordBeginMarker[] = RECORD_BEGIN_MARKER.toCharArray();
int recordBeginMarkerLen = recordBeginMarker.length;
int ch;
int idx;
while (recordsToAdvance > 0) {
try {
// If we read past the end, throw exception
ch = reader.read();
filePosition++;
// Compare to RECORD_BEGIN_MARKER
for (idx=0; idx<recordBeginMarkerLen; idx++) {
if (ch != recordBeginMarker[idx]) {
break;
}
ch = reader.read();
filePosition++;
}
if (idx == recordBeginMarkerLen) {
// Begining of a new record
recordsToAdvance--;
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
// Return the reader
return reader;
|
public void | setIndexSize(long indexSize)The number of records between indexes. This is also used as the max
number of records returned from getLogEntries(long).
_indexSize = indexSize;
|
public void | setLogFileName(java.lang.String filename)
if (filename.equals(getLogFileName())) {
return;
}
_logFileName = filename;
_recordIdx = new ArrayList();
_recordIdx.add(new Long(0));
|