Repository: cberner/redb (Rust)

Issue:
Checksum assert
I just pushed a branch that updates redb to 0.6.0, and I'm hitting an assert on CI that seems to be checking the checksum type:

```
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `Zero`,
 right: `XXH3_128`', /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/redb-0.6.0/src/tree_store/page_store/page_manager.rs:883:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
```

https://github.com/casey/ord/runs/8294934765?check_suite_focus=true#step:8:523


Discussion:
Oops, I even had a TODO about this, but didn't realize it completely broke `open()` at the time =P
Ok, I released 0.6.1 with the fix. Give that a try :)
Nice, that fixed it!



Patch:
diff --git a/src/db.rs b/src/db.rs
index 682b583c..7a056904 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -183,7 +183,14 @@ impl Database {
                 .open(path)?
         };
 
-        Database::new(file, db_size, None, None, true, WriteStrategy::default())
+        Database::new(
+            file,
+            db_size,
+            None,
+            None,
+            true,
+            Some(WriteStrategy::default()),
+        )
     }
 
     /// Opens an existing redb database.
@@ -195,14 +202,7 @@ impl Database {
         if File::open(path.as_ref())?.metadata()?.len() > 0 {
             let existing_size = get_db_size(path.as_ref())?;
             let file = OpenOptions::new().read(true).write(true).open(path)?;
-            Database::new(
-                file,
-                existing_size,
-                None,
-                None,
-                true,
-                WriteStrategy::default(),
-            )
+            Database::new(file, existing_size, None, None, true, None)
         } else {
             Err(Error::Io(io::Error::from(ErrorKind::InvalidData)))
         }
@@ -268,7 +268,7 @@ impl Database {
         page_size: Option<usize>,
         region_size: Option<usize>,
         dynamic_growth: bool,
-        write_strategy: WriteStrategy,
+        write_strategy: Option<WriteStrategy>,
     ) -> Result<Self> {
         #[cfg(feature = "logging")]
         info!(
@@ -536,7 +536,7 @@ impl DatabaseBuilder {
             self.page_size,
             self.region_size,
             self.dynamic_growth,
-            self.write_strategy,
+            Some(self.write_strategy),
         )
     }
 }
diff --git a/src/tree_store/page_store/page_manager.rs b/src/tree_store/page_store/page_manager.rs
index 59f1d875..acc7a074 100644
--- a/src/tree_store/page_store/page_manager.rs
+++ b/src/tree_store/page_store/page_manager.rs
@@ -753,7 +753,7 @@ impl TransactionalMemory {
         requested_page_size: Option<usize>,
         requested_region_size: Option<usize>,
         dynamic_growth: bool,
-        write_strategy: WriteStrategy,
+        write_strategy: Option<WriteStrategy>,
     ) -> Result<Self> {
         #[allow(clippy::assertions_on_constants)]
         {
@@ -813,7 +813,7 @@ impl TransactionalMemory {
             metadata.set_region_header_length(layout.full_region_layout().data_section().start);
             metadata.set_region_max_data_pages(layout.full_region_layout().num_pages());
             metadata.set_max_capacity(max_capacity);
-            let checksum_type = match write_strategy {
+            let checksum_type = match write_strategy.unwrap_or_default() {
                 WriteStrategy::Checksum => ChecksumType::XXH3_128,
                 WriteStrategy::TwoPhase => ChecksumType::Zero,
             };
@@ -872,13 +872,6 @@ impl TransactionalMemory {
             mmap.flush()?;
         }
 
-        // TODO: make the write_strategy argument optional when opening an existing db, and read it from the file instead
-        let checksum_type = match write_strategy {
-            WriteStrategy::Checksum => ChecksumType::XXH3_128,
-            WriteStrategy::TwoPhase => ChecksumType::Zero,
-        };
-        assert_eq!(metadata.get_checksum_type(), checksum_type);
-
         let page_size = metadata.get_page_size();
         if let Some(size) = requested_page_size {
             assert_eq!(page_size, size);
@@ -1743,7 +1736,7 @@ mod test {
             None,
             None,
             true,
-            WriteStrategy::TwoPhase
+            Some(WriteStrategy::TwoPhase)
         )
         .unwrap()
         .needs_repair()
@@ -1824,7 +1817,7 @@ mod test {
             None,
             None,
             true,
-            WriteStrategy::Checksum
+            Some(WriteStrategy::Checksum)
         )
         .unwrap()
         .needs_repair()
@@ -1888,7 +1881,7 @@ mod test {
             None,
             None,
             true,
-            WriteStrategy::Checksum
+            Some(WriteStrategy::Checksum)
         )
         .unwrap()
         .needs_repair()


Test patch:
diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs
index a46e4761..36fd845d 100644
--- a/tests/integration_tests.rs
+++ b/tests/integration_tests.rs
@@ -851,6 +851,23 @@ fn regression17() {
     tx.abort().unwrap();
 }
 
+#[test]
+fn twophase_open() {
+    let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
+
+    let db_size = 1024 * 1024;
+    let db = unsafe {
+        Database::builder()
+            .set_write_strategy(WriteStrategy::TwoPhase)
+            .create(tmpfile.path(), db_size)
+            .unwrap()
+    };
+    drop(db);
+    unsafe {
+        Database::open(tmpfile.path()).unwrap();
+    }
+}
+
 #[test]
 fn non_durable_read_isolation() {
     let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();