@@ -3,7 +3,9 @@ use std::ptr;
33use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
44use std:: sync:: Arc ;
55
6+ use lance:: dataset:: { DEFAULT_INDEX_CACHE_SIZE , DEFAULT_METADATA_CACHE_SIZE } ;
67use lance:: session:: Session ;
8+ use lance_core:: cache:: { CacheBackend , MokaCacheBackend } ;
79
810use crate :: error:: { clear_last_error, set_last_error, ErrorCode } ;
911
@@ -62,24 +64,42 @@ fn create_session_inner(
6264 index_cache_size_bytes : u64 ,
6365 metadata_cache_size_bytes : u64 ,
6466) -> FfiResult < SessionHandle > {
65- let session = if index_cache_size_bytes == 0 && metadata_cache_size_bytes == 0 {
66- Arc :: new ( Session :: default ( ) )
67- } else {
68- Arc :: new ( Session :: new (
69- u64_to_usize ( index_cache_size_bytes, "index_cache_size_bytes" ) ?,
70- u64_to_usize ( metadata_cache_size_bytes, "metadata_cache_size_bytes" ) ?,
71- Default :: default ( ) ,
72- ) )
73- } ;
74- Ok ( SessionHandle { session } )
67+ let ( index_cache_size_bytes, metadata_cache_size_bytes) =
68+ if index_cache_size_bytes == 0 && metadata_cache_size_bytes == 0 {
69+ ( DEFAULT_INDEX_CACHE_SIZE , DEFAULT_METADATA_CACHE_SIZE )
70+ } else {
71+ (
72+ u64_to_usize ( index_cache_size_bytes, "index_cache_size_bytes" ) ?,
73+ u64_to_usize ( metadata_cache_size_bytes, "metadata_cache_size_bytes" ) ?,
74+ )
75+ } ;
76+ let index_cache: Arc < dyn CacheBackend > =
77+ Arc :: new ( MokaCacheBackend :: with_capacity ( index_cache_size_bytes) ) ;
78+ let session = Arc :: new ( Session :: with_index_cache_backend (
79+ index_cache. clone ( ) ,
80+ metadata_cache_size_bytes,
81+ Default :: default ( ) ,
82+ ) ) ;
83+ Ok ( SessionHandle {
84+ session,
85+ index_cache,
86+ } )
87+ }
88+
89+ fn clear_session_caches ( handle : & SessionHandle ) {
90+ if let Some ( runtime) = crate :: runtime:: initialized_runtime ( ) {
91+ runtime. block_on ( async {
92+ handle. index_cache . clear ( ) . await ;
93+ handle. session . file_metadata_cache ( ) . clear ( ) . await ;
94+ } ) ;
95+ }
7596}
7697
7798#[ no_mangle]
7899pub unsafe extern "C" fn lance_close_session ( session : * mut c_void ) {
79100 if !session. is_null ( ) {
80- unsafe {
81- let _ = Box :: from_raw ( session as * mut SessionHandle ) ;
82- }
101+ let handle = unsafe { Box :: from_raw ( session as * mut SessionHandle ) } ;
102+ clear_session_caches ( & handle) ;
83103 }
84104}
85105
0 commit comments