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
//! This provides reference-counted abstractions around table properties
//! collections. It is used by tikv in its own engine abstractions, to avoid the
//! complexities of lifetimes in associated types.
//!
//! FIXME: Safety - While this does guarantee that all the types created from
//! the collection stay valid for the lifetime of the collection, it doesn't
//! guarantee that the _DB_ stays valid for the lifetime of the collection.

use crocksdb_ffi::{DBTablePropertiesCollection, DBTableProperty};
use libc::size_t;
use librocksdb_sys as crocksdb_ffi;
use std::ops::Deref;
use std::slice;
use std::str;

use crate::table_properties_rc_handles::{
    TablePropertiesCollectionHandle, TablePropertiesCollectionIteratorHandle,
    TablePropertiesHandle, UserCollectedPropertiesHandle,
};

pub struct TablePropertiesCollection {
    handle: TablePropertiesCollectionHandle,
}

impl TablePropertiesCollection {
    pub unsafe fn new(ptr: *mut DBTablePropertiesCollection) -> TablePropertiesCollection {
        assert!(!ptr.is_null());
        TablePropertiesCollection {
            handle: TablePropertiesCollectionHandle::new(ptr),
        }
    }

    pub fn iter(&self) -> TablePropertiesCollectionIter {
        TablePropertiesCollectionIter::new(self.handle.clone())
    }

    pub fn len(&self) -> usize {
        unsafe { crocksdb_ffi::crocksdb_table_properties_collection_len(self.handle.ptr()) }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

pub struct TablePropertiesCollectionIter {
    handle: TablePropertiesCollectionIteratorHandle,
}

impl TablePropertiesCollectionIter {
    fn new(collection: TablePropertiesCollectionHandle) -> TablePropertiesCollectionIter {
        TablePropertiesCollectionIter {
            handle: TablePropertiesCollectionIteratorHandle::new(collection),
        }
    }
}

impl Iterator for TablePropertiesCollectionIter {
    type Item = (TablePropertiesKey, TableProperties);

    fn next(&mut self) -> Option<Self::Item> {
        unsafe {
            loop {
                if !crocksdb_ffi::crocksdb_table_properties_collection_iter_valid(self.handle.ptr())
                {
                    return None;
                }

                let mut keylen: size_t = 0;
                let key = crocksdb_ffi::crocksdb_table_properties_collection_iter_key(
                    self.handle.ptr(),
                    &mut keylen,
                );
                let props = crocksdb_ffi::crocksdb_table_properties_collection_iter_value(
                    self.handle.ptr(),
                );
                crocksdb_ffi::crocksdb_table_properties_collection_iter_next(self.handle.ptr());
                if !props.is_null() {
                    assert!(!key.is_null() && keylen != 0);
                    let key = TablePropertiesKey::new(key, keylen, self.handle.clone());
                    let props_handle = TablePropertiesHandle::new(props, self.handle.clone());
                    let val = TableProperties::new(props_handle);
                    return Some((key, val));
                }
            }
        }
    }
}

// # Safety
//
// The underlying iterator is over an unordered map of heap-allocated strings,
// so as long as the iterator and collection are alive, the key pointers are
// valid.
pub struct TablePropertiesKey {
    key: *const u8,
    keylen: size_t,
    _iter_handle: TablePropertiesCollectionIteratorHandle,
}

impl TablePropertiesKey {
    fn new(
        key: *const u8,
        keylen: size_t,
        _iter_handle: TablePropertiesCollectionIteratorHandle,
    ) -> TablePropertiesKey {
        // Caller must ensure slice is valid
        unsafe {
            let bytes = slice::from_raw_parts(key, keylen);
            assert!(str::from_utf8(bytes).is_ok());
        }
        TablePropertiesKey {
            key,
            keylen,
            _iter_handle,
        }
    }
}

impl Deref for TablePropertiesKey {
    type Target = str;

    fn deref(&self) -> &str {
        // Safety: creating slice from values reported by rocksdb, that should
        // be valid as long is this object is valid. The slice is guaranteed to
        // be UTF-8 by the constructor.
        unsafe {
            let bytes = slice::from_raw_parts(self.key, self.keylen);
            str::from_utf8_unchecked(bytes)
        }
    }
}

pub struct TableProperties {
    handle: TablePropertiesHandle,
}

impl TableProperties {
    fn new(handle: TablePropertiesHandle) -> TableProperties {
        TableProperties { handle }
    }

    fn get_u64(&self, prop: DBTableProperty) -> u64 {
        unsafe { crocksdb_ffi::crocksdb_table_properties_get_u64(self.handle.ptr(), prop) }
    }

    pub fn num_entries(&self) -> u64 {
        self.get_u64(DBTableProperty::NumEntries)
    }

    pub fn user_collected_properties(&self) -> UserCollectedProperties {
        UserCollectedProperties::new(self.handle.clone())
    }
}

pub struct UserCollectedProperties {
    handle: UserCollectedPropertiesHandle,
}

impl UserCollectedProperties {
    fn new(table_props_handle: TablePropertiesHandle) -> UserCollectedProperties {
        UserCollectedProperties {
            handle: UserCollectedPropertiesHandle::new(table_props_handle),
        }
    }

    pub fn get<Q: AsRef<[u8]>>(&self, index: Q) -> Option<&[u8]> {
        let bytes = index.as_ref();
        let mut size = 0;
        unsafe {
            let ptr = crocksdb_ffi::crocksdb_user_collected_properties_get(
                self.handle.ptr(),
                bytes.as_ptr(),
                bytes.len(),
                &mut size,
            );
            if ptr.is_null() {
                return None;
            }
            Some(slice::from_raw_parts(ptr, size))
        }
    }

    pub fn len(&self) -> usize {
        unsafe { crocksdb_ffi::crocksdb_user_collected_properties_len(self.handle.ptr()) }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}