exchange

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

iterate_early_aggregations.c (5425B)


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