fix dump v1

This commit is contained in:
Marin Postma 2021-05-31 10:42:31 +02:00
parent 33c6c4f0ee
commit bc5a5e37ea
No known key found for this signature in database
GPG key ID: D5241F0C0C865F30
4 changed files with 201 additions and 154 deletions

View file

@ -1,13 +1,13 @@
use std::path::Path;
use anyhow::Context;
use chrono::{DateTime, Utc};
use log::{info, warn};
use log::info;
use serde::{Deserialize, Serialize};
use crate::{index::Index, index_controller::{update_actor::UpdateStore, uuid_resolver::HeedUuidStore}, option::IndexerOpts};
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MetadataV2 {
db_version: String,
index_db_size: u64,
@ -29,6 +29,7 @@ impl MetadataV2 {
self,
src: impl AsRef<Path>,
dst: impl AsRef<Path>,
// TODO: use these variable to test if loading the index is possible.
_index_db_size: u64,
_update_db_size: u64,
indexing_options: &IndexerOpts,
@ -37,37 +38,21 @@ impl MetadataV2 {
"Loading dump from {}, dump database version: {}, dump version: V2",
self.dump_date, self.db_version
);
// get dir in which to load the db:
let dst_dir = dst
.as_ref()
.parent()
.with_context(|| format!("Invalid db path: {}", dst.as_ref().display()))?;
let tmp_dst = tempfile::tempdir_in(dst_dir)?;
info!("Loading index database.");
HeedUuidStore::load_dump(src.as_ref(), &tmp_dst)?;
HeedUuidStore::load_dump(src.as_ref(), &dst)?;
info!("Loading updates.");
UpdateStore::load_dump(&src, &tmp_dst, self.update_db_size)?;
UpdateStore::load_dump(&src, &dst, self.update_db_size)?;
info!("Loading indexes");
let indexes_path = src.as_ref().join("indexes");
let indexes = indexes_path.read_dir()?;
for index in indexes {
let index = index?;
Index::load_dump(&index.path(), &tmp_dst, self.index_db_size, indexing_options)?;
Index::load_dump(&index.path(), &dst, self.index_db_size, indexing_options)?;
}
// Persist and atomically rename the db
let persisted_dump = tmp_dst.into_path();
if dst.as_ref().exists() {
warn!("Overwriting database at {}", dst.as_ref().display());
std::fs::remove_dir_all(&dst)?;
}
std::fs::rename(&persisted_dump, &dst)?;
Ok(())
}
}