Repository: cberner/redb
Pull request #421 (2022-10-31 02:39:28)

Remove set_region_size()


Patch:
diff --git a/src/db.rs b/src/db.rs
index 8aeff91..f9e10d2 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -564,11 +564,9 @@ impl Builder {
         self
     }
 
-    /// Set the internal region size of the database
-    /// Smaller regions may allow the database to compact more effectively, but will limit the maximum
-    /// size of values that can be stored
-    /// Defaults to 4GiB
-    pub fn set_region_size(&mut self, size: usize) -> &mut Self {
+    #[cfg(test)]
+    #[cfg(unix)]
+    fn set_region_size(&mut self, size: usize) -> &mut Self {
         assert!(size.is_power_of_two());
         self.region_size = Some(size);
         self
@@ -616,3 +614,67 @@ impl std::fmt::Debug for Database {
         f.debug_struct("Database").finish()
     }
 }
+
+#[cfg(test)]
+mod test {
+    #[cfg(unix)]
+    use tempfile::NamedTempFile;
+
+    #[cfg(unix)]
+    use crate::{Database, TableDefinition};
+
+    #[test]
+    #[cfg(unix)]
+    fn dynamic_shrink() {
+        let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
+        let table_definition: TableDefinition<u64, [u8]> = TableDefinition::new("x");
+        let big_value = vec![0; 1024];
+
+        let db_size = 20 * 1024 * 1024;
+        let db = unsafe {
+            Database::builder()
+                .set_region_size(1024 * 1024)
+                .create(tmpfile.path(), db_size)
+                .unwrap()
+        };
+
+        let txn = db.begin_write().unwrap();
+        {
+            let mut table = txn.open_table(table_definition).unwrap();
+            for i in 0..2048 {
+                table.insert(&i, &big_value).unwrap();
+            }
+        }
+        txn.commit().unwrap();
+
+        let file_size = tmpfile.as_file().metadata().unwrap().len();
+
+        let txn = db.begin_write().unwrap();
+        {
+            let mut table = txn.open_table(table_definition).unwrap();
+            for i in 0..2048 {
+                table.remove(&i).unwrap();
+            }
+        }
+        txn.commit().unwrap();
+
+        // Perform a couple more commits to be sure the database has a chance to compact
+        let txn = db.begin_write().unwrap();
+        {
+            let mut table = txn.open_table(table_definition).unwrap();
+            table.insert(&0, &[]).unwrap();
+        }
+        txn.commit().unwrap();
+        let txn = db.begin_write().unwrap();
+        {
+            let mut table = txn.open_table(table_definition).unwrap();
+            table.remove(&0).unwrap();
+        }
+        txn.commit().unwrap();
+        let txn = db.begin_write().unwrap();
+        txn.commit().unwrap();
+
+        let final_file_size = tmpfile.as_file().metadata().unwrap().len();
+        assert!(final_file_size < file_size);
+    }
+}


Test patch:
diff --git a/tests/basic_tests.rs b/tests/basic_tests.rs
index 08083a8..78670a4 100644
--- a/tests/basic_tests.rs
+++ b/tests/basic_tests.rs
@@ -6,31 +6,6 @@ use tempfile::NamedTempFile;
 const SLICE_TABLE: TableDefinition<[u8], [u8]> = TableDefinition::new("x");
 const U64_TABLE: TableDefinition<u64, u64> = TableDefinition::new("u64");
 
-#[test]
-fn custom_region_size() {
-    let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
-    let db = unsafe {
-        Database::builder()
-            .set_region_size(512 * 1024)
-            .create(tmpfile.path(), 20 * 1024 * 1024)
-            .unwrap()
-    };
-    let rows = 4 * 1024;
-    let write_txn = db.begin_write().unwrap();
-    {
-        let mut table = write_txn.open_table(SLICE_TABLE).unwrap();
-        let big_value = vec![0u8; 1024];
-        for i in 0..rows {
-            table.insert(&(i as u32).to_le_bytes(), &big_value).unwrap();
-        }
-    }
-    write_txn.commit().unwrap();
-
-    let read_txn = db.begin_read().unwrap();
-    let table = read_txn.open_table(SLICE_TABLE).unwrap();
-    assert_eq!(table.len().unwrap(), rows);
-}
-
 #[test]
 fn len() {
     let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs
index 41218f2..589999b 100644
--- a/tests/integration_tests.rs
+++ b/tests/integration_tests.rs
@@ -306,61 +306,6 @@ fn dynamic_growth() {
     assert!(file_size > initial_file_size);
 }
 
-#[test]
-#[cfg(unix)]
-fn dynamic_shrink() {
-    let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();
-    let table_definition: TableDefinition<u64, [u8]> = TableDefinition::new("x");
-    let big_value = vec![0; 1024];
-
-    let db_size = 20 * 1024 * 1024;
-    let db = unsafe {
-        Database::builder()
-            .set_region_size(1024 * 1024)
-            .create(tmpfile.path(), db_size)
-            .unwrap()
-    };
-
-    let txn = db.begin_write().unwrap();
-    {
-        let mut table = txn.open_table(table_definition).unwrap();
-        for i in 0..2048 {
-            table.insert(&i, &big_value).unwrap();
-        }
-    }
-    txn.commit().unwrap();
-
-    let file_size = tmpfile.as_file().metadata().unwrap().len();
-
-    let txn = db.begin_write().unwrap();
-    {
-        let mut table = txn.open_table(table_definition).unwrap();
-        for i in 0..2048 {
-            table.remove(&i).unwrap();
-        }
-    }
-    txn.commit().unwrap();
-
-    // Perform a couple more commits to be sure the database has a chance to compact
-    let txn = db.begin_write().unwrap();
-    {
-        let mut table = txn.open_table(table_definition).unwrap();
-        table.insert(&0, &[]).unwrap();
-    }
-    txn.commit().unwrap();
-    let txn = db.begin_write().unwrap();
-    {
-        let mut table = txn.open_table(table_definition).unwrap();
-        table.remove(&0).unwrap();
-    }
-    txn.commit().unwrap();
-    let txn = db.begin_write().unwrap();
-    txn.commit().unwrap();
-
-    let final_file_size = tmpfile.as_file().metadata().unwrap().len();
-    assert!(final_file_size < file_size);
-}
-
 #[test]
 fn multi_page_kv() {
     let tmpfile: NamedTempFile = NamedTempFile::new().unwrap();