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

Function overflowize_index_cell

internal/cbm/sqlite_writer.c:1127–1149  ·  view source on GitHub ↗

If an index cell's payload exceeds X, rewrite it to spill the tail to overflow pages: varint(payload_len) + payload[0..local) + u32(first_ovfl). Returns the (possibly new, malloc'd) cell; frees the original when replaced.

Source from the content-addressed store, hash-verified

1125// overflow pages: varint(payload_len) + payload[0..local) + u32(first_ovfl).
1126// Returns the (possibly new, malloc'd) cell; frees the original when replaced.
1127static uint8_t *overflowize_index_cell(FILE *fp, uint32_t *next_page, uint8_t *cell,
1128 int *cell_len) {
1129 uint64_t plen = 0;
1130 int vlen = get_varint(cell, &plen);
1131 if ((int64_t)plen <= INDEX_OVERFLOW_MAX_LOCAL) {
1132 return cell;
1133 }
1134 int64_t per_ovfl = (int64_t)CBM_PAGE_SIZE - BTREE_PTR_SIZE;
1135 int64_t k = INDEX_OVERFLOW_MIN_LOCAL + (((int64_t)plen - INDEX_OVERFLOW_MIN_LOCAL) % per_ovfl);
1136 int local = (k <= INDEX_OVERFLOW_MAX_LOCAL) ? (int)k : INDEX_OVERFLOW_MIN_LOCAL;
1137 uint32_t first_ovfl =
1138 write_overflow_pages(fp, next_page, cell + vlen + local, (int)plen - local);
1139 int nlen = vlen + local + BTREE_PTR_SIZE;
1140 uint8_t *data = cells_alloc((size_t)nlen);
1141 if (!data) {
1142 return cell; /* fall back to the (broken) inline form on OOM */
1143 }
1144 memcpy(data, cell, (size_t)(vlen + local));
1145 put_u32(data + vlen + local, first_ovfl);
1146 cells_free(cell);
1147 *cell_len = nlen;
1148 return data;
1149}
1150#define TABLE_OVERFLOW_MIN_LOCAL 8199
1151
1152// Put a finished table cell on the current leaf, flushing a full leaf first.

Callers 1

write_index_btreeFunction · 0.85

Calls 5

get_varintFunction · 0.85
write_overflow_pagesFunction · 0.85
cells_allocFunction · 0.85
put_u32Function · 0.85
cells_freeFunction · 0.85

Tested by

no test coverage detected