Class FileLineIndex

java.lang.Object
org.apache.juneau.console.views.FileLineIndex

public final class FileLineIndex extends Object
A sparse, bounded index from 1-based line numbers to byte offsets in one UTF-8 text file that may keep growing while it is read.

The index keeps one offset per stride lines. When maxEntries offsets are held, the stride doubles and every other entry is dropped, so memory stays at most maxEntries longs whatever the file size. A seek(long) scans at most stride - 1 lines from the nearest entry. refresh() reads only bytes appended since the previous call.

The index resets (new epoch) when the file key changes (replaced or rotated by rename), when the file shrinks below the indexed size (truncated), or when its first bytes change (rewritten in place). A copy-truncate that regrows past the old indexed size with byte-identical first 64 bytes before the next refresh() cannot be detected; rotate logs by rename instead.

Thread-safe. Keep one instance per log file and share it across requests; otherwise every request re-scans the file from byte 0.

Example:

FileLineIndex index = FileLineIndex.of(logFile); FileLineIndex.Snapshot s = index.refresh(); long offset = index.seek(s.indexedLines());

Since:
10.0.0
  • Field Details

  • Method Details

    • of

      public static FileLineIndex of(Path file)
      Creates an index with the default stride and entry cap.
      Parameters:
      file - The file. Supplied by the application, never taken from a request.
      Returns:
      A new index.
    • create

      public static FileLineIndex.Builder create(Path file)
      Creates a builder.
      Parameters:
      file - The file. Supplied by the application, never taken from a request.
      Returns:
      A new builder.
    • file

      public Path file()
      The indexed file.
      Returns:
      The path given to the builder.
    • snapshot

      The result of the last refresh(), without I/O.
      Returns:
      The snapshot.
    • refresh

      Stats the file and indexes only the bytes appended since the previous call.
      Returns:
      The new snapshot. A missing file gives an empty snapshot.
      Throws:
      IOException - If reading fails.
    • seek

      public long seek(long n) throws IOException
      Returns the byte offset of the start of line n.
      Parameters:
      n - The 1-based line number.
      Returns:
      The offset; indexedBytes for n = indexedLines + 1; -1 outside [1, indexedLines + 1] or when the file no longer has that line.
      Throws:
      IOException - If reading fails.