sparkey_returncode sparkey_logiter_next(sparkey_logiter *iter, sparkey_logreader *log) { if (iter->state == SPARKEY_ITER_CLOSED) { return SPARKEY_SUCCESS; } uint64_t key_remaining = 0; uint64_t value_remaining = 0; if (iter->state == SPARKEY_ITER_ACTIVE) { key_remaining = iter->key_remaining; value_remaining = iter->value_remaining; } iter->state = SPARKEY_ITER_INVALID; iter->key_remaining = 0; iter->value_remaining = 0; iter->keylen = 0; iter->valuelen = 0; RETHROW(assert_iter_open(iter, log)); RETHROW(skip(iter, log, key_remaining)); RETHROW(skip(iter, log, value_remaining)); RETHROW(ensure_available(iter, log)); if (iter->block_len - iter->block_offset == 0) { // Reached end of data iter->state = SPARKEY_ITER_CLOSED; return SPARKEY_SUCCESS; } if (log->header.compression_type == SPARKEY_COMPRESSION_NONE) { iter->block_position += iter->block_offset; iter->block_len -= iter->block_offset; iter->block_offset = 0; iter->compression_buf = &log->data[iter->block_position]; iter->entry_count = -1; } iter->entry_count++; uint64_t a = read_vlq(iter->compression_buf, &iter->block_offset); uint64_t b = read_vlq(iter->compression_buf, &iter->block_offset); if (a == 0) { iter->keylen = iter->key_remaining = b; iter->valuelen = iter->value_remaining = 0; iter->type = SPARKEY_ENTRY_DELETE; } else { iter->keylen = iter->key_remaining = a - 1; iter->valuelen = iter->value_remaining = b; iter->type = SPARKEY_ENTRY_PUT; } iter->entry_block_position = iter->block_position; iter->entry_block_offset = iter->block_offset; iter->state = SPARKEY_ITER_ACTIVE; return SPARKEY_SUCCESS; }
static sparkey_returncode skip(sparkey_logiter *iter, sparkey_logreader *log, uint64_t len) { while (len > 0) { RETHROW(ensure_available(iter, log)); uint64_t m = min64(len, iter->block_len - iter->block_offset); len -= m; iter->block_offset += m; } return SPARKEY_SUCCESS; }
void jhn__buf_append(jhn__buf_t *buf, const void *data, size_t len) { ensure_available(buf, len); if (len > 0) { assert(data); memcpy(buf->data + buf->used, data, len); buf->used += len; buf->data[buf->used] = 0; } }
static sparkey_returncode sparkey_logiter_chunk(sparkey_logiter *iter, sparkey_logreader *log, uint64_t maxlen, uint64_t *len, uint8_t ** res, uint64_t *var) { RETHROW(assert_iter_open(iter, log)); if (iter->state != SPARKEY_ITER_ACTIVE) { return SPARKEY_LOG_ITERATOR_INACTIVE; } if (*var > 0) { RETHROW(ensure_available(iter, log)); uint64_t m = min64(*var, iter->block_len - iter->block_offset); m = min64(maxlen, m); *len = m; *res = &iter->compression_buf[iter->block_offset]; iter->block_offset += m; *var -= m; return SPARKEY_SUCCESS; } *len = 0; return SPARKEY_SUCCESS; }