Skip to content

Commit

Permalink
write footer on checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma committed Aug 7, 2024
1 parent 07dc9b5 commit 5924766
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 4 additions & 4 deletions libsql-wal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ const LIBSQL_MAGIC: u64 = u64::from_be_bytes(*b"LIBSQL\0\0");
const LIBSQL_PAGE_SIZE: u16 = 4096;
const LIBSQL_WAL_VERSION: u16 = 1;

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

#[cfg(any(debug_assertions, test))]
Expand Down
18 changes: 17 additions & 1 deletion libsql-wal/src/segment/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::error::Result;
use crate::io::buf::{ZeroCopyBoxIoBuf, ZeroCopyBuf};
use crate::io::FileExt;
use crate::segment::Frame;
use crate::{LibsqlFooter, LIBSQL_MAGIC, LIBSQL_PAGE_SIZE, LIBSQL_WAL_VERSION};

use super::Segment;

Expand Down Expand Up @@ -157,6 +158,21 @@ where
buf = read_buf.into_inner();
}

// update the footer at the end of the db file.
let footer = LibsqlFooter {
magic: LIBSQL_MAGIC.into(),
version: LIBSQL_WAL_VERSION.into(),
replication_index: last_replication_index.into(),
};

let footer_offset = size_after as usize * LIBSQL_PAGE_SIZE as usize;
let (_, ret) = db_file
.write_all_at_async(ZeroCopyBuf::new_init(footer), footer_offset as u64)
.await;
ret?;

// todo: truncate if necessary

//// todo: make async
db_file.sync_all()?;

Expand Down Expand Up @@ -185,7 +201,7 @@ where
Ok(Some(last_replication_index))
}

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

0 comments on commit 5924766

Please sign in to comment.