exchange

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

iterate_emergencies_by_count.c (5963B)


      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_error_codes.h"
     17 #include "taler/taler_pq_lib.h"
     18 #include "pg_helper.h"
     19 #include "auditor-database/iterate_emergencies_by_count.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 #emergency_cb().
     31  */
     32 struct EmergencyByCountContext
     33 {
     34 
     35   /**
     36    * Function to call for each deposit confirmation.
     37    */
     38   TALER_AUDITORDB_EmergenciesByCountCallback 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_emergencies_by_count().
     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 Emergency *`
     63  * @param result the postgres result
     64  * @param num_results the number of results in @a result
     65  */
     66 static void
     67 emergency_by_count_cb (void *cls,
     68                        PGresult *result,
     69                        unsigned int num_results)
     70 {
     71   struct EmergencyByCountContext *dcc = cls;
     72   struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg;
     73 
     74   for (unsigned int i = 0; i < num_results; i++)
     75   {
     76     struct TALER_AUDITORDB_EmergenciesByCount dc;
     77     struct GNUNET_PQ_ResultSpec rs[] = {
     78       GNUNET_PQ_result_spec_uint64 ("row_id",
     79                                     &dc.row_id),
     80       GNUNET_PQ_result_spec_auto_from_type ("denompub_h",
     81                                             &dc.denompub_h),
     82       GNUNET_PQ_result_spec_uint64 ("num_issued",
     83                                     &dc.num_issued),
     84       GNUNET_PQ_result_spec_uint64 ("num_known",
     85                                     &dc.num_known),
     86       TALER_PQ_RESULT_SPEC_AMOUNT ("risk",
     87                                    &dc.risk),
     88       GNUNET_PQ_result_spec_absolute_time ("start",
     89                                            &dc.start),
     90       GNUNET_PQ_result_spec_absolute_time ("deposit_end",
     91                                            &dc.deposit_end),
     92       TALER_PQ_RESULT_SPEC_AMOUNT ("value",
     93                                    &dc.value),
     94       GNUNET_PQ_result_spec_bool ("suppressed",
     95                                   &dc.suppressed),
     96       GNUNET_PQ_result_spec_end
     97     };
     98     enum GNUNET_GenericReturnValue rval;
     99 
    100     if (GNUNET_OK !=
    101         GNUNET_PQ_extract_result (result,
    102                                   rs,
    103                                   i))
    104     {
    105       GNUNET_break (0);
    106       dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    107       return;
    108     }
    109     dcc->qs = i + 1;
    110     rval = dcc->cb (dcc->cb_cls,
    111                     &dc);
    112     GNUNET_PQ_cleanup_result (rs);
    113     if (GNUNET_OK != rval)
    114       break;
    115   }
    116 }
    117 
    118 
    119 enum GNUNET_DB_QueryStatus
    120 TALER_AUDITORDB_iterate_emergencies_by_count (struct
    121                                               TALER_AUDITORDB_PostgresContext *
    122                                               pg,
    123                                               int64_t limit,
    124                                               uint64_t offset,
    125                                               bool return_suppressed,
    126                                               TALER_AUDITORDB_EmergenciesByCountCallback
    127                                               cb,
    128                                               void *cb_cls)
    129 {
    130   uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
    131                                 TALER_AUDITORDB_abs_limit (limit));
    132   struct GNUNET_PQ_QueryParam params[] = {
    133     GNUNET_PQ_query_param_uint64 (&offset),
    134     GNUNET_PQ_query_param_bool (return_suppressed),
    135     GNUNET_PQ_query_param_uint64 (&plimit),
    136     GNUNET_PQ_query_param_end
    137   };
    138   struct EmergencyByCountContext dcc = {
    139     .cb = cb,
    140     .cb_cls = cb_cls,
    141     .pg = pg
    142   };
    143   enum GNUNET_DB_QueryStatus qs;
    144 
    145   PREPARE (pg,
    146            "iterate_emergencies_by_count_emergency_by_count_get_desc",
    147            "SELECT"
    148            " row_id"
    149            ",denompub_h"
    150            ",num_issued"
    151            ",num_known"
    152            ",risk"
    153            ",start"
    154            ",deposit_end"
    155            ",value"
    156            ",suppressed"
    157            " FROM auditor_emergency_by_count"
    158            " WHERE (row_id < $1)"
    159            " AND ($2 OR NOT suppressed)"
    160            " ORDER BY row_id DESC"
    161            " LIMIT $3"
    162            );
    163   PREPARE (pg,
    164            "iterate_emergencies_by_count_emergency_by_count_get_asc",
    165            "SELECT"
    166            " row_id"
    167            ",denompub_h"
    168            ",num_issued"
    169            ",num_known"
    170            ",risk"
    171            ",start"
    172            ",deposit_end"
    173            ",value"
    174            ",suppressed"
    175            " FROM auditor_emergency_by_count"
    176            " WHERE (row_id > $1)"
    177            " AND ($2 OR NOT suppressed)"
    178            " ORDER BY row_id ASC"
    179            " LIMIT $3"
    180            );
    181   qs = GNUNET_PQ_eval_prepared_multi_select (
    182     pg->conn,
    183     (limit > 0)
    184     ? "iterate_emergencies_by_count_emergency_by_count_get_asc"
    185     : "iterate_emergencies_by_count_emergency_by_count_get_desc",
    186     params,
    187     &emergency_by_count_cb,
    188     &dcc);
    189   if (qs > 0)
    190     return dcc.qs;
    191   GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
    192   return qs;
    193 }