Write a streaming B-tree table from count items, or an empty table if count == 0. */
| 1725 | |
| 1726 | /* Write a streaming B-tree table from count items, or an empty table if count == 0. */ |
| 1727 | static int write_one_table(write_db_ctx_t *w, uint32_t *root, const void *items, int count, |
| 1728 | build_record_fn build_rec, get_rowid_fn get_id) { |
| 1729 | if (count <= 0 || !items) { |
| 1730 | *root = write_table_btree(w->fp, &w->next_page, NULL, NULL, NULL, 0, false); |
| 1731 | return 0; |
| 1732 | } |
| 1733 | PageBuilder pb; |
| 1734 | pb_init(&pb, w->fp, w->next_page, false); |
| 1735 | for (int i = 0; i < count; i++) { |
| 1736 | int rec_len; |
| 1737 | uint8_t *rec = build_rec(items, i, &rec_len); |
| 1738 | if (!rec) { |
| 1739 | return ERR_WRITE_FAILED; |
| 1740 | } |
| 1741 | int64_t rowid = get_id(items, i); |
| 1742 | int64_t prev_id = i > 0 ? get_id(items, i - SKIP_ONE) : 0; |
| 1743 | pb_add_table_cell_with_flush(&pb, rowid, rec, rec_len, prev_id); |
| 1744 | free(rec); |
| 1745 | } |
| 1746 | *root = pb_finalize_table(&pb, &w->next_page, get_id(items, count - SKIP_ONE)); |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | /* Adapter functions for write_one_table (nodes are written via the streaming |
| 1751 | * PageBuilder in cbm_writer_append_nodes, so no node adapter is needed here). */ |
no test coverage detected