mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-04 12:02:55 +00:00
Updates storage components to edition 2024. We like to stay on the latest edition if possible. There is no functional changes, however some code changes had to be done to accommodate the edition's breaking changes. The PR has two commits: * the first commit updates storage crates to edition 2024 and appeases `cargo clippy` by changing code. i have accidentially ran the formatter on some files that had other edits. * the second commit performs a `cargo fmt` I would recommend a closer review of the first commit and a less close review of the second one (as it just runs `cargo fmt`). part of https://github.com/neondatabase/neon/issues/10918
74 lines
2.1 KiB
Rust
74 lines
2.1 KiB
Rust
use std::ffi::CStr;
|
|
use std::sync::Arc;
|
|
|
|
use parking_lot::{Mutex, MutexGuard};
|
|
use postgres_ffi::v16::wal_generator::{LogicalMessageGenerator, WalGenerator};
|
|
use utils::lsn::Lsn;
|
|
|
|
use super::block_storage::BlockStorage;
|
|
|
|
/// Simulation implementation of walproposer WAL storage.
|
|
pub struct DiskWalProposer {
|
|
state: Mutex<State>,
|
|
}
|
|
|
|
impl DiskWalProposer {
|
|
pub fn new() -> Arc<DiskWalProposer> {
|
|
Arc::new(DiskWalProposer {
|
|
state: Mutex::new(State {
|
|
internal_available_lsn: Lsn(0),
|
|
prev_lsn: Lsn(0),
|
|
disk: BlockStorage::new(),
|
|
wal_generator: WalGenerator::new(LogicalMessageGenerator::new(c"", &[]), Lsn(0)),
|
|
}),
|
|
})
|
|
}
|
|
|
|
pub fn lock(&self) -> MutexGuard<State> {
|
|
self.state.lock()
|
|
}
|
|
}
|
|
|
|
pub struct State {
|
|
// flush_lsn
|
|
internal_available_lsn: Lsn,
|
|
// needed for WAL generation
|
|
prev_lsn: Lsn,
|
|
// actual WAL storage
|
|
disk: BlockStorage,
|
|
// WAL record generator
|
|
wal_generator: WalGenerator<LogicalMessageGenerator>,
|
|
}
|
|
|
|
impl State {
|
|
pub fn read(&self, pos: u64, buf: &mut [u8]) {
|
|
self.disk.read(pos, buf);
|
|
// TODO: fail on reading uninitialized data
|
|
}
|
|
|
|
pub fn write(&mut self, pos: u64, buf: &[u8]) {
|
|
self.disk.write(pos, buf);
|
|
}
|
|
|
|
/// Update the internal available LSN to the given value.
|
|
pub fn reset_to(&mut self, lsn: Lsn) {
|
|
self.internal_available_lsn = lsn;
|
|
self.prev_lsn = Lsn(0); // Safekeeper doesn't care if this is omitted
|
|
self.wal_generator.lsn = self.internal_available_lsn;
|
|
self.wal_generator.prev_lsn = self.prev_lsn;
|
|
}
|
|
|
|
/// Get current LSN.
|
|
pub fn flush_rec_ptr(&self) -> Lsn {
|
|
self.internal_available_lsn
|
|
}
|
|
|
|
/// Inserts a logical record in the WAL at the current LSN.
|
|
pub fn insert_logical_message(&mut self, prefix: &CStr, msg: &[u8]) {
|
|
let (_, record) = self.wal_generator.append_logical_message(prefix, msg);
|
|
self.disk.write(self.internal_available_lsn.into(), &record);
|
|
self.prev_lsn = self.internal_available_lsn;
|
|
self.internal_available_lsn += record.len() as u64;
|
|
}
|
|
}
|