Typed database table.
| 102 | |
| 103 | /// Typed database table. |
| 104 | pub trait Table: RawTable + Sized + From<rocksdb::DB> { |
| 105 | /// Key format. |
| 106 | type Key: TableKey; |
| 107 | |
| 108 | /// Value format. |
| 109 | type Value: rkyv::Archive + rkyv::Serialize<AllocSerializer<4096>> + 'static; |
| 110 | |
| 111 | /// Defines the table's merge behavior. |
| 112 | const MERGE_OP: MergeOperator<Self> = MergeOperator::Default; |
| 113 | |
| 114 | /// Defines the table's storage optimization. |
| 115 | const STORAGE_OPT: StorageOpt = StorageOpt::RandomAccess; |
| 116 | |
| 117 | /// LRU cache size for this table. Set to 0 to disable caching. |
| 118 | const CACHE_SIZE: usize = 16384; |
| 119 | |
| 120 | /// Removes the record with the given key from the table. |
| 121 | fn remove(&self, key: Self::Key) { |
| 122 | let key_raw = key.into_raw(); |
| 123 | |
| 124 | // Remove from cache |
| 125 | if Self::CACHE_SIZE > 0 { |
| 126 | let mut cache = self.cache().lock().unwrap(); |
| 127 | cache.pop(key_raw.as_ref()); |
| 128 | } |
| 129 | |
| 130 | self.raw().delete(key_raw).unwrap(); |
| 131 | } |
| 132 | |
| 133 | /// Inserts the given value at the given key. |
| 134 | /// |
| 135 | /// If the record already exists, the previous value is replaced. |
| 136 | fn insert(&self, key: Self::Key, value: Self::Value) { |
| 137 | let key_raw = key.into_raw(); |
| 138 | let value_bytes = rkyv::to_bytes(&value).unwrap(); |
| 139 | |
| 140 | // Update cache |
| 141 | if Self::CACHE_SIZE > 0 { |
| 142 | let mut cache = self.cache().lock().unwrap(); |
| 143 | cache.put(key_raw.as_ref().to_vec(), value_bytes.to_vec()); |
| 144 | } |
| 145 | |
| 146 | match Self::MERGE_OP { |
| 147 | MergeOperator::Default => self.raw().put(key_raw, value_bytes).unwrap(), |
| 148 | MergeOperator::Associative(_) => self.raw().merge(key_raw, value_bytes).unwrap(), |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// Create a new insertion batch. |
| 153 | fn batched_insert(&self) -> InsertionBatch<'_, Self> { |
| 154 | InsertionBatch(self, rocksdb::WriteBatch::default()) |
| 155 | } |
| 156 | |
| 157 | /// Get the value at the given key. |
| 158 | /// |
| 159 | /// Returns `None` if the key isn't present. |
| 160 | fn get(&self, key: Self::Key) -> Option<TableValueRef<Self::Value, SmallVec<[u8; 64]>>> { |
| 161 | let key_raw = key.into_raw(); |
no outgoing calls
no test coverage detected