Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/core/src/db/commit_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ impl CommitLog {
mlog.append(&bytes)?;
if self.fsync {
mlog.sync_all()?;
let mut odb = self.odb.lock().unwrap();
odb.sync_all()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems important 😅

log::trace!("DATABASE: FSYNC");
} else {
mlog.flush()?;
Expand Down
9 changes: 5 additions & 4 deletions crates/core/src/db/datastore/locking_tx_datastore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ impl CommittedState {
/// - any row in `delete_tables` must be in the associated `CommittedState`
/// - any row cannot be in both `insert_tables` and `delete_tables`
struct TxState {
//NOTE: Need to preserve order to correctly restore the db after reopen
/// For each table, additions have
insert_tables: HashMap<TableId, Table>,
delete_tables: HashMap<TableId, BTreeSet<RowId>>,
insert_tables: BTreeMap<TableId, Table>,
delete_tables: BTreeMap<TableId, BTreeSet<RowId>>,
}

/// Represents whether a row has been previously committed, inserted
Expand All @@ -247,8 +248,8 @@ enum RowState {
impl TxState {
pub fn new() -> Self {
Self {
insert_tables: HashMap::new(),
delete_tables: HashMap::new(),
insert_tables: BTreeMap::new(),
delete_tables: BTreeMap::new(),
}
}

Expand Down
55 changes: 54 additions & 1 deletion crates/core/src/db/relational_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ mod tests {
use crate::db::datastore::traits::IndexDef;
use crate::db::datastore::traits::TableDef;
use crate::db::message_log::MessageLog;
use crate::db::relational_db::ST_TABLES_ID;
use crate::db::relational_db::{open_db, ST_TABLES_ID};

use super::RelationalDB;
use crate::db::relational_db::make_default_ostorage;
Expand Down Expand Up @@ -840,6 +840,59 @@ mod tests {
Ok(())
}

#[test]
fn test_auto_inc_reload() -> ResultTest<()> {
let (stdb, tmp_dir) = make_test_db()?;

let mut tx = stdb.begin_tx();
let schema = TableDef {
table_name: "MyTable".to_string(),
columns: vec![ColumnDef {
col_name: "my_col".to_string(),
col_type: AlgebraicType::I64,
is_autoinc: true,
}],
indexes: vec![],
table_type: StTableType::User,
table_access: StAccess::Public,
};
let table_id = stdb.create_table(&mut tx, schema)?;

let sequence = stdb.sequence_id_from_name(&tx, "MyTable_my_col_seq")?;
assert!(sequence.is_some(), "Sequence not created");

stdb.insert(&mut tx, table_id, product![AlgebraicValue::I64(0)])?;

let mut rows = stdb
.iter_by_col_range(&tx, table_id, 0, AlgebraicValue::I64(0)..)?
.map(|r| *r.view().elements[0].as_i64().unwrap())
.collect::<Vec<i64>>();
rows.sort();

assert_eq!(rows, vec![1]);

stdb.commit_tx(tx)?;
drop(stdb);

dbg!("reopen...");
let stdb = open_db(&tmp_dir, false, true)?;

let mut tx = stdb.begin_tx();

stdb.insert(&mut tx, table_id, product![AlgebraicValue::I64(0)])?;

let mut rows = stdb
.iter_by_col_range(&tx, table_id, 0, AlgebraicValue::I64(0)..)?
.map(|r| *r.view().elements[0].as_i64().unwrap())
.collect::<Vec<i64>>();
rows.sort();

//TODO: This need the PR that recover the auto_inc, this is for check the db is correctly reopened...
//assert_eq!(rows, vec![1, 2]);
assert_eq!(rows, vec![1]);
Ok(())
}

#[test]
fn test_indexed() -> ResultTest<()> {
let (stdb, _tmp_dir) = make_test_db()?;
Expand Down