lsm_tree/segment/
inner.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5use super::{block_index::BlockIndexImpl, file_offsets::FileOffsets, meta::Metadata};
6use crate::{cache::Cache, descriptor_table::FileDescriptorTable, tree::inner::TreeId};
7use std::{
8    path::PathBuf,
9    sync::{atomic::AtomicBool, Arc},
10};
11
12pub struct Inner {
13    pub path: PathBuf,
14
15    pub(crate) tree_id: TreeId,
16
17    #[doc(hidden)]
18    pub descriptor_table: Arc<FileDescriptorTable>,
19
20    /// Segment metadata object
21    #[doc(hidden)]
22    pub metadata: Metadata,
23
24    pub(crate) offsets: FileOffsets,
25
26    /// Translates key (first item of a block) to block offset (address inside file) and (compressed) size
27    #[doc(hidden)]
28    pub block_index: Arc<BlockIndexImpl>,
29
30    /// Block cache
31    ///
32    /// Stores index and data blocks
33    #[doc(hidden)]
34    pub cache: Arc<Cache>,
35
36    /// Bloom filter
37    #[doc(hidden)]
38    pub bloom_filter: Option<crate::bloom::BloomFilter>,
39
40    pub is_deleted: AtomicBool,
41}
42
43impl Drop for Inner {
44    fn drop(&mut self) {
45        let global_id = (self.tree_id, self.metadata.id).into();
46
47        if self.is_deleted.load(std::sync::atomic::Ordering::Acquire) {
48            if let Err(e) = std::fs::remove_file(&self.path) {
49                log::warn!(
50                    "Failed to cleanup deleted segment {global_id:?} at {:?}: {e:?}",
51                    self.path,
52                );
53            }
54
55            log::trace!("Closing file handles of deleted segment file {global_id:?}");
56            self.descriptor_table.remove(global_id);
57        }
58    }
59}