Skip to main content

uni_db/api/
compaction.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2024-2026 Dragonscale Team
3
4//! Compaction facade for triggering and waiting on data compaction.
5
6use uni_common::{Result, UniError};
7
8use super::UniInner;
9
10/// Facade for compaction operations.
11///
12/// Obtained via `db.compaction()`.
13pub struct Compaction<'a> {
14    pub(crate) inner: &'a UniInner,
15}
16
17impl Compaction<'_> {
18    /// Compact data for a label or edge type.
19    ///
20    /// Automatically detects whether the name refers to a label or edge type
21    /// by checking the schema.
22    pub async fn compact(&self, name: &str) -> Result<uni_store::compaction::CompactionStats> {
23        let schema = self.inner.schema.schema();
24        if schema.labels.contains_key(name) {
25            self.inner
26                .storage
27                .compact_label(name)
28                .await
29                .map_err(UniError::Internal)
30        } else if schema.edge_types.contains_key(name) {
31            self.inner
32                .storage
33                .compact_edge_type(name)
34                .await
35                .map_err(UniError::Internal)
36        } else {
37            Err(UniError::Internal(anyhow::anyhow!(
38                "No label or edge type named '{}'",
39                name
40            )))
41        }
42    }
43
44    /// Wait for all background compaction to complete.
45    pub async fn wait(&self) -> Result<()> {
46        self.inner
47            .storage
48            .wait_for_compaction()
49            .await
50            .map_err(UniError::Internal)
51    }
52}