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 REFTABLE_ALLOC_GROW(w
->restarts
, w
->restart_len
+ 1, w
->restart_cap
);
55 w
->restarts
[w
->restart_len
++] = w
->next
;
60 strbuf_reset(&w
->last_key
);
61 strbuf_addbuf(&w
->last_key
, key
);
66 void block_writer_init(struct block_writer
*bw
, uint8_t typ
, uint8_t *buf
,
67 uint32_t block_size
, uint32_t header_off
, int hash_size
)
70 bw
->hash_size
= hash_size
;
71 bw
->block_size
= block_size
;
72 bw
->header_off
= header_off
;
73 bw
->buf
[header_off
] = typ
;
74 bw
->next
= header_off
+ 4;
75 bw
->restart_interval
= 16;
81 uint8_t block_writer_type(struct block_writer
*bw
)
83 return bw
->buf
[bw
->header_off
];
86 /* Adds the reftable_record to the block. Returns -1 if it does not fit, 0 on
87 success. Returns REFTABLE_API_ERROR if attempting to write a record with
89 int block_writer_add(struct block_writer
*w
, struct reftable_record
*rec
)
91 struct strbuf empty
= STRBUF_INIT
;
93 w
->entries
% w
->restart_interval
== 0 ? empty
: w
->last_key
;
94 struct string_view out
= {
95 .buf
= w
->buf
+ w
->next
,
96 .len
= w
->block_size
- w
->next
,
99 struct string_view start
= out
;
102 struct strbuf key
= STRBUF_INIT
;
106 reftable_record_key(rec
, &key
);
108 err
= REFTABLE_API_ERROR
;
112 n
= reftable_encode_key(&is_restart
, out
, last
, key
,
113 reftable_record_val_type(rec
));
116 string_view_consume(&out
, n
);
118 n
= reftable_record_encode(rec
, out
, w
->hash_size
);
121 string_view_consume(&out
, n
);
123 err
= block_writer_register_restart(w
, start
.len
- out
.len
, is_restart
,
126 strbuf_release(&key
);
130 int block_writer_finish(struct block_writer
*w
)
133 for (i
= 0; i
< w
->restart_len
; i
++) {
134 put_be24(w
->buf
+ w
->next
, w
->restarts
[i
]);
138 put_be16(w
->buf
+ w
->next
, w
->restart_len
);
140 put_be24(w
->buf
+ 1 + w
->header_off
, w
->next
);
142 if (block_writer_type(w
) == BLOCK_TYPE_LOG
) {
143 int block_header_skip
= 4 + w
->header_off
;
144 uLongf src_len
= w
->next
- block_header_skip
;
145 uLongf dest_cap
= src_len
* 1.001 + 12;
148 REFTABLE_ALLOC_ARRAY(compressed
, dest_cap
);
151 uLongf out_dest_len
= dest_cap
;
152 int zresult
= compress2(compressed
, &out_dest_len
,
153 w
->buf
+ block_header_skip
,
155 if (zresult
== Z_BUF_ERROR
&& dest_cap
< LONG_MAX
) {
158 reftable_realloc(compressed
, dest_cap
);
163 if (Z_OK
!= zresult
) {
164 reftable_free(compressed
);
165 return REFTABLE_ZLIB_ERROR
;
168 memcpy(w
->buf
+ block_header_skip
, compressed
,
170 w
->next
= out_dest_len
+ block_header_skip
;
171 reftable_free(compressed
);
178 uint8_t block_reader_type(struct block_reader
*r
)
180 return r
->block
.data
[r
->header_off
];
183 int block_reader_init(struct block_reader
*br
, struct reftable_block
*block
,
184 uint32_t header_off
, uint32_t table_block_size
,
187 uint32_t full_block_size
= table_block_size
;
188 uint8_t typ
= block
->data
[header_off
];
189 uint32_t sz
= get_be24(block
->data
+ header_off
+ 1);
191 uint16_t restart_count
= 0;
192 uint32_t restart_start
= 0;
193 uint8_t *restart_bytes
= NULL
;
194 uint8_t *uncompressed
= NULL
;
196 if (!reftable_is_block_type(typ
)) {
197 err
= REFTABLE_FORMAT_ERROR
;
201 if (typ
== BLOCK_TYPE_LOG
) {
202 int block_header_skip
= 4 + header_off
;
203 uLongf dst_len
= sz
- block_header_skip
; /* total size of dest
205 uLongf src_len
= block
->len
- block_header_skip
;
207 /* Log blocks specify the *uncompressed* size in their header. */
208 REFTABLE_ALLOC_ARRAY(uncompressed
, sz
);
210 /* Copy over the block header verbatim. It's not compressed. */
211 memcpy(uncompressed
, block
->data
, block_header_skip
);
215 uncompress2(uncompressed
+ block_header_skip
, &dst_len
,
216 block
->data
+ block_header_skip
, &src_len
)) {
217 err
= REFTABLE_ZLIB_ERROR
;
221 if (dst_len
+ block_header_skip
!= sz
) {
222 err
= REFTABLE_FORMAT_ERROR
;
226 /* We're done with the input data. */
227 reftable_block_done(block
);
228 block
->data
= uncompressed
;
231 block
->source
= malloc_block_source();
232 full_block_size
= src_len
+ block_header_skip
;
233 } else if (full_block_size
== 0) {
234 full_block_size
= sz
;
235 } else if (sz
< full_block_size
&& sz
< block
->len
&&
236 block
->data
[sz
] != 0) {
237 /* If the block is smaller than the full block size, it is
238 padded (data followed by '\0') or the next block is
240 full_block_size
= sz
;
243 restart_count
= get_be16(block
->data
+ sz
- 2);
244 restart_start
= sz
- 2 - 3 * restart_count
;
245 restart_bytes
= block
->data
+ restart_start
;
247 /* transfer ownership. */
252 br
->hash_size
= hash_size
;
253 br
->block_len
= restart_start
;
254 br
->full_block_size
= full_block_size
;
255 br
->header_off
= header_off
;
256 br
->restart_count
= restart_count
;
257 br
->restart_bytes
= restart_bytes
;
260 reftable_free(uncompressed
);
264 static uint32_t block_reader_restart_offset(struct block_reader
*br
, int i
)
266 return get_be24(br
->restart_bytes
+ 3 * i
);
269 void block_reader_start(struct block_reader
*br
, struct block_iter
*it
)
272 strbuf_reset(&it
->last_key
);
273 it
->next_off
= br
->header_off
+ 4;
276 struct restart_needle_less_args
{
278 struct strbuf needle
;
279 struct block_reader
*reader
;
282 static int restart_needle_less(size_t idx
, void *_args
)
284 struct restart_needle_less_args
*args
= _args
;
285 uint32_t off
= block_reader_restart_offset(args
->reader
, idx
);
286 struct string_view in
= {
287 .buf
= args
->reader
->block
.data
+ off
,
288 .len
= args
->reader
->block_len
- off
,
290 struct strbuf kth_restart_key
= STRBUF_INIT
;
291 uint8_t unused_extra
;
295 * TODO: The restart key is verbatim in the block, so we can in theory
296 * avoid decoding the key and thus save some allocations.
298 n
= reftable_decode_key(&kth_restart_key
, &unused_extra
, in
);
304 result
= strbuf_cmp(&args
->needle
, &kth_restart_key
);
305 strbuf_release(&kth_restart_key
);
309 void block_iter_copy_from(struct block_iter
*dest
, struct block_iter
*src
)
312 dest
->next_off
= src
->next_off
;
313 strbuf_reset(&dest
->last_key
);
314 strbuf_addbuf(&dest
->last_key
, &src
->last_key
);
317 int block_iter_next(struct block_iter
*it
, struct reftable_record
*rec
)
319 struct string_view in
= {
320 .buf
= it
->br
->block
.data
+ it
->next_off
,
321 .len
= it
->br
->block_len
- it
->next_off
,
323 struct string_view start
= in
;
327 if (it
->next_off
>= it
->br
->block_len
)
330 n
= reftable_decode_key(&it
->last_key
, &extra
, in
);
333 if (!it
->last_key
.len
)
334 return REFTABLE_FORMAT_ERROR
;
336 string_view_consume(&in
, n
);
337 n
= reftable_record_decode(rec
, it
->last_key
, extra
, in
, it
->br
->hash_size
,
341 string_view_consume(&in
, n
);
343 it
->next_off
+= start
.len
- in
.len
;
347 int block_reader_first_key(struct block_reader
*br
, struct strbuf
*key
)
349 int off
= br
->header_off
+ 4, n
;
350 struct string_view in
= {
351 .buf
= br
->block
.data
+ off
,
352 .len
= br
->block_len
- off
,
358 n
= reftable_decode_key(key
, &extra
, in
);
362 return REFTABLE_FORMAT_ERROR
;
367 int block_iter_seek(struct block_iter
*it
, struct strbuf
*want
)
369 return block_reader_seek(it
->br
, it
, want
);
372 void block_iter_close(struct block_iter
*it
)
374 strbuf_release(&it
->last_key
);
375 strbuf_release(&it
->scratch
);
378 int block_reader_seek(struct block_reader
*br
, struct block_iter
*it
,
381 struct restart_needle_less_args args
= {
385 struct block_iter next
= BLOCK_ITER_INIT
;
386 struct reftable_record rec
;
391 * Perform a binary search over the block's restart points, which
392 * avoids doing a linear scan over the whole block. Like this, we
393 * identify the section of the block that should contain our key.
395 * Note that we explicitly search for the first restart point _greater_
396 * than the sought-after record, not _greater or equal_ to it. In case
397 * the sought-after record is located directly at the restart point we
398 * would otherwise start doing the linear search at the preceding
399 * restart point. While that works alright, we would end up scanning
402 i
= binsearch(br
->restart_count
, &restart_needle_less
, &args
);
404 err
= REFTABLE_FORMAT_ERROR
;
409 * Now there are multiple cases:
411 * - `i == 0`: The wanted record is smaller than the record found at
412 * the first restart point. As the first restart point is the first
413 * record in the block, our wanted record cannot be located in this
414 * block at all. We still need to position the iterator so that the
415 * next call to `block_iter_next()` will yield an end-of-iterator
418 * - `i == restart_count`: The wanted record was not found at any of
419 * the restart points. As there is no restart point at the end of
420 * the section the record may thus be contained in the last block.
422 * - `i > 0`: The wanted record must be contained in the section
423 * before the found restart point. We thus do a linear search
424 * starting from the preceding restart point.
427 it
->next_off
= block_reader_restart_offset(br
, i
- 1);
429 it
->next_off
= br
->header_off
+ 4;
432 reftable_record_init(&rec
, block_reader_type(br
));
435 * We're looking for the last entry less than the wanted key so that
436 * the next call to `block_reader_next()` would yield the wanted
437 * record. We thus don't want to position our reader at the sought
438 * after record, but one before. To do so, we have to go one entry too
439 * far and then back up.
442 block_iter_copy_from(&next
, it
);
443 err
= block_iter_next(&next
, &rec
);
452 * Check whether the current key is greater or equal to the
453 * sought-after key. In case it is greater we know that the
454 * record does not exist in the block and can thus abort early.
455 * In case it is equal to the sought-after key we have found
456 * the desired record.
458 reftable_record_key(&rec
, &it
->last_key
);
459 if (strbuf_cmp(&it
->last_key
, want
) >= 0)
462 block_iter_copy_from(it
, &next
);
466 block_iter_close(&next
);
467 reftable_record_release(&rec
);
472 void block_writer_release(struct block_writer
*bw
)
474 FREE_AND_NULL(bw
->restarts
);
475 strbuf_release(&bw
->last_key
);
476 /* the block is not owned. */
479 void reftable_block_done(struct reftable_block
*blockp
)
481 struct reftable_block_source source
= blockp
->source
;
482 if (blockp
&& source
.ops
)
483 source
.ops
->return_block(source
.arg
, blockp
);
486 blockp
->source
.ops
= NULL
;
487 blockp
->source
.arg
= NULL
;