| 161 | } |
| 162 | |
| 163 | fn compact_file(path: &Path) -> anyhow::Result<CompactDatabaseResponse> { |
| 164 | let readonly = redb::Builder::new().open_read_only(path)?; |
| 165 | let txn = readonly.begin_read()?; |
| 166 | atomic_core::pristine::schema::check_schema_version(&txn)?; |
| 167 | atomic_core::pristine::schema::check_required_capabilities(&txn)?; |
| 168 | drop(txn); |
| 169 | drop(readonly); |
| 170 | let mut database = redb::Builder::new().open(path)?; |
| 171 | let txn = database.begin_read()?; |
| 172 | atomic_core::pristine::schema::check_schema_version(&txn)?; |
| 173 | atomic_core::pristine::schema::check_required_capabilities(&txn)?; |
| 174 | drop(txn); |
| 175 | let before_bytes = std::fs::metadata(path)?.len(); |
| 176 | database.compact()?; |
| 177 | // Closing persists allocator metadata; include that in the final size. |
| 178 | drop(database); |
| 179 | let after_bytes = std::fs::metadata(path)?.len(); |
| 180 | Ok(CompactDatabaseResponse { |
| 181 | database: path.to_string_lossy().into_owned(), |
| 182 | before_bytes, |
| 183 | after_bytes, |
| 184 | reclaimed_bytes: before_bytes.saturating_sub(after_bytes), |
| 185 | meta: None, |
| 186 | }) |
| 187 | } |
| 188 | |
| 189 | fn with_request_id(status: Status, request_id: &str) -> Status { |
| 190 | let code = match status.code() { |