diff --git a/src/main/java/net/openhft/chronicle/queue/BufferMode.java b/src/main/java/net/openhft/chronicle/queue/BufferMode.java index fb261cc6de..74c70a4dfc 100644 --- a/src/main/java/net/openhft/chronicle/queue/BufferMode.java +++ b/src/main/java/net/openhft/chronicle/queue/BufferMode.java @@ -18,10 +18,26 @@ package net.openhft.chronicle.queue; +/** + * Enum representing the different buffer modes that can be used within Chronicle Queue. + * Each mode has a specific use case and behavior depending on the configuration. + */ public enum BufferMode { - None, // The default + /** + * The default buffer mode. + * No additional buffering or special handling is applied. + */ + None, - Copy, // used in conjunction with encryption + /** + * Buffer mode used in conjunction with encryption. + * Data is copied into a buffer before being processed. + */ + Copy, - Asynchronous // used by chronicle-ring [ which is an enterprise product ] + /** + * Buffer mode used for asynchronous processing. + * This mode is specific to Chronicle Ring, an enterprise product. + */ + Asynchronous } diff --git a/src/main/java/net/openhft/chronicle/queue/ChronicleHistoryReaderMain.java b/src/main/java/net/openhft/chronicle/queue/ChronicleHistoryReaderMain.java index 6c100f88b7..b3f4cf73c0 100644 --- a/src/main/java/net/openhft/chronicle/queue/ChronicleHistoryReaderMain.java +++ b/src/main/java/net/openhft/chronicle/queue/ChronicleHistoryReaderMain.java @@ -19,28 +19,45 @@ package net.openhft.chronicle.queue; import net.openhft.chronicle.queue.reader.ChronicleHistoryReader; +import net.openhft.chronicle.wire.MessageHistory; import org.apache.commons.cli.*; import org.jetbrains.annotations.NotNull; import java.io.PrintWriter; import java.nio.file.Paths; +import java.util.Arrays; import java.util.concurrent.TimeUnit; /** - * Reads @see MessageHistory from a chronicle and outputs histograms for + * The main class for reading {@link MessageHistory} from a Chronicle queue + * and generating histograms for: * - * - * @author Jerry Shea + * This class provides options to configure the reader via command line arguments and outputs the results + * to the console. */ public class ChronicleHistoryReaderMain { + private static final int HELP_OUTPUT_LINE_WIDTH = 180; + + /** + * Entry point of the application. + * Initializes the {@link ChronicleHistoryReaderMain} and passes command-line arguments. + * + * @param args Command-line arguments + */ public static void main(@NotNull String[] args) { new ChronicleHistoryReaderMain().run(args); } + /** + * Runs the ChronicleHistoryReader setup and execution. + * Parses command-line options and configures the {@link ChronicleHistoryReader}. + * + * @param args Command-line arguments + */ protected void run(String[] args) { final Options options = options(); final CommandLine commandLine = parseCommandLine(args, options); @@ -51,6 +68,12 @@ protected void run(String[] args) { } } + /** + * Configures the {@link ChronicleHistoryReader} based on the command-line options. + * + * @param commandLine Parsed command-line options + * @param chronicleHistoryReader The history reader to configure + */ protected void setup(@NotNull final CommandLine commandLine, @NotNull final ChronicleHistoryReader chronicleHistoryReader) { chronicleHistoryReader. withMessageSink(System.out::println). @@ -67,36 +90,65 @@ protected void setup(@NotNull final CommandLine commandLine, @NotNull final Chro chronicleHistoryReader.withSummaryOutput(Integer.parseInt(commandLine.getOptionValue('u'))); } + /** + * Initializes a new instance of {@link ChronicleHistoryReader}. + * + * @return A new {@link ChronicleHistoryReader} instance + */ @NotNull protected ChronicleHistoryReader chronicleHistoryReader() { return new ChronicleHistoryReader(); } + /** + * Parses command-line arguments using Apache Commons CLI. + * If help is requested, it prints the help message and exits. + * + * @param args Command-line arguments + * @param options Available command-line options + * @return Parsed {@link CommandLine} object + */ protected CommandLine parseCommandLine(@NotNull final String[] args, final Options options) { - final CommandLineParser parser = new DefaultParser(); + final CommandLineParser parser = new DefaultParser(); // Initialize command-line parser CommandLine commandLine = null; try { commandLine = parser.parse(options, args); + // If help option is selected, print help and exit if (commandLine.hasOption('h')) { printHelpAndExit(options, 0); } } catch (ParseException e) { - printHelpAndExit(options, 1, e.getMessage()); + // If parsing fails, print help with an error message and exit + String cmdLine = Arrays.toString(args); + printHelpAndExit(options, "[-h]".equals(cmdLine) ? 0 : 1, e.getMessage()); } return commandLine; } + /** + * Prints help and exits the program. + * + * @param options Command-line options + * @param status Exit status + */ protected void printHelpAndExit(final Options options, int status) { printHelpAndExit(options, status, null); } + /** + * Prints help and exits the program, optionally with a message. + * + * @param options Command-line options + * @param status Exit status + * @param message Optional message to print before help + */ protected void printHelpAndExit(final Options options, int status, String message) { final PrintWriter writer = new PrintWriter(System.out); new HelpFormatter().printHelp( writer, - 180, + HELP_OUTPUT_LINE_WIDTH, this.getClass().getSimpleName(), message, options, @@ -109,6 +161,11 @@ protected void printHelpAndExit(final Options options, int status, String messag System.exit(status); } + /** + * Configures command-line options for the ChronicleHistoryReaderMain. + * + * @return Configured {@link Options} object + */ @NotNull protected Options options() { final Options options = new Options(); diff --git a/src/main/java/net/openhft/chronicle/queue/ChronicleReaderMain.java b/src/main/java/net/openhft/chronicle/queue/ChronicleReaderMain.java index c0bac2bd4c..756f74a6ad 100644 --- a/src/main/java/net/openhft/chronicle/queue/ChronicleReaderMain.java +++ b/src/main/java/net/openhft/chronicle/queue/ChronicleReaderMain.java @@ -34,14 +34,34 @@ import static java.util.Arrays.stream; /** - * Display records in a Chronicle in a text form. + * Main class for reading and displaying records from a Chronicle Queue in text form. + * Provides several command-line options to control behavior such as including/excluding records + * based on regex, following a live queue, or displaying records in various formats. */ public class ChronicleReaderMain { + private static final int HELP_OUTPUT_LINE_WIDTH = 180; + + /** + * Entry point of the application. Initializes the {@link ChronicleReaderMain} instance and + * passes command-line arguments for execution. + * + * @param args Command-line arguments + */ public static void main(@NotNull String[] args) { new ChronicleReaderMain().run(args); } + /** + * Adds an option to the provided {@link Options} object for command-line parsing. + * + * @param options The options object to add the option to + * @param opt The short name of the option + * @param argName The name of the argument + * @param hasArg Whether the option takes an argument + * @param description Description of the option + * @param isRequired Whether the option is required + */ public static void addOption(final Options options, final String opt, final String argName, @@ -54,6 +74,12 @@ public static void addOption(final Options options, options.addOption(option); } + /** + * Runs the Chronicle Reader with the provided command-line arguments. + * Configures the {@link ChronicleReader} and executes the reader. + * + * @param args Command-line arguments + */ protected void run(@NotNull String[] args) { final Options options = options(); final CommandLine commandLine = parseCommandLine(args, options); @@ -65,35 +91,63 @@ protected void run(@NotNull String[] args) { chronicleReader.execute(); } + /** + * Creates and returns a new instance of {@link ChronicleReader}. + * + * @return A new instance of {@link ChronicleReader} + */ protected ChronicleReader chronicleReader() { return new ChronicleReader(); } + /** + * Parses the command-line arguments using Apache Commons CLI. + * If the help option is provided, prints the help message and exits. + * + * @param args Command-line arguments + * @param options Command-line options available + * @return The parsed {@link CommandLine} object + */ protected CommandLine parseCommandLine(final @NotNull String[] args, final Options options) { final CommandLineParser parser = new DefaultParser(); CommandLine commandLine = null; try { commandLine = parser.parse(options, args); + // Print help if 'h' option is provided if (commandLine.hasOption('h')) { printHelpAndExit(options, 0); } } catch (ParseException e) { + // On parsing error, print help with an error message printHelpAndExit(options, 1, e.getMessage()); } return commandLine; } + /** + * Prints help information and exits the application. + * + * @param options Command-line options + * @param status Exit status code + */ protected void printHelpAndExit(final Options options, int status) { printHelpAndExit(options, status, null); } + /** + * Prints help information along with an optional message and exits the application. + * + * @param options Command-line options + * @param status Exit status code + * @param message Optional message to display before help + */ protected void printHelpAndExit(final Options options, int status, String message) { final PrintWriter writer = new PrintWriter(System.out); new HelpFormatter().printHelp( writer, - 180, + HELP_OUTPUT_LINE_WIDTH, this.getClass().getSimpleName(), message, options, @@ -106,14 +160,23 @@ protected void printHelpAndExit(final Options options, int status, String messag System.exit(status); } + /** + * Configures the {@link ChronicleReader} based on the command-line options. + * Supports various options like regex filtering, tailing the queue, and more. + * + * @param chronicleReader The ChronicleReader instance to configure + * @param commandLine Parsed command-line options + */ protected void configureReader(final ChronicleReader chronicleReader, final CommandLine commandLine) { + // Set up message sink; squash output to single line if 'l' option is provided final Consumer messageSink = commandLine.hasOption('l') ? s -> System.out.println(s.replaceAll("\n", "")) : System.out::println; - chronicleReader. - withMessageSink(messageSink). - withBasePath(Paths.get(commandLine.getOptionValue('d'))); + chronicleReader + .withMessageSink(messageSink) // Configure the message sink + .withBasePath(Paths.get(commandLine.getOptionValue('d'))); // Set base path for chronicle queue files + // Apply various optional configurations based on command-line options if (commandLine.hasOption('i')) { stream(commandLine.getOptionValues('i')).forEach(chronicleReader::withInclusionRegex); } @@ -178,6 +241,11 @@ protected void configureReader(final ChronicleReader chronicleReader, final Comm } } + /** + * Configures the available command-line options for the {@link ChronicleReaderMain}. + * + * @return A configured {@link Options} object with all available options + */ @NotNull protected Options options() { final Options options = new Options(); diff --git a/src/main/java/net/openhft/chronicle/queue/ChronicleWriterMain.java b/src/main/java/net/openhft/chronicle/queue/ChronicleWriterMain.java index 90700f79d7..58f00251d2 100644 --- a/src/main/java/net/openhft/chronicle/queue/ChronicleWriterMain.java +++ b/src/main/java/net/openhft/chronicle/queue/ChronicleWriterMain.java @@ -20,8 +20,19 @@ import org.jetbrains.annotations.NotNull; +/** + * The entry point for writing records to a Chronicle Queue. + * This class delegates the writing task to the internal {@link net.openhft.chronicle.queue.internal.writer.ChronicleWriterMain}. + */ public class ChronicleWriterMain { + /** + * Main method for executing the ChronicleWriterMain. + * It delegates to the internal writer implementation to run the application with the given arguments. + * + * @param args Command-line arguments for the writer + * @throws Exception if an error occurs during execution + */ public static void main(@NotNull String[] args) throws Exception { new net.openhft.chronicle.queue.internal.writer.ChronicleWriterMain().run(args); } diff --git a/src/main/java/net/openhft/chronicle/queue/CycleCalculator.java b/src/main/java/net/openhft/chronicle/queue/CycleCalculator.java index ab10b60655..70e6bcd4ad 100644 --- a/src/main/java/net/openhft/chronicle/queue/CycleCalculator.java +++ b/src/main/java/net/openhft/chronicle/queue/CycleCalculator.java @@ -20,7 +20,23 @@ import net.openhft.chronicle.core.time.TimeProvider; +/** + * Functional interface representing a calculator for determining the current cycle based on + * a {@link RollCycle}, a {@link TimeProvider}, and an optional offset in milliseconds. + *

+ * This interface is intended to be used for customizing the cycle calculation logic in + * Chronicle Queue, particularly when working with different roll cycles or time-based patterns. + */ @FunctionalInterface public interface CycleCalculator { + + /** + * Calculates the current cycle based on the provided {@link RollCycle}, {@link TimeProvider}, and an offset in milliseconds. + * + * @param rollCycle The roll cycle that defines the periodicity of the data rolls + * @param timeProvider The time provider that supplies the current time + * @param offsetMillis The time offset in milliseconds, typically used for adjusting the cycle calculation + * @return The current cycle as an integer, calculated according to the given roll cycle and time + */ int currentCycle(final RollCycle rollCycle, final TimeProvider timeProvider, final long offsetMillis); } diff --git a/src/main/java/net/openhft/chronicle/queue/DefaultCycleCalculator.java b/src/main/java/net/openhft/chronicle/queue/DefaultCycleCalculator.java index 27314a7213..6b460c5c61 100644 --- a/src/main/java/net/openhft/chronicle/queue/DefaultCycleCalculator.java +++ b/src/main/java/net/openhft/chronicle/queue/DefaultCycleCalculator.java @@ -20,9 +20,27 @@ import net.openhft.chronicle.core.time.TimeProvider; +/** + * Singleton enum implementation of {@link CycleCalculator} that provides a default mechanism + * for calculating the current cycle based on the provided {@link RollCycle} and {@link TimeProvider}. + *

+ * This enum ensures there is only one instance of the cycle calculator, represented by {@code INSTANCE}. + */ public enum DefaultCycleCalculator implements CycleCalculator { + /** + * The single instance of the {@code DefaultCycleCalculator}. + */ INSTANCE; + /** + * Calculates the current cycle by delegating to the provided {@link RollCycle}. + * Uses the {@link TimeProvider} and an optional offset in milliseconds to determine the current cycle. + * + * @param rollCycle The roll cycle that defines the periodicity of the data rolls + * @param timeProvider The time provider that supplies the current time + * @param offsetMillis The time offset in milliseconds, typically used for adjusting the cycle calculation + * @return The current cycle as an integer, calculated according to the given roll cycle and time + */ @Override public int currentCycle(final RollCycle rollCycle, final TimeProvider timeProvider, final long offsetMillis) { return rollCycle.current(timeProvider, offsetMillis); diff --git a/src/main/java/net/openhft/chronicle/queue/ExcerptTailer.java b/src/main/java/net/openhft/chronicle/queue/ExcerptTailer.java index ec3ca76056..c8f2657c53 100644 --- a/src/main/java/net/openhft/chronicle/queue/ExcerptTailer.java +++ b/src/main/java/net/openhft/chronicle/queue/ExcerptTailer.java @@ -85,7 +85,7 @@ default long lastReadIndex() { *

* Usually, each cycle will have its own unique data file to store excerpts. * - * @return Returns the current cycle for this Tailer + * @return Returns the current cycle for this Tailer, or Integer.MIN_VALUE if no cycle has been loaded. */ int cycle(); diff --git a/src/main/java/net/openhft/chronicle/queue/NoMessageHistory.java b/src/main/java/net/openhft/chronicle/queue/NoMessageHistory.java index 54da9a330b..8b6055d705 100644 --- a/src/main/java/net/openhft/chronicle/queue/NoMessageHistory.java +++ b/src/main/java/net/openhft/chronicle/queue/NoMessageHistory.java @@ -19,60 +19,129 @@ import net.openhft.chronicle.wire.*; -// Is this being used? -@SuppressWarnings("deprecation") +/** + * A singleton implementation of the {@link MessageHistory} interface that represents a "no-op" or "empty" message history. + * This is used when message history tracking is not required. All methods in this class return default values, + * indicating no history is being stored. + *

+ * It suppresses warnings for deprecated methods, though this may need to be verified in the broader context to ensure + * it's still appropriate. + */ +@SuppressWarnings("deprecation") // Suppressing warnings related to deprecated methods public enum NoMessageHistory implements MessageHistory { + /** + * The single instance of {@code NoMessageHistory}, following the singleton pattern. + */ INSTANCE; + /** + * Returns the number of timings recorded in the message history. Always returns 0 since this implementation does not track timings. + * + * @return 0, indicating no timings are recorded + */ @Override public int timings() { return 0; } + /** + * Returns the timing for the specified index. Always returns -1 as no timings are recorded in this implementation. + * + * @param n The index of the timing (ignored) + * @return -1, indicating no timing available + */ @Override public long timing(int n) { return -1; } + /** + * Returns the number of sources that processed the message. Always returns 0 as no source tracking is done. + * + * @return 0, indicating no sources processed the message + */ @Override public int sources() { - return 0; + return 0; // No sources recorded } + /** + * Returns the source ID for the specified index. Always returns -1 since source tracking is not done. + * + * @param n The index of the source ID (ignored) + * @return -1, indicating no source ID available + */ @Override public int sourceId(int n) { return -1; } + /** + * Determines if the source IDs end with the specified array of source IDs. Always returns false as no source IDs are tracked. + * + * @param sourceIds Array of source IDs to check (ignored) + * @return false, indicating the source IDs do not match + */ @Override public boolean sourceIdsEndsWith(int[] sourceIds) { return false; } + /** + * Returns the source index for the specified index. Always returns -1 as source index tracking is not done. + * + * @param n The index of the source (ignored) + * @return -1, indicating no source index available + */ @Override public long sourceIndex(int n) { return -1; } + /** + * Resets the message history with the provided source ID and source index. + * This implementation ignores the reset as no history is stored. + * + * @param sourceId The source ID to reset (ignored) + * @param sourceIndex The source index to reset (ignored) + */ @Override public void reset(int sourceId, long sourceIndex) { // ignored } + /** + * Resets the message history. This implementation performs no action as no history is stored. + */ public void reset() { // no-op } + /** + * Returns the ID of the last source that processed the message. Always returns -1 as no source tracking is done. + * + * @return -1, indicating no last source ID available + */ @Override public int lastSourceId() { return -1; } + /** + * Returns the index of the last source that processed the message. Always returns -1 as no source tracking is done. + * + * @return -1, indicating no last source index available + */ @Override public long lastSourceIndex() { return -1; } + /** + * Indicates if the message history has been modified. Always returns false as no history is stored or tracked. + * + * @return false, indicating the history is not dirty + */ @Override public boolean isDirty() { return false; diff --git a/src/main/java/net/openhft/chronicle/queue/QueueSystemProperties.java b/src/main/java/net/openhft/chronicle/queue/QueueSystemProperties.java index 05cbc3b95f..b4bd2cb04d 100644 --- a/src/main/java/net/openhft/chronicle/queue/QueueSystemProperties.java +++ b/src/main/java/net/openhft/chronicle/queue/QueueSystemProperties.java @@ -20,39 +20,55 @@ import net.openhft.chronicle.core.Jvm; +/** + * A utility class for managing Chronicle Queue related system properties. + * This class defines constants for various system property keys and provides mechanisms + * for configuring aspects of Chronicle Queue behavior via system properties. + *

+ * The class cannot be instantiated and only contains static fields and methods. + */ public final class QueueSystemProperties { private QueueSystemProperties() { } /** - * Returns if Chronicle Queue shall assert certain index invariants on various - * occasions throughout the code. Setting this property to "", "yes" or "true" - * will enable this feature. Enabling the feature will slow down execution if - * assertions (-ea) are enabled. + * Indicates whether Chronicle Queue should assert certain index invariants at various points in the code. + *

+ * This system property can be used to enable index checking, which will slow down execution if assertions are enabled (-ea). + * The feature is enabled by setting the system property "queue.check.index" to one of the following values: "", "yes", or "true". *

- * System Property key: "queue.check.index" - * Default unset value: false + * System Property key: "queue.check.index"
+ * Default unset value: false
* Activation values : "", "yes", or "true" + * + * @see Jvm#getBoolean(String) for more details on how boolean properties are evaluated. */ public static boolean CHECK_INDEX = Jvm.getBoolean("queue.check.index"); /** - * Name of a system property used to specify the default roll cycle. + * The system property key used to specify the default roll cycle for a Chronicle Queue. *

- * System Property key: "net.openhft.queue.builder.defaultRollCycle" - * Fallback if unset : to {@link net.openhft.chronicle.queue.RollCycles#DEFAULT} - * Valid values : Class name of an entity implementing RollCycle such as "net.openhft.chronicle.queue.harness.WeeklyRollCycle" - * or enum value in class:name format such as "net.openhft.chronicle.queue.RollCycles:HOURLY" + * This property allows configuration of a custom roll cycle by setting the property to either: + *

+ *

+ * System Property key: "net.openhft.queue.builder.defaultRollCycle"
+ * Fallback if unset: {@link net.openhft.chronicle.queue.RollCycles#DEFAULT} */ public static final String DEFAULT_ROLL_CYCLE_PROPERTY = "net.openhft.queue.builder.defaultRollCycle"; /** - * Name of a system property used to specify the default epoch offset property. + * The system property key used to specify the default epoch offset for Chronicle Queue timestamps. + *

+ * This property can be set to any long value, representing the epoch offset in milliseconds. + * The default value is 0L if the property is not set. *

- * System Property key: "net.openhft.queue.builder.defaultEpoch" - * Default unset value: 0L - * Valid values : Any long value + * System Property key: "net.openhft.queue.builder.defaultEpoch"
+ * Default unset value: 0L
+ * Valid values: Any long value */ public static final String DEFAULT_EPOCH_PROPERTY = "net.openhft.queue.builder.defaultEpoch"; diff --git a/src/main/java/net/openhft/chronicle/queue/RollCycle.java b/src/main/java/net/openhft/chronicle/queue/RollCycle.java index b164059a11..cf8961dcb1 100644 --- a/src/main/java/net/openhft/chronicle/queue/RollCycle.java +++ b/src/main/java/net/openhft/chronicle/queue/RollCycle.java @@ -20,56 +20,65 @@ import net.openhft.chronicle.core.time.TimeProvider; import org.jetbrains.annotations.NotNull; +/** + * Interface defining the behavior of a roll cycle in Chronicle Queue. A roll cycle dictates how frequently data rolls over + * to a new file and manages the indexing of records within those cycles. + *

+ * Roll cycles are defined by a period of time, such as MINUTELY or DAILY, and they provide methods for managing the + * cycle duration, formatting, and indexing. + */ public interface RollCycle { /** - * Returns the format that is to be applied when file names are calculated for a new roll cycle. + * Returns the format to be applied when file names are calculated for a new roll cycle. *

- * For example, the following formats can be returned: + * For example, the following formats may be returned: *

- * Lexicographical order of formatted cycles must preserve chronological order, i.e. if {@code cycle1 < cycle2}, - * the same relation must be kept for their string representations. + * The lexicographical order of formatted cycles must preserve chronological order, meaning that if {@code cycle1 < cycle2}, + * the same relation must hold true for their string representations. * - * @return the format that is to be applied when file names are calculated for a new roll cycle + * @return the format that is applied when file names are calculated for a new roll cycle */ @NotNull String format(); /** - * Returns the length in milliseconds (i.e. the maximum duration) for a roll cycle. + * Returns the length in milliseconds for a roll cycle, representing the maximum duration for one cycle. *

- * For example, the following lengths can be returned: + * Examples of cycle lengths: *

* - * @return the length in milliseconds (i.e. the maximum duration) for a roll cycle + * @return the length in milliseconds for the roll cycle */ int lengthInMillis(); /** - * @return the default epoch offset if one is not set + * Returns the default epoch offset if none is set. The epoch offset allows users to define their own start time for the cycle. + * + * @return the default epoch offset, typically 0 */ default int defaultEpoch() { return 0; } /** - * @return the size of each index array, note: indexCount^2 is the maximum number of index queue entries. + * Returns the size of each index array. The value {@code indexCount^2} represents the maximum number of index queue entries. + * + * @return the size of each index array */ int defaultIndexCount(); /** * Returns the space between excerpts that are explicitly indexed. + * A larger value improves sequential write performance but slows down random access reads. Sequential read performance is not impacted. *

- * A higher number means higher sequential write performance but slower random access read. The sequential read performance is not affected by - * this property. - *

- * For example, the following default index spacing can be returned: + * Example values: *