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

Function write_db_after_nodes

internal/cbm/sqlite_writer.c:2114–2324  ·  view source on GitHub ↗

Write everything after the nodes table: the edges/vectors/token_vectors data * tables, metadata tables, all indexes, and the sqlite_master page-1 + file * header. `nodes_root` is the root of the already-written nodes table. Closes * w->fp before returning (success or error). */

Source from the content-addressed store, hash-verified

2112 * header. `nodes_root` is the root of the already-written nodes table. Closes
2113 * w->fp before returning (success or error). */
2114static int write_db_after_nodes(write_db_ctx_t *w, uint32_t nodes_root) {
2115 FILE *fp = w->fp;
2116 CBMDumpNode *nodes = w->nodes;
2117 int node_count = w->node_count;
2118 CBMDumpEdge *edges = w->edges;
2119 int edge_count = w->edge_count;
2120
2121 // Phase 1 (cont.): remaining data tables (edge + vector + token_vector records)
2122 CBM_PROF_START(t_data);
2123 uint32_t edges_root;
2124 uint32_t vectors_root;
2125 uint32_t token_vecs_root;
2126 int rc =
2127 write_one_table(w, &edges_root, w->edges, w->edge_count, adapt_fill_edge, adapt_edge_id);
2128 if (rc != 0) {
2129 return discard_writer_output(w, rc);
2130 }
2131 rc = write_one_table(w, &vectors_root, w->vectors, w->vector_count, adapt_fill_vector,
2132 adapt_vector_id);
2133 if (rc != 0) {
2134 return discard_writer_output(w, rc);
2135 }
2136 rc = write_one_table(w, &token_vecs_root, w->token_vecs, w->token_vec_count,
2137 adapt_fill_token_vec, adapt_token_vec_id);
2138 if (rc != 0) {
2139 return discard_writer_output(w, rc);
2140 }
2141 CBM_PROF_END_N("write_db", "1_data_tables", t_data, node_count + edge_count);
2142
2143 // Phase 2: Metadata tables (projects, file_hashes, summaries, sqlite_sequence)
2144 CBM_PROF_START(t_meta);
2145 uint32_t projects_root;
2146 uint32_t file_hashes_root;
2147 uint32_t summaries_root;
2148 uint32_t sqlite_seq_root;
2149 write_metadata_tables(w, &projects_root, &file_hashes_root, &summaries_root, &sqlite_seq_root);
2150 uint32_t next_page = w->next_page;
2151 CBM_PROF_END("write_db", "2_metadata_tables", t_meta);
2152
2153 // --- Build indexes (all sorted by key columns before writing) ---
2154
2155 // Set sort contexts for qsort comparators.
2156 g_sort_nodes = nodes;
2157 g_sort_edges = edges;
2158
2159 // Parallel sort: all 11 index permutations sorted simultaneously.
2160 // Sorting is O(N log N) per index — the dominant CPU cost in index building.
2161 // Cell building + B-tree writing remains serial (sequential page allocation).
2162 SortJob nsorts[] = {
2163 {node_count, cmp_node_by_label, NULL},
2164 {node_count, cmp_node_by_name, NULL},
2165 {node_count, cmp_node_by_file, NULL},
2166 {node_count, cmp_node_by_qn, NULL},
2167 };
2168 SortJob esorts[] = {
2169 {edge_count, cmp_edge_by_source_type, NULL},
2170 {edge_count, cmp_edge_by_target_type, NULL},
2171 {edge_count, cmp_edge_by_type, NULL},

Callers 1

cbm_writer_finalizeFunction · 0.85

Calls 15

write_one_tableFunction · 0.85
discard_writer_outputFunction · 0.85
write_metadata_tablesFunction · 0.85
parallel_sort_indexesFunction · 0.85
build_node_indexesFunction · 0.85
build_edge_indexesFunction · 0.85
rec_initFunction · 0.85
rec_add_textFunction · 0.85
rec_add_intFunction · 0.85
rec_finalizeFunction · 0.85
rec_freeFunction · 0.85
varint_lenFunction · 0.85

Tested by

no test coverage detected