exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

iterate_row_inconsistencies.c (4737B)


      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 
     17 #include "taler/taler_pq_lib.h"
     18 #include "pg_helper.h"
     19 #include "auditor-database/iterate_row_inconsistencies.h"
     20 
     21 
     22 /**
     23  * Hard upper bound on the number of records returned by a single
     24  * call, regardless of the limit requested by the client.
     25  */
     26 #define MAX_RECORDS 50000
     27 
     28 
     29 /**
     30  * Closure for #deposit_confirmation_cb().
     31  */
     32 struct RowInconsistencyContext
     33 {
     34 
     35   /**
     36    * Function to call for each deposit confirmation.
     37    */
     38   TALER_AUDITORDB_RowInconsistencyCallback cb;
     39 
     40   /**
     41    * Closure for @e cb
     42    */
     43   void *cb_cls;
     44 
     45   /**
     46    * Plugin context.
     47    */
     48   struct TALER_AUDITORDB_PostgresContext *pg;
     49 
     50   /**
     51    * Query status to return.
     52    */
     53   enum GNUNET_DB_QueryStatus qs;
     54 };
     55 
     56 
     57 /**
     58  * Helper function for #TALER_AUDITORDB_iterate_row_inconsistencies().
     59  * To be called with the results of a SELECT statement
     60  * that has returned @a num_results results.
     61  *
     62  * @param cls closure of type `struct DepositConfirmationContext *`
     63  * @param result the postgres result
     64  * @param num_results the number of results in @a result
     65  */
     66 static void
     67 row_inconsistency_cb (void *cls,
     68                       PGresult *result,
     69                       unsigned int num_results)
     70 {
     71   struct RowInconsistencyContext *dcc = cls;
     72 
     73   for (unsigned int i = 0; i < num_results; i++)
     74   {
     75     uint64_t serial_id;
     76 
     77     struct TALER_AUDITORDB_RowInconsistency dc;
     78 
     79     struct GNUNET_PQ_ResultSpec rs[] = {
     80       GNUNET_PQ_result_spec_uint64 ("row_id", &serial_id),
     81 
     82       GNUNET_PQ_result_spec_string ("row_table", &dc.row_table),
     83       GNUNET_PQ_result_spec_string ("diagnostic", &dc.diagnostic),
     84       GNUNET_PQ_result_spec_bool ("suppressed",  &dc.suppressed),
     85 
     86 
     87       GNUNET_PQ_result_spec_end
     88     };
     89     enum GNUNET_GenericReturnValue rval;
     90 
     91     if (GNUNET_OK !=
     92         GNUNET_PQ_extract_result (result,
     93                                   rs,
     94                                   i))
     95     {
     96       GNUNET_break (0);
     97       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
     98       return;
     99     }
    100 
    101     dcc->qs = i + 1;
    102 
    103 
    104     rval = dcc->cb (dcc->cb_cls,
    105                     serial_id,
    106                     &dc);
    107     GNUNET_PQ_cleanup_result (rs);
    108     if (GNUNET_OK != rval)
    109       break;
    110   }
    111 }
    112 
    113 
    114 enum GNUNET_DB_QueryStatus
    115 TALER_AUDITORDB_iterate_row_inconsistencies (
    116   struct TALER_AUDITORDB_PostgresContext *pg,
    117   int64_t limit,
    118   uint64_t offset,
    119   bool return_suppressed,            // maybe not needed
    120   TALER_AUDITORDB_RowInconsistencyCallback cb,
    121   void *cb_cls)
    122 {
    123 
    124   uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
    125                                 TALER_AUDITORDB_abs_limit (limit));
    126   struct GNUNET_PQ_QueryParam params[] = {
    127     GNUNET_PQ_query_param_uint64 (&offset),
    128     GNUNET_PQ_query_param_bool (return_suppressed),
    129     GNUNET_PQ_query_param_uint64 (&plimit),
    130     GNUNET_PQ_query_param_end
    131   };
    132   struct RowInconsistencyContext dcc = {
    133     .cb = cb,
    134     .cb_cls = cb_cls,
    135     .pg = pg
    136   };
    137   enum GNUNET_DB_QueryStatus qs;
    138 
    139 
    140   PREPARE (pg,
    141            "iterate_row_inconsistencies_inconsistency_select_desc",
    142            "SELECT"
    143            " row_id"
    144            ",row_table"
    145            ",diagnostic"
    146            ",suppressed"
    147            " FROM auditor_row_inconsistency"
    148            " WHERE (row_id < $1)"
    149            " AND ($2 OR suppressed is false)"
    150            " ORDER BY row_id DESC"
    151            " LIMIT $3"
    152            );
    153   PREPARE (pg,
    154            "iterate_row_inconsistencies_inconsistency_select_asc",
    155            "SELECT"
    156            " row_id"
    157            ",row_table"
    158            ",diagnostic"
    159            ",suppressed"
    160            " FROM auditor_row_inconsistency"
    161            " WHERE (row_id > $1)"
    162            " AND ($2 OR suppressed is false)"
    163            " ORDER BY row_id ASC"
    164            " LIMIT $3"
    165            );
    166   qs = GNUNET_PQ_eval_prepared_multi_select (
    167     pg->conn,
    168     (limit > 0)
    169     ? "iterate_row_inconsistencies_inconsistency_select_asc"
    170     : "iterate_row_inconsistencies_inconsistency_select_desc",
    171     params,
    172     &row_inconsistency_cb,
    173     &dcc);
    174 
    175   if (qs > 0)
    176     return dcc.qs;
    177   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    178   return qs;
    179 }