use crate::io::FileIO;
use crate::spec::manifest_file_meta::MANIFEST_FILE_META_SCHEMA;
use crate::spec::ManifestFileMeta;
use crate::Result;
pub struct ManifestList;
impl ManifestList {
pub async fn read(file_io: &FileIO, path: &str) -> Result<Vec<ManifestFileMeta>> {
let input = file_io.new_input(path)?;
if !input.exists().await? {
return Ok(Vec::new());
}
let content = input.read().await?;
crate::spec::from_avro_bytes(&content)
}
pub async fn write(file_io: &FileIO, path: &str, metas: &[ManifestFileMeta]) -> Result<()> {
let bytes = crate::spec::to_avro_bytes(MANIFEST_FILE_META_SCHEMA, metas)?;
let output = file_io.new_output(path)?;
output.write(bytes::Bytes::from(bytes)).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::io::FileIOBuilder;
use crate::spec::stats::BinaryTableStats;
fn test_file_io() -> FileIO {
FileIOBuilder::new("memory").build().unwrap()
}
#[tokio::test]
async fn test_manifest_list_roundtrip() {
let file_io = test_file_io();
let path = "memory:/test_manifest_list_roundtrip/manifest-list-0";
file_io
.mkdirs("memory:/test_manifest_list_roundtrip/")
.await
.unwrap();
let value_bytes = vec![
0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 129,
];
let original = vec![
ManifestFileMeta::new(
"manifest-a".to_string(),
1024,
5,
2,
BinaryTableStats::new(value_bytes.clone(), value_bytes.clone(), vec![Some(1)]),
0,
),
ManifestFileMeta::new(
"manifest-b".to_string(),
2048,
10,
0,
BinaryTableStats::new(value_bytes.clone(), value_bytes.clone(), vec![Some(3)]),
1,
),
];
ManifestList::write(&file_io, path, &original)
.await
.unwrap();
let decoded = ManifestList::read(&file_io, path).await.unwrap();
assert_eq!(original, decoded);
}
#[tokio::test]
async fn test_manifest_list_read_nonexistent() {
let file_io = test_file_io();
let result = ManifestList::read(&file_io, "memory:/nonexistent/manifest-list")
.await
.unwrap();
assert!(result.is_empty());
}
#[tokio::test]
async fn test_manifest_list_write_empty() {
let file_io = test_file_io();
let path = "memory:/test_manifest_list_empty/manifest-list-0";
file_io
.mkdirs("memory:/test_manifest_list_empty/")
.await
.unwrap();
ManifestList::write(&file_io, path, &[]).await.unwrap();
let decoded = ManifestList::read(&file_io, path).await.unwrap();
assert!(decoded.is_empty());
}
}