Skip to content

Commit 8c225fe

Browse files
authored
Feat/restore revision (#1549)
* chore: write snapshot * chore: add tests * chore: sync close * chore: restore from snapshot * chore: delete invalid revisions after restored from snapshot * chore: create default view if it fail to deserialize view's revisions when there is no snapshot * chore: auto generate snapshot Co-authored-by: nathan <[email protected]>
1 parent a507fb8 commit 8c225fe

File tree

67 files changed

+1140
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1140
-582
lines changed

frontend/app_flowy/lib/plugins/grid/application/cell/text_cell_bloc.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ class TextCellBloc extends Bloc<TextCellEvent, TextCellState> {
1818
_startListening();
1919
},
2020
updateText: (text) {
21-
cellController.saveCellData(text);
22-
emit(state.copyWith(content: text));
21+
if (state.content != text) {
22+
cellController.saveCellData(text);
23+
emit(state.copyWith(content: text));
24+
}
2325
},
2426
didReceiveCellUpdate: (content) {
2527
emit(state.copyWith(content: content));

frontend/app_flowy/lib/plugins/grid/presentation/widgets/cell/text_cell.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ class _GridTextCellState extends GridFocusNodeCellState<GridTextCell> {
6565
child: TextField(
6666
controller: _controller,
6767
focusNode: focusNode,
68-
onSubmitted: (text) => _cellBloc.add(
69-
TextCellEvent.updateText(text),
70-
),
71-
onEditingComplete: () => focusNode.unfocus(),
7268
maxLines: null,
7369
style: Theme.of(context).textTheme.bodyMedium,
7470
decoration: InputDecoration(
@@ -99,4 +95,12 @@ class _GridTextCellState extends GridFocusNodeCellState<GridTextCell> {
9995
void onInsert(String value) {
10096
_cellBloc.add(TextCellEvent.updateText(value));
10197
}
98+
99+
@override
100+
Future<void> focusChanged() {
101+
_cellBloc.add(
102+
TextCellEvent.updateText(_controller.text),
103+
);
104+
return super.focusChanged();
105+
}
102106
}

frontend/app_flowy/test/bloc_test/home_test/app_bloc_test.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,35 @@ void main() {
133133
workspaceSetting.latestView.id == document1.id;
134134
});
135135

136-
test('open latest grid test', () async {
136+
test('open latest document test', () async {
137137
final app = await testContext.createTestApp();
138138
final bloc = AppBloc(app: app)..add(const AppEvent.initial());
139139
await blocResponseFuture();
140140

141-
bloc.add(AppEvent.createView("grid 1", GridPluginBuilder()));
141+
bloc.add(AppEvent.createView("document 1", DocumentPluginBuilder()));
142142
await blocResponseFuture();
143-
final grid1 = bloc.state.latestCreatedView;
144-
assert(grid1!.name == "grid 1");
143+
final document = bloc.state.latestCreatedView;
144+
assert(document!.name == "document 1");
145145

146146
bloc.add(AppEvent.createView("grid 2", GridPluginBuilder()));
147147
await blocResponseFuture();
148-
final grid2 = bloc.state.latestCreatedView;
149-
assert(grid2!.name == "grid 2");
148+
final grid = bloc.state.latestCreatedView;
149+
assert(grid!.name == "grid 2");
150150

151151
var workspaceSetting = await FolderEventReadCurrentWorkspace()
152152
.send()
153153
.then((result) => result.fold((l) => l, (r) => throw Exception()));
154-
workspaceSetting.latestView.id == grid1!.id;
154+
workspaceSetting.latestView.id == grid!.id;
155155

156156
// Open grid 1
157157
// ignore: unused_local_variable
158-
final documentBloc = DocumentBloc(view: grid1)
158+
final documentBloc = DocumentBloc(view: document!)
159159
..add(const DocumentEvent.initial());
160160
await blocResponseFuture();
161161

162162
workspaceSetting = await FolderEventReadCurrentWorkspace()
163163
.send()
164164
.then((result) => result.fold((l) => l, (r) => throw Exception()));
165-
workspaceSetting.latestView.id == grid1.id;
165+
workspaceSetting.latestView.id == document.id;
166166
});
167167
}

frontend/rust-lib/Cargo.lock

Lines changed: 25 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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 grid_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 grid_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/macros.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ macro_rules! diesel_insert_table {
3737
) => {
3838
{
3939
let _ = diesel::insert_into($table_name::table)
40-
.values($table.clone())
40+
.values($table)
4141
// .on_conflict($table_name::dsl::id)
4242
// .do_update()
4343
// .set(WorkspaceTableChangeset::from_table(workspace_table))
@@ -61,6 +61,21 @@ macro_rules! diesel_record_count {
6161
};
6262
}
6363

64+
#[macro_export]
65+
macro_rules! diesel_revision_record_count {
66+
(
67+
$table_name:expr,
68+
$filter:expr,
69+
$connection:expr
70+
) => {
71+
$table_name
72+
.filter($table_name::dsl::id.eq($id))
73+
.count()
74+
.get_result($connection)
75+
.unwrap_or(0);
76+
};
77+
}
78+
6479
#[macro_export]
6580
macro_rules! diesel_update_table {
6681
(

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
table! {
1+
// @generated automatically by Diesel CLI.
2+
3+
diesel::table! {
24
app_table (id) {
35
id -> Text,
46
workspace_id -> Text,
@@ -13,7 +15,7 @@ table! {
1315
}
1416
}
1517

16-
table! {
18+
diesel::table! {
1719
document_rev_table (id) {
1820
id -> Integer,
1921
document_id -> Text,
@@ -24,14 +26,14 @@ table! {
2426
}
2527
}
2628

27-
table! {
29+
diesel::table! {
2830
grid_block_index_table (row_id) {
2931
row_id -> Text,
3032
block_id -> Text,
3133
}
3234
}
3335

34-
table! {
36+
diesel::table! {
3537
grid_meta_rev_table (id) {
3638
id -> Integer,
3739
object_id -> Text,
@@ -42,7 +44,18 @@ table! {
4244
}
4345
}
4446

45-
table! {
47+
diesel::table! {
48+
grid_rev_snapshot (snapshot_id) {
49+
snapshot_id -> Text,
50+
object_id -> Text,
51+
rev_id -> BigInt,
52+
base_rev_id -> BigInt,
53+
timestamp -> BigInt,
54+
data -> Binary,
55+
}
56+
}
57+
58+
diesel::table! {
4659
grid_rev_table (id) {
4760
id -> Integer,
4861
object_id -> Text,
@@ -53,7 +66,7 @@ table! {
5366
}
5467
}
5568

56-
table! {
69+
diesel::table! {
5770
grid_view_rev_table (id) {
5871
id -> Integer,
5972
object_id -> Text,
@@ -64,14 +77,14 @@ table! {
6477
}
6578
}
6679

67-
table! {
80+
diesel::table! {
6881
kv_table (key) {
6982
key -> Text,
7083
value -> Binary,
7184
}
7285
}
7386

74-
table! {
87+
diesel::table! {
7588
rev_snapshot (id) {
7689
id -> Integer,
7790
object_id -> Text,
@@ -80,7 +93,7 @@ table! {
8093
}
8194
}
8295

83-
table! {
96+
diesel::table! {
8497
rev_table (id) {
8598
id -> Integer,
8699
doc_id -> Text,
@@ -92,7 +105,7 @@ table! {
92105
}
93106
}
94107

95-
table! {
108+
diesel::table! {
96109
trash_table (id) {
97110
id -> Text,
98111
name -> Text,
@@ -103,7 +116,7 @@ table! {
103116
}
104117
}
105118

106-
table! {
119+
diesel::table! {
107120
user_table (id) {
108121
id -> Text,
109122
name -> Text,
@@ -114,7 +127,7 @@ table! {
114127
}
115128
}
116129

117-
table! {
130+
diesel::table! {
118131
view_table (id) {
119132
id -> Text,
120133
belong_to_id -> Text,
@@ -130,7 +143,7 @@ table! {
130143
}
131144
}
132145

133-
table! {
146+
diesel::table! {
134147
workspace_table (id) {
135148
id -> Text,
136149
name -> Text,
@@ -142,11 +155,12 @@ table! {
142155
}
143156
}
144157

145-
allow_tables_to_appear_in_same_query!(
158+
diesel::allow_tables_to_appear_in_same_query!(
146159
app_table,
147160
document_rev_table,
148161
grid_block_index_table,
149162
grid_meta_rev_table,
163+
grid_rev_snapshot,
150164
grid_rev_table,
151165
grid_view_rev_table,
152166
kv_table,

frontend/rust-lib/flowy-document/src/editor/document.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ impl RevisionObjectSerializer for DocumentRevisionSerde {
9595
}
9696
}
9797

98-
pub(crate) struct DocumentRevisionCompress();
99-
impl RevisionMergeable for DocumentRevisionCompress {
98+
pub(crate) struct DocumentRevisionMergeable();
99+
impl RevisionMergeable for DocumentRevisionMergeable {
100100
fn combine_revisions(&self, revisions: Vec<Revision>) -> FlowyResult<Bytes> {
101101
DocumentRevisionSerde::combine_revisions(revisions)
102102
}

0 commit comments

Comments
 (0)