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
11 #include "blocksource.h"
12 #include "constants.h"
14 #include "reftable-error.h"
18 int header_size(int version
)
29 int footer_size(int version
)
40 static int block_writer_register_restart(struct block_writer
*w
, int n
,
41 int is_restart
, struct strbuf
*key
)
43 int rlen
= w
->restart_len
;
44 if (rlen
>= MAX_RESTARTS
) {
51 if (2 + 3 * rlen
+ n
> w
->block_size
- w
->next
)
54 if (w
->restart_len
== w
->restart_cap
) {
55 w
->restart_cap
= w
->restart_cap
* 2 + 1;
56 w
->restarts
= reftable_realloc(
57 w
->restarts
, sizeof(uint32_t) * w
->restart_cap
);
60 w
->restarts
[w
->restart_len
++] = w
->next
;
65 strbuf_reset(&w
->last_key
);
66 strbuf_addbuf(&w
->last_key
, key
);
71 void block_writer_init(struct block_writer
*bw
, uint8_t typ
, uint8_t *buf
,
72 uint32_t block_size
, uint32_t header_off
, int hash_size
)
75 bw
->hash_size
= hash_size
;
76 bw
->block_size
= block_size
;
77 bw
->header_off
= header_off
;
78 bw
->buf
[header_off
] = typ
;
79 bw
->next
= header_off
+ 4;
80 bw
->restart_interval
= 16;
86 uint8_t block_writer_type(struct block_writer
*bw
)
88 return bw
->buf
[bw
->header_off
];
91 /* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on
92 success. Returns REFTABLE_API_ERROR if attempting to write a record with
94 int block_writer_add(struct block_writer
*w
, struct reftable_record
*rec
)
96 struct strbuf empty
= STRBUF_INIT
;
98 w
->entries
% w
->restart_interval
== 0 ? empty
: w
->last_key
;
99 struct string_view out
= {
100 .buf
= w
->buf
+ w
->next
,
101 .len
= w
->block_size
- w
->next
,
104 struct string_view start
= out
;
107 struct strbuf key
= STRBUF_INIT
;
111 reftable_record_key(rec
, &key
);
113 err
= REFTABLE_API_ERROR
;
117 n
= reftable_encode_key(&is_restart
, out
, last
, key
,
118 reftable_record_val_type(rec
));
121 string_view_consume(&out
, n
);
123 n
= reftable_record_encode(rec
, out
, w
->hash_size
);
126 string_view_consume(&out
, n
);
128 err
= block_writer_register_restart(w
, start
.len
- out
.len
, is_restart
,
131 strbuf_release(&key
);
135 int block_writer_finish(struct block_writer
*w
)
138 for (i
= 0; i
< w
->restart_len
; i
++) {
139 put_be24(w
->buf
+ w
->next
, w
->restarts
[i
]);
143 put_be16(w
->buf
+ w
->next
, w
->restart_len
);
145 put_be24(w
->buf
+ 1 + w
->header_off
, w
->next
);
147 if (block_writer_type(w
) == BLOCK_TYPE_LOG
) {
148 int block_header_skip
= 4 + w
->header_off
;
149 uLongf src_len
= w
->next
- block_header_skip
;
150 uLongf dest_cap
= src_len
* 1.001 + 12;
152 uint8_t *compressed
= reftable_malloc(dest_cap
);
154 uLongf out_dest_len
= dest_cap
;
155 int zresult
= compress2(compressed
, &out_dest_len
,
156 w
->buf
+ block_header_skip
,
158 if (zresult
== Z_BUF_ERROR
&& dest_cap
< LONG_MAX
) {
161 reftable_realloc(compressed
, dest_cap
);
166 if (Z_OK
!= zresult
) {
167 reftable_free(compressed
);
168 return REFTABLE_ZLIB_ERROR
;
171 memcpy(w
->buf
+ block_header_skip
, compressed
,
173 w
->next
= out_dest_len
+ block_header_skip
;
174 reftable_free(compressed
);
181 uint8_t block_reader_type(struct block_reader
*r
)
183 return r
->block
.data
[r
->header_off
];
186 int block_reader_init(struct block_reader
*br
, struct reftable_block
*block
,
187 uint32_t header_off
, uint32_t table_block_size
,
190 uint32_t full_block_size
= table_block_size
;
191 uint8_t typ
= block
->data
[header_off
];
192 uint32_t sz
= get_be24(block
->data
+ header_off
+ 1);
194 uint16_t restart_count
= 0;
195 uint32_t restart_start
= 0;
196 uint8_t *restart_bytes
= NULL
;
197 uint8_t *uncompressed
= NULL
;
199 if (!reftable_is_block_type(typ
)) {
200 err
= REFTABLE_FORMAT_ERROR
;
204 if (typ
== BLOCK_TYPE_LOG
) {
205 int block_header_skip
= 4 + header_off
;
206 uLongf dst_len
= sz
- block_header_skip
; /* total size of dest
208 uLongf src_len
= block
->len
- block_header_skip
;
209 /* Log blocks specify the *uncompressed* size in their header.
211 uncompressed
= reftable_malloc(sz
);
213 /* Copy over the block header verbatim. It's not compressed. */
214 memcpy(uncompressed
, block
->data
, block_header_skip
);
218 uncompress2(uncompressed
+ block_header_skip
, &dst_len
,
219 block
->data
+ block_header_skip
, &src_len
)) {
220 err
= REFTABLE_ZLIB_ERROR
;
224 if (dst_len
+ block_header_skip
!= sz
) {
225 err
= REFTABLE_FORMAT_ERROR
;
229 /* We're done with the input data. */
230 reftable_block_done(block
);
231 block
->data
= uncompressed
;
234 block
->source
= malloc_block_source();
235 full_block_size
= src_len
+ block_header_skip
;
236 } else if (full_block_size
== 0) {
237 full_block_size
= sz
;
238 } else if (sz
< full_block_size
&& sz
< block
->len
&&
239 block
->data
[sz
] != 0) {
240 /* If the block is smaller than the full block size, it is
241 padded (data followed by '\0') or the next block is
243 full_block_size
= sz
;
246 restart_count
= get_be16(block
->data
+ sz
- 2);
247 restart_start
= sz
- 2 - 3 * restart_count
;
248 restart_bytes
= block
->data
+ restart_start
;
250 /* transfer ownership. */
255 br
->hash_size
= hash_size
;
256 br
->block_len
= restart_start
;
257 br
->full_block_size
= full_block_size
;
258 br
->header_off
= header_off
;
259 br
->restart_count
= restart_count
;
260 br
->restart_bytes
= restart_bytes
;
263 reftable_free(uncompressed
);
267 static uint32_t block_reader_restart_offset(struct block_reader
*br
, int i
)
269 return get_be24(br
->restart_bytes
+ 3 * i
);
272 void block_reader_start(struct block_reader
*br
, struct block_iter
*it
)
275 strbuf_reset(&it
->last_key
);
276 it
->next_off
= br
->header_off
+ 4;
279 struct restart_find_args
{
282 struct block_reader
*r
;
285 static int restart_key_less(size_t idx
, void *args
)
287 struct restart_find_args
*a
= args
;
288 uint32_t off
= block_reader_restart_offset(a
->r
, idx
);
289 struct string_view in
= {
290 .buf
= a
->r
->block
.data
+ off
,
291 .len
= a
->r
->block_len
- off
,
294 /* the restart key is verbatim in the block, so this could avoid the
295 alloc for decoding the key */
296 struct strbuf rkey
= STRBUF_INIT
;
297 struct strbuf last_key
= STRBUF_INIT
;
298 uint8_t unused_extra
;
299 int n
= reftable_decode_key(&rkey
, &unused_extra
, last_key
, in
);
306 result
= strbuf_cmp(&a
->key
, &rkey
);
307 strbuf_release(&rkey
);
311 void block_iter_copy_from(struct block_iter
*dest
, struct block_iter
*src
)
314 dest
->next_off
= src
->next_off
;
315 strbuf_reset(&dest
->last_key
);
316 strbuf_addbuf(&dest
->last_key
, &src
->last_key
);
319 int block_iter_next(struct block_iter
*it
, struct reftable_record
*rec
)
321 struct string_view in
= {
322 .buf
= it
->br
->block
.data
+ it
->next_off
,
323 .len
= it
->br
->block_len
- it
->next_off
,
325 struct string_view start
= in
;
329 if (it
->next_off
>= it
->br
->block_len
)
332 n
= reftable_decode_key(&it
->key
, &extra
, it
->last_key
, in
);
337 return REFTABLE_FORMAT_ERROR
;
339 string_view_consume(&in
, n
);
340 n
= reftable_record_decode(rec
, it
->key
, extra
, in
, it
->br
->hash_size
);
343 string_view_consume(&in
, n
);
345 strbuf_reset(&it
->last_key
);
346 strbuf_addbuf(&it
->last_key
, &it
->key
);
347 it
->next_off
+= start
.len
- in
.len
;
351 int block_reader_first_key(struct block_reader
*br
, struct strbuf
*key
)
353 struct strbuf empty
= STRBUF_INIT
;
354 int off
= br
->header_off
+ 4;
355 struct string_view in
= {
356 .buf
= br
->block
.data
+ off
,
357 .len
= br
->block_len
- off
,
361 int n
= reftable_decode_key(key
, &extra
, empty
, in
);
365 return REFTABLE_FORMAT_ERROR
;
370 int block_iter_seek(struct block_iter
*it
, struct strbuf
*want
)
372 return block_reader_seek(it
->br
, it
, want
);
375 void block_iter_close(struct block_iter
*it
)
377 strbuf_release(&it
->last_key
);
378 strbuf_release(&it
->key
);
381 int block_reader_seek(struct block_reader
*br
, struct block_iter
*it
,
384 struct restart_find_args args
= {
388 struct reftable_record rec
= reftable_new_record(block_reader_type(br
));
390 struct block_iter next
= BLOCK_ITER_INIT
;
392 int i
= binsearch(br
->restart_count
, &restart_key_less
, &args
);
394 err
= REFTABLE_FORMAT_ERROR
;
401 it
->next_off
= block_reader_restart_offset(br
, i
);
403 it
->next_off
= br
->header_off
+ 4;
406 /* We're looking for the last entry less/equal than the wanted key, so
407 we have to go one entry too far and then back up.
410 block_iter_copy_from(&next
, it
);
411 err
= block_iter_next(&next
, &rec
);
415 reftable_record_key(&rec
, &it
->key
);
416 if (err
> 0 || strbuf_cmp(&it
->key
, want
) >= 0) {
421 block_iter_copy_from(it
, &next
);
425 block_iter_close(&next
);
426 reftable_record_release(&rec
);
431 void block_writer_release(struct block_writer
*bw
)
433 FREE_AND_NULL(bw
->restarts
);
434 strbuf_release(&bw
->last_key
);
435 /* the block is not owned. */
438 void reftable_block_done(struct reftable_block
*blockp
)
440 struct reftable_block_source source
= blockp
->source
;
441 if (blockp
&& source
.ops
)
442 source
.ops
->return_block(source
.arg
, blockp
);
445 blockp
->source
.ops
= NULL
;
446 blockp
->source
.arg
= NULL
;