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
//! `CompactionJobStats` used by event listener callback.
use rocks_sys as ll;

use std::fmt;
use std::slice;

use crate::to_raw::FromRaw;

pub const MAX_PREFIX_LENGTH: usize = 8;


#[repr(C)]
pub struct CompactionJobStats {
    raw: *mut ll::rocks_compaction_job_stats_t,
}

impl FromRaw<ll::rocks_compaction_job_stats_t> for CompactionJobStats {
    unsafe fn from_ll(raw: *mut ll::rocks_compaction_job_stats_t) -> Self {
        CompactionJobStats { raw: raw }
    }
}

impl fmt::Debug for CompactionJobStats {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("CompactionJobStats")
            .field("elapsed_micros", &self.elapsed_micros())
            .field("num_input_records", &self.num_input_records())
            .field("num_input_files", &self.num_input_files())
            .field("num_input_files_at_output_level", &self.num_input_files_at_output_level())
            .field("num_output_records", &self.num_output_records())
            .field("num_output_files", &self.num_output_files())
            .field("is_manual_compaction", &self.is_manual_compaction())
            .field("total_input_bytes", &self.total_input_bytes())
            .field("total_output_bytes", &self.total_output_bytes())
            .field("num_records_replaced", &self.num_records_replaced())
            .field("total_input_raw_key_bytes", &self.total_input_raw_key_bytes())
            .field("total_input_raw_value_bytes", &self.total_input_raw_value_bytes())
            .field("num_input_deletion_records", &self.num_input_deletion_records())
            .field("num_expired_deletion_records", &self.num_expired_deletion_records())
            .field("num_corrupt_keys", &self.num_corrupt_keys())
            .finish()
    }
}

impl CompactionJobStats {
    /// the elapsed time in micro of this compaction.
    pub fn elapsed_micros(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_elapsed_micros(self.raw) }
    }

    /// the number of compaction input records.
    pub fn num_input_records(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_input_records(self.raw) }
    }

    /// the number of compaction input files.
    pub fn num_input_files(&self) -> usize {
        unsafe { ll::rocks_compaction_job_stats_get_num_input_files(self.raw) }
    }

    /// the number of compaction input files at the output level.
    pub fn num_input_files_at_output_level(&self) -> usize {
        unsafe { ll::rocks_compaction_job_stats_get_num_input_files_at_output_level(self.raw) }
    }

    /// the number of compaction output records.
    pub fn num_output_records(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_output_records(self.raw) }
    }

    /// the number of compaction output files.
    pub fn num_output_files(&self) -> usize {
        unsafe { ll::rocks_compaction_job_stats_get_num_output_files(self.raw) }
    }

    /// true if the compaction is a manual compaction
    pub fn is_manual_compaction(&self) -> bool {
        unsafe { ll::rocks_compaction_job_stats_get_is_manual_compaction(self.raw) != 0 }
    }

    /// the size of the compaction input in bytes.
    pub fn total_input_bytes(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_total_input_bytes(self.raw) }
    }

    /// the size of the compaction output in bytes.
    pub fn total_output_bytes(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_total_output_bytes(self.raw) }
    }

    /// number of records being replaced by newer record associated with same key.
    /// this could be a new value or a deletion entry for that key so this field
    /// sums up all updated and deleted keys
    pub fn num_records_replaced(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_records_replaced(self.raw) }
    }

    /// the sum of the uncompressed input keys in bytes.
    pub fn total_input_raw_key_bytes(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_total_input_raw_key_bytes(self.raw) }
    }

    /// the sum of the uncompressed input values in bytes.
    pub fn total_input_raw_value_bytes(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_total_input_raw_value_bytes(self.raw) }
    }

    /// the number of deletion entries before compaction. Deletion entries
    /// can disappear after compaction because they expired
    pub fn num_input_deletion_records(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_input_deletion_records(self.raw) }
    }

    /// number of deletion records that were found obsolete and discarded
    /// because it is not possible to delete any more keys with this entry
    /// (i.e. all possible deletions resulting from it have been completed)
    pub fn num_expired_deletion_records(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_expired_deletion_records(self.raw) }
    }

    /// number of corrupt keys (ParseInternalKey returned false when applied to
    /// the key) encountered and written out.
    pub fn num_corrupt_keys(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_corrupt_keys(self.raw) }
    }

    /// Following counters are only populated if
    /// options.report_bg_io_stats = true;

    /// Time spent on file's Append() call.
    pub fn file_write_nanos(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_file_write_nanos(self.raw) }
    }
    /// Time spent on sync file range.
    pub fn file_range_sync_nanos(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_file_range_sync_nanos(self.raw) }
    }

    /// Time spent on file fsync.
    pub fn file_fsync_nanos(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_file_fsync_nanos(self.raw) }
    }

    /// Time spent on preparing file write (falocate, etc)
    pub fn file_prepare_write_nanos(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_file_prepare_write_nanos(self.raw) }
    }

    /// 0-terminated strings storing the first 8 bytes of the smallest and
    /// largest key in the output.
    pub fn smallest_output_key_prefix(&self) -> &[u8] {
        let mut len = 0;
        unsafe {
            let ptr = ll::rocks_compaction_job_stats_get_smallest_output_key_prefix(self.raw, &mut len);
            slice::from_raw_parts(ptr as *const u8, len)
        }
    }
    pub fn largest_output_key_prefix(&self) -> &[u8] {
        let mut len = 0;
        unsafe {
            let ptr = ll::rocks_compaction_job_stats_get_largest_output_key_prefix(self.raw, &mut len);
            slice::from_raw_parts(ptr as *const u8, len)
        }
    }

    /// number of single-deletes which do not meet a put
    pub fn num_single_del_fallthru(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_single_del_fallthru(self.raw) }
    }

    /// number of single-deletes which meet something other than a put
    pub fn num_single_del_mismatch(&self) -> u64 {
        unsafe { ll::rocks_compaction_job_stats_get_num_single_del_mismatch(self.raw) }
    }
}