12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- //! The event cache is an abstraction layer, sitting between the Rust SDK and a
16
- //! final client, that acts as a global observer of all the rooms, gathering and
17
- //! inferring some extra useful information about each room. In particular, this
18
- //! doesn't require subscribing to a specific room to get access to this
19
- //! information.
20
- //!
21
- //! It's intended to be fast, robust and easy to maintain, having learned from
22
- //! previous endeavours at implementing middle to high level features elsewhere
23
- //! in the SDK, notably in the UI's Timeline object.
24
- //!
25
- //! See the [github issue](https://github.com/matrix-org/matrix-rust-sdk/issues/3058) for more
26
- //! details about the historical reasons that led us to start writing this.
27
-
28
15
use std:: { fmt, fs, path:: Path , sync:: Arc } ;
29
16
30
- use ruma:: { OwnedEventId , OwnedRoomId , RoomId , events:: AnyMessageLikeEvent } ;
17
+ use ruma:: { OwnedEventId , OwnedRoomId , RoomId , events:: AnySyncMessageLikeEvent } ;
31
18
use tantivy:: {
32
19
Index , IndexReader , TantivyDocument ,
33
20
collector:: TopDocs ,
@@ -44,6 +31,11 @@ use crate::{
44
31
writer:: SearchIndexWriter ,
45
32
} ;
46
33
34
+ /// A struct to represent the operations on a [`RoomIndex`]
35
+ pub ( crate ) enum RoomIndexOperation {
36
+ Add ( TantivyDocument ) ,
37
+ }
38
+
47
39
/// A struct that holds all data pertaining to a particular room's
48
40
/// message index.
49
41
pub struct RoomIndex {
@@ -91,9 +83,9 @@ impl RoomIndex {
91
83
RoomIndex :: new_with ( index, schema, room_id)
92
84
}
93
85
94
- /// Create new [`RoomIndex`] which stores the index in RAM .
86
+ /// Create new [`RoomIndex`] which stores the index in memory .
95
87
/// Intended for testing.
96
- pub fn new_in_ram ( room_id : & RoomId ) -> Result < RoomIndex , IndexError > {
88
+ pub fn new_in_memory ( room_id : & RoomId ) -> Result < RoomIndex , IndexError > {
97
89
let schema = RoomMessageSchema :: new ( ) ;
98
90
let index = Index :: create_in_ram ( schema. as_tantivy_schema ( ) ) ;
99
91
RoomIndex :: new_with ( index, schema, room_id)
@@ -130,10 +122,14 @@ impl RoomIndex {
130
122
RoomIndex :: new_with ( index, schema, room_id)
131
123
}
132
124
133
- /// Add [`AnyMessageLikeEvent`] to [`RoomIndex`]
134
- pub fn add_event ( & mut self , event : AnyMessageLikeEvent ) -> Result < ( ) , IndexError > {
135
- let doc = self . schema . make_doc ( event) ?;
136
- self . writer . add_document ( doc) ?; // TODO: This is blocking. Handle it.
125
+ /// Handle [`AnySyncMessageLikeEvent`]
126
+ ///
127
+ /// This which will add/remove/edit an event in the index based on the
128
+ /// event type.
129
+ pub fn handle_event ( & mut self , event : AnySyncMessageLikeEvent ) -> Result < ( ) , IndexError > {
130
+ match self . schema . handle_event ( event) ? {
131
+ RoomIndexOperation :: Add ( document) => self . writer . add_document ( document) ?,
132
+ } ;
137
133
Ok ( ( ) )
138
134
}
139
135
@@ -193,74 +189,77 @@ mod tests {
193
189
use std:: { collections:: HashSet , error:: Error } ;
194
190
195
191
use matrix_sdk_test:: event_factory:: EventFactory ;
196
- use ruma:: { event_id, owned_event_id , room_id, user_id} ;
192
+ use ruma:: { event_id, room_id, user_id} ;
197
193
198
194
use crate :: index:: RoomIndex ;
199
195
200
196
#[ test]
201
- fn test_make_index_in_ram ( ) {
197
+ fn test_make_index_in_memory ( ) {
202
198
let room_id = room_id ! ( "!room_id:localhost" ) ;
203
- let index = RoomIndex :: new_in_ram ( room_id) ;
199
+ let index = RoomIndex :: new_in_memory ( room_id) ;
204
200
205
201
index. expect ( "failed to make index in ram: {index:?}" ) ;
206
202
}
207
203
208
204
#[ test]
209
- fn test_add_event ( ) {
205
+ fn test_handle_event ( ) {
210
206
let room_id = room_id ! ( "!room_id:localhost" ) ;
211
207
let mut index =
212
- RoomIndex :: new_in_ram ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
208
+ RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
213
209
214
210
let event = EventFactory :: new ( )
215
211
. text_msg ( "event message" )
216
212
. event_id ( event_id ! ( "$event_id:localhost" ) )
217
213
. room ( room_id)
218
214
. sender ( user_id ! ( "@user_id:localhost" ) )
219
- . into_any_message_like_event ( ) ;
215
+ . into_any_sync_message_like_event ( ) ;
220
216
221
- index. add_event ( event) . expect ( "failed to add event: {res:?}" ) ;
217
+ index. handle_event ( event) . expect ( "failed to add event: {res:?}" ) ;
222
218
}
223
219
224
220
#[ test]
225
221
fn test_search_populated_index ( ) -> Result < ( ) , Box < dyn Error > > {
226
222
let room_id = room_id ! ( "!room_id:localhost" ) ;
227
223
let mut index =
228
- RoomIndex :: new_in_ram ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
224
+ RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
225
+
226
+ let event_id_1 = event_id ! ( "$event_id_1:localhost" ) ;
227
+ let event_id_2 = event_id ! ( "$event_id_2:localhost" ) ;
228
+ let event_id_3 = event_id ! ( "$event_id_3:localhost" ) ;
229
229
230
- index. add_event (
230
+ index. handle_event (
231
231
EventFactory :: new ( )
232
232
. text_msg ( "This is a sentence" )
233
- . event_id ( event_id ! ( "$ event_id_1:localhost" ) )
233
+ . event_id ( event_id_1)
234
234
. room ( room_id)
235
235
. sender ( user_id ! ( "@user_id:localhost" ) )
236
- . into_any_message_like_event ( ) ,
236
+ . into_any_sync_message_like_event ( ) ,
237
237
) ?;
238
238
239
- index. add_event (
239
+ index. handle_event (
240
240
EventFactory :: new ( )
241
241
. text_msg ( "All new words" )
242
- . event_id ( event_id ! ( "$ event_id_2:localhost" ) )
242
+ . event_id ( event_id_2)
243
243
. room ( room_id)
244
244
. sender ( user_id ! ( "@user_id:localhost" ) )
245
- . into_any_message_like_event ( ) ,
245
+ . into_any_sync_message_like_event ( ) ,
246
246
) ?;
247
247
248
- index. add_event (
248
+ index. handle_event (
249
249
EventFactory :: new ( )
250
250
. text_msg ( "A similar sentence" )
251
- . event_id ( event_id ! ( "$ event_id_3:localhost" ) )
251
+ . event_id ( event_id_3)
252
252
. room ( room_id)
253
253
. sender ( user_id ! ( "@user_id:localhost" ) )
254
- . into_any_message_like_event ( ) ,
254
+ . into_any_sync_message_like_event ( ) ,
255
255
) ?;
256
256
257
257
index. commit_and_reload ( ) ?;
258
258
259
259
let result = index. search ( "sentence" , 10 ) . expect ( "search failed with: {result:?}" ) ;
260
260
let result: HashSet < _ > = result. iter ( ) . collect ( ) ;
261
261
262
- let true_value =
263
- [ owned_event_id ! ( "$event_id_1:localhost" ) , owned_event_id ! ( "$event_id_3:localhost" ) ] ;
262
+ let true_value = [ event_id_1. to_owned ( ) , event_id_3. to_owned ( ) ] ;
264
263
let true_value: HashSet < _ > = true_value. iter ( ) . collect ( ) ;
265
264
266
265
assert_eq ! ( result, true_value, "search result not correct: {result:?}" ) ;
@@ -272,7 +271,7 @@ mod tests {
272
271
fn test_search_empty_index ( ) -> Result < ( ) , Box < dyn Error > > {
273
272
let room_id = room_id ! ( "!room_id:localhost" ) ;
274
273
let mut index =
275
- RoomIndex :: new_in_ram ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
274
+ RoomIndex :: new_in_memory ( room_id) . expect ( "failed to make index in ram: {index:?}" ) ;
276
275
277
276
index. commit_and_reload ( ) ?;
278
277
0 commit comments