summaryrefslogtreecommitdiffstats
path: root/src/knot/journal/journal_write.h
blob: a55fd341bdfe06efaa7340a145e73ae342ee9c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

#include "knot/journal/journal_basic.h"
#include "knot/journal/journal_metadata.h"
#include "knot/journal/serialization.h"

/*!
 * \brief Serialize a changeset into chunks and write it into DB with no checks and metadata update.
 *
 * \param txn   Journal DB transaction.
 * \param ch    Changeset to be written.
 */
void journal_write_changeset(knot_lmdb_txn_t *txn, const changeset_t *ch);

/*!
 * \brief Serialize zone contents aka "bootstrap" changeset into journal, no checks.
 *
 * \param txn   Journal DB transaction.
 * \param z     Zone contents to be written.
 */
void journal_write_zone(knot_lmdb_txn_t *txn, const zone_contents_t *z);

/*!
 * \brief Merge all following changeset into one of journal changeset.
 *
 * \param j                    Zone journal.
 * \param txn                  Journal DB transaction.
 * \param merge_zij            True if we shall merge into zone-in-journal.
 * \param merge_serial         Serial-from of the changeset to be merged into (ignored if 'merge_zij').
 * \param original_serial_to   Output: previous serial-to of the merged changeset before merge.
 *
 * \note The error code will be in thx->ret.
 */
void journal_merge(zone_journal_t j, knot_lmdb_txn_t *txn, bool merge_zij,
                   uint32_t merge_serial, uint32_t *original_serial_to);

/*!
 * \brief Delete some journal changesets in attempt to fulfill usage quotas.
 *
 * \param txn              Journal DB transaction.
 * \param from             Serial-from of the first changeset to be deleted.
 * \param zone             Zone name.
 * \param tofree_size      Amount of data (in bytes) to be at least deleted.
 * \param tofree_count     Number of changesets to be at least deleted.
 * \param stop_at_serial   Must not delete the changeset with this serial-from.
 * \param freed_size       Output: amount of data really deleted.
 * \param freed_count      Output: number of changesets really freed.
 * \param stopped_at       Output: serial-to of the last deleted changeset.
 *
 * \return True if something was deleted (not necessarily fulfilling tofree_*).
 */
bool journal_delete(knot_lmdb_txn_t *txn, uint32_t from, const knot_dname_t *zone,
                    uint64_t tofree_size, size_t tofree_count, uint32_t stop_at_serial,
                    uint64_t *freed_size, size_t *freed_count, uint32_t *stopped_at);

/*!
 * \brief Perform a merge or zone flush in order to enable deleting more changesets.
 *
 * \param j     Zone journal.
 * \param txn   Journal DB transaction.
 * \param md    Journal metadata.
 *
 * \note It might set txn->ret to KNOT_EBUSY to fail out from this operation and let the zone flush itself.
 */
void journal_try_flush(zone_journal_t j, knot_lmdb_txn_t *txn, journal_metadata_t *md);

/*!
 * \brief Perform delete/merge/flush operations to fulfill configured journal quotas.
 *
 * \param j           Zone journal.
 * \param txn         Journal DB transaction.
 * \param md          Journal metadata.
 * \param max_usage   Configured maximum usage (in bytes) of journal DB by this zone.
 * \param max_count   Configured maximum number of changesets.
 */
void journal_fix_occupation(zone_journal_t j, knot_lmdb_txn_t *txn, journal_metadata_t *md,
			    int64_t max_usage, ssize_t max_count);

/*!
 * \brief Store zone-in-journal into the journal, update metadata.
 *
 * \param j    Zone journal.
 * \param z    Zone contents to be stored.
 *
 * \return KNOT_E*
 */
int journal_insert_zone(zone_journal_t j, const zone_contents_t *z);

/*!
 * \brief Store changeset into journal, fulfilling quotas and updating metadata.
 *
 * \param j    Zone journal.
 * \param ch   Changeset to be stored.
 * \param extra   Extra changeset to be stored in the role of merged changeset.
 * \param zdiff   Zone diff to be stored instead of changeset.
 *
 * \note The extra changesetis being stored on zone load, it is basically the diff
 *       between zonefile and loaded zone contents. Afterwards, it will be treated
 *       the same like merged changeset. Inserting it requires no zone-in-journal
 *       present and leads to deleting any previous merged changeset.
 *
 * \return KNOT_E*
 */
int journal_insert(zone_journal_t j, const changeset_t *ch, const changeset_t *extra,
                   const zone_diff_t *zdiff);