MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / write_table_btree

Function write_table_btree

internal/cbm/sqlite_writer.c:1264–1295  ·  view source on GitHub ↗

Write leaf pages for a table, returns root page. rowids must be sequential starting from 1 (or single-row PK text).

Source from the content-addressed store, hash-verified

1262// Write leaf pages for a table, returns root page.
1263// rowids must be sequential starting from 1 (or single-row PK text).
1264static uint32_t write_table_btree(FILE *fp, uint32_t *next_page, const uint8_t **records,
1265 const int *record_lens, const int64_t *rowids, int count,
1266 bool first_is_page1) {
1267 if (count == 0) {
1268 // Empty table: write a single empty leaf page
1269 *next_page = cbm_skip_pending_byte(*next_page);
1270 uint32_t pnum = (*next_page)++;
1271 uint8_t page[CBM_PAGE_SIZE];
1272 memset(page, 0, CBM_PAGE_SIZE);
1273 int hdr = first_is_page1 ? SQLITE_HEADER_SIZE : 0;
1274 page[hdr] = BTREE_LEAF_TABLE; // leaf table
1275 put_u16(page + hdr + HDR_FREEBLOCK_OFF, 0); // no freeblocks
1276 put_u16(page + hdr + HDR_CELLCOUNT_OFF, 0); // 0 cells
1277 put_u16(page + hdr + HDR_CONTENT_OFF, (uint16_t)CBM_PAGE_SIZE); // content at end of page
1278 page[hdr + HDR_FRAGBYTES_OFF] = 0; // 0 fragmented bytes
1279 (void)fseek(fp, (long)(pnum - SKIP_ONE) * CBM_PAGE_SIZE, SEEK_SET);
1280 (void)fwrite(page, SKIP_ONE, CBM_PAGE_SIZE, fp);
1281 return pnum;
1282 }
1283
1284 PageBuilder pb;
1285 pb_init(&pb, fp, *next_page, false);
1286 pb.page1_offset = first_is_page1 ? SQLITE_HEADER_SIZE : 0;
1287 pb.ptr_offset = pb.page1_offset + BTREE_HEADER_SIZE;
1288
1289 for (int i = 0; i < count; i++) {
1290 pb_add_table_cell_with_flush(&pb, rowids[i], records[i], record_lens[i],
1291 i > 0 ? rowids[i - SKIP_ONE] : 0);
1292 }
1293
1294 return pb_finalize_table(&pb, next_page, rowids[count - SKIP_ONE]);
1295}
1296
1297// Promote the last cell from current page to separator, un-add it, and flush.
1298static bool pb_promote_and_flush(PageBuilder *pb, uint8_t **cells, int *cell_lens, int prev_idx) {

Callers 3

write_one_tableFunction · 0.85
write_metadata_tablesFunction · 0.85
cbm_writer_finalizeFunction · 0.85

Calls 6

cbm_skip_pending_byteFunction · 0.85
put_u16Function · 0.85
fwriteFunction · 0.85
pb_initFunction · 0.85
pb_finalize_tableFunction · 0.85

Tested by

no test coverage detected