Commit 2f7db318 authored by Dominik Taborsky's avatar Dominik Taborsky

apply: refactoring, better use with zone_update

parent 5f0b9863
......@@ -81,8 +81,8 @@ int event_dnssec(conf_t *conf, zone_t *zone)
bool zone_changed = !changeset_empty(&ch);
if (zone_changed) {
/* Apply change. */
apply_ctx_t a_ctx = { { 0 } };
apply_init_ctx(&a_ctx, APPLY_STRICT);
apply_ctx_t a_ctx = { 0 };
apply_init_ctx(&a_ctx, NULL, APPLY_STRICT);
zone_contents_t *new_contents = NULL;
int ret = apply_changeset(&a_ctx, zone, &ch, &new_contents);
......
......@@ -372,8 +372,8 @@ static int ixfrin_finalize(struct answer_data *adata)
struct ixfr_proc *ixfr = adata->ext;
assert(ixfr->state == IXFR_DONE);
apply_ctx_t a_ctx = { { 0 } };
apply_init_ctx(&a_ctx, APPLY_STRICT);
apply_ctx_t a_ctx = { 0 };
apply_init_ctx(&a_ctx, NULL, APPLY_STRICT);
zone_contents_t *new_contents;
int ret = apply_changesets(&a_ctx, ixfr->zone, &ixfr->changesets, &new_contents);
......
This diff is collapsed.
......@@ -33,9 +33,9 @@ enum {
};
struct apply_ctx {
zone_contents_t *contents;
list_t old_data; /*!< Old data, to be freed after successful update. */
list_t new_data; /*!< New data, to be freed after failed update. */
zone_node_t *apex;
uint32_t flags;
};
......@@ -44,30 +44,71 @@ typedef struct apply_ctx apply_ctx_t;
/*!
* \brief Initialize a new context structure.
*
* \param ctx Context to be initialized.
* \param ctx Context to be initialized.
* \param contents Zone contents to apply changes onto.
* \param flags Flags to control the application process.
*/
void apply_init_ctx(apply_ctx_t *ctx, uint32_t flags);
void apply_init_ctx(apply_ctx_t *ctx, zone_contents_t *contents, uint32_t flags);
/*! \brief Removes single RR from zone contents. */
int apply_remove_rr(apply_ctx_t *ctx, zone_contents_t *contents,
const knot_rrset_t *rr);
/*!
* \brief Creates a shallow zone contents copy.
*
* \param old_contents Source.
* \param new_contents Target.
*
* \return KNOT_E*
*/
int apply_prepare_zone_copy(zone_contents_t *old_contents,
zone_contents_t **new_contents);
/*! \brief Adds a single RR into zone contents. */
int apply_add_rr(apply_ctx_t *ctx, zone_contents_t *contents,
const knot_rrset_t *rr);
/*!
* \brief Adds a single RR into zone contents.
*
* \param ctx Apply context.
* \param rr RRSet to add.
*
* \return KNOT_E*
*/
int apply_add_rr(apply_ctx_t *ctx, const knot_rrset_t *rr);
int apply_replace_soa(apply_ctx_t *ctx, zone_contents_t *contents, changeset_t *chset);
/*!
* \brief Removes single RR from zone contents.
*
* \param ctx Apply context.
* \param rr RRSet to remove.
*
* \return KNOT_E*
*/
int apply_remove_rr(apply_ctx_t *ctx, const knot_rrset_t *rr);
/*! \brief Creates a shallow zone contents copy. */
int apply_prepare_zone_copy(zone_contents_t *old_contents,
zone_contents_t **new_contents);
/*!
* \brief Adds a single RR into zone contents.
*
* \param ctx Apply context.
* \param ch
*
* \return KNOT_E*
*/
int apply_replace_soa(apply_ctx_t *ctx, changeset_t *ch);
/*!
* \brief Prepares the new zone contents for signing.
*
* Adjusted pointers are required for DNSSEC.
*
* \param ctx Apply context.
* \param ch
*
* \return KNOT_E*
*/
int apply_prepare_to_sign(apply_ctx_t *ctx);
/*!
* \brief Applies changesets *with* zone shallow copy.
* \brief Applies changesets to a shallow zone-copy.
*
* \param zone Zone to be updated.
* \param chsets Changes to be made.
* \param new_contents New zone will be returned using this arg.
* \param chsets List of changesets to be applied.
* \param new_contents Storage for the new zone contents pointer.
*
* \return KNOT_E*
*/
......@@ -75,11 +116,11 @@ int apply_changesets(apply_ctx_t *ctx, zone_t *zone, list_t *chsets,
zone_contents_t **new_contents);
/*!
* \brief Applies changeset *with* zone shallow copy.
* \brief Applies changeset to a shallow zone-copy.
*
* \param zone Zone to be updated.
* \param ch Change to be made.
* \param new_contents New zone will be returned using this arg.
* \param ch Changeset to be applied.
* \param new_contents Storage for the new zone contents pointer.
*
* \return KNOT_E*
*/
......@@ -89,26 +130,38 @@ int apply_changeset(apply_ctx_t *ctx, zone_t *zone, changeset_t *ch,
/*!
* \brief Applies changesets directly to the zone, without copying it.
*
* \param contents Zone contents to apply the changesets to. Will be modified.
* \param chsets Changesets to be applied to the zone.
* \param ctx Apply context.
* \param chsets List of changesets to be applied to the zone.
*
* \return KNOT_E*
*/
int apply_changesets_directly(apply_ctx_t *ctx, zone_contents_t *contents, list_t *chsets);
int apply_changesets_directly(apply_ctx_t *ctx, list_t *chsets);
/*!
* \brief Applies changeset directly to the zone, without copying it.
*
* \param contents Zone contents to apply the changesets to. Will be modified.
* \param chsets Changeset to be applied to the zone.
* \param ctx Apply context.
* \param ch Changeset to be applied to the zone.
*
* \return KNOT_E*
*/
int apply_changeset_directly(apply_ctx_t *ctx, zone_contents_t *contents, changeset_t *ch);
int apply_changeset_directly(apply_ctx_t *ctx, changeset_t *ch);
/*!
* \brief Cleanups successful zone update.
* \brief Finalizes the zone contents for publishing.
*
* Fully adjusts the zone.
*
* \param ctx Apply context.
*
* \return KNOT_E*
*/
int apply_finalize(apply_ctx_t *ctx);
/*!
* \brief Cleanups successful zone update.
*
* \param chgs Changeset used to create the update.
*/
void update_cleanup(apply_ctx_t *ctx);
......
......@@ -41,8 +41,7 @@ static int init_incremental(zone_update_t *update, zone_t *zone)
return ret;
}
//! \todo is there a better way?
update->a_ctx.apex = update->new_cont->apex;
apply_init_ctx(&update->a_ctx, update->new_cont, 0);
/* Copy base SOA RR. */
update->change.soa_from =
......@@ -63,6 +62,8 @@ static int init_full(zone_update_t *update, zone_t *zone)
return KNOT_ENOMEM;
}
apply_init_ctx(&update->a_ctx, update->new_cont, 0);
return KNOT_EOK;
}
......@@ -77,8 +78,6 @@ int zone_update_init(zone_update_t *update, zone_t *zone, zone_update_flags_t fl
memset(update, 0, sizeof(*update));
update->zone = zone;
apply_init_ctx(&update->a_ctx, 0);
mm_ctx_mempool(&update->mm, MM_DEFAULT_BLKSIZE);
update->flags = flags;
......@@ -184,14 +183,14 @@ int zone_update_add(zone_update_t *update, const knot_rrset_t *rrset)
if (rrset->type == KNOT_RRTYPE_SOA) {
/* replace previous SOA */
ret = apply_replace_soa(&update->a_ctx, update->new_cont, &update->change);
ret = apply_replace_soa(&update->a_ctx, &update->change);
if (ret != KNOT_EOK) {
changeset_remove_addition(&update->change, rrset);
}
return ret;
}
ret = apply_add_rr(&update->a_ctx, update->new_cont, rrset);
ret = apply_add_rr(&update->a_ctx, rrset);
if (ret != KNOT_EOK) {
changeset_remove_addition(&update->change, rrset);
return ret;
......@@ -223,7 +222,7 @@ int zone_update_remove(zone_update_t *update, const knot_rrset_t *rrset)
return KNOT_EOK;
}
ret = apply_remove_rr(&update->a_ctx, update->new_cont, rrset);
ret = apply_remove_rr(&update->a_ctx, rrset);
if (ret != KNOT_EOK) {
changeset_remove_removal(&update->change, rrset);
return ret;
......@@ -262,7 +261,7 @@ int zone_update_remove_rrset(zone_update_t *update, knot_dname_t *owner, uint16_
return KNOT_EOK;
}
ret = apply_remove_rr(&update->a_ctx, update->new_cont, &rrset);
ret = apply_remove_rr(&update->a_ctx, &rrset);
if (ret != KNOT_EOK) {
return ret;
}
......@@ -309,7 +308,7 @@ int zone_update_remove_node(zone_update_t *update, const knot_dname_t *owner)
return KNOT_EOK;
}
ret = apply_remove_rr(&update->a_ctx, update->new_cont, &rrset);
ret = apply_remove_rr(&update->a_ctx, &rrset);
if (ret != KNOT_EOK) {
return ret;
}
......@@ -397,7 +396,7 @@ static int sign_update(zone_update_t *update,
}
/* Apply DNSSEC changeset */
ret = apply_changeset_directly(&update->a_ctx, new_contents, &sec_ch);
ret = apply_changeset_directly(&update->a_ctx, &sec_ch);
if (ret != KNOT_EOK) {
changeset_clear(&sec_ch);
return ret;
......@@ -472,7 +471,7 @@ static int commit_incremental(conf_t *conf, zone_update_t *update, zone_contents
return ret;
}
ret = apply_replace_soa(&update->a_ctx, new_contents, &update->change);
ret = apply_replace_soa(&update->a_ctx, &update->change);
if (ret != KNOT_EOK) {
update_rollback(&update->a_ctx);
update_free_zone(&new_contents);
......@@ -486,7 +485,14 @@ static int commit_incremental(conf_t *conf, zone_update_t *update, zone_contents
/* Sign the update. */
if (dnssec_enable) {
zone_contents_adjust_pointers(new_contents);
ret = apply_prepare_to_sign(&update->a_ctx);
if (ret != KNOT_EOK) {
update_rollback(&update->a_ctx);
update_free_zone(&new_contents);
changeset_clear(&update->change);
return ret;
}
ret = sign_update(update, new_contents);
if (ret != KNOT_EOK) {
update_rollback(&update->a_ctx);
......@@ -495,8 +501,13 @@ static int commit_incremental(conf_t *conf, zone_update_t *update, zone_contents
return ret;
}
} else {
//! \todo refactor apply_* to make this shinier
zone_contents_adjust_full(new_contents);
ret = apply_finalize(&update->a_ctx);
if (ret != KNOT_EOK) {
update_rollback(&update->a_ctx);
update_free_zone(&new_contents);
changeset_clear(&update->change);
return ret;
}
}
/* Write changes to journal if all went well. (DNSSEC merged) */
......@@ -609,15 +620,7 @@ static int iter_init_tree_iters(zone_update_iter_t *it, zone_update_t *update,
zone_tree_t *tree;
/* Set zone iterator. */
zone_contents_t *_contents = NULL;
if (update->flags & UPDATE_FULL) {
_contents = update->new_cont;
} else if (update->flags & UPDATE_INCREMENTAL) {
//_contents = update->zone->contents;
_contents = update->new_cont;
} else {
return KNOT_EINVAL;
}
zone_contents_t *_contents = update->new_cont;
/* Begin iteration. We can safely assume _contents is a valid pointer. */
tree = nsec3 ? _contents->nsec3_nodes : _contents->nodes;
......@@ -630,7 +633,6 @@ static int iter_init_tree_iters(zone_update_iter_t *it, zone_update_t *update,
return KNOT_EOK;
}
static int iter_get_synth_node(zone_update_iter_t *it)
{
hattrie_iter_next(it->base_it);
......@@ -644,7 +646,6 @@ static int iter_get_synth_node(zone_update_iter_t *it)
if (it->update->flags & UPDATE_FULL) {
it->base_node = n;
} else {
//! \todo we can probably use just zone_contents_find_node directly
it->base_node = zone_update_get_node(it->update, n->owner);
if (it->base_node == NULL) {
return KNOT_ENOMEM;
......@@ -669,7 +670,6 @@ static int iter_init(zone_update_iter_t *it, zone_update_t *update, const bool n
it->base_node = (zone_node_t *)(*hattrie_iter_val(it->base_it));
assert(it->base_node);
if (it->update->flags & UPDATE_INCREMENTAL) {
//! \todo we can probably use just zone_contents_find_node directly
it->base_node = zone_update_get_node(it->update, it->base_node->owner);
if (it->base_node == NULL) {
return KNOT_ENOMEM;
......@@ -677,7 +677,6 @@ static int iter_init(zone_update_iter_t *it, zone_update_t *update, const bool n
}
}
//! get rid of this, use base_node directly, and better yet rename it to something more fitting
select_next_node(it);
return KNOT_EOK;
}
......@@ -727,7 +726,6 @@ int zone_update_iter_next(zone_update_iter_t *it)
}
}
//! get rid of this, use base_node directly, and better yet rename it to something more fitting
select_next_node(it);
return KNOT_EOK;
}
......
......@@ -128,10 +128,10 @@ int zone_load_journal(conf_t *conf, zone_t *zone, zone_contents_t *contents)
}
/* Apply changesets. */
apply_ctx_t a_ctx = { { 0 } };
apply_init_ctx(&a_ctx, 0);
apply_ctx_t a_ctx = { 0 };
apply_init_ctx(&a_ctx, contents, 0);
ret = apply_changesets_directly(&a_ctx, contents, &chgs);
ret = apply_changesets_directly(&a_ctx, &chgs);
if (ret == KNOT_EOK) {
log_zone_info(zone->name, "changes from journal applied %u -> %u",
serial, zone_contents_serial(contents));
......@@ -174,10 +174,10 @@ int zone_load_post(conf_t *conf, zone_t *zone, zone_contents_t *contents,
/* Apply DNSSEC changes. */
if (!changeset_empty(&change)) {
apply_ctx_t a_ctx = { { 0 } };
apply_init_ctx(&a_ctx, APPLY_STRICT);
apply_ctx_t a_ctx = { 0 };
apply_init_ctx(&a_ctx, contents, APPLY_STRICT);
ret = apply_changeset_directly(&a_ctx, contents, &change);
ret = apply_changeset_directly(&a_ctx, &change);
update_cleanup(&a_ctx);
if (ret != KNOT_EOK) {
changeset_clear(&change);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment