Merge pull request #228 from meilisearch/copy-and-compact-db

Introduce a function to copy and compact a database env
This commit is contained in:
Clément Renault 2019-10-18 13:21:55 +02:00 committed by GitHub
commit 47d777c8f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -33,6 +33,9 @@ struct IndexCommand {
#[structopt(long)] #[structopt(long)]
update_group_size: Option<usize>, update_group_size: Option<usize>,
#[structopt(long, parse(from_os_str))]
compact_to_path: Option<PathBuf>,
} }
#[derive(Debug, StructOpt)] #[derive(Debug, StructOpt)]
@ -165,6 +168,12 @@ fn index_command(command: IndexCommand, database: Database) -> Result<(), Box<dy
println!("database created in {:.2?} at: {:?}", start.elapsed(), command.database_path); println!("database created in {:.2?} at: {:?}", start.elapsed(), command.database_path);
if let Some(path) = command.compact_to_path {
let start = Instant::now();
let _file = database.copy_and_compact_to_path(&path)?;
println!("database compacted in {:.2?} at: {:?}", start.elapsed(), path);
}
Ok(()) Ok(())
} }

View File

@ -1,8 +1,10 @@
use std::collections::hash_map::{HashMap, Entry}; use std::collections::hash_map::{HashMap, Entry};
use std::fs::File;
use std::path::Path; use std::path::Path;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::{fs, thread}; use std::{fs, thread};
use zlmdb::{Result as ZResult, CompactionOption};
use zlmdb::types::{Str, Unit}; use zlmdb::types::{Str, Unit};
use crossbeam_channel::Receiver; use crossbeam_channel::Receiver;
use log::{debug, error}; use log::{debug, error};
@ -163,6 +165,10 @@ impl Database {
} }
} }
pub fn copy_and_compact_to_path<P: AsRef<Path>>(&self, path: P) -> ZResult<File> {
self.env.copy_to_path(path, CompactionOption::Enabled)
}
pub fn indexes_names(&self) -> MResult<Vec<String>> { pub fn indexes_names(&self) -> MResult<Vec<String>> {
let indexes = self.indexes.read().unwrap(); let indexes = self.indexes.read().unwrap();
Ok(indexes.keys().cloned().collect()) Ok(indexes.keys().cloned().collect())