Skip to content

Commit 5924766

Browse files
committed
write footer on checkpoint
1 parent 07dc9b5 commit 5924766

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

libsql-wal/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ const LIBSQL_MAGIC: u64 = u64::from_be_bytes(*b"LIBSQL\0\0");
1515
const LIBSQL_PAGE_SIZE: u16 = 4096;
1616
const LIBSQL_WAL_VERSION: u16 = 1;
1717

18-
use zerocopy::byteorder::big_endian::{U64 as bu64, U16 as bu16};
18+
use zerocopy::byteorder::big_endian::{U16 as bu16, U64 as bu64};
1919
/// LibsqlFooter is located at the end of the libsql file. I contains libsql specific metadata,
2020
/// while remaining fully compatible with sqlite (which just ignores that footer)
2121
///
2222
/// The fields are in big endian to remain coherent with sqlite
2323
#[derive(Copy, Clone, Debug, zerocopy::FromBytes, zerocopy::FromZeroes, zerocopy::AsBytes)]
2424
#[repr(C)]
2525
pub struct LibsqlFooter {
26-
magic: bu64,
27-
version: bu16,
26+
pub magic: bu64,
27+
pub version: bu16,
2828
/// Replication index checkpointed into this file.
2929
/// only valid if there are no outstanding segments to checkpoint, since a checkpoint could be
3030
/// partial.
31-
replication_index: bu64,
31+
pub replication_index: bu64,
3232
}
3333

3434
#[cfg(any(debug_assertions, test))]

libsql-wal/src/segment/list.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::error::Result;
1515
use crate::io::buf::{ZeroCopyBoxIoBuf, ZeroCopyBuf};
1616
use crate::io::FileExt;
1717
use crate::segment::Frame;
18+
use crate::{LibsqlFooter, LIBSQL_MAGIC, LIBSQL_PAGE_SIZE, LIBSQL_WAL_VERSION};
1819

1920
use super::Segment;
2021

@@ -157,6 +158,21 @@ where
157158
buf = read_buf.into_inner();
158159
}
159160

161+
// update the footer at the end of the db file.
162+
let footer = LibsqlFooter {
163+
magic: LIBSQL_MAGIC.into(),
164+
version: LIBSQL_WAL_VERSION.into(),
165+
replication_index: last_replication_index.into(),
166+
};
167+
168+
let footer_offset = size_after as usize * LIBSQL_PAGE_SIZE as usize;
169+
let (_, ret) = db_file
170+
.write_all_at_async(ZeroCopyBuf::new_init(footer), footer_offset as u64)
171+
.await;
172+
ret?;
173+
174+
// todo: truncate if necessary
175+
160176
//// todo: make async
161177
db_file.sync_all()?;
162178

@@ -185,7 +201,7 @@ where
185201
Ok(Some(last_replication_index))
186202
}
187203

188-
/// returnsstream pages from the sealed segment list, and what's the lowest replication index
204+
/// returns a stream of pages from the sealed segment list, and what's the lowest replication index
189205
/// that was covered. If the returned index is less than start frame_no, the missing frames
190206
/// must be read somewhere else.
191207
pub async fn stream_pages_from<'a>(

0 commit comments

Comments
 (0)