thread_references_state.rs

Overview

This file manages the state and logic related to cross-thread block references within a distributed or concurrent system. It tracks which blocks from various threads are referenced, updates these references, and determines if a given set of explicit block references can be validly incorporated into the current state. The file handles maintaining the latest known references per thread, walking back through block histories to resolve references, and moving references forward in the state. It is central to ensuring thread-safe consistency and correctness in scenarios where blocks from multiple threads inter-reference.

Data Structures and Types

ReferencedBlock

Represents a reference to a specific block within a thread.

ThreadReferencesState

Tracks the current known references to blocks across all active threads.

ResultingState

Represents the outcome of a reference query, detailing which blocks are explicitly and implicitly referenced.

CanRefQueryResult

Indicates whether a set of explicit block references can be accepted.

Methods of ThreadReferencesState

all_thread_refs(&self) -> &HashMap<ThreadIdentifier, ReferencedBlock>

Returns a reference to the internal map of all thread references.

update(&mut self, thread: ThreadIdentifier, referenced_block: impl Into<ReferencedBlock>)

Updates the reference for a specific thread to a new referenced block.

can_reference<F>(&self, explicit_references: Vec<BlockIdentifier>, get_ref_data: F) -> anyhow::Result<CanRefQueryResult>

Determines if the given explicit block references can be incorporated into the current state.

move_refs<F>(&mut self, refs: Vec<BlockIdentifier>, get_ref_data: F) -> anyhow::Result<Vec<CrossThreadRefData>>

Moves the references forward in the state using a set of block identifiers.

Helper Functions

walk_back_into_history<F>(cursor: &BlockIdentifier, read: F, cutoff: &HashMap<ThreadIdentifier, ReferencedBlock>) -> anyhow::Result<Vec<CrossThreadRefData>>

Walks backward through the block history starting from cursor until reaching a cutoff block per thread.

keep_tails(referenced_blocks: &mut Vec<(ThreadIdentifier, BlockIdentifier, BlockSeqNo)>)

Filters a list of referenced blocks to keep only the latest (tail) block per thread.

Implementation Details and Algorithms

Interaction with Other System Components

Visual Diagram: Class and Function Structure

classDiagram
class ReferencedBlock {
+block_thread_identifier: ThreadIdentifier
+block_identifier: BlockIdentifier
+block_seq_no: BlockSeqNo
+from(tuple): Self
}
class ThreadReferencesState {
-all_thread_refs: HashMap<ThreadIdentifier, ReferencedBlock>
+all_thread_refs(): &HashMap<ThreadIdentifier, ReferencedBlock>
+update(thread: ThreadIdentifier, referenced_block: Into<ReferencedBlock>)
+can_reference(explicit_refs: Vec<BlockIdentifier>, get_ref_data: F) -> anyhow::Result<CanRefQueryResult>
+move_refs(refs: Vec<BlockIdentifier>, get_ref_data: F) -> anyhow::Result<Vec<CrossThreadRefData>>
}
class ResultingState {
+implicitly_referenced_blocks: Vec<BlockIdentifier>
+explicitly_referenced_blocks: Vec<BlockIdentifier>
}
class CanRefQueryResult {
<<enumeration>>
+No
+Yes(ResultingState)
}
class walk_back_into_history {
+walk_back_into_history(cursor: &BlockIdentifier, read: F, cutoff: &HashMap) -> anyhow::Result<Vec<CrossThreadRefData>>
}
class keep_tails {
+keep_tails(referenced_blocks: &mut Vec<(ThreadIdentifier, BlockIdentifier, BlockSeqNo)>)
}
ThreadReferencesState --> ReferencedBlock : uses
ThreadReferencesState --> ResultingState : returns
ThreadReferencesState --> CanRefQueryResult : returns
ThreadReferencesState ..> walk_back_into_history : calls
ThreadReferencesState ..> keep_tails : calls

This diagram shows the main structs and enums, their key methods and fields, and the relationships between them. The utility functions walk_back_into_history and keep_tails are used internally by ThreadReferencesState.