Purging¶
Aeron Archive supports two techniques to manage the underlying archive data files:
- Truncation - enables the removal of data after a position;
- Purging - allows the deletion of data by segment, typically at some point before the current segment in use.
Truncation is useful when you need to delete all messages after a point. Purging helps with data feeds, such as when you only want to keep the most recent records in an archive.
Purging¶
Sample¶
The sample performs the following actions:
- Finds the latest recording (see Basic Sample for sample code to do this)
- Reads the dimensions of the archive
- Computes a new start position - in the sample, the archive is set to a size, but this could be a time or similar if the data is indexed
- Purges all archive segments before the new start position
Important
The new start position specified during a purge must be the same as the start of a segment. In the example below, the max length is set in whole in megabytes. Truncation will only function if you set a segment size compatible with this, for example, 1 megabyte. Segment file lengths are controlled by the configuration parameter segmentFileLength
in the Archive context.
The code sample assumes a valid Aeron Archive is available as aeronArchive
.
final int maxLengthInMegabytes = 250;
final long recordingId = findLatestRecording(aeronArchive);
final long startPosition = aeronArchive.getStartPosition(recordingId);
final long recordingPosition = aeronArchive.getRecordingPosition(recordingId);
final long length = recordingPosition - startPosition;
final long currentLengthWholeMegabytes = length / (1024 * 1024);
if (currentLengthWholeMegabytes > maxLengthInMegabytes)
{
final long megabytesToPurge = wholeMegabytes - maxLengthInMegabytes;
final long newStartPosition = startPosition + (megabytesToPurge * 1024 * 1024);
aeronArchive.purgeSegments(recordingId, newStartPosition);
}