Package ai.pipestream.common.util
Class ProcessingBufferImpl<T extends com.google.protobuf.Message>
java.lang.Object
ai.pipestream.common.util.ProcessingBufferImpl<T>
- Type Parameters:
T- the type of protobuf message to store
- All Implemented Interfaces:
ProcessingBuffer<T>
public class ProcessingBufferImpl<T extends com.google.protobuf.Message>
extends Object
implements ProcessingBuffer<T>
Implementation of the ProcessingBuffer interface.
This class uses a bounded queue to store protobuf messages and provides methods to save them to disk.
It is thread-safe and clones the protobufs to prevent memory reference issues.
-
Constructor Summary
ConstructorsConstructorDescriptionProcessingBufferImpl(int capacity, Class<T> messageClass) Creates a new ProcessingBufferImpl with the specified capacity and message class.ProcessingBufferImpl(int capacity, Class<T> messageClass, FileSystem fileSystem) Creates a new ProcessingBufferImpl with the specified capacity, message class, and file system.ProcessingBufferImpl(int capacity, Class<T> messageClass, FileSystem fileSystem, Path saveLocation, String savePrefix, int savePrecision) Creates a new ProcessingBufferImpl with the specified capacity, message class, file system, and save parameters.ProcessingBufferImpl(int capacity, Class<T> messageClass, Path saveLocation, String savePrefix, int savePrecision) Creates a new ProcessingBufferImpl with the specified capacity, message class, and save parameters. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a message to the buffer.voidclear()Clears all messages from the buffer.voidSaves the buffer contents to disk when the bean is destroyed.voidSaves all messages in the buffer to disk using default parameters.voidsaveToDisk(String prefix, int numberPrecision) Saves all messages in the buffer to disk to "voidsaveToDisk(Path location, String fileNamePrefix, int numberPrecision) Saves all messages in the buffer to disk.intsize()Returns the number of messages in the buffer.snapshot()Returns a snapshot of all items currently in the buffer.
-
Constructor Details
-
ProcessingBufferImpl
public ProcessingBufferImpl(int capacity, Class<T> messageClass, FileSystem fileSystem, Path saveLocation, String savePrefix, int savePrecision) Creates a new ProcessingBufferImpl with the specified capacity, message class, file system, and save parameters.- Parameters:
capacity- the maximum number of messages to storemessageClass- the class of the protobuf messagefileSystem- the file system to use for saving filessaveLocation- the directory to save the files tosavePrefix- the prefix to use for the file namessavePrecision- the number of digits to use for the file name suffix
-
ProcessingBufferImpl
public ProcessingBufferImpl(int capacity, Class<T> messageClass, Path saveLocation, String savePrefix, int savePrecision) Creates a new ProcessingBufferImpl with the specified capacity, message class, and save parameters. Uses the default file system.- Parameters:
capacity- the maximum number of messages to storemessageClass- the class of the protobuf messagesaveLocation- the directory to save the files tosavePrefix- the prefix to use for the file namessavePrecision- the number of digits to use for the file name suffix
-
ProcessingBufferImpl
Creates a new ProcessingBufferImpl with the specified capacity, message class, and file system. Uses default save parameters.- Parameters:
capacity- the maximum number of messages to storemessageClass- the class of the protobuf messagefileSystem- the file system to use for saving files
-
ProcessingBufferImpl
Creates a new ProcessingBufferImpl with the specified capacity and message class. Uses the default file system and default save parameters.- Parameters:
capacity- the maximum number of messages to storemessageClass- the class of the protobuf message
-
-
Method Details
-
saveOnShutdown
@PreDestroy public void saveOnShutdown()Saves the buffer contents to disk when the bean is destroyed. This method is called automatically during application shutdown. If the buffer is empty, nothing is saved. -
add
Description copied from interface:ProcessingBufferAdds a message to the buffer. If the buffer is full, the oldest message will be removed.- Specified by:
addin interfaceProcessingBuffer<T extends com.google.protobuf.Message>- Parameters:
message- the message to add
-
saveToDisk
public void saveToDisk()Description copied from interface:ProcessingBufferSaves all messages in the buffer to disk using default parameters. Default location is the current directory, default prefix is "protobuf", and default precision is 3 digits.- Specified by:
saveToDiskin interfaceProcessingBuffer<T extends com.google.protobuf.Message>
-
saveToDisk
Description copied from interface:ProcessingBufferSaves all messages in the buffer to disk to "./
" directory.- Specified by:
saveToDiskin interfaceProcessingBuffer<T extends com.google.protobuf.Message>- Parameters:
prefix- the prefix to use for the file namesnumberPrecision- the number of digits to use for the file name suffix
-
saveToDisk
Description copied from interface:ProcessingBufferSaves all messages in the buffer to disk.- Specified by:
saveToDiskin interfaceProcessingBuffer<T extends com.google.protobuf.Message>- Parameters:
location- the directory to save the files tofileNamePrefix- the prefix to use for the file namesnumberPrecision- the number of digits to use for the file name suffix
-
size
public int size()Description copied from interface:ProcessingBufferReturns the number of messages in the buffer.- Specified by:
sizein interfaceProcessingBuffer<T extends com.google.protobuf.Message>- Returns:
- the number of messages
-
clear
public void clear()Description copied from interface:ProcessingBufferClears all messages from the buffer.- Specified by:
clearin interfaceProcessingBuffer<T extends com.google.protobuf.Message>
-
snapshot
Description copied from interface:ProcessingBufferReturns a snapshot of all items currently in the buffer. This method creates a new list to prevent external modification of the buffer. The returned list represents the buffer contents at the moment of the call. This is useful for analysis and reporting.- Specified by:
snapshotin interfaceProcessingBuffer<T extends com.google.protobuf.Message>- Returns:
- a new list containing a snapshot of all messages in the buffer
-