Class FileLineIndex
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
- Since:
- 10.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder forFileLineIndex.static final recordThe result of arefresh(). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum number of index entries.static final intDefault lines per index entry.static final intSize of the head fingerprint in bytes. -
Method Summary
Modifier and TypeMethodDescriptionstatic FileLineIndex.BuilderCreates a builder.file()The indexed file.static FileLineIndexCreates an index with the default stride and entry cap.refresh()Stats the file and indexes only the bytes appended since the previous call.longseek(long n) Returns the byte offset of the start of linen.snapshot()The result of the lastrefresh(), without I/O.
-
Field Details
-
DEFAULT_STRIDE
Default lines per index entry.- See Also:
-
DEFAULT_MAX_ENTRIES
Default maximum number of index entries.- See Also:
-
FINGERPRINT_BYTES
Size of the head fingerprint in bytes.- See Also:
-
-
Method Details
-
of
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
Creates a builder.- Parameters:
file- The file. Supplied by the application, never taken from a request.- Returns:
- A new builder.
-
file
The indexed file.- Returns:
- The path given to the builder.
-
snapshot
The result of the lastrefresh(), 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
Returns the byte offset of the start of linen.- Parameters:
n- The 1-based line number.- Returns:
- The offset;
indexedBytesforn = indexedLines + 1;-1outside[1, indexedLines + 1]or when the file no longer has that line. - Throws:
IOException- If reading fails.
-