|
| 1 | +// Copyright Kamu Data, Inc. and contributors. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the LICENSE file. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0. |
| 9 | + |
| 10 | +use std::sync::Arc; |
| 11 | + |
| 12 | +use kamu::domain::compact_service::CompactService; |
| 13 | +use kamu::domain::{ |
| 14 | + DatasetRepository, |
| 15 | + VerificationMultiListener, |
| 16 | + VerificationOptions, |
| 17 | + VerificationService, |
| 18 | +}; |
| 19 | +use opendatafabric::{DatasetHandle, DatasetRef}; |
| 20 | + |
| 21 | +use crate::{CLIError, Command, CompactionMultiProgress, VerificationMultiProgress}; |
| 22 | + |
| 23 | +pub struct CompactCommand { |
| 24 | + dataset_repo: Arc<dyn DatasetRepository>, |
| 25 | + verification_svc: Arc<dyn VerificationService>, |
| 26 | + compact_svc: Arc<dyn CompactService>, |
| 27 | + dataset_ref: DatasetRef, |
| 28 | + max_slice_size: u64, |
| 29 | + max_slice_records: u64, |
| 30 | + is_hard: bool, |
| 31 | + is_verify: bool, |
| 32 | +} |
| 33 | + |
| 34 | +impl CompactCommand { |
| 35 | + pub fn new( |
| 36 | + dataset_repo: Arc<dyn DatasetRepository>, |
| 37 | + verification_svc: Arc<dyn VerificationService>, |
| 38 | + compact_svc: Arc<dyn CompactService>, |
| 39 | + dataset_ref: DatasetRef, |
| 40 | + max_slice_size: u64, |
| 41 | + max_slice_records: u64, |
| 42 | + is_hard: bool, |
| 43 | + is_verify: bool, |
| 44 | + ) -> Self { |
| 45 | + Self { |
| 46 | + dataset_repo, |
| 47 | + verification_svc, |
| 48 | + compact_svc, |
| 49 | + dataset_ref, |
| 50 | + max_slice_size, |
| 51 | + max_slice_records, |
| 52 | + is_hard, |
| 53 | + is_verify, |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + async fn verify_dataset(&self, dataset_handle: &DatasetHandle) -> Result<(), CLIError> { |
| 58 | + let progress = VerificationMultiProgress::new(); |
| 59 | + let listener = Arc::new(progress.clone()); |
| 60 | + let draw_thread = std::thread::spawn(move || { |
| 61 | + progress.draw(); |
| 62 | + }); |
| 63 | + |
| 64 | + let result = self |
| 65 | + .verification_svc |
| 66 | + .verify( |
| 67 | + &dataset_handle.as_local_ref(), |
| 68 | + (None, None), |
| 69 | + VerificationOptions::default(), |
| 70 | + listener.begin_verify(dataset_handle), |
| 71 | + ) |
| 72 | + .await; |
| 73 | + |
| 74 | + listener.finish(); |
| 75 | + draw_thread.join().unwrap(); |
| 76 | + |
| 77 | + result.outcome.map_err(CLIError::failure) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[async_trait::async_trait(?Send)] |
| 82 | +impl Command for CompactCommand { |
| 83 | + async fn run(&mut self) -> Result<(), CLIError> { |
| 84 | + if !self.is_hard { |
| 85 | + return Err(CLIError::usage_error( |
| 86 | + "Soft compactions are not yet supported", |
| 87 | + )); |
| 88 | + } |
| 89 | + let dataset_handle = self |
| 90 | + .dataset_repo |
| 91 | + .resolve_dataset_ref(&self.dataset_ref) |
| 92 | + .await |
| 93 | + .map_err(CLIError::failure)?; |
| 94 | + |
| 95 | + if self.is_verify { |
| 96 | + if let Err(err) = self.verify_dataset(&dataset_handle).await { |
| 97 | + eprintln!( |
| 98 | + "{}", |
| 99 | + console::style("Cannot perform compacting, dataset is invalid".to_string()) |
| 100 | + .red() |
| 101 | + ); |
| 102 | + return Err(err); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + let progress = CompactionMultiProgress::new(); |
| 107 | + let listener = Arc::new(progress.clone()); |
| 108 | + |
| 109 | + let draw_thread = std::thread::spawn(move || { |
| 110 | + progress.draw(); |
| 111 | + }); |
| 112 | + |
| 113 | + self.compact_svc |
| 114 | + .compact_dataset( |
| 115 | + &dataset_handle, |
| 116 | + self.max_slice_size, |
| 117 | + self.max_slice_records, |
| 118 | + Some(listener.clone()), |
| 119 | + ) |
| 120 | + .await |
| 121 | + .map_err(CLIError::failure)?; |
| 122 | + |
| 123 | + listener.finish(); |
| 124 | + draw_thread.join().unwrap(); |
| 125 | + |
| 126 | + Ok(()) |
| 127 | + } |
| 128 | +} |
0 commit comments