use std::time::{Duration, SystemTime, UNIX_EPOCH};
use lance::Dataset;
use crate::error::Result;
use crate::format::check_format;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableVersion {
pub version: u64,
pub timestamp: SystemTime,
}
pub async fn versions(dataset: &Dataset) -> Result<Vec<TableVersion>> {
check_format(dataset)?;
let mut versions: Vec<TableVersion> = dataset
.versions()
.await?
.into_iter()
.map(|version| TableVersion {
version: version.version,
timestamp: UNIX_EPOCH
+ Duration::from_millis(version.timestamp.timestamp_millis().max(0) as u64),
})
.collect();
versions.sort_by_key(|version| version.version);
Ok(versions)
}
pub async fn checkout(dataset: &Dataset, version: u64) -> Result<Dataset> {
check_format(dataset)?;
let old = dataset.checkout_version(version).await?;
check_format(&old)?;
Ok(old)
}
#[tracing::instrument(skip_all, fields(uri = %dataset.uri(), version = version), err)]
pub async fn restore(dataset: &mut Dataset, version: u64) -> Result<()> {
let mut old = checkout(dataset, version).await?;
old.restore().await?;
*dataset = old;
Ok(())
}