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

Function node_scan_page

src/store/store.c:3538–3598  ·  view source on GitHub ↗

One page of the current phase into out[0..max). A short page ends the * phase; the statement is reset before returning either way. */

Source from the content-addressed store, hash-verified

3536/* One page of the current phase into out[0..max). A short page ends the
3537 * phase; the statement is reset before returning either way. */
3538static int node_scan_page(cbm_store_node_scan_t *scan, cbm_node_t *out, int max, int *got) {
3539 *got = 0;
3540 cbm_store_t *s = scan->store;
3541 node_scan_stmt_t which = scan->phase;
3542 if (which == NODE_SCAN_FILE_AFTER && !scan->keyed) {
3543 which = NODE_SCAN_FILE_FIRST;
3544 }
3545 bool after_id =
3546 which == NODE_SCAN_LABEL || which == NODE_SCAN_NULL_FILE || which == NODE_SCAN_FILE_SAME;
3547 if (after_id && scan->keyed && scan->last_id == INT64_MAX) {
3548 node_scan_phase_done(scan); /* nothing can follow the largest id */
3549 return CBM_STORE_OK;
3550 }
3551#ifdef CBM_ENABLE_TEST_SEAMS
3552 if (graph_compare_test_countdown_fires(&node_scan_test_fail_countdown)) {
3553 snprintf(s->errbuf, sizeof(s->errbuf), "node scan aborted: injected page failure");
3554 return CBM_STORE_ERR;
3555 }
3556#endif
3557 read_batch_renew(s);
3558 sqlite3_stmt **slot = &scan->stmts[which];
3559 if (!*slot &&
3560 sqlite3_prepare_v2(s->db, node_scan_sql[which], CBM_NOT_FOUND, slot, NULL) != SQLITE_OK) {
3561 store_set_error_sqlite(s, "node scan prepare");
3562 return CBM_STORE_ERR;
3563 }
3564 sqlite3_stmt *stmt = *slot;
3565 node_scan_bind(scan, stmt, which, max);
3566
3567 int n = 0;
3568 int rc = SQLITE_ROW;
3569 while (n < max && (rc = sqlite3_step(stmt)) == SQLITE_ROW) {
3570 scan_node(stmt, &out[n]);
3571 n++;
3572 }
3573 bool failed = n < max && rc != SQLITE_DONE;
3574 if (n == max) { /* the statement still sits on the page's last row */
3575 scan->keyed = true;
3576 scan->last_id = out[n - SKIP_ONE].id;
3577 if (which == NODE_SCAN_FILE_FIRST || which == NODE_SCAN_FILE_AFTER) {
3578 /* a new last file: continue with the rest of it */
3579 sqlite3_value_free(scan->last_file);
3580 scan->last_file = sqlite3_value_dup(sqlite3_column_value(stmt, NODE_SCAN_COL_FILE));
3581 failed = !scan->last_file;
3582 scan->phase = NODE_SCAN_FILE_SAME;
3583 }
3584 }
3585 sqlite3_reset(stmt);
3586 if (failed) {
3587 store_set_error_sqlite(s, "node scan aborted");
3588 for (int i = 0; i < n; i++) {
3589 cbm_node_free_fields(&out[i]);
3590 }
3591 return CBM_STORE_ERR;
3592 }
3593 if (n < max) {
3594 node_scan_phase_done(scan);
3595 }

Callers 1

cbm_store_node_scan_nextFunction · 0.85

Calls 7

node_scan_phase_doneFunction · 0.85
read_batch_renewFunction · 0.85
store_set_error_sqliteFunction · 0.85
node_scan_bindFunction · 0.85
scan_nodeFunction · 0.85
cbm_node_free_fieldsFunction · 0.85

Tested by

no test coverage detected