pub struct IdentityMap { /* private fields */ }Expand description
Identity Map for tracking unique object instances.
The map is keyed by (TypeId, pk_hash) to ensure each model type has its own namespace, and objects with the same PK return the same reference.
Implementations§
Source§impl IdentityMap
impl IdentityMap
Sourcepub fn insert<M: Model + Send + Sync + 'static>(
&mut self,
model: M,
) -> Arc<RwLock<M>>
pub fn insert<M: Model + Send + Sync + 'static>( &mut self, model: M, ) -> Arc<RwLock<M>>
Insert a model into the identity map.
If an object with the same PK already exists, returns the existing reference (the new object is ignored). Otherwise, inserts the new object and returns a reference to it.
§Returns
An Arc<RwLock<M>> pointing to the object in the map.
Sourcepub fn get<M: Model + Send + Sync + 'static>(
&self,
pk_values: &[Value],
) -> Option<Arc<RwLock<M>>>
pub fn get<M: Model + Send + Sync + 'static>( &self, pk_values: &[Value], ) -> Option<Arc<RwLock<M>>>
Get an object from the identity map by primary key.
§Returns
Some(Arc<RwLock<M>>) if found, None otherwise.
The returned Arc is a clone of the stored Arc, so modifications are shared.
Sourcepub fn contains<M: Model + 'static>(&self, pk_values: &[Value]) -> bool
pub fn contains<M: Model + 'static>(&self, pk_values: &[Value]) -> bool
Check if an object with the given PK exists in the map.
Sourcepub fn contains_model<M: Model + 'static>(&self, model: &M) -> bool
pub fn contains_model<M: Model + 'static>(&self, model: &M) -> bool
Check if a model instance exists in the map.
Sourcepub fn remove<M: Model + 'static>(&mut self, pk_values: &[Value]) -> bool
pub fn remove<M: Model + 'static>(&mut self, pk_values: &[Value]) -> bool
Remove an object from the identity map.
§Returns
true if the object was removed, false if it wasn’t in the map.
Sourcepub fn remove_model<M: Model + 'static>(&mut self, model: &M) -> bool
pub fn remove_model<M: Model + 'static>(&mut self, model: &M) -> bool
Remove a model instance from the identity map.
Sourcepub fn get_or_insert<M: Model + Clone + Send + Sync + 'static>(
&mut self,
model: M,
) -> Arc<RwLock<M>>
pub fn get_or_insert<M: Model + Clone + Send + Sync + 'static>( &mut self, model: M, ) -> Arc<RwLock<M>>
Get or insert a model into the identity map.
If an object with the same PK already exists, returns a reference to it. Otherwise, inserts the new object and returns a reference.
This is useful when you want to either get an existing object or insert a new one atomically.