14
14
// See the License for the specific language governing permissions and
15
15
// limitations under the License.
16
16
17
- #[ cfg( feature = "experimental-search" ) ]
18
- use std:: collections:: HashMap ;
19
17
use std:: {
20
18
collections:: { btree_map, BTreeMap , BTreeSet } ,
21
19
fmt:: { self , Debug } ,
@@ -40,12 +38,6 @@ use matrix_sdk_base::{
40
38
StateStoreDataKey , StateStoreDataValue , SyncOutsideWasm , ThreadingSupport ,
41
39
} ;
42
40
use matrix_sdk_common:: ttl_cache:: TtlCache ;
43
- #[ cfg( feature = "experimental-search" ) ]
44
- use matrix_sdk_search:: error:: IndexError ;
45
- #[ cfg( feature = "experimental-search" ) ]
46
- use matrix_sdk_search:: index:: RoomIndex ;
47
- #[ cfg( feature = "experimental-search" ) ]
48
- use ruma:: events:: AnyMessageLikeEvent ;
49
41
#[ cfg( feature = "e2e-encryption" ) ]
50
42
use ruma:: events:: { room:: encryption:: RoomEncryptionEventContent , InitialStateEvent } ;
51
43
use ruma:: {
@@ -80,15 +72,11 @@ use ruma::{
80
72
RoomAliasId , RoomId , RoomOrAliasId , ServerName , UInt , UserId ,
81
73
} ;
82
74
use serde:: de:: DeserializeOwned ;
83
- #[ cfg( feature = "experimental-search" ) ]
84
- use tokio:: sync:: MutexGuard ;
85
75
use tokio:: sync:: { broadcast, Mutex , OnceCell , RwLock , RwLockReadGuard } ;
86
76
use tracing:: { debug, error, instrument, trace, warn, Instrument , Span } ;
87
77
use url:: Url ;
88
78
89
79
use self :: futures:: SendRequest ;
90
- #[ cfg( feature = "experimental-search" ) ]
91
- use crate :: client:: builder:: IndexBaseDir ;
92
80
use crate :: {
93
81
authentication:: {
94
82
matrix:: MatrixAuth , oauth:: OAuth , AuthCtx , AuthData , ReloadSessionCallback ,
@@ -123,8 +111,12 @@ use crate::{
123
111
mod builder;
124
112
pub ( crate ) mod caches;
125
113
pub ( crate ) mod futures;
114
+ #[ cfg( feature = "experimental-search" ) ]
115
+ pub ( crate ) mod search;
126
116
127
117
pub use self :: builder:: { sanitize_server_name, ClientBuildError , ClientBuilder } ;
118
+ #[ cfg( feature = "experimental-search" ) ]
119
+ use crate :: client:: search:: SearchIndex ;
128
120
129
121
#[ cfg( not( target_family = "wasm" ) ) ]
130
122
type NotificationHandlerFut = Pin < Box < dyn Future < Output = ( ) > + Send > > ;
@@ -363,13 +355,9 @@ pub(crate) struct ClientInner {
363
355
/// [`LatestEvent`]: crate::latest_event::LatestEvent
364
356
latest_events : OnceCell < LatestEvents > ,
365
357
366
- /// HashMap that links each joined room to its RoomIndex
367
- #[ cfg( feature = "experimental-search" ) ]
368
- room_indexes : Arc < Mutex < HashMap < OwnedRoomId , RoomIndex > > > ,
369
-
370
- /// Base directory that stores the directories for each RoomIndex
371
358
#[ cfg( feature = "experimental-search" ) ]
372
- index_base_dir : IndexBaseDir ,
359
+ /// Handler for [`RoomIndex`]'s of each room
360
+ search_index : SearchIndex ,
373
361
}
374
362
375
363
impl ClientInner {
@@ -394,10 +382,7 @@ impl ClientInner {
394
382
#[ cfg( feature = "e2e-encryption" ) ] encryption_settings : EncryptionSettings ,
395
383
#[ cfg( feature = "e2e-encryption" ) ] enable_share_history_on_invite : bool ,
396
384
cross_process_store_locks_holder_name : String ,
397
- #[ cfg( feature = "experimental-search" ) ] room_indexes : Arc <
398
- Mutex < HashMap < OwnedRoomId , RoomIndex > > ,
399
- > ,
400
- #[ cfg( feature = "experimental-search" ) ] index_base_dir : IndexBaseDir ,
385
+ #[ cfg( feature = "experimental-search" ) ] search_index_handler : SearchIndex ,
401
386
) -> Arc < Self > {
402
387
let caches = ClientCaches {
403
388
server_info : server_info. into ( ) ,
@@ -434,9 +419,7 @@ impl ClientInner {
434
419
enable_share_history_on_invite,
435
420
server_max_upload_size : Mutex :: new ( OnceCell :: new ( ) ) ,
436
421
#[ cfg( feature = "experimental-search" ) ]
437
- room_indexes,
438
- #[ cfg( feature = "experimental-search" ) ]
439
- index_base_dir,
422
+ search_index : search_index_handler,
440
423
} ;
441
424
442
425
#[ allow( clippy:: let_and_return) ]
@@ -2742,9 +2725,7 @@ impl Client {
2742
2725
self . inner . enable_share_history_on_invite ,
2743
2726
cross_process_store_locks_holder_name,
2744
2727
#[ cfg( feature = "experimental-search" ) ]
2745
- self . inner . room_indexes . clone ( ) ,
2746
- #[ cfg( feature = "experimental-search" ) ]
2747
- self . inner . index_base_dir . clone ( ) ,
2728
+ self . inner . search_index . clone ( ) ,
2748
2729
)
2749
2730
. await ,
2750
2731
} ;
@@ -2842,105 +2823,9 @@ impl Client {
2842
2823
& self . base_client ( ) . decryption_settings
2843
2824
}
2844
2825
2845
- /// Add [`AnyMessageLikeEvent`] to [`RoomIndex`] of given [`RoomId`]
2846
- #[ cfg( feature = "experimental-search" ) ]
2847
- pub async fn index_event (
2848
- & self ,
2849
- event : AnyMessageLikeEvent ,
2850
- room_id : & RoomId ,
2851
- ) -> Result < ( ) , IndexError > {
2852
- let mut hash_map = self . inner . room_indexes . lock ( ) . await ;
2853
-
2854
- let result = if let Some ( index) = hash_map. get_mut ( room_id) {
2855
- index. add_event ( event)
2856
- } else {
2857
- self . add_index_impl ( room_id, & mut hash_map) ?;
2858
- let index = hash_map. get_mut ( room_id) . expect ( "key just added" ) ;
2859
- index. add_event ( event)
2860
- } ;
2861
-
2862
- match result {
2863
- Ok ( _) => { }
2864
- Err ( IndexError :: CannotIndexRedactedMessage )
2865
- | Err ( IndexError :: EmptyMessage )
2866
- | Err ( IndexError :: MessageTypeNotSupported ) => {
2867
- debug ! ( "failed to parse event for indexing: {result:?}" )
2868
- }
2869
- Err ( IndexError :: TantivyError ( err) ) => {
2870
- error ! ( "failed to add/commit event to index: {err:?}" )
2871
- }
2872
- Err ( _) => error ! ( "unexpected error during indexing: {result:?}" ) ,
2873
- } ;
2874
- Ok ( ( ) )
2875
- }
2876
-
2877
- /// Add [`RoomIndex`] for given [`RoomId`] to room_indexes
2878
2826
#[ cfg( feature = "experimental-search" ) ]
2879
- pub async fn add_index ( & self , room_id : & RoomId ) -> Result < ( ) , IndexError > {
2880
- let mut hash_map = self . inner . room_indexes . lock ( ) . await ;
2881
- self . add_index_impl ( room_id, & mut hash_map) ?;
2882
- Ok ( ( ) )
2883
- }
2884
-
2885
- #[ cfg( feature = "experimental-search" ) ]
2886
- fn add_index_impl (
2887
- & self ,
2888
- room_id : & RoomId ,
2889
- hash_map : & mut MutexGuard < ' _ , HashMap < OwnedRoomId , RoomIndex > > ,
2890
- ) -> Result < ( ) , IndexError > {
2891
- if !hash_map. contains_key ( room_id) {
2892
- let index = match & self . inner . index_base_dir {
2893
- IndexBaseDir :: Directory ( path) => RoomIndex :: open_or_create ( path, room_id) ?,
2894
- IndexBaseDir :: Ram => RoomIndex :: new_in_ram ( room_id) ?,
2895
- } ;
2896
- hash_map. insert ( room_id. to_owned ( ) , index) ;
2897
- }
2898
- Ok ( ( ) )
2899
- }
2900
-
2901
- /// Search a [`Room`]'s index for the query and return at most
2902
- /// max_number_of_results results.
2903
- #[ cfg( feature = "experimental-search" ) ]
2904
- pub async fn search_index (
2905
- & self ,
2906
- query : & str ,
2907
- max_number_of_results : usize ,
2908
- room_id : & RoomId ,
2909
- ) -> Option < Vec < OwnedEventId > > {
2910
- let hash_map = self . inner . room_indexes . lock ( ) . await ;
2911
- if let Some ( index) = hash_map. get ( room_id) {
2912
- index
2913
- . search ( query, max_number_of_results)
2914
- . inspect_err ( |err| {
2915
- error ! ( "error occurred while searching index: {err:?}" ) ;
2916
- } )
2917
- . ok ( )
2918
- } else {
2919
- warn ! ( "Tried to search in a room with no index" ) ;
2920
- None
2921
- }
2922
- }
2923
-
2924
- /// Commit a [`Room`]'s [`RoomIndex`]
2925
- #[ cfg( feature = "experimental-search" ) ]
2926
- pub async fn commit ( & self , room_id : & RoomId ) {
2927
- let mut hash_map = self . inner . room_indexes . lock ( ) . await ;
2928
- if let Some ( index) = hash_map. get_mut ( room_id) {
2929
- let _ = index. commit ( ) . inspect_err ( |err| {
2930
- error ! ( "error occurred while committing: {err:?}" ) ;
2931
- } ) ;
2932
- }
2933
- }
2934
-
2935
- /// Commit a [`Room`]'s [`RoomIndex`] and reload searchers
2936
- #[ cfg( feature = "experimental-search" ) ]
2937
- pub async fn commit_and_reload ( & self , room_id : & RoomId ) {
2938
- let mut hash_map = self . inner . room_indexes . lock ( ) . await ;
2939
- if let Some ( index) = hash_map. get_mut ( room_id) {
2940
- let _ = index. commit_and_reload ( ) . inspect_err ( |err| {
2941
- error ! ( "error occurred while committing: {err:?}" ) ;
2942
- } ) ;
2943
- }
2827
+ pub ( crate ) fn search_index ( & self ) -> & SearchIndex {
2828
+ & self . inner . search_index
2944
2829
}
2945
2830
2946
2831
/// Whether the client is configured to take thread subscriptions (MSC4306
0 commit comments