Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drop column primitive #210

Merged
merged 6 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/btree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@ impl BTreeTable {

result
}

pub(crate) fn drop_files(&self) -> Result<()> {
let tables = self.tables.write();
for table in tables.iter() {
table.drop_files()?;
}
Ok(())
}
}

pub mod commit_overlay {
Expand Down
16 changes: 16 additions & 0 deletions src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ impl HashColumn {
compression: &self.compression,
}
}

fn drop_files(&self) -> Result<()> {
let tables = self.tables.write();
tables.index.drop_files()?;
for table in tables.value.iter() {
table.drop_files()?;
}
Ok(())
}
}

impl Column {
Expand Down Expand Up @@ -335,6 +344,13 @@ impl Column {
let entry_size = SIZES.get(tier as usize).cloned();
ValueTable::open(path, id, entry_size, options, db_version)
}

pub fn drop_files(&self) -> Result<()> {
match self {
Column::Hash(c) => c.drop_files(),
Column::Tree(c) => c.drop_files(),
}
}
}

impl HashColumn {
Expand Down
107 changes: 105 additions & 2 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ impl DbInner {
Ok(())
}

fn replay_all_logs(&mut self) -> Result<()> {
fn replay_all_logs(&self) -> Result<()> {
while let Some(id) = self.log.replay_next()? {
log::debug!(target: "parity-db", "Replaying database log {}", id);
while self.enact_logs(true)? {}
Expand Down Expand Up @@ -918,9 +918,45 @@ impl Db {
Self::open_inner(options, OpeningMode::ReadOnly)
}

/// Drop a column from the database, optionally changing its options.
/// This shutdown and restart the db in write mode.
pub fn drop_column(&mut self, index: u8, new_options: Option<ColumnOptions>) -> Result<()> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be called clear_column, as the column is not really removed. Also, similar to add_column, this should be an associated function that does not require &self

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw there is clear_column in migration.rs:

pub fn clear_column(path: &Path, column: ColId) -> Result<()> {

It removes all data without removing the actual column. I think clear_column for this function too will be confusing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes, it is basically doing the same thing (I should have use this 🤦 ), there is also one for moving column, will fix that redundancy (and make that if column is at last index and no new option is provided, we remove the column (as your use case is the last number it will be usefull).

self.drop_inner();
if let Err(e) = self.inner.replay_all_logs() {
log::debug!(target: "parity-db", "Error during log replay.");
return Err(e)
} else {
self.inner.log.clear_replay_logs();
self.inner.clean_all_logs()?;
self.inner.log.kill_logs()?;
}
let salt = self.inner.options.salt;
let mut options = self.inner.options.clone();
if index as usize >= options.columns.len() {
return Err(Error::IncompatibleColumnConfig {
id: index,
reason: "Column not found".to_string(),
})
}

self.inner.columns[index as usize].drop_files()?;

if let Some(new_options) = new_options {
options.columns[index as usize] = new_options;
options.write_metadata(
&options.path,
&salt.expect("`salt` is always `Some` after opening the DB; qed"),
)?;
}

try_io!(self.inner._lock_file.unlock());
*self = Db::open_or_create(&options)?;
Ok(())
}

fn open_inner(options: &Options, opening_mode: OpeningMode) -> Result<Db> {
assert!(options.is_valid());
let mut db = DbInner::open(options, opening_mode)?;
let db = DbInner::open(options, opening_mode)?;
// This needs to be call before log thread: so first reindexing
// will run in correct state.
if let Err(e) = db.replay_all_logs() {
Expand Down Expand Up @@ -1175,6 +1211,12 @@ impl Db {

impl Drop for Db {
fn drop(&mut self) {
self.drop_inner()
}
}

impl Db {
fn drop_inner(&mut self) {
self.inner.shutdown();
if let Some(t) = self.log_thread.take() {
if let Err(e) = t.join() {
Expand Down Expand Up @@ -2622,4 +2664,65 @@ mod tests {
assert_eq!(db.inner.columns[0].index_bits(), Some(17));
}
}

#[test]
fn test_remove_column() {
let tmp = tempdir().unwrap();
let db_test_file = EnableCommitPipelineStages::DbFile;
let mut options_db_files = db_test_file.options(tmp.path(), 2);
options_db_files.salt = Some(options_db_files.salt.unwrap_or_default());
let mut options_std = EnableCommitPipelineStages::Standard.options(tmp.path(), 2);
options_std.salt = options_db_files.salt.clone();

let db = Db::open_inner(&options_db_files, OpeningMode::Create).unwrap();

let payload: Vec<(u8, _, _)> = (0u16..10_000)
.map(|i| (1, i.to_le_bytes().to_vec(), Some(i.to_be_bytes().to_vec())))
.collect();

db.commit(payload.clone()).unwrap();

db_test_file.run_stages(&db);
drop(db);

let mut db = Db::open_inner(&options_std, OpeningMode::Write).unwrap();
for (col, key, value) in payload.iter() {
assert_eq!(db.get(*col, key).unwrap().as_ref(), value.as_ref());
}
db.drop_column(1, None).unwrap();
for (col, key, _value) in payload.iter() {
assert_eq!(db.get(*col, key).unwrap(), None);
}

drop(db);

let db = Db::open_inner(&options_db_files, OpeningMode::Write).unwrap();
let payload: Vec<(u8, _, _)> = (0u16..10)
.map(|i| (1, i.to_le_bytes().to_vec(), Some(i.to_be_bytes().to_vec())))
.collect();

db.commit(payload.clone()).unwrap();

db_test_file.run_stages(&db);
drop(db);

let mut db = Db::open_inner(&options_std, OpeningMode::Write).unwrap();
let payload: Vec<(u8, _, _)> = (10u16..1000)
.map(|i| (1, i.to_le_bytes().to_vec(), Some(i.to_be_bytes().to_vec())))
.collect();

db.commit(payload.clone()).unwrap();
assert!(db.iter(1).is_err());

let mut col_option = options_std.columns[1].clone();
col_option.btree_index = true;
db.drop_column(1, Some(col_option)).unwrap();

let payload: Vec<(u8, _, _)> = (0u16..10)
.map(|i| (1, i.to_le_bytes().to_vec(), Some(i.to_be_bytes().to_vec())))
.collect();

db.commit(payload.clone()).unwrap();
assert!(db.iter(1).is_ok());
}
}
8 changes: 8 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,14 @@ impl IndexTable {

#[cfg(not(unix))]
fn madvise_random(&self, _map: &mut memmap2::MmapMut) {}

pub(crate) fn drop_files(&self) -> Result<()> {
*self.map.write() = None;
if self.path.exists() {
try_io!(std::fs::remove_file(&self.path));
}
Ok(())
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ impl Log {
Ok(false)
}

pub fn replay_next(&mut self) -> Result<Option<u32>> {
pub fn replay_next(&self) -> Result<Option<u32>> {
let mut reading = self.reading.write();
{
if let Some(reading) = reading.take() {
Expand Down
8 changes: 8 additions & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,14 @@ impl ValueTable {
}
Ok(len)
}

pub(crate) fn drop_files(&self) -> Result<()> {
*self.file.file.write() = None;
if self.file.path.exists() {
try_io!(std::fs::remove_file(&self.file.path));
}
Ok(())
}
}

pub mod key {
Expand Down