message_storage.rs

Overview

This file implements the DurableStorageRead trait for the MessageDurableStorage struct, providing methods to load and iterate over stored messages identified by MessageIdentifier. The main responsibility of this module is to facilitate reading message data from durable storage, handling error conditions, and providing navigation between messages in message queues. It abstracts the underlying storage mechanism and exposes a read-only API to access messages wrapped in Arc<WrappedMessage> for thread-safe shared ownership.

The constant BATCH_SIZE is used to limit the number of messages fetched in batch operations to optimize performance and resource usage during iteration.


Implemented Trait: DurableStorageRead<MessageIdentifier, Arc>

The trait DurableStorageRead defines functionality for loading individual messages and iterating over them. This file provides three key methods:

1. load_message

fn load_message(
    &self,
    key: &MessageIdentifier,
) -> Result<Arc<WrappedMessage>, Self::LoadError>
let message = message_storage.load_message(&message_identifier)?;
println!("Loaded message: {:?}", message);

2. next

fn next(&self, key: &MessageIdentifier) -> Result<Option<MessageIdentifier>, Self::LoadError>
if let Some(next_id) = message_storage.next(&current_message_id)? {
    println!("Next message ID: {:?}", next_id);
} else {
    println!("No next message found.");
}

3. remaining_messages

fn remaining_messages(
    &self,
    starting_key: &MessageIdentifier,
    limit: usize,
) -> Result<Vec<Arc<WrappedMessage>>, Self::LoadError>
let messages = message_storage.remaining_messages(&start_message_id, 100)?;
for msg in messages {
    println!("Message: {:?}", msg);
}

Important Implementation Details


Interactions with Other Components


Diagram: Class and Trait Implementation Structure

classDiagram
class MessageDurableStorage {
+read_message()
+next_simple()
}
class DurableStorageRead~K,V~ {
<<trait>>
+load_message(key: &K) -> Result<V, LoadErr>
+next(key: &K) -> Result<Option<K>, LoadErr>
+remaining_messages(starting_key: &K, limit: usize) -> Result<Vec<V>, LoadErr>
}
class MessageIdentifier {
+inner()
}
class WrappedMessage
MessageDurableStorage ..|> DurableStorageRead~MessageIdentifier, Arc<WrappedMessage>~
DurableStorageRead~MessageIdentifier, Arc<WrappedMessage>~ : implements >
MessageDurableStorage --> MessageIdentifier : uses
MessageDurableStorage --> WrappedMessage : loads returns

This diagram models the relationship between MessageDurableStorage, the DurableStorageRead trait it implements, and the key and value types involved (MessageIdentifier and WrappedMessage).


For more information on message identifiers, storage abstractions, and error handling, see the topics related to Message Identification, Durable Storage, and Error Handling.