exchange

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

iterate_historic_denom_revenue.c (5286B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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 iterate_historic_denom_revenue.c
     18  * @brief Low-level (statement-level) Postgres database access for the exchange
     19  * @author Christian Grothoff
     20  */
     21 #include "taler/taler_pq_lib.h"
     22 #include "auditor-database/iterate_historic_denom_revenue.h"
     23 #include "pg_helper.h"
     24 
     25 
     26 /**
     27  * Hard upper bound on the number of records returned by a single
     28  * call, regardless of the limit requested by the client.
     29  */
     30 #define MAX_RECORDS 50000
     31 
     32 
     33 /**
     34  * Closure for #historic_denom_revenue_cb().
     35  */
     36 struct HistoricDenomRevenueContext
     37 {
     38   /**
     39    * Function to call for each result.
     40    */
     41   TALER_AUDITORDB_HistoricDenominationRevenueDataCallback cb;
     42 
     43   /**
     44    * Closure for @e cb.
     45    */
     46   void *cb_cls;
     47 
     48   /**
     49    * Plugin context.
     50    */
     51   struct TALER_AUDITORDB_PostgresContext *pg;
     52 
     53   /**
     54    * Number of results processed.
     55    */
     56   enum GNUNET_DB_QueryStatus qs;
     57 };
     58 
     59 
     60 /**
     61  * Helper function for #TALER_AUDITORDB_iterate_historic_denom_revenue().
     62  * To be called with the results of a SELECT statement
     63  * that has returned @a num_results results.
     64  *
     65  * @param cls closure of type `struct HistoricRevenueContext *`
     66  * @param result the postgres result
     67  * @param num_results the number of results in @a result
     68  */
     69 static void
     70 historic_denom_revenue_cb (void *cls,
     71                            PGresult *result,
     72                            unsigned int num_results)
     73 {
     74   struct HistoricDenomRevenueContext *hrc = cls;
     75   struct TALER_AUDITORDB_PostgresContext *pg = hrc->pg;
     76 
     77   for (unsigned int i = 0; i < num_results; i++)
     78   {
     79     uint64_t rowid;
     80     struct TALER_DenominationHashP denom_pub_hash;
     81     struct GNUNET_TIME_Timestamp revenue_timestamp;
     82     struct TALER_Amount revenue_balance;
     83     struct TALER_Amount loss;
     84     struct GNUNET_PQ_ResultSpec rs[] = {
     85       GNUNET_PQ_result_spec_uint64 ("row_id",
     86                                     &rowid),
     87       GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
     88                                             &denom_pub_hash),
     89       GNUNET_PQ_result_spec_timestamp ("revenue_timestamp",
     90                                        &revenue_timestamp),
     91       TALER_PQ_RESULT_SPEC_AMOUNT ("revenue_balance",
     92                                    &revenue_balance),
     93       TALER_PQ_RESULT_SPEC_AMOUNT ("loss_balance",
     94                                    &loss),
     95       GNUNET_PQ_result_spec_end
     96     };
     97 
     98     if (GNUNET_OK !=
     99         GNUNET_PQ_extract_result (result,
    100                                   rs,
    101                                   i))
    102     {
    103       GNUNET_break (0);
    104       hrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
    105       return;
    106     }
    107 
    108     hrc->qs = i + 1;
    109     if (GNUNET_OK !=
    110         hrc->cb (hrc->cb_cls,
    111                  rowid,
    112                  &denom_pub_hash,
    113                  revenue_timestamp,
    114                  &revenue_balance,
    115                  &loss))
    116       break;
    117   }
    118 }
    119 
    120 
    121 enum GNUNET_DB_QueryStatus
    122 TALER_AUDITORDB_iterate_historic_denom_revenue (
    123   struct TALER_AUDITORDB_PostgresContext *pg,
    124   int64_t limit,
    125   uint64_t offset,
    126   TALER_AUDITORDB_HistoricDenominationRevenueDataCallback cb,
    127   void *cb_cls)
    128 {
    129   uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
    130                                 TALER_AUDITORDB_abs_limit (limit));
    131   struct GNUNET_PQ_QueryParam params[] = {
    132     GNUNET_PQ_query_param_uint64 (&offset),
    133     GNUNET_PQ_query_param_uint64 (&ulimit),
    134     GNUNET_PQ_query_param_end
    135   };
    136   struct HistoricDenomRevenueContext hrc = {
    137     .cb = cb,
    138     .cb_cls = cb_cls,
    139     .pg = pg
    140   };
    141   enum GNUNET_DB_QueryStatus qs;
    142 
    143   PREPARE (pg,
    144            "iterate_historic_denom_revenue_denomination_revenue_select_inc",
    145            "SELECT"
    146            " row_id"
    147            ",denom_pub_hash"
    148            ",revenue_timestamp"
    149            ",revenue_balance"
    150            ",loss_balance"
    151            " FROM auditor_historic_denomination_revenue"
    152            " WHERE row_id > $1"
    153            " ORDER BY row_id ASC"
    154            " LIMIT $2;");
    155   PREPARE (pg,
    156            "iterate_historic_denom_revenue_denomination_revenue_select_dec",
    157            "SELECT"
    158            " row_id"
    159            ",denom_pub_hash"
    160            ",revenue_timestamp"
    161            ",revenue_balance"
    162            ",loss_balance"
    163            " FROM auditor_historic_denomination_revenue"
    164            " WHERE row_id < $1"
    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_historic_denom_revenue_denomination_revenue_select_inc"
    171     : "iterate_historic_denom_revenue_denomination_revenue_select_dec",
    172     params,
    173     &historic_denom_revenue_cb,
    174     &hrc);
    175   if (qs <= 0)
    176     return qs;
    177   return hrc.qs;
    178 }