| 4736 | } |
| 4737 | |
| 4738 | int cbm_store_coverage_replace_ex(cbm_store_t *s, const char *project, |
| 4739 | const cbm_coverage_row_t *rows, int count, |
| 4740 | const cbm_coverage_meta_t *meta) { |
| 4741 | if (!s || !s->db || !project || count < 0 || (count > 0 && !rows)) { |
| 4742 | return CBM_STORE_ERR; |
| 4743 | } |
| 4744 | if (exec_sql(s, "BEGIN;") != CBM_STORE_OK) { |
| 4745 | return CBM_STORE_ERR; |
| 4746 | } |
| 4747 | /* Sub-block timings (publish.timing style): coverage_replace measured 9 s |
| 4748 | * on the TypeScript corpus and the caller-level block could not say WHY — |
| 4749 | * delete, 13k row inserts with large error_ranges payloads, the NOT-IN |
| 4750 | * prune, meta, and COMMIT are very different suspects. */ |
| 4751 | struct timespec cov_t0; |
| 4752 | struct timespec cov_t1; |
| 4753 | long cov_ms[5] = {0, 0, 0, 0, 0}; |
| 4754 | long long cov_detail_bytes = 0; |
| 4755 | cbm_clock_gettime(CLOCK_MONOTONIC, &cov_t0); |
| 4756 | sqlite3_stmt *del = NULL; |
| 4757 | if (sqlite3_prepare_v2(s->db, "DELETE FROM index_coverage WHERE project = ?1;", CBM_NOT_FOUND, |
| 4758 | &del, NULL) != SQLITE_OK) { |
| 4759 | store_set_error_sqlite(s, "coverage delete prepare"); |
| 4760 | (void)exec_sql(s, "ROLLBACK;"); |
| 4761 | return CBM_STORE_ERR; |
| 4762 | } |
| 4763 | bind_text(del, SKIP_ONE, project); |
| 4764 | int rc = sqlite3_step(del); |
| 4765 | sqlite3_finalize(del); |
| 4766 | if (rc != SQLITE_DONE) { |
| 4767 | store_set_error_sqlite(s, "coverage delete"); |
| 4768 | (void)exec_sql(s, "ROLLBACK;"); |
| 4769 | return CBM_STORE_ERR; |
| 4770 | } |
| 4771 | cbm_clock_gettime(CLOCK_MONOTONIC, &cov_t1); |
| 4772 | cov_ms[0] = |
| 4773 | (cov_t1.tv_sec - cov_t0.tv_sec) * 1000 + (cov_t1.tv_nsec - cov_t0.tv_nsec) / 1000000; |
| 4774 | cov_t0 = cov_t1; |
| 4775 | sqlite3_stmt *ins = NULL; |
| 4776 | if (sqlite3_prepare_v2( |
| 4777 | s->db, |
| 4778 | "INSERT OR REPLACE INTO index_coverage (project, rel_path, kind, detail) " |
| 4779 | "VALUES (?1, ?2, ?3, ?4);", |
| 4780 | CBM_NOT_FOUND, &ins, NULL) != SQLITE_OK) { |
| 4781 | store_set_error_sqlite(s, "coverage insert prepare"); |
| 4782 | (void)exec_sql(s, "ROLLBACK;"); |
| 4783 | return CBM_STORE_ERR; |
| 4784 | } |
| 4785 | for (int i = 0; i < count; i++) { |
| 4786 | if (!rows[i].rel_path || !rows[i].kind) { |
| 4787 | continue; |
| 4788 | } |
| 4789 | bind_text(ins, SKIP_ONE, project); |
| 4790 | bind_text(ins, ST_COL_2, rows[i].rel_path); |
| 4791 | bind_text(ins, ST_COL_3, rows[i].kind); |
| 4792 | cov_detail_bytes += rows[i].detail ? (long long)strlen(rows[i].detail) : 0; |
| 4793 | bind_text(ins, CBM_SZ_4, rows[i].detail ? rows[i].detail : ""); |
| 4794 | if (sqlite3_step(ins) != SQLITE_DONE) { |
| 4795 | store_set_error_sqlite(s, "coverage insert"); |