-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Ability to restore Table or Tables from Backup
- Loading branch information
Showing
10 changed files
with
281 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use my_no_sql_server_core::rust_extensions::base64::FromBase64; | ||
|
||
use crate::scripts::TABLE_METADATA_FILE_NAME; | ||
|
||
pub enum ArchiveFileType { | ||
Metadata, | ||
PartitionKey(String), | ||
} | ||
|
||
impl ArchiveFileType { | ||
pub fn is_metadata(&self) -> bool { | ||
matches!(self, Self::Metadata) | ||
} | ||
|
||
pub fn unwrap_as_partition_key(self) -> String { | ||
match self { | ||
Self::PartitionKey(key) => key, | ||
_ => panic!("Can not unwrap partition key"), | ||
} | ||
} | ||
} | ||
|
||
pub struct RestoreFileName { | ||
pub table_name: String, | ||
pub file_type: ArchiveFileType, | ||
pub file_name: String, | ||
} | ||
|
||
impl RestoreFileName { | ||
pub fn new(file_name: &str) -> Result<Self, String> { | ||
let table_separator = file_name.find(std::path::MAIN_SEPARATOR); | ||
|
||
if table_separator.is_none() { | ||
return Err(format!("Invalid table file_name [{}]", file_name)); | ||
} | ||
|
||
let table_separator = table_separator.unwrap(); | ||
|
||
let partition_key = &file_name[table_separator + 1..]; | ||
|
||
if partition_key == TABLE_METADATA_FILE_NAME { | ||
return Ok(Self { | ||
table_name: file_name[..table_separator].to_string(), | ||
file_type: ArchiveFileType::Metadata, | ||
file_name: file_name.to_string(), | ||
}); | ||
} | ||
|
||
let partition_key = partition_key.from_base64(); | ||
|
||
if partition_key.is_err() { | ||
return Err(format!( | ||
"Invalid file_name key [{}]. Can not extract partition key", | ||
file_name | ||
)); | ||
} | ||
|
||
let partition_key = partition_key.unwrap(); | ||
|
||
let partition_key = match String::from_utf8(partition_key) { | ||
Ok(result) => result, | ||
Err(_) => { | ||
return Err(format!( | ||
"Invalid file_name key [{}]. Can not convert partition key to string", | ||
file_name | ||
)); | ||
} | ||
}; | ||
|
||
let result = Self { | ||
table_name: file_name[..table_separator].to_string(), | ||
file_type: ArchiveFileType::PartitionKey(partition_key), | ||
file_name: file_name.to_string(), | ||
}; | ||
|
||
Ok(result) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
#[test] | ||
fn test_metadata_file() { | ||
let result = super::RestoreFileName::new("key-value/.metadata").unwrap(); | ||
assert_eq!(result.table_name, "key-value"); | ||
assert!(result.file_type.is_metadata()); | ||
} | ||
|
||
#[test] | ||
fn test_partition_key() { | ||
let result = super::RestoreFileName::new("key-value/Yw==").unwrap(); | ||
assert_eq!(result.table_name, "key-value"); | ||
assert!(result.file_type.unwrap_as_partition_key() == "c"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::io::Read; | ||
|
||
pub struct ZipReader { | ||
zip: zip::ZipArchive<std::io::Cursor<Vec<u8>>>, | ||
} | ||
|
||
impl ZipReader { | ||
pub fn new(zip_content: Vec<u8>) -> Self { | ||
let zip_cursor = std::io::Cursor::new(zip_content); | ||
let zip = zip::ZipArchive::new(zip_cursor).unwrap(); | ||
Self { zip } | ||
} | ||
|
||
pub fn get_file_names(&mut self) -> impl Iterator<Item = &str> { | ||
self.zip.file_names() | ||
} | ||
|
||
pub fn get_content_as_vec(&mut self, file_name: &str) -> Result<Vec<u8>, std::io::Error> { | ||
let mut file = self.zip.by_name(file_name)?; | ||
let file_size = file.size() as usize; | ||
let mut content: Vec<u8> = Vec::with_capacity(file_size); | ||
|
||
let mut pos = 0; | ||
while pos < file_size { | ||
let size = file.read(&mut content[pos..])?; | ||
|
||
if size == 0 { | ||
break; | ||
} | ||
|
||
pos += size; | ||
} | ||
|
||
Ok(content) | ||
} | ||
} |