2 Copyright 2020 Google LLC
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
15 #include "reftable-error.h"
16 #include "reftable-record.h"
17 #include "reftable-merged.h"
20 static int stack_try_add(struct reftable_stack
*st
,
21 int (*write_table
)(struct reftable_writer
*wr
,
24 static int stack_write_compact(struct reftable_stack
*st
,
25 struct reftable_writer
*wr
, int first
, int last
,
26 struct reftable_log_expiry_config
*config
);
27 static int stack_check_addition(struct reftable_stack
*st
,
28 const char *new_tab_name
);
29 static void reftable_addition_close(struct reftable_addition
*add
);
30 static int reftable_stack_reload_maybe_reuse(struct reftable_stack
*st
,
33 static void stack_filename(struct strbuf
*dest
, struct reftable_stack
*st
,
37 strbuf_addstr(dest
, st
->reftable_dir
);
38 strbuf_addstr(dest
, "/");
39 strbuf_addstr(dest
, name
);
42 static ssize_t
reftable_fd_write(void *arg
, const void *data
, size_t sz
)
44 int *fdp
= (int *)arg
;
45 return write(*fdp
, data
, sz
);
48 int reftable_new_stack(struct reftable_stack
**dest
, const char *dir
,
49 struct reftable_write_options config
)
51 struct reftable_stack
*p
=
52 reftable_calloc(sizeof(struct reftable_stack
));
53 struct strbuf list_file_name
= STRBUF_INIT
;
56 if (config
.hash_id
== 0) {
57 config
.hash_id
= GIT_SHA1_FORMAT_ID
;
62 strbuf_reset(&list_file_name
);
63 strbuf_addstr(&list_file_name
, dir
);
64 strbuf_addstr(&list_file_name
, "/tables.list");
66 p
->list_file
= strbuf_detach(&list_file_name
, NULL
);
67 p
->reftable_dir
= xstrdup(dir
);
70 err
= reftable_stack_reload_maybe_reuse(p
, 1);
72 reftable_stack_destroy(p
);
79 static int fd_read_lines(int fd
, char ***namesp
)
81 off_t size
= lseek(fd
, 0, SEEK_END
);
85 err
= REFTABLE_IO_ERROR
;
88 err
= lseek(fd
, 0, SEEK_SET
);
90 err
= REFTABLE_IO_ERROR
;
94 buf
= reftable_malloc(size
+ 1);
95 if (read(fd
, buf
, size
) != size
) {
96 err
= REFTABLE_IO_ERROR
;
101 parse_names(buf
, size
, namesp
);
108 int read_lines(const char *filename
, char ***namesp
)
110 int fd
= open(filename
, O_RDONLY
);
113 if (errno
== ENOENT
) {
114 *namesp
= reftable_calloc(sizeof(char *));
118 return REFTABLE_IO_ERROR
;
120 err
= fd_read_lines(fd
, namesp
);
125 struct reftable_merged_table
*
126 reftable_stack_merged_table(struct reftable_stack
*st
)
131 static int has_name(char **names
, const char *name
)
134 if (!strcmp(*names
, name
))
141 /* Close and free the stack */
142 void reftable_stack_destroy(struct reftable_stack
*st
)
147 reftable_merged_table_free(st
->merged
);
151 err
= read_lines(st
->list_file
, &names
);
153 FREE_AND_NULL(names
);
158 struct strbuf filename
= STRBUF_INIT
;
159 for (i
= 0; i
< st
->readers_len
; i
++) {
160 const char *name
= reader_name(st
->readers
[i
]);
161 strbuf_reset(&filename
);
162 if (names
&& !has_name(names
, name
)) {
163 stack_filename(&filename
, st
, name
);
165 reftable_reader_free(st
->readers
[i
]);
168 /* On Windows, can only unlink after closing. */
169 unlink(filename
.buf
);
172 strbuf_release(&filename
);
174 FREE_AND_NULL(st
->readers
);
176 FREE_AND_NULL(st
->list_file
);
177 FREE_AND_NULL(st
->reftable_dir
);
182 static struct reftable_reader
**stack_copy_readers(struct reftable_stack
*st
,
185 struct reftable_reader
**cur
=
186 reftable_calloc(sizeof(struct reftable_reader
*) * cur_len
);
188 for (i
= 0; i
< cur_len
; i
++) {
189 cur
[i
] = st
->readers
[i
];
194 static int reftable_stack_reload_once(struct reftable_stack
*st
, char **names
,
197 int cur_len
= !st
->merged
? 0 : st
->merged
->stack_len
;
198 struct reftable_reader
**cur
= stack_copy_readers(st
, cur_len
);
200 int names_len
= names_length(names
);
201 struct reftable_reader
**new_readers
=
202 reftable_calloc(sizeof(struct reftable_reader
*) * names_len
);
203 struct reftable_table
*new_tables
=
204 reftable_calloc(sizeof(struct reftable_table
) * names_len
);
205 int new_readers_len
= 0;
206 struct reftable_merged_table
*new_merged
= NULL
;
210 struct reftable_reader
*rd
= NULL
;
211 char *name
= *names
++;
213 /* this is linear; we assume compaction keeps the number of
214 tables under control so this is not quadratic. */
216 for (j
= 0; reuse_open
&& j
< cur_len
; j
++) {
217 if (cur
[j
] && 0 == strcmp(cur
[j
]->name
, name
)) {
225 struct reftable_block_source src
= { NULL
};
226 struct strbuf table_path
= STRBUF_INIT
;
227 stack_filename(&table_path
, st
, name
);
229 err
= reftable_block_source_from_file(&src
,
231 strbuf_release(&table_path
);
236 err
= reftable_new_reader(&rd
, &src
, name
);
241 new_readers
[new_readers_len
] = rd
;
242 reftable_table_from_reader(&new_tables
[new_readers_len
], rd
);
247 err
= reftable_new_merged_table(&new_merged
, new_tables
,
248 new_readers_len
, st
->config
.hash_id
);
253 st
->readers_len
= new_readers_len
;
255 merged_table_release(st
->merged
);
256 reftable_merged_table_free(st
->merged
);
259 reftable_free(st
->readers
);
261 st
->readers
= new_readers
;
265 new_merged
->suppress_deletions
= 1;
266 st
->merged
= new_merged
;
267 for (i
= 0; i
< cur_len
; i
++) {
269 const char *name
= reader_name(cur
[i
]);
270 struct strbuf filename
= STRBUF_INIT
;
271 stack_filename(&filename
, st
, name
);
273 reader_close(cur
[i
]);
274 reftable_reader_free(cur
[i
]);
276 /* On Windows, can only unlink after closing. */
277 unlink(filename
.buf
);
279 strbuf_release(&filename
);
284 for (i
= 0; i
< new_readers_len
; i
++) {
285 reader_close(new_readers
[i
]);
286 reftable_reader_free(new_readers
[i
]);
288 reftable_free(new_readers
);
289 reftable_free(new_tables
);
294 /* return negative if a before b. */
295 static int tv_cmp(struct timeval
*a
, struct timeval
*b
)
297 time_t diff
= a
->tv_sec
- b
->tv_sec
;
298 int udiff
= a
->tv_usec
- b
->tv_usec
;
306 static int reftable_stack_reload_maybe_reuse(struct reftable_stack
*st
,
309 struct timeval deadline
= { 0 };
310 int err
= gettimeofday(&deadline
, NULL
);
316 deadline
.tv_sec
+= 3;
319 char **names_after
= NULL
;
320 struct timeval now
= { 0 };
321 int err
= gettimeofday(&now
, NULL
);
327 /* Only look at deadlines after the first few times. This
328 simplifies debugging in GDB */
330 if (tries
> 3 && tv_cmp(&now
, &deadline
) >= 0) {
334 err
= read_lines(st
->list_file
, &names
);
339 err
= reftable_stack_reload_once(st
, names
, reuse_open
);
344 if (err
!= REFTABLE_NOT_EXIST_ERROR
) {
349 /* err == REFTABLE_NOT_EXIST_ERROR can be caused by a concurrent
350 writer. Check if there was one by checking if the name list
353 err2
= read_lines(st
->list_file
, &names_after
);
359 if (names_equal(names_after
, names
)) {
361 free_names(names_after
);
365 free_names(names_after
);
367 delay
= delay
+ (delay
* rand()) / RAND_MAX
+ 1;
368 sleep_millisec(delay
);
377 static int stack_uptodate(struct reftable_stack
*st
)
380 int err
= read_lines(st
->list_file
, &names
);
385 for (i
= 0; i
< st
->readers_len
; i
++) {
391 if (strcmp(st
->readers
[i
]->name
, names
[i
])) {
397 if (names
[st
->merged
->stack_len
]) {
407 int reftable_stack_reload(struct reftable_stack
*st
)
409 int err
= stack_uptodate(st
);
411 return reftable_stack_reload_maybe_reuse(st
, 1);
415 int reftable_stack_add(struct reftable_stack
*st
,
416 int (*write
)(struct reftable_writer
*wr
, void *arg
),
419 int err
= stack_try_add(st
, write
, arg
);
421 if (err
== REFTABLE_LOCK_ERROR
) {
422 /* Ignore error return, we want to propagate
425 reftable_stack_reload(st
);
430 if (!st
->disable_auto_compact
)
431 return reftable_stack_auto_compact(st
);
436 static void format_name(struct strbuf
*dest
, uint64_t min
, uint64_t max
)
439 uint32_t rnd
= (uint32_t)rand();
440 snprintf(buf
, sizeof(buf
), "0x%012" PRIx64
"-0x%012" PRIx64
"-%08x",
443 strbuf_addstr(dest
, buf
);
446 struct reftable_addition
{
448 struct strbuf lock_file_name
;
449 struct reftable_stack
*stack
;
453 uint64_t next_update_index
;
456 #define REFTABLE_ADDITION_INIT \
458 .lock_file_name = STRBUF_INIT \
461 static int reftable_stack_init_addition(struct reftable_addition
*add
,
462 struct reftable_stack
*st
)
467 strbuf_reset(&add
->lock_file_name
);
468 strbuf_addstr(&add
->lock_file_name
, st
->list_file
);
469 strbuf_addstr(&add
->lock_file_name
, ".lock");
471 add
->lock_file_fd
= open(add
->lock_file_name
.buf
,
472 O_EXCL
| O_CREAT
| O_WRONLY
, 0644);
473 if (add
->lock_file_fd
< 0) {
474 if (errno
== EEXIST
) {
475 err
= REFTABLE_LOCK_ERROR
;
477 err
= REFTABLE_IO_ERROR
;
481 err
= stack_uptodate(st
);
486 err
= REFTABLE_LOCK_ERROR
;
490 add
->next_update_index
= reftable_stack_next_update_index(st
);
493 reftable_addition_close(add
);
498 static void reftable_addition_close(struct reftable_addition
*add
)
501 struct strbuf nm
= STRBUF_INIT
;
502 for (i
= 0; i
< add
->new_tables_len
; i
++) {
503 stack_filename(&nm
, add
->stack
, add
->new_tables
[i
]);
505 reftable_free(add
->new_tables
[i
]);
506 add
->new_tables
[i
] = NULL
;
508 reftable_free(add
->new_tables
);
509 add
->new_tables
= NULL
;
510 add
->new_tables_len
= 0;
512 if (add
->lock_file_fd
> 0) {
513 close(add
->lock_file_fd
);
514 add
->lock_file_fd
= 0;
516 if (add
->lock_file_name
.len
> 0) {
517 unlink(add
->lock_file_name
.buf
);
518 strbuf_release(&add
->lock_file_name
);
524 void reftable_addition_destroy(struct reftable_addition
*add
)
529 reftable_addition_close(add
);
533 int reftable_addition_commit(struct reftable_addition
*add
)
535 struct strbuf table_list
= STRBUF_INIT
;
538 if (add
->new_tables_len
== 0)
541 for (i
= 0; i
< add
->stack
->merged
->stack_len
; i
++) {
542 strbuf_addstr(&table_list
, add
->stack
->readers
[i
]->name
);
543 strbuf_addstr(&table_list
, "\n");
545 for (i
= 0; i
< add
->new_tables_len
; i
++) {
546 strbuf_addstr(&table_list
, add
->new_tables
[i
]);
547 strbuf_addstr(&table_list
, "\n");
550 err
= write(add
->lock_file_fd
, table_list
.buf
, table_list
.len
);
551 strbuf_release(&table_list
);
553 err
= REFTABLE_IO_ERROR
;
557 err
= close(add
->lock_file_fd
);
558 add
->lock_file_fd
= 0;
560 err
= REFTABLE_IO_ERROR
;
564 err
= rename(add
->lock_file_name
.buf
, add
->stack
->list_file
);
566 err
= REFTABLE_IO_ERROR
;
570 /* success, no more state to clean up. */
571 strbuf_release(&add
->lock_file_name
);
572 for (i
= 0; i
< add
->new_tables_len
; i
++) {
573 reftable_free(add
->new_tables
[i
]);
575 reftable_free(add
->new_tables
);
576 add
->new_tables
= NULL
;
577 add
->new_tables_len
= 0;
579 err
= reftable_stack_reload(add
->stack
);
581 reftable_addition_close(add
);
585 int reftable_stack_new_addition(struct reftable_addition
**dest
,
586 struct reftable_stack
*st
)
589 struct reftable_addition empty
= REFTABLE_ADDITION_INIT
;
590 *dest
= reftable_calloc(sizeof(**dest
));
592 err
= reftable_stack_init_addition(*dest
, st
);
594 reftable_free(*dest
);
600 static int stack_try_add(struct reftable_stack
*st
,
601 int (*write_table
)(struct reftable_writer
*wr
,
605 struct reftable_addition add
= REFTABLE_ADDITION_INIT
;
606 int err
= reftable_stack_init_addition(&add
, st
);
610 err
= REFTABLE_LOCK_ERROR
;
614 err
= reftable_addition_add(&add
, write_table
, arg
);
618 err
= reftable_addition_commit(&add
);
620 reftable_addition_close(&add
);
624 int reftable_addition_add(struct reftable_addition
*add
,
625 int (*write_table
)(struct reftable_writer
*wr
,
629 struct strbuf temp_tab_file_name
= STRBUF_INIT
;
630 struct strbuf tab_file_name
= STRBUF_INIT
;
631 struct strbuf next_name
= STRBUF_INIT
;
632 struct reftable_writer
*wr
= NULL
;
636 strbuf_reset(&next_name
);
637 format_name(&next_name
, add
->next_update_index
, add
->next_update_index
);
639 stack_filename(&temp_tab_file_name
, add
->stack
, next_name
.buf
);
640 strbuf_addstr(&temp_tab_file_name
, ".temp.XXXXXX");
642 tab_fd
= mkstemp(temp_tab_file_name
.buf
);
644 err
= REFTABLE_IO_ERROR
;
648 wr
= reftable_new_writer(reftable_fd_write
, &tab_fd
,
649 &add
->stack
->config
);
650 err
= write_table(wr
, arg
);
654 err
= reftable_writer_close(wr
);
655 if (err
== REFTABLE_EMPTY_TABLE_ERROR
) {
665 err
= REFTABLE_IO_ERROR
;
669 err
= stack_check_addition(add
->stack
, temp_tab_file_name
.buf
);
673 if (wr
->min_update_index
< add
->next_update_index
) {
674 err
= REFTABLE_API_ERROR
;
678 format_name(&next_name
, wr
->min_update_index
, wr
->max_update_index
);
679 strbuf_addstr(&next_name
, ".ref");
681 stack_filename(&tab_file_name
, add
->stack
, next_name
.buf
);
684 On windows, this relies on rand() picking a unique destination name.
685 Maybe we should do retry loop as well?
687 err
= rename(temp_tab_file_name
.buf
, tab_file_name
.buf
);
689 err
= REFTABLE_IO_ERROR
;
693 add
->new_tables
= reftable_realloc(add
->new_tables
,
694 sizeof(*add
->new_tables
) *
695 (add
->new_tables_len
+ 1));
696 add
->new_tables
[add
->new_tables_len
] = strbuf_detach(&next_name
, NULL
);
697 add
->new_tables_len
++;
703 if (temp_tab_file_name
.len
> 0) {
704 unlink(temp_tab_file_name
.buf
);
707 strbuf_release(&temp_tab_file_name
);
708 strbuf_release(&tab_file_name
);
709 strbuf_release(&next_name
);
710 reftable_writer_free(wr
);
714 uint64_t reftable_stack_next_update_index(struct reftable_stack
*st
)
716 int sz
= st
->merged
->stack_len
;
718 return reftable_reader_max_update_index(st
->readers
[sz
- 1]) +
723 static int stack_compact_locked(struct reftable_stack
*st
, int first
, int last
,
724 struct strbuf
*temp_tab
,
725 struct reftable_log_expiry_config
*config
)
727 struct strbuf next_name
= STRBUF_INIT
;
729 struct reftable_writer
*wr
= NULL
;
732 format_name(&next_name
,
733 reftable_reader_min_update_index(st
->readers
[first
]),
734 reftable_reader_max_update_index(st
->readers
[last
]));
736 stack_filename(temp_tab
, st
, next_name
.buf
);
737 strbuf_addstr(temp_tab
, ".temp.XXXXXX");
739 tab_fd
= mkstemp(temp_tab
->buf
);
740 wr
= reftable_new_writer(reftable_fd_write
, &tab_fd
, &st
->config
);
742 err
= stack_write_compact(st
, wr
, first
, last
, config
);
745 err
= reftable_writer_close(wr
);
753 reftable_writer_free(wr
);
758 if (err
!= 0 && temp_tab
->len
> 0) {
759 unlink(temp_tab
->buf
);
760 strbuf_release(temp_tab
);
762 strbuf_release(&next_name
);
766 static int stack_write_compact(struct reftable_stack
*st
,
767 struct reftable_writer
*wr
, int first
, int last
,
768 struct reftable_log_expiry_config
*config
)
770 int subtabs_len
= last
- first
+ 1;
771 struct reftable_table
*subtabs
= reftable_calloc(
772 sizeof(struct reftable_table
) * (last
- first
+ 1));
773 struct reftable_merged_table
*mt
= NULL
;
775 struct reftable_iterator it
= { NULL
};
776 struct reftable_ref_record ref
= { NULL
};
777 struct reftable_log_record log
= { NULL
};
779 uint64_t entries
= 0;
782 for (i
= first
, j
= 0; i
<= last
; i
++) {
783 struct reftable_reader
*t
= st
->readers
[i
];
784 reftable_table_from_reader(&subtabs
[j
++], t
);
785 st
->stats
.bytes
+= t
->size
;
787 reftable_writer_set_limits(wr
, st
->readers
[first
]->min_update_index
,
788 st
->readers
[last
]->max_update_index
);
790 err
= reftable_new_merged_table(&mt
, subtabs
, subtabs_len
,
793 reftable_free(subtabs
);
797 err
= reftable_merged_table_seek_ref(mt
, &it
, "");
802 err
= reftable_iterator_next_ref(&it
, &ref
);
811 if (first
== 0 && reftable_ref_record_is_deletion(&ref
)) {
815 err
= reftable_writer_add_ref(wr
, &ref
);
821 reftable_iterator_destroy(&it
);
823 err
= reftable_merged_table_seek_log(mt
, &it
, "");
828 err
= reftable_iterator_next_log(&it
, &log
);
836 if (first
== 0 && reftable_log_record_is_deletion(&log
)) {
840 if (config
&& config
->min_update_index
> 0 &&
841 log
.update_index
< config
->min_update_index
) {
845 if (config
&& config
->time
> 0 &&
846 log
.value
.update
.time
< config
->time
) {
850 err
= reftable_writer_add_log(wr
, &log
);
858 reftable_iterator_destroy(&it
);
860 merged_table_release(mt
);
861 reftable_merged_table_free(mt
);
863 reftable_ref_record_release(&ref
);
864 reftable_log_record_release(&log
);
865 st
->stats
.entries_written
+= entries
;
869 /* < 0: error. 0 == OK, > 0 attempt failed; could retry. */
870 static int stack_compact_range(struct reftable_stack
*st
, int first
, int last
,
871 struct reftable_log_expiry_config
*expiry
)
873 struct strbuf temp_tab_file_name
= STRBUF_INIT
;
874 struct strbuf new_table_name
= STRBUF_INIT
;
875 struct strbuf lock_file_name
= STRBUF_INIT
;
876 struct strbuf ref_list_contents
= STRBUF_INIT
;
877 struct strbuf new_table_path
= STRBUF_INIT
;
880 int lock_file_fd
= 0;
881 int compact_count
= last
- first
+ 1;
883 char **delete_on_success
=
884 reftable_calloc(sizeof(char *) * (compact_count
+ 1));
885 char **subtable_locks
=
886 reftable_calloc(sizeof(char *) * (compact_count
+ 1));
889 int is_empty_table
= 0;
891 if (first
> last
|| (!expiry
&& first
== last
)) {
896 st
->stats
.attempts
++;
898 strbuf_reset(&lock_file_name
);
899 strbuf_addstr(&lock_file_name
, st
->list_file
);
900 strbuf_addstr(&lock_file_name
, ".lock");
903 open(lock_file_name
.buf
, O_EXCL
| O_CREAT
| O_WRONLY
, 0644);
904 if (lock_file_fd
< 0) {
905 if (errno
== EEXIST
) {
908 err
= REFTABLE_IO_ERROR
;
912 /* Don't want to write to the lock for now. */
917 err
= stack_uptodate(st
);
921 for (i
= first
, j
= 0; i
<= last
; i
++) {
922 struct strbuf subtab_file_name
= STRBUF_INIT
;
923 struct strbuf subtab_lock
= STRBUF_INIT
;
924 int sublock_file_fd
= -1;
926 stack_filename(&subtab_file_name
, st
,
927 reader_name(st
->readers
[i
]));
929 strbuf_reset(&subtab_lock
);
930 strbuf_addbuf(&subtab_lock
, &subtab_file_name
);
931 strbuf_addstr(&subtab_lock
, ".lock");
933 sublock_file_fd
= open(subtab_lock
.buf
,
934 O_EXCL
| O_CREAT
| O_WRONLY
, 0644);
935 if (sublock_file_fd
> 0) {
936 close(sublock_file_fd
);
937 } else if (sublock_file_fd
< 0) {
938 if (errno
== EEXIST
) {
941 err
= REFTABLE_IO_ERROR
;
945 subtable_locks
[j
] = subtab_lock
.buf
;
946 delete_on_success
[j
] = subtab_file_name
.buf
;
953 err
= unlink(lock_file_name
.buf
);
958 err
= stack_compact_locked(st
, first
, last
, &temp_tab_file_name
,
960 /* Compaction + tombstones can create an empty table out of non-empty
962 is_empty_table
= (err
== REFTABLE_EMPTY_TABLE_ERROR
);
963 if (is_empty_table
) {
970 open(lock_file_name
.buf
, O_EXCL
| O_CREAT
| O_WRONLY
, 0644);
971 if (lock_file_fd
< 0) {
972 if (errno
== EEXIST
) {
975 err
= REFTABLE_IO_ERROR
;
981 format_name(&new_table_name
, st
->readers
[first
]->min_update_index
,
982 st
->readers
[last
]->max_update_index
);
983 strbuf_addstr(&new_table_name
, ".ref");
985 stack_filename(&new_table_path
, st
, new_table_name
.buf
);
987 if (!is_empty_table
) {
989 err
= rename(temp_tab_file_name
.buf
, new_table_path
.buf
);
991 err
= REFTABLE_IO_ERROR
;
996 for (i
= 0; i
< first
; i
++) {
997 strbuf_addstr(&ref_list_contents
, st
->readers
[i
]->name
);
998 strbuf_addstr(&ref_list_contents
, "\n");
1000 if (!is_empty_table
) {
1001 strbuf_addbuf(&ref_list_contents
, &new_table_name
);
1002 strbuf_addstr(&ref_list_contents
, "\n");
1004 for (i
= last
+ 1; i
< st
->merged
->stack_len
; i
++) {
1005 strbuf_addstr(&ref_list_contents
, st
->readers
[i
]->name
);
1006 strbuf_addstr(&ref_list_contents
, "\n");
1009 err
= write(lock_file_fd
, ref_list_contents
.buf
, ref_list_contents
.len
);
1011 err
= REFTABLE_IO_ERROR
;
1012 unlink(new_table_path
.buf
);
1015 err
= close(lock_file_fd
);
1018 err
= REFTABLE_IO_ERROR
;
1019 unlink(new_table_path
.buf
);
1023 err
= rename(lock_file_name
.buf
, st
->list_file
);
1025 err
= REFTABLE_IO_ERROR
;
1026 unlink(new_table_path
.buf
);
1031 /* Reload the stack before deleting. On windows, we can only delete the
1032 files after we closed them.
1034 err
= reftable_stack_reload_maybe_reuse(st
, first
< last
);
1036 listp
= delete_on_success
;
1038 if (strcmp(*listp
, new_table_path
.buf
)) {
1045 free_names(delete_on_success
);
1047 listp
= subtable_locks
;
1052 free_names(subtable_locks
);
1053 if (lock_file_fd
> 0) {
1054 close(lock_file_fd
);
1058 unlink(lock_file_name
.buf
);
1060 strbuf_release(&new_table_name
);
1061 strbuf_release(&new_table_path
);
1062 strbuf_release(&ref_list_contents
);
1063 strbuf_release(&temp_tab_file_name
);
1064 strbuf_release(&lock_file_name
);
1068 int reftable_stack_compact_all(struct reftable_stack
*st
,
1069 struct reftable_log_expiry_config
*config
)
1071 return stack_compact_range(st
, 0, st
->merged
->stack_len
- 1, config
);
1074 static int stack_compact_range_stats(struct reftable_stack
*st
, int first
,
1076 struct reftable_log_expiry_config
*config
)
1078 int err
= stack_compact_range(st
, first
, last
, config
);
1080 st
->stats
.failures
++;
1085 static int segment_size(struct segment
*s
)
1087 return s
->end
- s
->start
;
1090 int fastlog2(uint64_t sz
)
1095 for (; sz
; sz
/= 2) {
1101 struct segment
*sizes_to_segments(int *seglen
, uint64_t *sizes
, int n
)
1103 struct segment
*segs
= reftable_calloc(sizeof(struct segment
) * n
);
1105 struct segment cur
= { 0 };
1112 for (i
= 0; i
< n
; i
++) {
1113 int log
= fastlog2(sizes
[i
]);
1114 if (cur
.log
!= log
&& cur
.bytes
> 0) {
1115 struct segment fresh
= {
1125 cur
.bytes
+= sizes
[i
];
1132 struct segment
suggest_compaction_segment(uint64_t *sizes
, int n
)
1135 struct segment
*segs
= sizes_to_segments(&seglen
, sizes
, n
);
1136 struct segment min_seg
= {
1140 for (i
= 0; i
< seglen
; i
++) {
1141 if (segment_size(&segs
[i
]) == 1) {
1145 if (segs
[i
].log
< min_seg
.log
) {
1150 while (min_seg
.start
> 0) {
1151 int prev
= min_seg
.start
- 1;
1152 if (fastlog2(min_seg
.bytes
) < fastlog2(sizes
[prev
])) {
1156 min_seg
.start
= prev
;
1157 min_seg
.bytes
+= sizes
[prev
];
1160 reftable_free(segs
);
1164 static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack
*st
)
1167 reftable_calloc(sizeof(uint64_t) * st
->merged
->stack_len
);
1168 int version
= (st
->config
.hash_id
== GIT_SHA1_FORMAT_ID
) ? 1 : 2;
1169 int overhead
= header_size(version
) - 1;
1171 for (i
= 0; i
< st
->merged
->stack_len
; i
++) {
1172 sizes
[i
] = st
->readers
[i
]->size
- overhead
;
1177 int reftable_stack_auto_compact(struct reftable_stack
*st
)
1179 uint64_t *sizes
= stack_table_sizes_for_compaction(st
);
1180 struct segment seg
=
1181 suggest_compaction_segment(sizes
, st
->merged
->stack_len
);
1182 reftable_free(sizes
);
1183 if (segment_size(&seg
) > 0)
1184 return stack_compact_range_stats(st
, seg
.start
, seg
.end
- 1,
1190 struct reftable_compaction_stats
*
1191 reftable_stack_compaction_stats(struct reftable_stack
*st
)
1196 int reftable_stack_read_ref(struct reftable_stack
*st
, const char *refname
,
1197 struct reftable_ref_record
*ref
)
1199 struct reftable_table tab
= { NULL
};
1200 reftable_table_from_merged_table(&tab
, reftable_stack_merged_table(st
));
1201 return reftable_table_read_ref(&tab
, refname
, ref
);
1204 int reftable_stack_read_log(struct reftable_stack
*st
, const char *refname
,
1205 struct reftable_log_record
*log
)
1207 struct reftable_iterator it
= { NULL
};
1208 struct reftable_merged_table
*mt
= reftable_stack_merged_table(st
);
1209 int err
= reftable_merged_table_seek_log(mt
, &it
, refname
);
1213 err
= reftable_iterator_next_log(&it
, log
);
1217 if (strcmp(log
->refname
, refname
) ||
1218 reftable_log_record_is_deletion(log
)) {
1225 reftable_log_record_release(log
);
1227 reftable_iterator_destroy(&it
);
1231 static int stack_check_addition(struct reftable_stack
*st
,
1232 const char *new_tab_name
)
1235 struct reftable_block_source src
= { NULL
};
1236 struct reftable_reader
*rd
= NULL
;
1237 struct reftable_table tab
= { NULL
};
1238 struct reftable_ref_record
*refs
= NULL
;
1239 struct reftable_iterator it
= { NULL
};
1244 if (st
->config
.skip_name_check
)
1247 err
= reftable_block_source_from_file(&src
, new_tab_name
);
1251 err
= reftable_new_reader(&rd
, &src
, new_tab_name
);
1255 err
= reftable_reader_seek_ref(rd
, &it
, "");
1264 struct reftable_ref_record ref
= { NULL
};
1265 err
= reftable_iterator_next_ref(&it
, &ref
);
1274 refs
= reftable_realloc(refs
, cap
* sizeof(refs
[0]));
1280 reftable_table_from_merged_table(&tab
, reftable_stack_merged_table(st
));
1282 err
= validate_ref_record_addition(tab
, refs
, len
);
1285 for (i
= 0; i
< len
; i
++) {
1286 reftable_ref_record_release(&refs
[i
]);
1290 reftable_iterator_destroy(&it
);
1291 reftable_reader_free(rd
);
1295 static int is_table_name(const char *s
)
1297 const char *dot
= strrchr(s
, '.');
1298 return dot
&& !strcmp(dot
, ".ref");
1301 static void remove_maybe_stale_table(struct reftable_stack
*st
, uint64_t max
,
1305 uint64_t update_idx
= 0;
1306 struct reftable_block_source src
= { NULL
};
1307 struct reftable_reader
*rd
= NULL
;
1308 struct strbuf table_path
= STRBUF_INIT
;
1309 stack_filename(&table_path
, st
, name
);
1311 err
= reftable_block_source_from_file(&src
, table_path
.buf
);
1315 err
= reftable_new_reader(&rd
, &src
, name
);
1319 update_idx
= reftable_reader_max_update_index(rd
);
1320 reftable_reader_free(rd
);
1322 if (update_idx
<= max
) {
1323 unlink(table_path
.buf
);
1326 strbuf_release(&table_path
);
1329 static int reftable_stack_clean_locked(struct reftable_stack
*st
)
1331 uint64_t max
= reftable_merged_table_max_update_index(
1332 reftable_stack_merged_table(st
));
1333 DIR *dir
= opendir(st
->reftable_dir
);
1334 struct dirent
*d
= NULL
;
1336 return REFTABLE_IO_ERROR
;
1339 while ((d
= readdir(dir
))) {
1342 if (!is_table_name(d
->d_name
))
1345 for (i
= 0; !found
&& i
< st
->readers_len
; i
++) {
1346 found
= !strcmp(reader_name(st
->readers
[i
]), d
->d_name
);
1351 remove_maybe_stale_table(st
, max
, d
->d_name
);
1358 int reftable_stack_clean(struct reftable_stack
*st
)
1360 struct reftable_addition
*add
= NULL
;
1361 int err
= reftable_stack_new_addition(&add
, st
);
1366 err
= reftable_stack_reload(st
);
1371 err
= reftable_stack_clean_locked(st
);
1374 reftable_addition_destroy(add
);
1378 int reftable_stack_print_directory(const char *stackdir
, uint32_t hash_id
)
1380 struct reftable_stack
*stack
= NULL
;
1381 struct reftable_write_options cfg
= { .hash_id
= hash_id
};
1382 struct reftable_merged_table
*merged
= NULL
;
1383 struct reftable_table table
= { NULL
};
1385 int err
= reftable_new_stack(&stack
, stackdir
, cfg
);
1389 merged
= reftable_stack_merged_table(stack
);
1390 reftable_table_from_merged_table(&table
, merged
);
1391 err
= reftable_table_print(&table
);
1394 reftable_stack_destroy(stack
);