refactor: fix name err & add counter state fast snapshot

This commit is contained in:
Zixuan Chen 2024-06-18 18:17:46 +08:00
parent d6c8a632ad
commit 9c050b8b0b
No known key found for this signature in database
8 changed files with 46 additions and 12 deletions

View file

@ -44,7 +44,8 @@
"DEBUG": "*" "DEBUG": "*"
}, },
"rust-analyzer.cargo.features": [ "rust-analyzer.cargo.features": [
"test_utils" "test_utils",
"counter"
], ],
"editor.defaultFormatter": "rust-lang.rust-analyzer", "editor.defaultFormatter": "rust-lang.rust-analyzer",
"rust-analyzer.server.extraEnv": { "rust-analyzer.server.extraEnv": {

View file

@ -90,7 +90,7 @@ pub(crate) struct ContainerCreationContext<'a> {
peer: PeerID, peer: PeerID,
} }
pub(crate) trait FastStateSnashot { pub(crate) trait FastStateSnapshot {
fn encode_snapshot_fast<W: Write>(&mut self, w: W); fn encode_snapshot_fast<W: Write>(&mut self, w: W);
fn decode_value(bytes: &[u8]) -> LoroResult<(LoroValue, &[u8])>; fn decode_value(bytes: &[u8]) -> LoroResult<(LoroValue, &[u8])>;
fn decode_snapshot_fast( fn decode_snapshot_fast(

View file

@ -125,3 +125,36 @@ impl ContainerState for CounterState {
false false
} }
} }
mod snapshot {
use crate::state::FastStateSnapshot;
use super::*;
impl FastStateSnapshot for CounterState {
fn encode_snapshot_fast<W: std::io::Write>(&mut self, mut w: W) {
let bytes = self.value.to_le_bytes();
w.write_all(&bytes).unwrap();
}
fn decode_value(bytes: &[u8]) -> LoroResult<(LoroValue, &[u8])> {
Ok((
LoroValue::Double(f64::from_le_bytes(bytes.try_into().unwrap())),
&[],
))
}
fn decode_snapshot_fast(
idx: ContainerIdx,
v: (LoroValue, &[u8]),
ctx: crate::state::ContainerCreationContext,
) -> LoroResult<Self>
where
Self: Sized,
{
let mut counter = CounterState::new(idx);
counter.value = *v.0.as_double().unwrap();
Ok(counter)
}
}
}

View file

@ -4,7 +4,7 @@ use std::{
sync::{Arc, Mutex, Weak}, sync::{Arc, Mutex, Weak},
}; };
use super::{ContainerState, FastStateSnashot}; use super::{ContainerState, FastStateSnapshot};
use crate::{ use crate::{
arena::SharedArena, arena::SharedArena,
container::{idx::ContainerIdx, list::list_op::ListOp, ContainerID}, container::{idx::ContainerIdx, list::list_op::ListOp, ContainerID},
@ -552,7 +552,7 @@ mod snapshot {
use super::*; use super::*;
impl FastStateSnashot for ListState { impl FastStateSnapshot for ListState {
fn encode_snapshot_fast<W: Write>(&mut self, mut w: W) { fn encode_snapshot_fast<W: Write>(&mut self, mut w: W) {
let value = self.get_value(); let value = self.get_value();
postcard::to_io(&value, &mut w).unwrap(); postcard::to_io(&value, &mut w).unwrap();

View file

@ -245,12 +245,12 @@ mod snapshot {
use crate::{ use crate::{
delta::MapValue, delta::MapValue,
state::{ContainerCreationContext, ContainerState, FastStateSnashot}, state::{ContainerCreationContext, ContainerState, FastStateSnapshot},
}; };
use super::MapState; use super::MapState;
impl FastStateSnashot for MapState { impl FastStateSnapshot for MapState {
fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) { fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) {
let value = self.get_value(); let value = self.get_value();
postcard::to_io(&value, &mut w).unwrap(); postcard::to_io(&value, &mut w).unwrap();

View file

@ -1524,7 +1524,7 @@ mod snapshot {
use crate::{ use crate::{
encoding::value_register::ValueRegister, encoding::value_register::ValueRegister,
state::{ContainerCreationContext, ContainerState, FastStateSnashot}, state::{ContainerCreationContext, ContainerState, FastStateSnapshot},
}; };
use super::{ use super::{
@ -1532,7 +1532,7 @@ mod snapshot {
EncodedItemForFastSnapshot, MovableListState, EncodedItemForFastSnapshot, MovableListState,
}; };
impl FastStateSnashot for MovableListState { impl FastStateSnapshot for MovableListState {
fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) { fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) {
let value = self.get_value(); let value = self.get_value();
postcard::to_io(&value, &mut w).unwrap(); postcard::to_io(&value, &mut w).unwrap();

View file

@ -877,7 +877,7 @@ mod snapshot {
TextStyleInfoFlag, TextStyleInfoFlag,
}, },
encoding::value_register::ValueRegister, encoding::value_register::ValueRegister,
state::{ContainerCreationContext, ContainerState, FastStateSnashot}, state::{ContainerCreationContext, ContainerState, FastStateSnapshot},
utils::lazy::LazyLoad, utils::lazy::LazyLoad,
}; };
@ -915,7 +915,7 @@ mod snapshot {
marks: Vec<EncodedMark>, marks: Vec<EncodedMark>,
} }
impl FastStateSnashot for RichtextState { impl FastStateSnapshot for RichtextState {
fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) { fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) {
let value = self.get_value(); let value = self.get_value();
postcard::to_io(&value, &mut w).unwrap(); postcard::to_io(&value, &mut w).unwrap();

View file

@ -1234,7 +1234,7 @@ mod snapshot {
use loro_common::{IdFull, PeerID, TreeID}; use loro_common::{IdFull, PeerID, TreeID};
use serde_columnar::columnar; use serde_columnar::columnar;
use crate::{encoding::value_register::ValueRegister, state::FastStateSnashot}; use crate::{encoding::value_register::ValueRegister, state::FastStateSnapshot};
use super::{TreeNode, TreeParentId, TreeState}; use super::{TreeNode, TreeParentId, TreeState};
@ -1302,7 +1302,7 @@ mod snapshot {
) )
} }
impl FastStateSnashot for TreeState { impl FastStateSnapshot for TreeState {
fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) { fn encode_snapshot_fast<W: std::io::prelude::Write>(&mut self, mut w: W) {
let alive_tree_nodes = self.tree_nodes(); let alive_tree_nodes = self.tree_nodes();
let deleted_tree_nodes = self.deleted_tree_nodes(); let deleted_tree_nodes = self.deleted_tree_nodes();