Skip to content

Commit c294146

Browse files
committed
clear cache if we are over limit
Signed-off-by: Somtochi Onyekwere <[email protected]>
1 parent 74967a4 commit c294146

File tree

5 files changed

+11
-9
lines changed

5 files changed

+11
-9
lines changed

core/rs/core/src/c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ fn bindgen_test_layout_crsql_ExtData() {
271271
let ptr = UNINIT.as_ptr();
272272
assert_eq!(
273273
::core::mem::size_of::<crsql_ExtData>(),
274-
176usize,
274+
168usize,
275275
concat!("Size of: ", stringify!(crsql_ExtData))
276276
);
277277
assert_eq!(

core/rs/core/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ use sqlite::{Destructor, ResultCode};
7474
use sqlite_nostd as sqlite;
7575
use sqlite_nostd::{Connection, Context, Value};
7676
use tableinfo::{
77-
crsql_ensure_table_infos_are_up_to_date, is_table_compatible, pull_table_info, TableInfo,
77+
crsql_ensure_table_infos_are_up_to_date, is_table_compatible, pull_table_info,
7878
};
7979
use teardown::*;
8080
use triggers::create_triggers;
81+
#[cfg(feature = "test")]
82+
use tableinfo::TableInfo;
8183

8284
pub use debug::debug_log;
8385

@@ -469,6 +471,7 @@ pub extern "C" fn sqlite3_crsqlcore_init(
469471
return null_mut();
470472
}
471473

474+
#[cfg(feature = "test")]
472475
if let Err(_) = db.create_function_v2(
473476
"crsql_cache_pk_cl",
474477
2,
@@ -992,6 +995,7 @@ unsafe extern "C" fn x_crsql_cache_site_ordinal(
992995
* Get the pk cl cached in the ext data for the current transaction.
993996
* only used for test to inspect the cl cache.
994997
*/
998+
#[cfg(feature = "test")]
995999
unsafe extern "C" fn x_crsql_cache_pk_cl(
9961000
ctx: *mut sqlite::context,
9971001
argc: i32,

core/rs/core/src/tableinfo.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use sqlite_nostd::ResultCode;
2828
use sqlite_nostd::Stmt;
2929
use sqlite_nostd::StrRef;
3030

31+
const MAX_CL_CACHE_SIZE: usize = 1500;
3132
pub struct TableInfo {
3233
pub tbl_name: String,
3334
pub pks: Vec<ColumnInfo>,
@@ -76,6 +77,10 @@ impl TableInfo {
7677
}
7778

7879
pub fn set_cl(&mut self, key: i64, cl: i64) {
80+
// clear the cache if we are over limit
81+
if self.cl_cache.len() >= MAX_CL_CACHE_SIZE {
82+
self.cl_cache.clear();
83+
}
7984
self.cl_cache.insert(key, cl);
8085
}
8186

core/src/ext-data.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ void crsql_drop_ordinal_map(crsql_ExtData *pExtData);
1212
void crsql_drop_table_info_vec(crsql_ExtData *pExtData);
1313
void crsql_init_last_db_versions_map(crsql_ExtData *pExtData);
1414
void crsql_drop_last_db_versions_map(crsql_ExtData *pExtData);
15-
void crsql_init_cl_cache(crsql_ExtData *pExtData);
16-
void crsql_drop_cl_cache(crsql_ExtData *pExtData);
1715
// void crsql_init_table_info_vec(crsql_ExtData *pExtData);
1816
// void crsql_drop_table_info_vec(crsql_ExtData *pExtData);
1917

@@ -77,7 +75,6 @@ crsql_ExtData *crsql_newExtData(sqlite3 *db) {
7775
crsql_init_table_info_vec(pExtData);
7876
crsql_init_last_db_versions_map(pExtData);
7977
crsql_init_ordinal_map(pExtData);
80-
crsql_init_cl_cache(pExtData);
8178

8279
sqlite3_stmt *pStmt;
8380

@@ -170,7 +167,6 @@ void crsql_freeExtData(crsql_ExtData *pExtData) {
170167
crsql_drop_table_info_vec(pExtData);
171168
crsql_drop_last_db_versions_map(pExtData);
172169
crsql_drop_ordinal_map(pExtData);
173-
crsql_drop_cl_cache(pExtData);
174170
sqlite3_free(pExtData);
175171
}
176172

core/src/ext-data.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ struct crsql_ExtData {
4949
int mergeEqualValues;
5050
unsigned long long timestamp;
5151
void *ordinalMap;
52-
void *clCache;
5352
};
5453

5554
crsql_ExtData *crsql_newExtData(sqlite3 *db);
@@ -62,7 +61,5 @@ int crsql_recreate_db_version_stmt(sqlite3 *db, crsql_ExtData *pExtData);
6261
void crsql_finalize(crsql_ExtData *pExtData);
6362
void crsql_init_ordinal_map(crsql_ExtData *pExtData);
6463
void crsql_drop_ordinal_map(crsql_ExtData *pExtData);
65-
void crsql_init_cl_cache(crsql_ExtData *pExtData);
66-
void crsql_drop_cl_cache(crsql_ExtData *pExtData);
6764

6865
#endif

0 commit comments

Comments
 (0)