From 34f0b85a88ed65db80b2f7cc5f34cbadb9cd5257 Mon Sep 17 00:00:00 2001 From: michaelvlach Date: Sat, 26 Nov 2022 16:08:43 +0100 Subject: [PATCH] Update vec_storage.rs --- src/agdb/collections/vec_storage.rs | 96 ++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/src/agdb/collections/vec_storage.rs b/src/agdb/collections/vec_storage.rs index c743aad3..5c95d97e 100644 --- a/src/agdb/collections/vec_storage.rs +++ b/src/agdb/collections/vec_storage.rs @@ -57,7 +57,7 @@ where self.len() == 0 } - fn iter(&self) -> VecStorageIterator { + pub fn iter(&self) -> VecStorageIterator { VecStorageIterator:: { index: 0, vec: self, @@ -136,6 +136,31 @@ where self.storage_index } + pub fn swap(&mut self, index: u64, other: u64) -> Result<(), DbError> { + if index == other { + return Ok(()); + } + + self.validate_index(index)?; + self.validate_index(other)?; + + let bytes = self.value_bytes(index)?; + let offset_from = Self::offset(other); + let offset_to = Self::offset(index); + let size = T::storage_len(); + + self.storage.borrow_mut().transaction(); + self.storage + .borrow_mut() + .move_at(&self.storage_index, offset_from, offset_to, size)?; + self.storage.borrow_mut().insert_bytes_at( + &self.storage_index, + Self::offset(other), + &bytes, + )?; + self.storage.borrow_mut().commit() + } + pub fn to_vec(&self) -> Result, DbError> { let mut vector = Vec::::new(); vector.reserve(self.len() as usize); @@ -674,6 +699,75 @@ mod tests { assert_eq!(vec.capacity(), 0); } + #[test] + fn swap() { + let test_file = TestFile::new(); + let storage = Rc::new(RefCell::new( + FileStorage::new(test_file.file_name()).unwrap(), + )); + let mut vec = VecStorage::::new(storage).unwrap(); + vec.push(&"Hello".to_string()).unwrap(); + vec.push(&", ".to_string()).unwrap(); + vec.push(&"World".to_string()).unwrap(); + vec.push(&"!".to_string()).unwrap(); + vec.swap(0, 2).unwrap(); + assert_eq!( + vec.to_vec(), + Ok(vec![ + "World".to_string(), + ", ".to_string(), + "Hello".to_string(), + "!".to_string() + ]) + ); + } + #[test] + fn swap_self() { + let test_file = TestFile::new(); + let storage = Rc::new(RefCell::new( + FileStorage::new(test_file.file_name()).unwrap(), + )); + let mut vec = VecStorage::::new(storage).unwrap(); + vec.push(&"Hello".to_string()).unwrap(); + vec.push(&", ".to_string()).unwrap(); + vec.push(&"World".to_string()).unwrap(); + vec.push(&"!".to_string()).unwrap(); + vec.swap(1, 1).unwrap(); + assert_eq!( + vec.to_vec(), + Ok(vec![ + "Hello".to_string(), + ", ".to_string(), + "World".to_string(), + "!".to_string() + ]) + ); + } + #[test] + fn swap_invalid() { + let test_file = TestFile::new(); + let storage = Rc::new(RefCell::new( + FileStorage::new(test_file.file_name()).unwrap(), + )); + let mut vec = VecStorage::::new(storage).unwrap(); + vec.push(&"Hello".to_string()).unwrap(); + vec.push(&", ".to_string()).unwrap(); + vec.push(&"World".to_string()).unwrap(); + vec.push(&"!".to_string()).unwrap(); + assert_eq!( + vec.swap(1, 10), + Err(DbError::from( + "VecStorage error: index (10) out of bounds (4)" + )) + ); + assert_eq!( + vec.swap(10, 1), + Err(DbError::from( + "VecStorage error: index (10) out of bounds (4)" + )) + ); + } + #[test] fn to_vec() { let test_file = TestFile::new();