Skip to content

Commit 015fcfa

Browse files
authored
chore: change view id type from String to Arc<str> (#412)
* chore: turn view id into arc * chore: fix compatibility issues in collab importer * chore: fix clippy issues * chore: post rebase fixes * chore: cargo fmt
1 parent ac58509 commit 015fcfa

28 files changed

+240
-263
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

collab-folder/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ tracing.workspace = true
2222
dashmap = "5"
2323
arc-swap = "1.7"
2424
uuid.workspace = true
25+
async-trait.workspace = true
2526

2627
[target.'cfg(target_arch = "wasm32")'.dependencies]
2728
getrandom = { version = "0.2", features = ["js"] }

collab-folder/src/entities.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use serde::{Deserialize, Serialize};
22

3-
use crate::{SectionsByUid, View, Workspace};
3+
use crate::{SectionsByUid, View, ViewId, Workspace};
44

55
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
66
pub struct FolderData {
77
pub uid: i64,
88
pub workspace: Workspace,
9-
pub current_view: String,
9+
pub current_view: ViewId,
1010
pub views: Vec<View>,
1111
#[serde(default)]
1212
pub favorites: SectionsByUid,
@@ -23,7 +23,7 @@ impl FolderData {
2323
Self {
2424
uid,
2525
workspace,
26-
current_view: "".to_string(),
26+
current_view: "".into(),
2727
views: vec![],
2828
favorites: SectionsByUid::new(),
2929
recent: SectionsByUid::new(),
@@ -35,7 +35,7 @@ impl FolderData {
3535

3636
#[derive(Clone, Debug)]
3737
pub struct TrashInfo {
38-
pub id: String,
38+
pub id: ViewId,
3939
pub name: String,
4040
pub created_at: i64,
4141
}

collab-folder/src/folder.rs

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::revision::RevisionMapping;
1919
use crate::section::{Section, SectionItem, SectionMap};
2020
use crate::{
2121
FolderData, ParentChildRelations, SectionChangeSender, SpacePermission, TrashInfo, View,
22-
ViewChangeReceiver, ViewUpdate, ViewsMap, Workspace, impl_section_op,
22+
ViewChangeReceiver, ViewId, ViewUpdate, ViewsMap, Workspace, impl_section_op,
2323
};
2424

2525
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
@@ -215,7 +215,7 @@ impl Folder {
215215
&mut self,
216216
view_id: &str,
217217
new_parent_id: &str,
218-
prev_view_id: Option<String>,
218+
prev_view_id: Option<ViewId>,
219219
uid: i64,
220220
) -> Option<Arc<View>> {
221221
let mut txn = self.collab.transact_mut();
@@ -224,12 +224,12 @@ impl Folder {
224224
.move_nested_view(&mut txn, view_id, new_parent_id, prev_view_id, uid)
225225
}
226226

227-
pub fn set_current_view(&mut self, view_id: String, uid: i64) {
227+
pub fn set_current_view(&mut self, view_id: ViewId, uid: i64) {
228228
let mut txn = self.collab.transact_mut();
229229
self.body.set_current_view(&mut txn, view_id, uid);
230230
}
231231

232-
pub fn get_current_view(&self, uid: i64) -> Option<String> {
232+
pub fn get_current_view(&self, uid: i64) -> Option<ViewId> {
233233
let txn = self.collab.transact();
234234
self.body.get_current_view(&txn, uid)
235235
}
@@ -343,7 +343,7 @@ impl Folder {
343343
}
344344
}
345345

346-
pub fn replace_view(&mut self, from: &str, to: &str, uid: i64) -> bool {
346+
pub fn replace_view(&mut self, from: &ViewId, to: &ViewId, uid: i64) -> bool {
347347
let mut txn = self.collab.transact_mut();
348348
self.body.replace_view(&mut txn, from, to, uid)
349349
}
@@ -715,13 +715,13 @@ impl FolderBody {
715715
txn: &mut TransactionMut,
716716
view_id: &str,
717717
new_parent_id: &str,
718-
prev_view_id: Option<String>,
718+
prev_view_id: Option<ViewId>,
719719
uid: i64,
720720
) -> Option<Arc<View>> {
721721
tracing::debug!("Move nested view: {}", view_id);
722722
let view = self.views.get_view_with_txn(txn, view_id, uid)?;
723723
let current_workspace_id = self.get_workspace_id_with_txn(txn)?;
724-
let parent_id = view.parent_view_id.as_str();
724+
let parent_id = view.parent_view_id.as_ref();
725725

726726
let new_parent_view = self.views.get_view_with_txn(txn, new_parent_id, uid);
727727

@@ -740,7 +740,7 @@ impl FolderBody {
740740
// place it as the first child.
741741
self
742742
.views
743-
.associate_parent_child_with_txn(txn, new_parent_id, view_id, prev_view_id.clone());
743+
.associate_parent_child_with_txn(txn, new_parent_id, view_id, prev_view_id);
744744
// Update the view's parent ID.
745745
self
746746
.views
@@ -750,7 +750,7 @@ impl FolderBody {
750750
Some(view)
751751
}
752752

753-
pub fn get_child_of_first_public_view<T: ReadTxn>(&self, txn: &T, uid: i64) -> Option<String> {
753+
pub fn get_child_of_first_public_view<T: ReadTxn>(&self, txn: &T, uid: i64) -> Option<ViewId> {
754754
self
755755
.get_workspace_id(txn)
756756
.and_then(|workspace_id| self.views.get_view(txn, &workspace_id, uid))
@@ -784,7 +784,7 @@ impl FolderBody {
784784
})
785785
}
786786

787-
pub fn get_current_view<T: ReadTxn>(&self, txn: &T, uid: i64) -> Option<String> {
787+
pub fn get_current_view<T: ReadTxn>(&self, txn: &T, uid: i64) -> Option<ViewId> {
788788
// Fallback to CURRENT_VIEW if CURRENT_VIEW_FOR_USER is not present. This could happen for
789789
// workspace folder created by older version of the app before CURRENT_VIEW_FOR_USER is introduced.
790790
// If user cannot be found in CURRENT_VIEW_FOR_USER, use the first child of the first public space
@@ -795,24 +795,24 @@ impl FolderBody {
795795
};
796796
match current_view_for_user_map {
797797
Some(current_view_for_user) => {
798-
let view_for_user: Option<String> =
798+
let view_for_user: Option<ViewId> =
799799
current_view_for_user.get_with_txn(txn, uid.to_string().as_ref());
800800
view_for_user.or(self.get_child_of_first_public_view(txn, uid))
801801
},
802802
None => self.meta.get_with_txn(txn, CURRENT_VIEW),
803803
}
804804
}
805805

806-
pub fn set_current_view(&self, txn: &mut TransactionMut, view: String, uid: i64) {
806+
pub fn set_current_view(&self, txn: &mut TransactionMut, view: ViewId, uid: i64) {
807807
let current_view_for_user = self.meta.get_or_init_map(txn, CURRENT_VIEW_FOR_USER);
808808
current_view_for_user.try_update(txn, uid.to_string(), view);
809809
}
810810

811811
pub fn replace_view(
812812
&self,
813813
txn: &mut TransactionMut,
814-
old_view_id: &str,
815-
new_view_id: &str,
814+
old_view_id: &ViewId,
815+
new_view_id: &ViewId,
816816
uid: i64,
817817
) -> bool {
818818
self.views.replace_view(txn, old_view_id, new_view_id, uid)
@@ -821,7 +821,7 @@ impl FolderBody {
821821

822822
pub fn default_folder_data(uid: i64, workspace_id: &str) -> FolderData {
823823
let workspace = Workspace {
824-
id: workspace_id.to_string(),
824+
id: workspace_id.into(),
825825
name: "".to_string(),
826826
child_views: Default::default(),
827827
created_at: 0,
@@ -832,7 +832,7 @@ pub fn default_folder_data(uid: i64, workspace_id: &str) -> FolderData {
832832
FolderData {
833833
uid,
834834
workspace,
835-
current_view: "".to_string(),
835+
current_view: "".into(),
836836
views: vec![],
837837
favorites: HashMap::new(),
838838
recent: HashMap::new(),
@@ -846,7 +846,7 @@ mod tests {
846846
use std::collections::HashMap;
847847

848848
use crate::{
849-
Folder, FolderData, RepeatedViewIdentifier, SectionItem, SpaceInfo, UserId, View,
849+
Folder, FolderData, RepeatedViewIdentifier, SectionItem, SpaceInfo, UserId, View, ViewId,
850850
ViewIdentifier, Workspace,
851851
};
852852
use collab::core::collab::default_client_id;
@@ -860,25 +860,25 @@ mod tests {
860860
let options = CollabOptions::new(workspace_id.to_string(), default_client_id());
861861
let collab = Collab::new_with_options(CollabOrigin::Empty, options).unwrap();
862862
let view_1 = View::new(
863-
"view_1".to_string(),
864-
workspace_id.to_string(),
865-
"View 1".to_string(),
863+
"view_1".into(),
864+
workspace_id.into(),
865+
"View 1".into(),
866866
crate::ViewLayout::Document,
867867
Some(uid),
868868
);
869869
let view_1_id = view_1.id.clone();
870870
let view_2 = View::new(
871-
"view_2".to_string(),
872-
workspace_id.to_string(),
873-
"View 2".to_string(),
871+
"view_2".into(),
872+
workspace_id.into(),
873+
"View 2".into(),
874874
crate::ViewLayout::Document,
875875
Some(uid),
876876
);
877877
let view_2_id = view_2.id.clone();
878878
let space_view = View {
879-
id: "space_1_id".to_string(),
880-
parent_view_id: workspace_id.to_string(),
881-
name: "Space 1".to_string(),
879+
id: "space_1_id".into(),
880+
parent_view_id: workspace_id.into(),
881+
name: "Space 1".into(),
882882
children: RepeatedViewIdentifier::new(vec![
883883
ViewIdentifier::new(view_1_id.clone()),
884884
ViewIdentifier::new(view_2_id.clone()),
@@ -895,7 +895,7 @@ mod tests {
895895
};
896896
let space_view_id = space_view.id.clone();
897897
let workspace = Workspace {
898-
id: workspace_id.to_string(),
898+
id: workspace_id.into(),
899899
name: "Workspace".to_string(),
900900
child_views: RepeatedViewIdentifier::new(vec![ViewIdentifier::new(space_view_id.clone())]),
901901
created_at: current_time,
@@ -916,12 +916,12 @@ mod tests {
916916
let mut folder = Folder::create(collab, None, folder_data);
917917

918918
folder.set_current_view(view_2_id.clone(), uid);
919-
assert_eq!(folder.get_current_view(uid), Some(view_2_id.to_string()));
919+
assert_eq!(folder.get_current_view(uid), Some(view_2_id.clone()));
920920
// First visit from user 2, should return the first child of the first public space with children.
921-
assert_eq!(folder.get_current_view(2), Some(view_1_id.to_string()));
922-
folder.set_current_view(view_1_id.to_string(), 2);
923-
assert_eq!(folder.get_current_view(1), Some(view_2_id.to_string()));
924-
assert_eq!(folder.get_current_view(2), Some(view_1_id.to_string()));
921+
assert_eq!(folder.get_current_view(2), Some(view_1_id.clone()));
922+
folder.set_current_view(view_1_id.clone(), 2);
923+
assert_eq!(folder.get_current_view(1), Some(view_2_id));
924+
assert_eq!(folder.get_current_view(2), Some(view_1_id));
925925
}
926926

927927
#[test]
@@ -931,11 +931,11 @@ mod tests {
931931
let uid = 1;
932932
let options = CollabOptions::new(workspace_id.to_string(), default_client_id());
933933
let collab = Collab::new_with_options(CollabOrigin::Empty, options).unwrap();
934-
let space_view_id = "space_view_id".to_string();
934+
let space_view_id: ViewId = "space_view_id".into();
935935
let views: Vec<View> = (0..3)
936936
.map(|i| {
937937
View::new(
938-
format!("view_{:?}", i),
938+
format!("view_{:?}", i).into(),
939939
space_view_id.clone(),
940940
format!("View {:?}", i),
941941
crate::ViewLayout::Document,
@@ -944,9 +944,9 @@ mod tests {
944944
})
945945
.collect();
946946
let space_view = View {
947-
id: "space_1_id".to_string(),
948-
parent_view_id: workspace_id.to_string(),
949-
name: "Space 1".to_string(),
947+
id: "space_1_id".into(),
948+
parent_view_id: workspace_id.into(),
949+
name: "Space 1".into(),
950950
children: RepeatedViewIdentifier::new(
951951
views
952952
.iter()
@@ -964,7 +964,7 @@ mod tests {
964964
extra: Some(serde_json::to_string(&SpaceInfo::default()).unwrap()),
965965
};
966966
let workspace = Workspace {
967-
id: workspace_id.to_string(),
967+
id: workspace_id.into(),
968968
name: "Workspace".to_string(),
969969
child_views: RepeatedViewIdentifier::new(vec![ViewIdentifier::new(space_view_id.clone())]),
970970
created_at: current_time,
@@ -996,25 +996,25 @@ mod tests {
996996
let mut folder = Folder::create(collab, None, folder_data);
997997
let favorite_sections = folder.get_all_favorites_sections(uid);
998998
let expected_favorites = vec![
999-
SectionItem::new("view_0".to_string()),
1000-
SectionItem::new("view_1".to_string()),
1001-
SectionItem::new("view_2".to_string()),
999+
SectionItem::new("view_0".into()),
1000+
SectionItem::new("view_1".into()),
1001+
SectionItem::new("view_2".into()),
10021002
];
10031003
assert_eq!(favorite_sections, expected_favorites);
10041004
folder.move_favorite_view_id("view_0", Some("view_1"), uid);
10051005
let favorite_sections = folder.get_all_favorites_sections(uid);
10061006
let expected_favorites = vec![
1007-
SectionItem::new("view_1".to_string()),
1008-
SectionItem::new("view_0".to_string()),
1009-
SectionItem::new("view_2".to_string()),
1007+
SectionItem::new("view_1".into()),
1008+
SectionItem::new("view_0".into()),
1009+
SectionItem::new("view_2".into()),
10101010
];
10111011
assert_eq!(favorite_sections, expected_favorites);
10121012
folder.move_favorite_view_id("view_2", None, uid);
10131013
let favorite_sections = folder.get_all_favorites_sections(uid);
10141014
let expected_favorites = vec![
1015-
SectionItem::new("view_2".to_string()),
1016-
SectionItem::new("view_1".to_string()),
1017-
SectionItem::new("view_0".to_string()),
1015+
SectionItem::new("view_2".into()),
1016+
SectionItem::new("view_1".into()),
1017+
SectionItem::new("view_0".into()),
10181018
];
10191019
assert_eq!(favorite_sections, expected_favorites);
10201020
}

collab-folder/src/folder_migration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use collab::preclude::{Any, Array, ArrayRef, Map, MapExt, MapRef, ReadTxn, YrsVa
33
use serde::{Deserialize, Serialize};
44

55
use crate::folder::FAVORITES_V1;
6-
use crate::{Folder, ParentChildRelations, SectionItem, Workspace};
6+
use crate::{Folder, ParentChildRelations, SectionItem, ViewId, Workspace};
77

88
const WORKSPACE_ID: &str = "id";
99
const WORKSPACE_NAME: &str = "name";
@@ -62,7 +62,7 @@ pub fn to_workspace_with_txn<T: ReadTxn>(
6262
map_ref: &MapRef,
6363
views: &ParentChildRelations,
6464
) -> Option<Workspace> {
65-
let id: String = map_ref.get_with_txn(txn, WORKSPACE_ID)?;
65+
let id: ViewId = map_ref.get_with_txn(txn, WORKSPACE_ID)?;
6666
let name = map_ref
6767
.get_with_txn(txn, WORKSPACE_NAME)
6868
.unwrap_or_default();
@@ -120,7 +120,7 @@ impl TryFrom<&YrsValue> for FavoriteId {
120120

121121
#[derive(Debug, Serialize, Deserialize)]
122122
struct TrashRecord {
123-
pub id: String,
123+
pub id: ViewId,
124124
#[serde(deserialize_with = "collab::preclude::deserialize_i64_from_numeric")]
125125
pub created_at: i64,
126126
#[serde(default)]

collab-folder/src/folder_observe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tokio::sync::broadcast;
99
use crate::revision::RevisionMapping;
1010
use crate::section::SectionMap;
1111
use crate::view::FOLDER_VIEW_ID;
12-
use crate::{ParentChildRelations, View, view_from_map_ref};
12+
use crate::{ParentChildRelations, View, ViewId, view_from_map_ref};
1313

1414
#[derive(Debug, Clone)]
1515
pub enum ViewChange {
@@ -23,7 +23,7 @@ pub type ViewChangeReceiver = broadcast::Receiver<ViewChange>;
2323

2424
pub(crate) fn subscribe_view_change(
2525
root: &MapRef,
26-
deletion_cache: Arc<DashMap<String, Arc<View>>>,
26+
deletion_cache: Arc<DashMap<ViewId, Arc<View>>>,
2727
change_tx: ViewChangeSender,
2828
view_relations: Arc<ParentChildRelations>,
2929
section_map: Arc<SectionMap>,

0 commit comments

Comments
 (0)