1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
pub mod block;
pub mod block_index;
pub mod data_block_handle_queue;
pub mod id;
pub mod index_block_consumer;
pub mod meta;
pub mod multi_reader;
pub mod multi_writer;
pub mod prefix;
pub mod range;
pub mod reader;
pub mod trailer;
pub mod value_block;
pub mod writer;

use self::{
    block_index::BlockIndex, prefix::PrefixedReader, range::Range, reader::Reader,
    trailer::SegmentFileTrailer,
};
use crate::{
    block_cache::BlockCache,
    descriptor_table::FileDescriptorTable,
    segment::value_block::ValueBlock,
    tree_inner::TreeId,
    value::{SeqNo, UserKey},
    Value,
};
use std::{ops::Bound, path::Path, sync::Arc};

#[cfg(feature = "bloom")]
use crate::bloom::BloomFilter;

/// Disk segment (a.k.a. `SSTable`, `SST`, `sorted string table`) that is located on disk
///
/// A segment is an immutable list of key-value pairs, split into compressed blocks.
/// A reference to the block (`block handle`) is saved in the "block index".
///
/// Deleted entries are represented by tombstones.
///
/// Segments can be merged together to remove duplicate items, reducing disk space and improving read performance.
#[doc(alias = "sstable")]
pub struct Segment {
    pub(crate) tree_id: TreeId,

    #[doc(hidden)]
    pub descriptor_table: Arc<FileDescriptorTable>,

    /// Segment metadata object
    pub metadata: meta::Metadata,

    /// Translates key (first item of a block) to block offset (address inside file) and (compressed) size
    #[doc(hidden)]
    pub block_index: Arc<BlockIndex>,

    /// Block cache
    ///
    /// Stores index and data blocks
    #[doc(hidden)]
    pub block_cache: Arc<BlockCache>,

    /// Bloom filter
    #[cfg(feature = "bloom")]
    #[doc(hidden)]
    pub bloom_filter: BloomFilter,
}

impl std::fmt::Debug for Segment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Segment:{}", self.metadata.id)
    }
}

impl Segment {
    pub(crate) fn verify(&self) -> crate::Result<usize> {
        use crate::segment::block_index::IndexBlock;

        let mut count = 0;
        let mut broken_count = 0;

        let guard = self
            .descriptor_table
            .access(&(self.tree_id, self.metadata.id).into())?
            .expect("should have gotten file");

        let mut f = guard.file.lock().expect("lock is poisoned");

        for handle in self.block_index.top_level_index.data.iter() {
            let block = IndexBlock::from_file_compressed(&mut *f, handle.offset)?;

            for handle in &*block.items {
                let value_block = match ValueBlock::from_file_compressed(&mut *f, handle.offset) {
                    Ok(v) => v,
                    Err(e) => {
                        log::error!(
                            "{handle:?} could not be loaded, it is probably corrupted: {e:?}"
                        );
                        broken_count += 1;
                        count += 1;
                        continue;
                    }
                };

                let expected_crc = value_block.header.crc;
                let actual_crc = ValueBlock::create_crc(&value_block.items)?;

                if expected_crc != actual_crc {
                    log::error!("{handle:?} is corrupt, invalid CRC value");
                    broken_count += 1;
                }

                count += 1;

                if broken_count % 10_000 == 0 {
                    log::info!("Checked {count} data blocks");
                }
            }
        }

        assert_eq!(count, self.metadata.block_count);

        Ok(broken_count)
    }

    /// Tries to recover a segment from a file.
    pub fn recover<P: AsRef<Path>>(
        file_path: P,
        tree_id: TreeId,
        block_cache: Arc<BlockCache>,
        descriptor_table: Arc<FileDescriptorTable>,
    ) -> crate::Result<Self> {
        let file_path = file_path.as_ref();

        let trailer = SegmentFileTrailer::from_file(file_path)?;

        let block_index = BlockIndex::from_file(
            file_path,
            trailer.offsets.tli_ptr,
            (tree_id, trailer.metadata.id).into(),
            descriptor_table.clone(),
            Arc::clone(&block_cache),
        )?;

        Ok(Self {
            tree_id,

            descriptor_table,
            metadata: trailer.metadata,
            block_index: Arc::new(block_index),
            block_cache,

            // TODO: as Bloom method
            #[cfg(feature = "bloom")]
            bloom_filter: {
                use crate::serde::Deserializable;
                use std::io::Seek;

                assert!(
                    trailer.offsets.bloom_ptr > 0,
                    "can not find bloom filter block"
                );

                let mut reader = std::fs::File::open(file_path)?;
                reader.seek(std::io::SeekFrom::Start(trailer.offsets.bloom_ptr))?;
                BloomFilter::deserialize(&mut reader)?
            },
        })
    }

    #[cfg(feature = "bloom")]
    #[must_use]
    /// Gets the bloom filter size
    pub fn bloom_filter_size(&self) -> usize {
        self.bloom_filter.len()
    }

    /// Retrieves an item from the segment.
    ///
    /// # Errors
    ///
    /// Will return `Err` if an IO error occurs.
    pub fn get<K: AsRef<[u8]>>(
        &self,
        key: K,
        seqno: Option<SeqNo>,
    ) -> crate::Result<Option<Value>> {
        use value_block::CachePolicy;

        if let Some(seqno) = seqno {
            if self.metadata.seqnos.0 >= seqno {
                return Ok(None);
            }
        }

        if !self.metadata.key_range.contains_key(&key) {
            return Ok(None);
        }

        let key = key.as_ref();

        #[cfg(feature = "bloom")]
        {
            if !self.bloom_filter.contains(key) {
                return Ok(None);
            }
        }

        if seqno.is_none() {
            // NOTE: Fastpath for non-seqno reads (which are most common)
            // This avoids setting up a rather expensive block iterator
            // (see explanation for that below)
            // This only really works because sequence numbers are sorted
            // in descending order

            if let Some(data_block_handle) = self
                .block_index
                .get_lowest_data_block_handle_containing_item(key.as_ref(), CachePolicy::Write)?
            {
                let block = ValueBlock::load_by_block_handle(
                    &self.descriptor_table,
                    &self.block_cache,
                    (self.tree_id, self.metadata.id).into(),
                    &data_block_handle,
                    CachePolicy::Write,
                )?;

                let item = block.map_or_else(
                    || Ok(None),
                    |block| {
                        // TODO: maybe binary search can be used, but it needs to find the max seqno
                        // TODO: if so, implement in ValueBlock
                        Ok(block
                            .items
                            .iter()
                            .find(|item| item.key == key.as_ref().into())
                            .cloned())
                    },
                );

                return item;
            }
        }

        // NOTE: For finding a specific seqno,
        // we need to use a reader
        // because nothing really prevents the version
        // we are searching for to be in the next block
        // after the one our key starts in
        //
        // Example (key:seqno), searching for a:2:
        //
        // [..., a:5, a:4] [a:3, a:2, b: 4, b:3]
        // ^               ^
        // Block A         Block B
        //
        // Based on get_lower_bound_block, "a" is in Block A
        // However, we are searching for A with seqno 2, which
        // unfortunately is in the next block

        let iter = Reader::new(
            self.descriptor_table.clone(),
            (self.tree_id, self.metadata.id).into(),
            self.block_cache.clone(),
            self.block_index.clone(),
        )
        .set_lower_bound(key.into());

        for item in iter {
            let item = item?;

            // Just stop iterating once we go past our desired key
            if &*item.key != key {
                return Ok(None);
            }

            if let Some(seqno) = seqno {
                if item.seqno < seqno {
                    return Ok(Some(item));
                }
            } else {
                return Ok(Some(item));
            }
        }

        Ok(None)
    }

    /// Creates an iterator over the `Segment`.
    ///
    /// # Errors
    ///
    /// Will return `Err` if an IO error occurs.
    #[must_use]
    #[allow(clippy::iter_without_into_iter)]
    pub fn iter(&self) -> Reader {
        Reader::new(
            Arc::clone(&self.descriptor_table),
            (self.tree_id, self.metadata.id).into(),
            Arc::clone(&self.block_cache),
            Arc::clone(&self.block_index),
        )
    }

    /// Creates a ranged iterator over the `Segment`.
    ///
    /// # Errors
    ///
    /// Will return `Err` if an IO error occurs.
    #[must_use]
    pub fn range(&self, range: (Bound<UserKey>, Bound<UserKey>)) -> Range {
        Range::new(
            Arc::clone(&self.descriptor_table),
            (self.tree_id, self.metadata.id).into(),
            Arc::clone(&self.block_cache),
            Arc::clone(&self.block_index),
            range,
        )
    }

    /// Creates a prefixed iterator over the `Segment`.
    ///
    /// # Errors
    ///
    /// Will return `Err` if an IO error occurs.
    #[must_use]
    pub fn prefix<K: Into<UserKey>>(&self, prefix: K) -> PrefixedReader {
        PrefixedReader::new(
            Arc::clone(&self.descriptor_table),
            (self.tree_id, self.metadata.id).into(),
            Arc::clone(&self.block_cache),
            Arc::clone(&self.block_index),
            prefix,
        )
    }

    /// Returns the highest sequence number in the segment.
    #[must_use]
    pub fn get_lsn(&self) -> SeqNo {
        self.metadata.seqnos.1
    }

    /// Returns the amount of tombstone markers in the `Segment`.
    #[must_use]
    pub fn tombstone_count(&self) -> u64 {
        self.metadata.tombstone_count
    }

    /// Checks if a key range is (partially or fully) contained in this segment.
    pub(crate) fn check_key_range_overlap(
        &self,
        bounds: &(Bound<UserKey>, Bound<UserKey>),
    ) -> bool {
        self.metadata.key_range.overlaps_with_bounds(bounds)
    }
}