Skip to content

Commit c19cc06

Browse files
sedax90datlechin
andauthored
feat(linux-grid): keep a table's highlight rules across restarts (#4129)
Co-authored-by: sedax90 <5001801+sedax90@users.noreply.github.com> Co-authored-by: Ngo Quoc Dat <datlechin@gmail.com>
1 parent 8d6ff69 commit c19cc06

34 files changed

Lines changed: 1836 additions & 227 deletions

‎core/crates/storage/src/paths.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ impl StoragePaths {
4646
self.config.join("filter_settings.json")
4747
}
4848

49+
pub fn highlight_rules_file(&self) -> PathBuf {
50+
self.config.join("highlight_rules.json")
51+
}
52+
4953
pub fn drafts_dir(&self) -> PathBuf {
5054
self.data.join("drafts")
5155
}
@@ -85,6 +89,10 @@ mod tests {
8589
assert_eq!(paths.runtime, root.join("runtime/app/app.tablepro.TablePro"));
8690
assert_eq!(paths.history_database(), root.join("state/tablepro/history.db"));
8791
assert_eq!(paths.connections_file(), root.join("config/tablepro/connections.json"));
92+
assert_eq!(
93+
paths.highlight_rules_file(),
94+
root.join("config/tablepro/highlight_rules.json")
95+
);
8896
assert_eq!(paths.snippets_dir(), root.join("data/tablepro/snippets"));
8997
assert_eq!(paths.account_file(), root.join("state/tablepro/account.json"));
9098
assert_eq!(paths.telemetry_file(), root.join("state/tablepro/telemetry.json"));

‎linux/changelog.d/4129.added.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Highlight Rules are kept for each table across restarts, including database files and other databases.

‎linux/crates/app/src/application/app_services.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ impl AppServices {
362362
)),
363363
column_layouts: self.storage.column_layouts().clone(),
364364
filters: self.storage.filter_settings().clone(),
365+
highlight_rules: self.storage.highlight_rules().clone(),
365366
workspaces: self.storage.workspaces().clone(),
366367
history,
367368
spawner: self.tasks.clone(),

‎linux/crates/app/src/persistence/column_layout_store.rs‎

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ use std::rc::Rc;
33
use tablepro_session::runtime::Tasks;
44
use uuid::Uuid;
55

6-
use super::StateFile;
76
use super::column_layouts_document::ColumnLayoutsDocument;
7+
use super::{StateFile, StateFileError, TableStateKey};
88
use crate::services::grid::ColumnLayout;
99

10-
/// Each table's column order, hidden columns and widths, remembered per
11-
/// connection, schema and table.
1210
#[derive(Clone)]
1311
pub struct ColumnLayoutStore {
1412
file: Rc<StateFile<ColumnLayoutsDocument>>,
@@ -31,16 +29,12 @@ impl ColumnLayoutStore {
3129
self.file.path()
3230
}
3331

34-
pub fn layout(&self, connection_id: Uuid, schema: Option<&str>, table: &str) -> ColumnLayout {
35-
self.read(connection_id, schema, table, |layout| {
36-
layout.cloned().unwrap_or_default()
37-
})
32+
pub fn layout(&self, key: impl Into<TableStateKey>, schema: Option<&str>, table: &str) -> ColumnLayout {
33+
self.read(&key.into(), schema, table, |layout| layout.cloned().unwrap_or_default())
3834
}
3935

40-
/// Keep `layout` as the table's whole layout. The grid merges what it
41-
/// shows into the stored layout before it saves.
42-
pub fn save(&self, connection_id: Uuid, schema: Option<&str>, table: &str, layout: &ColumnLayout) {
43-
self.change(connection_id, schema, table, |stored| {
36+
pub fn save(&self, key: impl Into<TableStateKey>, schema: Option<&str>, table: &str, layout: &ColumnLayout) {
37+
self.change(&key.into(), schema, table, |stored| {
4438
if stored == layout {
4539
return false;
4640
}
@@ -49,20 +43,43 @@ impl ColumnLayoutStore {
4943
});
5044
}
5145

52-
/// Drop everything for a connection the user deleted, so the file
53-
/// does not grow with entries nothing can reach.
5446
pub fn forget_connection(&self, connection_id: Uuid) {
55-
self.file
56-
.update(|document| document.connections.remove(&connection_id.to_string()).is_some());
47+
self.file.update(|document| {
48+
let before = document.connections.len();
49+
document
50+
.connections
51+
.retain(|key, _| !TableStateKey::belongs_to(key, connection_id));
52+
before != document.connections.len()
53+
});
54+
}
55+
56+
pub fn failing(&self) -> tokio::sync::watch::Receiver<bool> {
57+
self.file.failing()
58+
}
59+
60+
pub fn revision(&self) -> u64 {
61+
self.file.revision()
62+
}
63+
64+
pub fn has_unsaved_changes(&self) -> bool {
65+
self.file.has_unsaved_changes()
66+
}
67+
68+
pub fn current_error(&self) -> Option<StateFileError> {
69+
self.file.current_error()
70+
}
71+
72+
pub fn retry(&self) -> bool {
73+
self.file.retry()
5774
}
5875

59-
pub fn flush(&self) -> impl Future<Output = ()> + Send + use<> {
76+
pub fn flush(&self) -> impl Future<Output = Result<(), StateFileError>> + Send + use<> {
6077
self.file.flush()
6178
}
6279

6380
fn read<R>(
6481
&self,
65-
connection_id: Uuid,
82+
key: &TableStateKey,
6683
schema: Option<&str>,
6784
table: &str,
6885
with: impl FnOnce(Option<&ColumnLayout>) -> R,
@@ -71,33 +88,30 @@ impl ColumnLayoutStore {
7188
with(
7289
document
7390
.connections
74-
.get(&connection_id.to_string())
91+
.get(key.as_str())
7592
.and_then(|schemas| schemas.get(schema_key(schema)))
7693
.and_then(|tables| tables.get(table)),
7794
)
7895
})
7996
}
8097

81-
/// Change one table's layout. `change` returns whether anything moved.
82-
/// A layout left with nothing in it is removed with any map it empties,
83-
/// so a reset shrinks the file back.
8498
fn change(
8599
&self,
86-
connection_id: Uuid,
100+
key: &TableStateKey,
87101
schema: Option<&str>,
88102
table: &str,
89103
change: impl FnOnce(&mut ColumnLayout) -> bool,
90104
) {
91-
let mut layout = self.layout(connection_id, schema, table);
105+
let mut layout = self.layout(key, schema, table);
92106
if !change(&mut layout) {
93107
return;
94108
}
95109
self.file.update(|document| match layout == ColumnLayout::default() {
96-
true => prune(document, connection_id, schema, table),
110+
true => prune(document, key, schema, table),
97111
false => {
98112
document
99113
.connections
100-
.entry(connection_id.to_string())
114+
.entry(key.as_str().to_owned())
101115
.or_default()
102116
.entry(schema_key(schema).to_owned())
103117
.or_default()
@@ -108,14 +122,13 @@ impl ColumnLayoutStore {
108122
}
109123
}
110124

111-
/// `None` is stored as the empty string so every JSON key is concrete.
112125
fn schema_key(schema: Option<&str>) -> &str {
113126
schema.unwrap_or("")
114127
}
115128

116-
fn prune(document: &mut ColumnLayoutsDocument, connection_id: Uuid, schema: Option<&str>, table: &str) -> bool {
117-
let connection_key = connection_id.to_string();
118-
let Some(schemas) = document.connections.get_mut(&connection_key) else {
129+
fn prune(document: &mut ColumnLayoutsDocument, key: &TableStateKey, schema: Option<&str>, table: &str) -> bool {
130+
let connection_key = key.as_str();
131+
let Some(schemas) = document.connections.get_mut(connection_key) else {
119132
return false;
120133
};
121134
let mut removed = false;
@@ -126,7 +139,7 @@ fn prune(document: &mut ColumnLayoutsDocument, connection_id: Uuid, schema: Opti
126139
}
127140
}
128141
if schemas.is_empty() {
129-
document.connections.remove(&connection_key);
142+
document.connections.remove(connection_key);
130143
}
131144
removed
132145
}
@@ -160,7 +173,7 @@ mod tests {
160173
let store = store_at(&path);
161174

162175
store.save(id, Some("public"), "users", &arranged());
163-
store.flush().await;
176+
store.flush().await.expect("saved");
164177
let reopened = store_at(&path);
165178

166179
assert_eq!(reopened.layout(id, Some("public"), "users"), arranged());
@@ -203,7 +216,7 @@ mod tests {
203216
store.save(id, Some("public"), "users", &arranged());
204217

205218
store.save(id, Some("public"), "users", &ColumnLayout::default());
206-
store.flush().await;
219+
store.flush().await.expect("saved");
207220

208221
assert!(
209222
!read_text_file(&path).contains(&id.to_string()),

‎linux/crates/app/src/persistence/filter_settings_store.rs‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ use tablepro_core::FilterSet;
44
use tablepro_session::runtime::Tasks;
55
use uuid::Uuid;
66

7-
use super::StateFile;
87
use super::filter_settings_document::FilterSettingsDocument;
8+
use super::{StateFile, StateFileError, TableStateKey};
99

10-
/// The filter each table was left with, remembered per connection,
11-
/// schema and table.
1210
#[derive(Clone)]
1311
pub struct FilterSettingsStore {
1412
file: Rc<StateFile<FilterSettingsDocument>>,
@@ -25,27 +23,29 @@ impl FilterSettingsStore {
2523
self.file.path()
2624
}
2725

28-
pub fn filters(&self, connection_id: Uuid, schema: Option<&str>, table: &str) -> FilterSet {
26+
pub fn filters(&self, key: impl Into<TableStateKey>, schema: Option<&str>, table: &str) -> FilterSet {
27+
let key = key.into();
2928
self.file.read(|document| {
3029
document
3130
.connections
32-
.get(&connection_id.to_string())
31+
.get(key.as_str())
3332
.and_then(|schemas| schemas.get(schema_key(schema)))
3433
.and_then(|tables| tables.get(table))
3534
.cloned()
3635
.unwrap_or_default()
3736
})
3837
}
3938

40-
pub fn set_filters(&self, connection_id: Uuid, schema: Option<&str>, table: &str, set: FilterSet) {
39+
pub fn set_filters(&self, key: impl Into<TableStateKey>, schema: Option<&str>, table: &str, set: FilterSet) {
40+
let key = key.into();
4141
self.file.update(|document| match set.is_empty() {
4242
// Clearing a filter shrinks the file back rather than
4343
// leaving an empty blob behind.
44-
true => prune(document, connection_id, schema, table),
44+
true => prune(document, &key, schema, table),
4545
false => {
4646
let slot = document
4747
.connections
48-
.entry(connection_id.to_string())
48+
.entry(key.as_str().to_owned())
4949
.or_default()
5050
.entry(schema_key(schema).to_owned())
5151
.or_default()
@@ -60,26 +60,48 @@ impl FilterSettingsStore {
6060
});
6161
}
6262

63-
/// Drop everything for a connection the user deleted.
6463
pub fn forget_connection(&self, connection_id: Uuid) {
65-
self.file
66-
.update(|document| document.connections.remove(&connection_id.to_string()).is_some());
64+
self.file.update(|document| {
65+
let before = document.connections.len();
66+
document
67+
.connections
68+
.retain(|key, _| !TableStateKey::belongs_to(key, connection_id));
69+
before != document.connections.len()
70+
});
71+
}
72+
73+
pub fn failing(&self) -> tokio::sync::watch::Receiver<bool> {
74+
self.file.failing()
75+
}
76+
77+
pub fn revision(&self) -> u64 {
78+
self.file.revision()
79+
}
80+
81+
pub fn has_unsaved_changes(&self) -> bool {
82+
self.file.has_unsaved_changes()
83+
}
84+
85+
pub fn current_error(&self) -> Option<StateFileError> {
86+
self.file.current_error()
87+
}
88+
89+
pub fn retry(&self) -> bool {
90+
self.file.retry()
6791
}
6892

69-
pub fn flush(&self) -> impl Future<Output = ()> + Send + use<> {
93+
pub fn flush(&self) -> impl Future<Output = Result<(), StateFileError>> + Send + use<> {
7094
self.file.flush()
7195
}
7296
}
7397

74-
/// `None` is stored as the empty string so every JSON key is concrete.
7598
fn schema_key(schema: Option<&str>) -> &str {
7699
schema.unwrap_or("")
77100
}
78101

79-
/// Remove the entry and any map it leaves empty.
80-
fn prune(document: &mut FilterSettingsDocument, connection_id: Uuid, schema: Option<&str>, table: &str) -> bool {
81-
let connection_key = connection_id.to_string();
82-
let Some(schemas) = document.connections.get_mut(&connection_key) else {
102+
fn prune(document: &mut FilterSettingsDocument, key: &TableStateKey, schema: Option<&str>, table: &str) -> bool {
103+
let connection_key = key.as_str();
104+
let Some(schemas) = document.connections.get_mut(connection_key) else {
83105
return false;
84106
};
85107
let mut removed = false;
@@ -90,7 +112,7 @@ fn prune(document: &mut FilterSettingsDocument, connection_id: Uuid, schema: Opt
90112
}
91113
}
92114
if schemas.is_empty() {
93-
document.connections.remove(&connection_key);
115+
document.connections.remove(connection_key);
94116
}
95117
removed
96118
}
@@ -140,7 +162,7 @@ mod tests {
140162
store.set_filters(id, Some("public"), "users", sample_set());
141163

142164
store.set_filters(id, Some("public"), "users", FilterSet::default());
143-
store.flush().await;
165+
store.flush().await.expect("saved");
144166

145167
assert!(store.filters(id, Some("public"), "users").is_empty());
146168
let written = std::fs::read_to_string(&path).expect("the file");
@@ -173,7 +195,7 @@ mod tests {
173195
let id = Uuid::new_v4();
174196
let store = FilterSettingsStore::load(path.clone(), &paused_tasks());
175197
store.set_filters(id, None, "users", sample_set());
176-
store.flush().await;
198+
store.flush().await.expect("saved");
177199

178200
let reopened = FilterSettingsStore::load(path, &paused_tasks());
179201

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::collections::HashMap;
2+
3+
use serde::{Deserialize, Serialize};
4+
use tablepro_storage::document::VersionedDocument;
5+
6+
use crate::services::grid::HighlightRule;
7+
8+
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
9+
pub struct HighlightRulesDocument {
10+
#[serde(default)]
11+
pub connections: HashMap<String, Schemas>,
12+
}
13+
14+
pub type Schemas = HashMap<String, Tables>;
15+
pub type Tables = HashMap<String, Vec<HighlightRule>>;
16+
17+
impl VersionedDocument for HighlightRulesDocument {
18+
const KIND: &'static str = "highlight-rules";
19+
const VERSION: u32 = 1;
20+
}

0 commit comments

Comments
 (0)