iterate_row_minor_inconsistencies.c (4920B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 #include "taler/taler_pq_lib.h" 17 #include "pg_helper.h" 18 #include "auditor-database/iterate_row_minor_inconsistencies.h" 19 20 21 /** 22 * Hard upper bound on the number of records returned by a single 23 * call, regardless of the limit requested by the client. 24 */ 25 #define MAX_RECORDS 50000 26 27 28 struct RowMinorInconsistenciesContext 29 { 30 31 /** 32 * Function to call for each bad sig loss. 33 */ 34 TALER_AUDITORDB_RowMinorInconsistenciesCallback cb; 35 36 /** 37 * Closure for @e cb 38 */ 39 void *cb_cls; 40 41 /** 42 * Plugin context. 43 */ 44 struct TALER_AUDITORDB_PostgresContext *pg; 45 46 /** 47 * Query status to return. 48 */ 49 enum GNUNET_DB_QueryStatus qs; 50 }; 51 52 53 /** 54 * Helper function for #TALER_AUDITORDB_iterate_row_minor_inconsistencies(). 55 * To be called with the results of a SELECT statement 56 * that has returned @a num_results results. 57 * 58 * @param cls closure of type `struct RowMinorInconsistenciesContext *` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 row_minor_inconsistencies_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct RowMinorInconsistenciesContext *dcc = cls; 68 69 for (unsigned int i = 0; i < num_results; i++) 70 { 71 struct TALER_AUDITORDB_RowMinorInconsistencies dc; 72 struct GNUNET_PQ_ResultSpec rs[] = { 73 GNUNET_PQ_result_spec_uint64 ("row_id", 74 &dc.row_id), 75 GNUNET_PQ_result_spec_string ("row_table", 76 &dc.row_table), 77 GNUNET_PQ_result_spec_uint64 ("problem_row", 78 &dc.problem_row), 79 GNUNET_PQ_result_spec_string ("diagnostic", 80 &dc.diagnostic), 81 GNUNET_PQ_result_spec_bool ("suppressed", 82 &dc.suppressed), 83 GNUNET_PQ_result_spec_end 84 }; 85 enum GNUNET_GenericReturnValue rval; 86 87 if (GNUNET_OK != 88 GNUNET_PQ_extract_result (result, 89 rs, 90 i)) 91 { 92 GNUNET_break (0); 93 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 94 return; 95 } 96 dcc->qs = i + 1; 97 rval = dcc->cb (dcc->cb_cls, 98 &dc); 99 GNUNET_PQ_cleanup_result (rs); 100 if (GNUNET_OK != rval) 101 break; 102 } 103 } 104 105 106 enum GNUNET_DB_QueryStatus 107 TALER_AUDITORDB_iterate_row_minor_inconsistencies ( 108 struct TALER_AUDITORDB_PostgresContext *pg, 109 int64_t limit, 110 uint64_t offset, 111 bool return_suppressed, 112 TALER_AUDITORDB_RowMinorInconsistenciesCallback cb, 113 void *cb_cls) 114 { 115 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 116 TALER_AUDITORDB_abs_limit (limit)); 117 struct GNUNET_PQ_QueryParam params[] = { 118 GNUNET_PQ_query_param_uint64 (&offset), 119 GNUNET_PQ_query_param_bool (return_suppressed), 120 GNUNET_PQ_query_param_uint64 (&plimit), 121 GNUNET_PQ_query_param_end 122 }; 123 struct RowMinorInconsistenciesContext dcc = { 124 .cb = cb, 125 .cb_cls = cb_cls, 126 .pg = pg 127 }; 128 enum GNUNET_DB_QueryStatus qs; 129 130 PREPARE (pg, 131 "iterate_row_minor_inconsistencies_desc", 132 "SELECT" 133 " row_id" 134 ",problem_row" 135 ",row_table" 136 ",diagnostic" 137 ",suppressed" 138 " FROM auditor_row_minor_inconsistencies" 139 " WHERE (row_id < $1)" 140 " AND ($2 OR NOT suppressed)" 141 " ORDER BY row_id DESC" 142 " LIMIT $3" 143 ); 144 PREPARE (pg, 145 "iterate_row_minor_inconsistencies_asc", 146 "SELECT" 147 " row_id" 148 ",problem_row" 149 ",row_table" 150 ",diagnostic" 151 ",suppressed" 152 " FROM auditor_row_minor_inconsistencies" 153 " WHERE (row_id > $1)" 154 " AND ($2 OR NOT suppressed)" 155 " ORDER BY row_id ASC" 156 " LIMIT $3" 157 ); 158 qs = GNUNET_PQ_eval_prepared_multi_select ( 159 pg->conn, 160 (limit > 0) 161 ? "iterate_row_minor_inconsistencies_asc" 162 : "iterate_row_minor_inconsistencies_desc", 163 params, 164 &row_minor_inconsistencies_cb, 165 &dcc); 166 if (qs > 0) 167 return dcc.qs; 168 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 169 return qs; 170 }