cberner / redb

An embedded key-value database in pure Rust
https://www.redb.org
Apache License 2.0
3.38k stars 157 forks source link

What is the actual redb Page _content_ size? #877

Closed olebedev closed 1 month ago

olebedev commented 1 month ago

👋 Hi there, First, allow me to say thank you for building this, this is a truly great initiative.

My question is: what is the actual page size, meaning how much content can I fit into a single redb page?

Adding a context, I am trying to build something like singly-linked list on top of the redb Pages to fit potentially very large content - operations' log. It is important to keep all of it for the integrity checks purposes but the reads are usually happen on the recent chunk of that log, say latest page or head of the singly-linked list.

My thinking model currently is to have a fixed size structure like this:

// Matching the default page size of the redb storage
//  https://github.com/cberner/redb/blob/222a37f/src/tree_store/page_store/header.rs#L77
const PAGE_SIZE: usize = 4096;

pub struct OpLogPage {
    id: UUID,
    prev: UUID,
    length: u32,
    data: [u8; PAGE_SIZE - 36], // 36 == std::mem::size_of::<(UUID, UUID, u32)>())
}

With that I have hoped to make sure this structure can match the physical limit of the redb Page. However, I suspect I could be totally wrong here because the PAGE_SIZE I linked to above can be for internal representation. I haven't read the related code yet to figure it out by myself, I thought I would better ask here first to gather initial thoughts on the approach since it can be inefficient.

Thank you for your time.

Kind regards, Oleg

cberner commented 1 month ago

Pages come in multiple sizes -- "orders" as referred to in the code base -- which are currently a power of 2 multiplied by 4096. Then there is a variable amount of overhead, depending on how many key-value pairs end up in the page. It's subject to change, but you can find the current format in the design doc, here: https://github.com/cberner/redb/blob/master/docs/design.md#leaf-page

olebedev commented 1 month ago

Thanks for the insight, @cberner. I'll review the design first next time.