Skip to content

Commit 63bb070

Browse files
committed
Fix reopening of db in test, keep stable the binary data on files
1 parent ad8694b commit 63bb070

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

crates/core/src/db/commit_log.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ impl CommitLog {
5151
let mut mlog = mlog.lock().unwrap();
5252
mlog.append(&bytes)?;
5353
mlog.sync_all()?;
54+
let mut odb = self.odb.lock().unwrap();
55+
odb.sync_all()?;
5456
log::trace!("DATABASE: FSYNC");
5557
}
5658
Ok(Some(bytes.len()))

crates/core/src/db/datastore/locking_tx_datastore/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ impl CommittedState {
223223
/// - any row in `delete_tables` must be in the associated `CommittedState`
224224
/// - any row cannot be in both `insert_tables` and `delete_tables`
225225
struct TxState {
226+
//NOTE: Need to preserve order to correctly restore the db after reopen
226227
/// For each table, additions have
227-
insert_tables: HashMap<TableId, Table>,
228-
delete_tables: HashMap<TableId, BTreeSet<RowId>>,
228+
insert_tables: BTreeMap<TableId, Table>,
229+
delete_tables: BTreeMap<TableId, BTreeSet<RowId>>,
229230
}
230231

231232
/// Represents whether a row has been previously committed, inserted
@@ -247,8 +248,8 @@ enum RowState {
247248
impl TxState {
248249
pub fn new() -> Self {
249250
Self {
250-
insert_tables: HashMap::new(),
251-
delete_tables: HashMap::new(),
251+
insert_tables: BTreeMap::new(),
252+
delete_tables: BTreeMap::new(),
252253
}
253254
}
254255

crates/core/src/db/relational_db.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ mod tests {
524524
use crate::db::datastore::traits::IndexDef;
525525
use crate::db::datastore::traits::TableDef;
526526
use crate::db::message_log::MessageLog;
527-
use crate::db::relational_db::ST_TABLES_ID;
527+
use crate::db::relational_db::{open_db, ST_TABLES_ID};
528528

529529
use super::RelationalDB;
530530
use crate::db::relational_db::make_default_ostorage;
@@ -840,6 +840,59 @@ mod tests {
840840
Ok(())
841841
}
842842

843+
#[test]
844+
fn test_auto_inc_reload() -> ResultTest<()> {
845+
let (stdb, tmp_dir) = make_test_db()?;
846+
847+
let mut tx = stdb.begin_tx();
848+
let schema = TableDef {
849+
table_name: "MyTable".to_string(),
850+
columns: vec![ColumnDef {
851+
col_name: "my_col".to_string(),
852+
col_type: AlgebraicType::I64,
853+
is_autoinc: true,
854+
}],
855+
indexes: vec![],
856+
table_type: StTableType::User,
857+
table_access: StAccess::Public,
858+
};
859+
let table_id = stdb.create_table(&mut tx, schema)?;
860+
861+
let sequence = stdb.sequence_id_from_name(&tx, "MyTable_my_col_seq")?;
862+
assert!(sequence.is_some(), "Sequence not created");
863+
864+
stdb.insert(&mut tx, table_id, product![AlgebraicValue::I64(0)])?;
865+
866+
let mut rows = stdb
867+
.iter_by_col_range(&tx, table_id, 0, AlgebraicValue::I64(0)..)?
868+
.map(|r| *r.view().elements[0].as_i64().unwrap())
869+
.collect::<Vec<i64>>();
870+
rows.sort();
871+
872+
assert_eq!(rows, vec![1]);
873+
874+
stdb.commit_tx(tx)?;
875+
drop(stdb);
876+
877+
dbg!("reopen...");
878+
let stdb = open_db(&tmp_dir, false)?;
879+
880+
let mut tx = stdb.begin_tx();
881+
882+
stdb.insert(&mut tx, table_id, product![AlgebraicValue::I64(0)])?;
883+
884+
let mut rows = stdb
885+
.iter_by_col_range(&tx, table_id, 0, AlgebraicValue::I64(0)..)?
886+
.map(|r| *r.view().elements[0].as_i64().unwrap())
887+
.collect::<Vec<i64>>();
888+
rows.sort();
889+
890+
//TODO: This need the PR that recover the auto_inc, this is for check the db is correctly reopened...
891+
//assert_eq!(rows, vec![1, 2]);
892+
assert_eq!(rows, vec![1]);
893+
Ok(())
894+
}
895+
843896
#[test]
844897
fn test_indexed() -> ResultTest<()> {
845898
let (stdb, _tmp_dir) = make_test_db()?;

0 commit comments

Comments
 (0)