Skip to content

Commit 6f3a29d

Browse files
committed
add page size to segment header
1 parent 0048de9 commit 6f3a29d

File tree

6 files changed

+41
-6
lines changed

6 files changed

+41
-6
lines changed

libsql-wal/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub enum Error {
1515
InvalidHeaderMagic,
1616
#[error("invalid segment header version")]
1717
InvalidHeaderVersion,
18+
#[error("Invalid page size, only 4095 is supported")]
19+
InvalidPageSize,
1820
}
1921

2022
impl Into<libsql_sys::ffi::Error> for Error {

libsql-wal/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub mod transaction;
1212
pub mod wal;
1313

1414
const LIBSQL_MAGIC: u64 = u64::from_be_bytes(*b"LIBSQL\0\0");
15+
const LIBSQL_PAGE_SIZE: u16 = 4096;
16+
const LIBSQL_WAL_VERSION: u16 = 1;
1517

1618
#[cfg(any(debug_assertions, test))]
1719
pub mod test {

libsql-wal/src/segment/compacted.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use zerocopy::{AsBytes, FromBytes, FromZeroes};
66

77
use crate::io::buf::{ZeroCopyBoxIoBuf, ZeroCopyBuf};
88
use crate::io::FileExt;
9+
use crate::{LIBSQL_MAGIC, LIBSQL_PAGE_SIZE, LIBSQL_WAL_VERSION};
910

1011
use super::{Frame, Result};
1112

@@ -19,6 +20,25 @@ pub struct CompactedSegmentDataHeader {
1920
pub(crate) start_frame_no: lu64,
2021
pub(crate) end_frame_no: lu64,
2122
pub(crate) size_after: lu32,
23+
/// for now, always 4096
24+
pub(crate) page_size: lu16,
25+
}
26+
impl CompactedSegmentDataHeader {
27+
fn check(&self) -> Result<()> {
28+
if self.magic.get() != LIBSQL_MAGIC {
29+
return Err(super::Error::InvalidHeaderMagic);
30+
}
31+
32+
if self.page_size.get() != LIBSQL_PAGE_SIZE {
33+
return Err(super::Error::InvalidPageSize);
34+
}
35+
36+
if self.version.get() != LIBSQL_WAL_VERSION {
37+
return Err(super::Error::InvalidPageSize);
38+
}
39+
40+
Ok(())
41+
}
2242
}
2343

2444
#[derive(Debug, AsBytes, FromZeroes, FromBytes)]
@@ -37,7 +57,8 @@ impl<F: FileExt> CompactedSegment<F> {
3757
let buf = ZeroCopyBuf::new_uninit();
3858
let (buf, ret) = file.read_exact_at_async(buf, 0).await;
3959
ret?;
40-
let header = buf.into_inner();
60+
let header: CompactedSegmentDataHeader = buf.into_inner();
61+
header.check()?;
4162
Ok(Self { file, header })
4263
}
4364

libsql-wal/src/segment/current.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::io::Inspect;
2323
use crate::segment::{checked_frame_offset, SegmentFlags};
2424
use crate::segment::{frame_offset, page_offset, sealed::SealedSegment};
2525
use crate::transaction::{Transaction, TxGuard};
26-
use crate::LIBSQL_MAGIC;
26+
use crate::{LIBSQL_MAGIC, LIBSQL_PAGE_SIZE, LIBSQL_WAL_VERSION};
2727

2828
use super::list::SegmentList;
2929
use super::{CheckedFrame, Frame, FrameHeader, SegmentHeader};
@@ -67,8 +67,9 @@ impl<F> CurrentSegment<F> {
6767
header_cheksum: 0.into(),
6868
flags: 0.into(),
6969
magic: LIBSQL_MAGIC.into(),
70-
version: 1.into(),
70+
version: LIBSQL_WAL_VERSION.into(),
7171
salt: salt.into(),
72+
page_size: LIBSQL_PAGE_SIZE.into(),
7273
};
7374

7475
header.recompute_checksum();

libsql-wal/src/segment/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::error::{Error, Result};
2222
use crate::io::buf::IoBufMut;
2323
use crate::io::FileExt;
2424
use crate::LIBSQL_MAGIC;
25+
use crate::LIBSQL_PAGE_SIZE;
2526

2627
pub(crate) mod compacted;
2728
pub mod current;
@@ -58,6 +59,9 @@ pub struct SegmentHeader {
5859
pub flags: U32,
5960
/// salt for the segment checksum
6061
pub salt: U32,
62+
/// right now we only support 4096, but if se decided to support other sizes,
63+
/// we could do it without changing the header
64+
pub page_size: U16,
6165

6266
/// checksum of the header fields, excluding the checksum itself. This field must be the last
6367
pub header_cheksum: U32,
@@ -71,6 +75,10 @@ impl SegmentHeader {
7175
}
7276

7377
fn check(&self) -> Result<()> {
78+
if self.page_size.get() != LIBSQL_PAGE_SIZE {
79+
return Err(Error::InvalidPageSize);
80+
}
81+
7482
if self.magic.get() != LIBSQL_MAGIC {
7583
return Err(Error::InvalidHeaderChecksum);
7684
}
@@ -261,7 +269,7 @@ impl CheckedFrame {
261269
#[derive(Debug, zerocopy::AsBytes, zerocopy::FromBytes, zerocopy::FromZeroes)]
262270
pub struct Frame {
263271
header: FrameHeader,
264-
data: [u8; 4096],
272+
data: [u8; LIBSQL_PAGE_SIZE as usize],
265273
}
266274

267275
impl Frame {

libsql-wal/src/segment/sealed.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::io::buf::{IoBufMut, ZeroCopyBuf};
1717
use crate::io::file::{BufCopy, FileExt};
1818
use crate::io::Inspect;
1919
use crate::segment::{checked_frame_offset, CheckedFrame};
20-
use crate::LIBSQL_MAGIC;
20+
use crate::{LIBSQL_MAGIC, LIBSQL_WAL_VERSION};
2121

2222
use super::compacted::{CompactedSegmentDataFooter, CompactedSegmentDataHeader};
2323
use super::{frame_offset, page_offset, Frame, Segment, SegmentFlags, SegmentHeader};
@@ -91,8 +91,9 @@ where
9191
start_frame_no: self.header().start_frame_no,
9292
end_frame_no: self.header().last_commited_frame_no,
9393
size_after: self.header.size_after,
94-
version: 1.into(),
94+
version: LIBSQL_WAL_VERSION.into(),
9595
magic: LIBSQL_MAGIC.into(),
96+
page_size: self.header().page_size,
9697
};
9798

9899
hasher.update(header.as_bytes());

0 commit comments

Comments
 (0)