Skip to main content

DirtyTracker

Struct DirtyTracker 

Source
pub struct DirtyTracker { /* private fields */ }
Expand description

脏字段追踪器

通过维护「原始值快照」与「当前值」两份 HashMap,比较得到脏字段集合。

§设计要点

  • 快照机制new() 时将所有字段视为「原始值」
  • 修改追踪set() 修改字段时,仅写入 current,不动 original
  • 脏字段判定original[field] != current[field] 则该字段为脏
  • 新字段处理set() 一个 original 中不存在的字段时,自动视为脏字段 (等价于 original[field] = Null
  • mark_clean:写入成功后调用,将 original 同步为 current
  • rollback:写入失败时调用 rollback(),将 current 还原为 original

Implementations§

Source§

impl DirtyTracker

Source

pub fn new(initial: HashMap<String, Value>) -> Self

创建脏字段追踪器,传入初始字段值作为快照

Source

pub fn empty() -> Self

创建空追踪器

Source

pub fn set(&mut self, field: impl Into<String>, value: Value)

设置字段值(修改 current,不动 original)

若字段原本不存在(original 中无),则视为新增脏字段。

Source

pub fn set_many(&mut self, fields: HashMap<String, Value>)

批量设置字段值

Source

pub fn get(&self, field: &str) -> Option<&Value>

获取当前值

Source

pub fn get_original(&self, field: &str) -> Option<&Value>

获取原始值(写入失败时可用于回滚业务层)

Source

pub fn current(&self) -> &HashMap<String, Value>

获取所有字段的当前值(克隆)

Source

pub fn original(&self) -> &HashMap<String, Value>

获取所有字段的原始值(克隆)

Source

pub fn is_dirty(&self) -> bool

是否存在脏字段

Source

pub fn is_field_dirty(&self, field: &str) -> bool

判断指定字段是否为脏

Source

pub fn get_dirty_fields(&self) -> Vec<String>

获取所有脏字段名(按字段名字典序排列,保证测试稳定)

Source

pub fn get_dirty_attributes(&self) -> HashMap<String, Value>

获取脏字段及其当前值

Source

pub fn mark_clean(&mut self)

标记所有字段为干净(写入成功后调用)

original 同步为 current,下一次 is_dirty() 返回 false。

Source

pub fn rollback(&mut self)

回滚:将 current 还原为 original(写入失败时调用)

Source

pub fn clear(&mut self)

重置追踪器,丢弃所有数据

Trait Implementations§

Source§

impl Clone for DirtyTracker

Source§

fn clone(&self) -> DirtyTracker

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DirtyTracker

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more