Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/reftable/writer.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / reftable / writer.c` 896 lines ยท 21984 bytes Raw Blame History 1 /* 2 * Copyright 2020 Google LLC 3 * 4 * Use of this source code is governed by a BSD-style 5 * license that can be found in the LICENSE file or at 6 * https://developers.google.com/open-source/licenses/bsd 7 */ 8 9 #include "writer.h" 10 11 #include "system.h" 12 13 #include "block.h" 14 #include "constants.h" 15 #include "record.h" 16 #include "tree.h" 17 #include "reftable-error.h" 18 19 /* finishes a block, and writes it to storage */ 20 static int writer_flush_block(struct reftable_writer *w); 21 22 /* deallocates memory related to the index */ 23 static void writer_clear_index(struct reftable_writer *w); 24 25 /* finishes writing a 'r' (refs) or 'g' (reflogs) section */ 26 static int writer_finish_public_section(struct reftable_writer *w); 27 28 static struct reftable_block_stats * 29 writer_reftable_block_stats(struct reftable_writer *w, uint8_t typ) 30 { 31 switch (typ) { 32 case 'r': 33 return &w->stats.ref_stats; 34 case 'o': 35 return &w->stats.obj_stats; 36 case 'i': 37 return &w->stats.idx_stats; 38 case 'g': 39 return &w->stats.log_stats; 40 } 41 abort(); 42 return NULL; 43 } 44 45 /* write data, queuing the padding for the next write. Returns negative for 46 * error. */ 47 static int padded_write(struct reftable_writer *w, uint8_t *data, size_t len, 48 int padding) 49 { 50 int n = 0; 51 if (w->pending_padding > 0) { 52 uint8_t *zeroed; 53 int n; 54 55 zeroed = reftable_calloc(w->pending_padding, sizeof(*zeroed)); 56 if (!zeroed) 57 return -1; 58 59 n = w->write(w->write_arg, zeroed, w->pending_padding); 60 if (n < 0) { 61 reftable_free(zeroed); 62 return n; 63 } 64 65 w->pending_padding = 0; 66 reftable_free(zeroed); 67 } 68 69 w->pending_padding = padding; 70 n = w->write(w->write_arg, data, len); 71 if (n < 0) 72 return n; 73 n += padding; 74 return 0; 75 } 76 77 static void options_set_defaults(struct reftable_write_options *opts) 78 { 79 if (opts->restart_interval == 0) { 80 opts->restart_interval = 16; 81 } 82 83 if (opts->block_size == 0) { 84 opts->block_size = DEFAULT_BLOCK_SIZE; 85 } 86 } 87 88 static int writer_version(struct reftable_writer *w) 89 { 90 return (w->hash_id == 0 || w->hash_id == REFTABLE_HASH_SHA1) ? 91 1 : 92 2; 93 } 94 95 static int writer_write_header(struct reftable_writer *w, uint8_t *dest) 96 { 97 memcpy(dest, "REFT", 4); 98 99 dest[4] = writer_version(w); 100 101 reftable_put_be24(dest + 5, w->opts.block_size); 102 reftable_put_be64(dest + 8, w->min_update_index); 103 reftable_put_be64(dest + 16, w->max_update_index); 104 if (writer_version(w) == 2) { 105 uint32_t hash_id; 106 107 switch (w->hash_id) { 108 case REFTABLE_HASH_SHA1: 109 hash_id = REFTABLE_FORMAT_ID_SHA1; 110 break; 111 case REFTABLE_HASH_SHA256: 112 hash_id = REFTABLE_FORMAT_ID_SHA256; 113 break; 114 default: 115 return -1; 116 } 117 118 reftable_put_be32(dest + 24, hash_id); 119 } 120 121 return header_size(writer_version(w)); 122 } 123 124 static int writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) 125 { 126 int block_start = 0, ret; 127 128 if (w->next == 0) 129 block_start = header_size(writer_version(w)); 130 131 reftable_buf_reset(&w->last_key); 132 ret = block_writer_init(&w->block_writer_data, typ, w->block, 133 w->opts.block_size, block_start, 134 hash_size(w->hash_id)); 135 if (ret < 0) 136 return ret; 137 138 w->block_writer = &w->block_writer_data; 139 w->block_writer->restart_interval = w->opts.restart_interval; 140 141 return 0; 142 } 143 144 int reftable_writer_new(struct reftable_writer **out, 145 ssize_t (*writer_func)(void *, const void *, size_t), 146 int (*flush_func)(void *), 147 void *writer_arg, 148 enum reftable_hash hash_id, 149 const struct reftable_write_options *_opts) 150 { 151 struct reftable_write_options opts = {0}; 152 struct reftable_writer *wp; 153 int err; 154 155 if (_opts) 156 opts = *_opts; 157 options_set_defaults(&opts); 158 if (opts.block_size >= (1 << 24)) 159 return REFTABLE_API_ERROR; 160 161 if (!hash_id) 162 hash_id = REFTABLE_HASH_SHA1; 163 164 wp = reftable_calloc(1, sizeof(*wp)); 165 if (!wp) 166 return REFTABLE_OUT_OF_MEMORY_ERROR; 167 168 reftable_buf_init(&wp->block_writer_data.last_key); 169 reftable_buf_init(&wp->last_key); 170 reftable_buf_init(&wp->scratch); 171 REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size); 172 if (!wp->block) { 173 reftable_free(wp); 174 return REFTABLE_OUT_OF_MEMORY_ERROR; 175 } 176 wp->write = writer_func; 177 wp->write_arg = writer_arg; 178 wp->opts = opts; 179 wp->hash_id = hash_id; 180 wp->flush = flush_func; 181 err = writer_reinit_block_writer(wp, REFTABLE_BLOCK_TYPE_REF); 182 if (err < 0) { 183 reftable_free(wp->block); 184 reftable_free(wp); 185 return err; 186 } 187 188 *out = wp; 189 190 return 0; 191 } 192 193 int reftable_writer_set_limits(struct reftable_writer *w, uint64_t min, 194 uint64_t max) 195 { 196 /* 197 * Set the min/max update index limits for the reftable writer. 198 * This must be called before adding any records, since: 199 * - The 'next' field gets set after writing the first block. 200 * - The 'last_key' field updates with each new record (but resets 201 * after sections). 202 * Returns REFTABLE_API_ERROR if called after writing has begun. 203 */ 204 if (w->next || w->last_key.len) 205 return REFTABLE_API_ERROR; 206 207 w->min_update_index = min; 208 w->max_update_index = max; 209 210 return 0; 211 } 212 213 static void writer_release(struct reftable_writer *w) 214 { 215 if (w) { 216 reftable_free(w->block); 217 w->block = NULL; 218 block_writer_release(&w->block_writer_data); 219 w->block_writer = NULL; 220 writer_clear_index(w); 221 reftable_buf_release(&w->last_key); 222 reftable_buf_release(&w->scratch); 223 } 224 } 225 226 void reftable_writer_free(struct reftable_writer *w) 227 { 228 writer_release(w); 229 reftable_free(w); 230 } 231 232 struct obj_index_tree_node { 233 struct reftable_buf hash; 234 uint64_t *offsets; 235 size_t offset_len; 236 size_t offset_cap; 237 }; 238 239 #define OBJ_INDEX_TREE_NODE_INIT \ 240 { \ 241 .hash = REFTABLE_BUF_INIT \ 242 } 243 244 static int obj_index_tree_node_compare(const void *a, const void *b) 245 { 246 return reftable_buf_cmp(&((const struct obj_index_tree_node *)a)->hash, 247 &((const struct obj_index_tree_node *)b)->hash); 248 } 249 250 static int writer_index_hash(struct reftable_writer *w, struct reftable_buf *hash) 251 { 252 uint64_t off = w->next; 253 struct obj_index_tree_node want = { .hash = *hash }; 254 struct obj_index_tree_node *key; 255 struct tree_node *node; 256 257 node = tree_search(w->obj_index_tree, &want, &obj_index_tree_node_compare); 258 if (!node) { 259 struct obj_index_tree_node empty = OBJ_INDEX_TREE_NODE_INIT; 260 int err; 261 262 key = reftable_malloc(sizeof(*key)); 263 if (!key) 264 return REFTABLE_OUT_OF_MEMORY_ERROR; 265 266 *key = empty; 267 268 reftable_buf_reset(&key->hash); 269 err = reftable_buf_add(&key->hash, hash->buf, hash->len); 270 if (err < 0) { 271 reftable_free(key); 272 return err; 273 } 274 tree_insert(&w->obj_index_tree, key, 275 &obj_index_tree_node_compare); 276 } else { 277 key = node->key; 278 } 279 280 if (key->offset_len > 0 && key->offsets[key->offset_len - 1] == off) 281 return 0; 282 283 REFTABLE_ALLOC_GROW_OR_NULL(key->offsets, key->offset_len + 1, 284 key->offset_cap); 285 if (!key->offsets) 286 return REFTABLE_OUT_OF_MEMORY_ERROR; 287 key->offsets[key->offset_len++] = off; 288 289 return 0; 290 } 291 292 static int writer_add_record(struct reftable_writer *w, 293 struct reftable_record *rec) 294 { 295 int err; 296 297 err = reftable_record_key(rec, &w->scratch); 298 if (err < 0) 299 goto done; 300 301 if (reftable_buf_cmp(&w->last_key, &w->scratch) >= 0) { 302 err = REFTABLE_API_ERROR; 303 goto done; 304 } 305 306 reftable_buf_reset(&w->last_key); 307 err = reftable_buf_add(&w->last_key, w->scratch.buf, w->scratch.len); 308 if (err < 0) 309 goto done; 310 311 if (!w->block_writer) { 312 err = writer_reinit_block_writer(w, reftable_record_type(rec)); 313 if (err < 0) 314 goto done; 315 } 316 317 if (block_writer_type(w->block_writer) != reftable_record_type(rec)) 318 return REFTABLE_API_ERROR; 319 320 /* 321 * Try to add the record to the writer. If this succeeds then we're 322 * done. Otherwise the block writer may have hit the block size limit 323 * and needs to be flushed. 324 */ 325 err = block_writer_add(w->block_writer, rec); 326 if (err == 0) 327 goto done; 328 329 if (err != REFTABLE_ENTRY_TOO_BIG_ERROR) 330 goto done; 331 /* 332 * The current block is full, so we need to flush and reinitialize the 333 * writer to start writing the next block. 334 */ 335 err = writer_flush_block(w); 336 if (err < 0) 337 goto done; 338 err = writer_reinit_block_writer(w, reftable_record_type(rec)); 339 if (err < 0) 340 goto done; 341 342 /* 343 * Try to add the record to the writer again. If this still fails then 344 * the record does not fit into the block size. 345 */ 346 err = block_writer_add(w->block_writer, rec); 347 if (err) 348 goto done; 349 350 done: 351 return err; 352 } 353 354 int reftable_writer_add_ref(struct reftable_writer *w, 355 struct reftable_ref_record *ref) 356 { 357 struct reftable_record rec = { 358 .type = REFTABLE_BLOCK_TYPE_REF, 359 .u = { 360 .ref = *ref 361 }, 362 }; 363 int err; 364 365 if (!ref->refname || 366 ref->update_index < w->min_update_index || 367 ref->update_index > w->max_update_index) 368 return REFTABLE_API_ERROR; 369 370 rec.u.ref.update_index -= w->min_update_index; 371 372 err = writer_add_record(w, &rec); 373 if (err < 0) 374 goto out; 375 376 if (!w->opts.skip_index_objects && reftable_ref_record_val1(ref)) { 377 reftable_buf_reset(&w->scratch); 378 err = reftable_buf_add(&w->scratch, (char *)reftable_ref_record_val1(ref), 379 hash_size(w->hash_id)); 380 if (err < 0) 381 goto out; 382 383 err = writer_index_hash(w, &w->scratch); 384 if (err < 0) 385 goto out; 386 } 387 388 if (!w->opts.skip_index_objects && reftable_ref_record_val2(ref)) { 389 reftable_buf_reset(&w->scratch); 390 err = reftable_buf_add(&w->scratch, reftable_ref_record_val2(ref), 391 hash_size(w->hash_id)); 392 if (err < 0) 393 goto out; 394 395 err = writer_index_hash(w, &w->scratch); 396 if (err < 0) 397 goto out; 398 } 399 400 err = 0; 401 402 out: 403 return err; 404 } 405 406 int reftable_writer_add_refs(struct reftable_writer *w, 407 struct reftable_ref_record *refs, size_t n) 408 { 409 int err = 0; 410 411 if (n) 412 qsort(refs, n, sizeof(*refs), reftable_ref_record_compare_name); 413 414 for (size_t i = 0; err == 0 && i < n; i++) 415 err = reftable_writer_add_ref(w, &refs[i]); 416 417 return err; 418 } 419 420 static int reftable_writer_add_log_verbatim(struct reftable_writer *w, 421 struct reftable_log_record *log) 422 { 423 struct reftable_record rec = { 424 .type = REFTABLE_BLOCK_TYPE_LOG, 425 .u = { 426 .log = *log, 427 }, 428 }; 429 if (w->block_writer && 430 block_writer_type(w->block_writer) == REFTABLE_BLOCK_TYPE_REF) { 431 int err = writer_finish_public_section(w); 432 if (err < 0) 433 return err; 434 } 435 436 w->next -= w->pending_padding; 437 w->pending_padding = 0; 438 return writer_add_record(w, &rec); 439 } 440 441 int reftable_writer_add_log(struct reftable_writer *w, 442 struct reftable_log_record *log) 443 { 444 char *input_log_message = NULL; 445 struct reftable_buf cleaned_message = REFTABLE_BUF_INIT; 446 int err = 0; 447 448 if (log->value_type == REFTABLE_LOG_DELETION) 449 return reftable_writer_add_log_verbatim(w, log); 450 451 /* 452 * Verify only the upper limit of the update_index. Each reflog entry 453 * is tied to a specific update_index. Entries in the reflog can be 454 * replaced by adding a new entry with the same update_index, 455 * effectively canceling the old one. 456 * 457 * Consequently, reflog updates may include update_index values lower 458 * than the writer's min_update_index. 459 */ 460 if (log->update_index > w->max_update_index) 461 return REFTABLE_API_ERROR; 462 463 if (!log->refname) 464 return REFTABLE_API_ERROR; 465 466 input_log_message = log->value.update.message; 467 if (!w->opts.exact_log_message && log->value.update.message) { 468 err = reftable_buf_addstr(&cleaned_message, log->value.update.message); 469 if (err < 0) 470 goto done; 471 472 while (cleaned_message.len && 473 cleaned_message.buf[cleaned_message.len - 1] == '\n') { 474 err = reftable_buf_setlen(&cleaned_message, 475 cleaned_message.len - 1); 476 if (err < 0) 477 goto done; 478 } 479 if (strchr(cleaned_message.buf, '\n')) { 480 /* multiple lines not allowed. */ 481 err = REFTABLE_API_ERROR; 482 goto done; 483 } 484 485 err = reftable_buf_addstr(&cleaned_message, "\n"); 486 if (err < 0) 487 goto done; 488 489 log->value.update.message = cleaned_message.buf; 490 } 491 492 err = reftable_writer_add_log_verbatim(w, log); 493 log->value.update.message = input_log_message; 494 done: 495 reftable_buf_release(&cleaned_message); 496 return err; 497 } 498 499 int reftable_writer_add_logs(struct reftable_writer *w, 500 struct reftable_log_record *logs, size_t n) 501 { 502 int err = 0; 503 504 if (n) 505 qsort(logs, n, sizeof(*logs), reftable_log_record_compare_key); 506 507 for (size_t i = 0; err == 0 && i < n; i++) 508 err = reftable_writer_add_log(w, &logs[i]); 509 510 return err; 511 } 512 513 static int writer_finish_section(struct reftable_writer *w) 514 { 515 struct reftable_block_stats *bstats = NULL; 516 uint8_t typ = block_writer_type(w->block_writer); 517 uint64_t index_start = 0; 518 int max_level = 0; 519 size_t threshold = w->opts.unpadded ? 1 : 3; 520 int before_blocks = w->stats.idx_stats.blocks; 521 int err; 522 523 err = writer_flush_block(w); 524 if (err < 0) 525 return err; 526 527 /* 528 * When the section we are about to index has a lot of blocks then the 529 * index itself may span across multiple blocks, as well. This would 530 * require a linear scan over index blocks only to find the desired 531 * indexed block, which is inefficient. Instead, we write a multi-level 532 * index where index records of level N+1 will refer to index blocks of 533 * level N. This isn't constant time, either, but at least logarithmic. 534 * 535 * This loop handles writing this multi-level index. Note that we write 536 * the lowest-level index pointing to the indexed blocks first. We then 537 * continue writing additional index levels until the current level has 538 * less blocks than the threshold so that the highest level will be at 539 * the end of the index section. 540 * 541 * Readers are thus required to start reading the index section from 542 * its end, which is why we set `index_start` to the beginning of the 543 * last index section. 544 */ 545 while (w->index_len > threshold) { 546 struct reftable_index_record *idx = NULL; 547 size_t i, idx_len; 548 549 max_level++; 550 index_start = w->next; 551 err = writer_reinit_block_writer(w, REFTABLE_BLOCK_TYPE_INDEX); 552 if (err < 0) 553 return err; 554 555 idx = w->index; 556 idx_len = w->index_len; 557 558 w->index = NULL; 559 w->index_len = 0; 560 w->index_cap = 0; 561 for (i = 0; i < idx_len; i++) { 562 struct reftable_record rec = { 563 .type = REFTABLE_BLOCK_TYPE_INDEX, 564 .u = { 565 .idx = idx[i], 566 }, 567 }; 568 569 err = writer_add_record(w, &rec); 570 if (err < 0) 571 return err; 572 } 573 574 err = writer_flush_block(w); 575 if (err < 0) 576 return err; 577 578 for (i = 0; i < idx_len; i++) 579 reftable_buf_release(&idx[i].last_key); 580 reftable_free(idx); 581 } 582 583 /* 584 * The index may still contain a number of index blocks lower than the 585 * threshold. Clear it so that these entries don't leak into the next 586 * index section. 587 */ 588 writer_clear_index(w); 589 590 bstats = writer_reftable_block_stats(w, typ); 591 bstats->index_blocks = w->stats.idx_stats.blocks - before_blocks; 592 bstats->index_offset = index_start; 593 bstats->max_index_level = max_level; 594 595 /* Reinit lastKey, as the next section can start with any key. */ 596 reftable_buf_reset(&w->last_key); 597 598 return 0; 599 } 600 601 struct common_prefix_arg { 602 struct reftable_buf *last; 603 size_t max; 604 }; 605 606 static void update_common(void *void_arg, void *key) 607 { 608 struct common_prefix_arg *arg = void_arg; 609 struct obj_index_tree_node *entry = key; 610 if (arg->last) { 611 size_t n = common_prefix_size(&entry->hash, arg->last); 612 if (n > arg->max) 613 arg->max = n; 614 } 615 arg->last = &entry->hash; 616 } 617 618 struct write_record_arg { 619 struct reftable_writer *w; 620 int err; 621 }; 622 623 static void write_object_record(void *void_arg, void *key) 624 { 625 struct write_record_arg *arg = void_arg; 626 struct obj_index_tree_node *entry = key; 627 struct reftable_record 628 rec = { .type = REFTABLE_BLOCK_TYPE_OBJ, 629 .u.obj = { 630 .hash_prefix = (uint8_t *)entry->hash.buf, 631 .hash_prefix_len = arg->w->stats.object_id_len, 632 .offsets = entry->offsets, 633 .offset_len = entry->offset_len, 634 } }; 635 if (arg->err < 0) 636 goto done; 637 638 /* 639 * Try to add the record to the writer. If this succeeds then we're 640 * done. Otherwise the block writer may have hit the block size limit 641 * and needs to be flushed. 642 */ 643 arg->err = block_writer_add(arg->w->block_writer, &rec); 644 if (arg->err == 0) 645 goto done; 646 647 if (arg->err != REFTABLE_ENTRY_TOO_BIG_ERROR) 648 goto done; 649 650 /* 651 * The current block is full, so we need to flush and reinitialize the 652 * writer to start writing the next block. 653 */ 654 arg->err = writer_flush_block(arg->w); 655 if (arg->err < 0) 656 goto done; 657 658 arg->err = writer_reinit_block_writer(arg->w, REFTABLE_BLOCK_TYPE_OBJ); 659 if (arg->err < 0) 660 goto done; 661 662 /* 663 * If this still fails then we may need to reset record's offset 664 * length to reduce the data size to be written. 665 */ 666 arg->err = block_writer_add(arg->w->block_writer, &rec); 667 if (arg->err == 0) 668 goto done; 669 670 if (arg->err != REFTABLE_ENTRY_TOO_BIG_ERROR) 671 goto done; 672 673 rec.u.obj.offset_len = 0; 674 arg->err = block_writer_add(arg->w->block_writer, &rec); 675 676 /* Should be able to write into a fresh block. */ 677 assert(arg->err == 0); 678 679 done:; 680 } 681 682 static void object_record_free(void *void_arg REFTABLE_UNUSED, void *key) 683 { 684 struct obj_index_tree_node *entry = key; 685 686 REFTABLE_FREE_AND_NULL(entry->offsets); 687 reftable_buf_release(&entry->hash); 688 reftable_free(entry); 689 } 690 691 static int writer_dump_object_index(struct reftable_writer *w) 692 { 693 struct write_record_arg closure = { .w = w }; 694 struct common_prefix_arg common = { 695 .max = 1, /* obj_id_len should be >= 2. */ 696 }; 697 int err; 698 699 if (w->obj_index_tree) 700 infix_walk(w->obj_index_tree, &update_common, &common); 701 w->stats.object_id_len = common.max + 1; 702 703 err = writer_reinit_block_writer(w, REFTABLE_BLOCK_TYPE_OBJ); 704 if (err < 0) 705 return err; 706 707 if (w->obj_index_tree) 708 infix_walk(w->obj_index_tree, &write_object_record, &closure); 709 710 if (closure.err < 0) 711 return closure.err; 712 return writer_finish_section(w); 713 } 714 715 static int writer_finish_public_section(struct reftable_writer *w) 716 { 717 uint8_t typ = 0; 718 int err = 0; 719 720 if (!w->block_writer) 721 return 0; 722 723 typ = block_writer_type(w->block_writer); 724 err = writer_finish_section(w); 725 if (err < 0) 726 return err; 727 if (typ == REFTABLE_BLOCK_TYPE_REF && !w->opts.skip_index_objects && 728 w->stats.ref_stats.index_blocks > 0) { 729 err = writer_dump_object_index(w); 730 if (err < 0) 731 return err; 732 } 733 734 if (w->obj_index_tree) { 735 infix_walk(w->obj_index_tree, &object_record_free, NULL); 736 tree_free(w->obj_index_tree); 737 w->obj_index_tree = NULL; 738 } 739 740 w->block_writer = NULL; 741 return 0; 742 } 743 744 int reftable_writer_close(struct reftable_writer *w) 745 { 746 uint8_t footer[72]; 747 uint8_t *p = footer; 748 int err = writer_finish_public_section(w); 749 int empty_table = w->next == 0; 750 if (err != 0) 751 goto done; 752 w->pending_padding = 0; 753 if (empty_table) { 754 /* Empty tables need a header anyway. */ 755 uint8_t header[28]; 756 int n = writer_write_header(w, header); 757 err = padded_write(w, header, n, 0); 758 if (err < 0) 759 goto done; 760 } 761 762 p += writer_write_header(w, footer); 763 reftable_put_be64(p, w->stats.ref_stats.index_offset); 764 p += 8; 765 reftable_put_be64(p, (w->stats.obj_stats.offset) << 5 | w->stats.object_id_len); 766 p += 8; 767 reftable_put_be64(p, w->stats.obj_stats.index_offset); 768 p += 8; 769 770 reftable_put_be64(p, w->stats.log_stats.offset); 771 p += 8; 772 reftable_put_be64(p, w->stats.log_stats.index_offset); 773 p += 8; 774 775 reftable_put_be32(p, crc32(0, footer, p - footer)); 776 p += 4; 777 778 err = w->flush(w->write_arg); 779 if (err < 0) { 780 err = REFTABLE_IO_ERROR; 781 goto done; 782 } 783 784 err = padded_write(w, footer, footer_size(writer_version(w)), 0); 785 if (err < 0) 786 goto done; 787 788 if (empty_table) { 789 err = REFTABLE_EMPTY_TABLE_ERROR; 790 goto done; 791 } 792 793 done: 794 writer_release(w); 795 return err; 796 } 797 798 static void writer_clear_index(struct reftable_writer *w) 799 { 800 for (size_t i = 0; w->index && i < w->index_len; i++) 801 reftable_buf_release(&w->index[i].last_key); 802 REFTABLE_FREE_AND_NULL(w->index); 803 w->index_len = 0; 804 w->index_cap = 0; 805 } 806 807 static int writer_flush_nonempty_block(struct reftable_writer *w) 808 { 809 struct reftable_index_record index_record = { 810 .last_key = REFTABLE_BUF_INIT, 811 }; 812 uint8_t typ = block_writer_type(w->block_writer); 813 struct reftable_block_stats *bstats; 814 int raw_bytes, padding = 0, err; 815 uint64_t block_typ_off; 816 817 /* 818 * Finish the current block. This will cause the block writer to emit 819 * restart points and potentially compress records in case we are 820 * writing a log block. 821 * 822 * Note that this is still happening in memory. 823 */ 824 raw_bytes = block_writer_finish(w->block_writer); 825 if (raw_bytes < 0) 826 return raw_bytes; 827 828 /* 829 * By default, all records except for log records are padded to the 830 * block size. 831 */ 832 if (!w->opts.unpadded && typ != REFTABLE_BLOCK_TYPE_LOG) 833 padding = w->opts.block_size - raw_bytes; 834 835 bstats = writer_reftable_block_stats(w, typ); 836 block_typ_off = (bstats->blocks == 0) ? w->next : 0; 837 if (block_typ_off > 0) 838 bstats->offset = block_typ_off; 839 bstats->entries += w->block_writer->entries; 840 bstats->restarts += w->block_writer->restart_len; 841 bstats->blocks++; 842 w->stats.blocks++; 843 844 /* 845 * If this is the first block we're writing to the table then we need 846 * to also write the reftable header. 847 */ 848 if (!w->next) 849 writer_write_header(w, w->block); 850 851 err = padded_write(w, w->block, raw_bytes, padding); 852 if (err < 0) 853 return err; 854 855 /* 856 * Add an index record for every block that we're writing. If we end up 857 * having more than a threshold of index records we will end up writing 858 * an index section in `writer_finish_section()`. Each index record 859 * contains the last record key of the block it is indexing as well as 860 * the offset of that block. 861 * 862 * Note that this also applies when flushing index blocks, in which 863 * case we will end up with a multi-level index. 864 */ 865 REFTABLE_ALLOC_GROW_OR_NULL(w->index, w->index_len + 1, w->index_cap); 866 if (!w->index) 867 return REFTABLE_OUT_OF_MEMORY_ERROR; 868 869 index_record.offset = w->next; 870 reftable_buf_reset(&index_record.last_key); 871 err = reftable_buf_add(&index_record.last_key, w->block_writer->last_key.buf, 872 w->block_writer->last_key.len); 873 if (err < 0) 874 return err; 875 w->index[w->index_len] = index_record; 876 w->index_len++; 877 878 w->next += padding + raw_bytes; 879 w->block_writer = NULL; 880 881 return 0; 882 } 883 884 static int writer_flush_block(struct reftable_writer *w) 885 { 886 if (!w->block_writer) 887 return 0; 888 if (w->block_writer->entries == 0) 889 return 0; 890 return writer_flush_nonempty_block(w); 891 } 892 893 const struct reftable_stats *reftable_writer_stats(struct reftable_writer *w) 894 { 895 return &w->stats; 896 }