Skip to content

Commit a0b5f09

Browse files
authored
chore: save folder snapshot with custom config (#1670)
1 parent d4946f1 commit a0b5f09

File tree

8 files changed

+161
-59
lines changed

8 files changed

+161
-59
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- This file should undo anything in `up.sql`
2+
DROP TABLE folder_rev_snapshot;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Your SQL goes here
2+
CREATE TABLE folder_rev_snapshot (
3+
snapshot_id TEXT NOT NULL PRIMARY KEY DEFAULT '',
4+
object_id TEXT NOT NULL DEFAULT '',
5+
rev_id BIGINT NOT NULL DEFAULT 0,
6+
base_rev_id BIGINT NOT NULL DEFAULT 0,
7+
timestamp BIGINT NOT NULL DEFAULT 0,
8+
data BLOB NOT NULL DEFAULT (x'')
9+
);

frontend/rust-lib/flowy-database/src/schema.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// @generated automatically by Diesel CLI.
2-
3-
diesel::table! {
1+
table! {
42
app_table (id) {
53
id -> Text,
64
workspace_id -> Text,
@@ -15,7 +13,7 @@ diesel::table! {
1513
}
1614
}
1715

18-
diesel::table! {
16+
table! {
1917
document_rev_table (id) {
2018
id -> Integer,
2119
document_id -> Text,
@@ -26,14 +24,25 @@ diesel::table! {
2624
}
2725
}
2826

29-
diesel::table! {
27+
table! {
28+
folder_rev_snapshot (snapshot_id) {
29+
snapshot_id -> Text,
30+
object_id -> Text,
31+
rev_id -> BigInt,
32+
base_rev_id -> BigInt,
33+
timestamp -> BigInt,
34+
data -> Binary,
35+
}
36+
}
37+
38+
table! {
3039
grid_block_index_table (row_id) {
3140
row_id -> Text,
3241
block_id -> Text,
3342
}
3443
}
3544

36-
diesel::table! {
45+
table! {
3746
grid_meta_rev_table (id) {
3847
id -> Integer,
3948
object_id -> Text,
@@ -44,7 +53,7 @@ diesel::table! {
4453
}
4554
}
4655

47-
diesel::table! {
56+
table! {
4857
grid_rev_snapshot (snapshot_id) {
4958
snapshot_id -> Text,
5059
object_id -> Text,
@@ -55,7 +64,7 @@ diesel::table! {
5564
}
5665
}
5766

58-
diesel::table! {
67+
table! {
5968
grid_rev_table (id) {
6069
id -> Integer,
6170
object_id -> Text,
@@ -66,7 +75,7 @@ diesel::table! {
6675
}
6776
}
6877

69-
diesel::table! {
78+
table! {
7079
grid_view_rev_table (id) {
7180
id -> Integer,
7281
object_id -> Text,
@@ -77,14 +86,14 @@ diesel::table! {
7786
}
7887
}
7988

80-
diesel::table! {
89+
table! {
8190
kv_table (key) {
8291
key -> Text,
8392
value -> Binary,
8493
}
8594
}
8695

87-
diesel::table! {
96+
table! {
8897
rev_snapshot (id) {
8998
id -> Integer,
9099
object_id -> Text,
@@ -93,7 +102,7 @@ diesel::table! {
93102
}
94103
}
95104

96-
diesel::table! {
105+
table! {
97106
rev_table (id) {
98107
id -> Integer,
99108
doc_id -> Text,
@@ -105,7 +114,7 @@ diesel::table! {
105114
}
106115
}
107116

108-
diesel::table! {
117+
table! {
109118
trash_table (id) {
110119
id -> Text,
111120
name -> Text,
@@ -116,7 +125,7 @@ diesel::table! {
116125
}
117126
}
118127

119-
diesel::table! {
128+
table! {
120129
user_table (id) {
121130
id -> Text,
122131
name -> Text,
@@ -127,7 +136,7 @@ diesel::table! {
127136
}
128137
}
129138

130-
diesel::table! {
139+
table! {
131140
view_table (id) {
132141
id -> Text,
133142
belong_to_id -> Text,
@@ -143,7 +152,7 @@ diesel::table! {
143152
}
144153
}
145154

146-
diesel::table! {
155+
table! {
147156
workspace_table (id) {
148157
id -> Text,
149158
name -> Text,
@@ -155,9 +164,10 @@ diesel::table! {
155164
}
156165
}
157166

158-
diesel::allow_tables_to_appear_in_same_query!(
167+
allow_tables_to_appear_in_same_query!(
159168
app_table,
160169
document_rev_table,
170+
folder_rev_snapshot,
161171
grid_block_index_table,
162172
grid_meta_rev_table,
163173
grid_rev_snapshot,

frontend/rust-lib/flowy-folder/src/manager.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use lazy_static::lazy_static;
2323
use lib_infra::future::FutureResult;
2424

2525
use crate::services::clear_current_workspace;
26-
use crate::services::persistence::rev_sqlite::SQLiteFolderRevisionPersistence;
26+
use crate::services::persistence::rev_sqlite::{
27+
SQLiteFolderRevisionPersistence, SQLiteFolderRevisionSnapshotPersistence,
28+
};
2729
use flowy_http_model::ws_data::ServerRevisionWSData;
2830
use flowy_sync::client_folder::FolderPad;
2931
use std::convert::TryFrom;
@@ -174,12 +176,15 @@ impl FolderManager {
174176
let configuration = RevisionPersistenceConfiguration::new(100, false);
175177
let rev_persistence = RevisionPersistence::new(user_id, object_id, disk_cache, configuration);
176178
let rev_compactor = FolderRevisionMergeable();
179+
180+
let snapshot_object_id = format!("folder:{}", object_id);
181+
let snapshot_persistence = SQLiteFolderRevisionSnapshotPersistence::new(&snapshot_object_id, pool);
177182
let rev_manager = RevisionManager::new(
178183
user_id,
179184
folder_id.as_ref(),
180185
rev_persistence,
181186
rev_compactor,
182-
PhantomSnapshotPersistence(),
187+
snapshot_persistence,
183188
);
184189

185190
let folder_editor = FolderEditor::new(user_id, &folder_id, token, rev_manager, self.web_socket.clone()).await?;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
use bytes::Bytes;
2+
use flowy_database::{
3+
prelude::*,
4+
schema::{folder_rev_snapshot, folder_rev_snapshot::dsl},
5+
ConnectionPool,
6+
};
7+
use flowy_error::{internal_error, FlowyResult};
8+
use flowy_revision::{RevisionSnapshot, RevisionSnapshotDiskCache};
9+
use lib_infra::util::timestamp;
10+
use std::sync::Arc;
11+
12+
pub struct SQLiteFolderRevisionSnapshotPersistence {
13+
object_id: String,
14+
pool: Arc<ConnectionPool>,
15+
}
16+
17+
impl SQLiteFolderRevisionSnapshotPersistence {
18+
pub fn new(object_id: &str, pool: Arc<ConnectionPool>) -> Self {
19+
Self {
20+
object_id: object_id.to_string(),
21+
pool,
22+
}
23+
}
24+
25+
fn gen_snapshot_id(&self, rev_id: i64) -> String {
26+
format!("{}:{}", self.object_id, rev_id)
27+
}
28+
}
29+
30+
impl RevisionSnapshotDiskCache for SQLiteFolderRevisionSnapshotPersistence {
31+
fn should_generate_snapshot_from_range(&self, start_rev_id: i64, current_rev_id: i64) -> bool {
32+
(current_rev_id - start_rev_id) >= 2
33+
}
34+
35+
fn write_snapshot(&self, rev_id: i64, data: Vec<u8>) -> FlowyResult<()> {
36+
let conn = self.pool.get().map_err(internal_error)?;
37+
let snapshot_id = self.gen_snapshot_id(rev_id);
38+
let timestamp = timestamp();
39+
let record = (
40+
dsl::snapshot_id.eq(&snapshot_id),
41+
dsl::object_id.eq(&self.object_id),
42+
dsl::rev_id.eq(rev_id),
43+
dsl::base_rev_id.eq(rev_id),
44+
dsl::timestamp.eq(timestamp),
45+
dsl::data.eq(data),
46+
);
47+
let _ = insert_or_ignore_into(dsl::folder_rev_snapshot)
48+
.values(record)
49+
.execute(&*conn)?;
50+
Ok(())
51+
}
52+
53+
fn read_snapshot(&self, rev_id: i64) -> FlowyResult<Option<RevisionSnapshot>> {
54+
let conn = self.pool.get().map_err(internal_error)?;
55+
let snapshot_id = self.gen_snapshot_id(rev_id);
56+
let record = dsl::folder_rev_snapshot
57+
.filter(dsl::snapshot_id.eq(&snapshot_id))
58+
.first::<FolderSnapshotRecord>(&*conn)?;
59+
60+
Ok(Some(record.into()))
61+
}
62+
63+
fn read_last_snapshot(&self) -> FlowyResult<Option<RevisionSnapshot>> {
64+
let conn = self.pool.get().map_err(internal_error)?;
65+
let latest_record = dsl::folder_rev_snapshot
66+
.filter(dsl::object_id.eq(&self.object_id))
67+
.order(dsl::rev_id.desc())
68+
// .select(max(dsl::rev_id))
69+
// .select((dsl::id, dsl::object_id, dsl::rev_id, dsl::data))
70+
.first::<FolderSnapshotRecord>(&*conn)?;
71+
Ok(Some(latest_record.into()))
72+
}
73+
}
74+
75+
#[derive(PartialEq, Clone, Debug, Queryable, Identifiable, Insertable, Associations)]
76+
#[table_name = "folder_rev_snapshot"]
77+
#[primary_key("snapshot_id")]
78+
struct FolderSnapshotRecord {
79+
snapshot_id: String,
80+
object_id: String,
81+
rev_id: i64,
82+
base_rev_id: i64,
83+
timestamp: i64,
84+
data: Vec<u8>,
85+
}
86+
87+
impl std::convert::From<FolderSnapshotRecord> for RevisionSnapshot {
88+
fn from(record: FolderSnapshotRecord) -> Self {
89+
RevisionSnapshot {
90+
rev_id: record.rev_id,
91+
base_rev_id: record.base_rev_id,
92+
timestamp: record.timestamp,
93+
data: Bytes::from(record.data),
94+
}
95+
}
96+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
mod folder_rev_sqlite;
2+
mod folder_snapshot_sqlite_impl;
3+
24
pub use folder_rev_sqlite::*;
5+
pub use folder_snapshot_sqlite_impl::*;

frontend/rust-lib/flowy-grid/src/services/persistence/rev_sqlite/grid_snapshot_sqlite_impl.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,3 @@ impl std::convert::From<GridSnapshotRecord> for RevisionSnapshot {
115115
}
116116
}
117117
}
118-
119-
// pub(crate) fn get_latest_rev_id_from(rev_ids: Vec<i64>, anchor: i64) -> Option<i64> {
120-
// let mut target_rev_id = None;
121-
// let mut old_step: Option<i64> = None;
122-
// for rev_id in rev_ids {
123-
// let step = (rev_id - anchor).abs();
124-
// if let Some(old_step) = &mut old_step {
125-
// if *old_step > step {
126-
// *old_step = step;
127-
// target_rev_id = Some(rev_id);
128-
// }
129-
// } else {
130-
// old_step = Some(step);
131-
// target_rev_id = Some(rev_id);
132-
// }
133-
// }
134-
// target_rev_id
135-
// }
136-
137-
// #[cfg(test)]
138-
// mod tests {
139-
// use crate::services::persistence::rev_sqlite::get_latest_rev_id_from;
140-
//
141-
// #[test]
142-
// fn test_latest_rev_id() {
143-
// let ids = vec![1, 2, 3, 4, 5, 6];
144-
// for (anchor, expected_value) in vec![(3, 3), (7, 6), (1, 1)] {
145-
// let value = get_latest_rev_id_from(ids.clone(), anchor).unwrap();
146-
// assert_eq!(value, expected_value);
147-
// }
148-
// }
149-
// }

0 commit comments

Comments
 (0)