Skip to content

Commit d30dc71

Browse files
committed
refactor(sqlite): rename gaps to gap_chunks / events_chunks to event_chunks
And some comments have been tweaked too.
1 parent e42be87 commit d30dc71

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

crates/matrix-sdk-base/src/event_cache/store/memory_store.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use ruma::{
3232
time::{Instant, SystemTime},
3333
EventId, MxcUri, OwnedEventId, OwnedMxcUri, RoomId,
3434
};
35+
use tracing::error;
3536

3637
use super::{
3738
compute_filters_string, extract_event_relation,
@@ -258,6 +259,10 @@ impl EventCacheStore for MemoryStore {
258259
}
259260

260261
async fn save_event(&self, room_id: &RoomId, event: Event) -> Result<(), Self::Error> {
262+
if event.event_id().is_none() {
263+
error!(%room_id, "Trying to save an event with no ID");
264+
return Ok(());
265+
}
261266
self.inner.write().unwrap().events.save_item(room_id.to_owned(), event);
262267
Ok(())
263268
}

crates/matrix-sdk-base/src/event_cache/store/traits.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ pub trait EventCacheStore: AsyncTraitDeps {
132132
/// Save an event, that might or might not be part of an existing linked
133133
/// chunk.
134134
///
135-
/// If the event has no event id, it may not be saved.
135+
/// If the event has no event id, it will not be saved, and the function
136+
/// must return an Ok result early.
136137
///
137138
/// If the event was already stored with the same id, it must be replaced,
138139
/// without causing an error.

crates/matrix-sdk-sqlite/migrations/event_cache_store/007_events_chunks.sql renamed to crates/matrix-sdk-sqlite/migrations/event_cache_store/007_event_chunks.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- We're going to split the `events` table into two tables: `events` and `events_chunks`.
1+
-- We're going to split the `events` table into two tables: `events` and `event_chunks`.
22
-- The former table will include the events' content, while the latter will include the location of
33
-- each event in the linked chunk.
44

@@ -14,7 +14,7 @@ CREATE TABLE "events" (
1414
-- The room in which the event is located.
1515
"room_id" BLOB NOT NULL,
1616

17-
-- `OwnedEventId` for events.
17+
-- The `OwnedEventId` of this event.
1818
"event_id" BLOB NOT NULL,
1919

2020
-- JSON serialized `TimelineEvent` (encrypted value).
@@ -35,7 +35,7 @@ CREATE TABLE "events" (
3535
WITHOUT ROWID;
3636

3737
-- Entries inside an event chunk.
38-
CREATE TABLE "events_chunks" (
38+
CREATE TABLE "event_chunks" (
3939
-- Which room does this event belong to? (hashed key shared with linked_chunks)
4040
"room_id" BLOB NOT NULL,
4141
-- Which chunk does this event refer to? Corresponds to a `ChunkIdentifier`.
@@ -58,3 +58,6 @@ CREATE TABLE "events_chunks" (
5858
FOREIGN KEY (room_id, chunk_id) REFERENCES linked_chunks(room_id, id) ON DELETE CASCADE
5959
)
6060
WITHOUT ROWID;
61+
62+
-- For consistency, rename gaps to gap_chunks.
63+
ALTER TABLE gaps RENAME TO gap_chunks;

crates/matrix-sdk-sqlite/src/event_cache_store.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl TransactionExtForLinkedChunks for Transaction<'_> {
298298
// There's at most one row for it in the database, so a call to `query_row` is
299299
// sufficient.
300300
let encoded_prev_token: Vec<u8> = self.query_row(
301-
"SELECT prev_token FROM gaps WHERE chunk_id = ? AND room_id = ?",
301+
"SELECT prev_token FROM gap_chunks WHERE chunk_id = ? AND room_id = ?",
302302
(chunk_id.index(), &room_id),
303303
|row| row.get(0),
304304
)?;
@@ -319,8 +319,8 @@ impl TransactionExtForLinkedChunks for Transaction<'_> {
319319
for event_data in self
320320
.prepare(
321321
r#"
322-
SELECT content
323-
FROM events_chunks ec
322+
SELECT events.content
323+
FROM event_chunks ec
324324
INNER JOIN events USING (event_id, room_id)
325325
WHERE ec.chunk_id = ? AND ec.room_id = ?
326326
ORDER BY ec.position ASC
@@ -410,7 +410,7 @@ async fn run_migrations(conn: &SqliteAsyncConn, version: u8) -> Result<()> {
410410
if version < 7 {
411411
conn.with_transaction(|txn| {
412412
txn.execute_batch(include_str!(
413-
"../migrations/event_cache_store/007_events_chunks.sql"
413+
"../migrations/event_cache_store/007_event_chunks.sql"
414414
))?;
415415
txn.set_db_version(7)
416416
})
@@ -517,7 +517,7 @@ impl EventCacheStore for SqliteEventCacheStore {
517517
// Insert the gap's value.
518518
txn.execute(
519519
r#"
520-
INSERT INTO gaps(chunk_id, room_id, prev_token)
520+
INSERT INTO gap_chunks(chunk_id, room_id, prev_token)
521521
VALUES (?, ?, ?)
522522
"#,
523523
(new, &hashed_room_id, prev_token),
@@ -562,12 +562,13 @@ impl EventCacheStore for SqliteEventCacheStore {
562562
trace!(%room_id, "pushing {} items @ {chunk_id}", items.len());
563563

564564
let mut chunk_statement = txn.prepare(
565-
"INSERT INTO events_chunks(chunk_id, room_id, event_id, position) VALUES (?, ?, ?, ?)"
565+
"INSERT INTO event_chunks(chunk_id, room_id, event_id, position) VALUES (?, ?, ?, ?)"
566566
)?;
567567

568568
// Note: we use `OR REPLACE` here, because the event might have been
569569
// already inserted in the database. This is the case when an event is
570-
// deduplicated and moved to another position.
570+
// deduplicated and moved to another position; or because it was inserted
571+
// outside the context of a linked chunk (e.g. pinned event).
571572
let mut content_statement = txn.prepare(
572573
"INSERT OR REPLACE INTO events(room_id, event_id, content, relates_to, rel_type) VALUES (?, ?, ?, ?, ?)"
573574
)?;
@@ -615,7 +616,7 @@ impl EventCacheStore for SqliteEventCacheStore {
615616

616617
// Replace the event id in the linked chunk, in case it changed.
617618
txn.execute(
618-
r#"UPDATE events_chunks SET event_id = ? WHERE room_id = ? AND chunk_id = ? AND position = ?"#,
619+
r#"UPDATE event_chunks SET event_id = ? WHERE room_id = ? AND chunk_id = ? AND position = ?"#,
619620
(event_id, &hashed_room_id, chunk_id, index)
620621
)?;
621622
}
@@ -627,12 +628,12 @@ impl EventCacheStore for SqliteEventCacheStore {
627628
trace!(%room_id, "removing item @ {chunk_id}:{index}");
628629

629630
// Remove the entry in the chunk table.
630-
txn.execute("DELETE FROM events_chunks WHERE room_id = ? AND chunk_id = ? AND position = ?", (&hashed_room_id, chunk_id, index))?;
631+
txn.execute("DELETE FROM event_chunks WHERE room_id = ? AND chunk_id = ? AND position = ?", (&hashed_room_id, chunk_id, index))?;
631632

632633
// Decrement the index of each item after the one we're going to remove.
633634
txn.execute(
634635
r#"
635-
UPDATE events_chunks
636+
UPDATE event_chunks
636637
SET position = position - 1
637638
WHERE room_id = ? AND chunk_id = ? AND position > ?
638639
"#,
@@ -648,7 +649,7 @@ impl EventCacheStore for SqliteEventCacheStore {
648649
trace!(%room_id, "truncating items >= {chunk_id}:{index}");
649650

650651
// Remove these entries.
651-
txn.execute("DELETE FROM events_chunks WHERE room_id = ? AND chunk_id = ? AND position >= ?", (&hashed_room_id, chunk_id, index))?;
652+
txn.execute("DELETE FROM event_chunks WHERE room_id = ? AND chunk_id = ? AND position >= ?", (&hashed_room_id, chunk_id, index))?;
652653
}
653654

654655
Update::Clear => {
@@ -894,7 +895,7 @@ impl EventCacheStore for SqliteEventCacheStore {
894895
let query = format!(
895896
r#"
896897
SELECT event_id, chunk_id, position
897-
FROM events_chunks
898+
FROM event_chunks
898899
WHERE room_id = ? AND event_id IN ({})
899900
ORDER BY chunk_id ASC, position ASC
900901
"#,
@@ -999,7 +1000,7 @@ impl EventCacheStore for SqliteEventCacheStore {
9991000
.into_iter()
10001001
.map(|f| format!(r#""{f}""#))
10011002
.collect::<Vec<_>>()
1002-
.join(r#", "#)
1003+
.join(", ")
10031004
)
10041005
} else {
10051006
"".to_owned()
@@ -1825,7 +1826,7 @@ mod tests {
18251826
.with_transaction(|txn| -> rusqlite::Result<_> {
18261827
let mut gaps = Vec::new();
18271828
for data in txn
1828-
.prepare("SELECT chunk_id FROM gaps ORDER BY chunk_id")?
1829+
.prepare("SELECT chunk_id FROM gap_chunks ORDER BY chunk_id")?
18291830
.query_map((), |row| row.get::<_, u64>(0))?
18301831
{
18311832
gaps.push(data?);
@@ -1934,7 +1935,7 @@ mod tests {
19341935
.unwrap()
19351936
.with_transaction(move |txn| {
19361937
txn.query_row(
1937-
"SELECT COUNT(*) FROM events_chunks WHERE chunk_id = 42 AND room_id = ? AND position = 0",
1938+
"SELECT COUNT(*) FROM event_chunks WHERE chunk_id = 42 AND room_id = ? AND position = 0",
19381939
(room_id.as_bytes(),),
19391940
|row| row.get(0),
19401941
)
@@ -2080,12 +2081,12 @@ mod tests {
20802081
.unwrap()
20812082
.with_transaction(|txn| -> rusqlite::Result<_> {
20822083
let num_gaps = txn
2083-
.prepare("SELECT COUNT(chunk_id) FROM gaps ORDER BY chunk_id")?
2084+
.prepare("SELECT COUNT(chunk_id) FROM gap_chunks ORDER BY chunk_id")?
20842085
.query_row((), |row| row.get::<_, u64>(0))?;
20852086
assert_eq!(num_gaps, 0);
20862087

20872088
let num_events = txn
2088-
.prepare("SELECT COUNT(event_id) FROM events_chunks ORDER BY chunk_id")?
2089+
.prepare("SELECT COUNT(event_id) FROM event_chunks ORDER BY chunk_id")?
20892090
.query_row((), |row| row.get::<_, u64>(0))?;
20902091
assert_eq!(num_events, 0);
20912092

crates/matrix-sdk/src/event_cache/room/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,11 @@ mod private {
13901390
}
13911391

13921392
/// Save a single event into the database, without notifying observers.
1393+
///
1394+
/// Note: if the event was already saved as part of a linked chunk, and
1395+
/// its event id may have changed, it's not safe to use this
1396+
/// method because it may break the link between the chunk and
1397+
/// the event. Instead, an update to the linked chunk must be used.
13931398
pub async fn save_event(
13941399
&self,
13951400
events: impl IntoIterator<Item = TimelineEvent>,

0 commit comments

Comments
 (0)