Is there a better way to handle my use-case? I am working through "Let's write a database" which is in C. In it there are tables with pages filled with rows. The structs are below. Pages are created as-needed, as are Rows. In my case I check if the appropriate page is loaded already and, if not, allocate and stick in table.pages at the appropriate index. I was using Nullable (optional) pointers to handle whether I needed to allocate a new page||row, rather than keep page_counts and row_counts around as a secondary source.
const PAGE_SIZE = 4096; // 4KB = 4096 (Bytes, dec.)
const TABLE_MAX_PAGES = 200;
const ROWS_PER_PAGE = 14;
const TABLE_MAX_ROWS = ROWS_PER_PAGE * TABLE_MAX_PAGES;
const Table = struct {
num_rows: usize = 0,
num_pages: usize = 0,
pages: [TABLE_MAX_PAGES]*Page = undefined,
};
const Page = struct {
num_rows: u32 = 0,
rows: [ROWS_PER_PAGE]*Row = undefined,
};
const Row = struct {
id: []const u8 = undefined,
usernm: []const u8 = undefined,
email: []const u8 = undefined,
const COL_USERNM_LEN = 32; // bytes
const COL_EMAIL_LEN = 255; // bytes
const COL_ID_LEN = 4; // bytes (u32 bits)
};