/*
   This file is part of TALER
   Copyright (C) 2022, 2026 Taler Systems SA

   TALER is free software; you can redistribute it and/or modify it under the
   terms of the GNU General Public License as published by the Free Software
   Foundation; either version 3, or (at your option) any later version.

   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with
   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */
/**
 * @file src/include/exchange-database/update_shard_progress.h
 * @brief implementation of the update_shard_progress function for Postgres
 * @author Christian Grothoff
 */
#ifndef EXCHANGE_DATABASE_UPDATE_SHARD_PROGRESS_H
#define EXCHANGE_DATABASE_UPDATE_SHARD_PROGRESS_H

#include "exchangedb_lib.h"


/**
 * Record how far a worker has come within a shard, and renew its lease on
 * that shard.  The shard is marked completed if @a progress_row reached
 * @a end_row.
 *
 * Call this from within the same transaction that persists the work being
 * reported.  That is the point of the function: the progress marker and the
 * work it describes have to become visible together, or a crash in between
 * leaves the two disagreeing.
 *
 * Progress only ever moves forward.  A second worker that grabbed the same
 * shard (because our lease ran out while we were still going) therefore
 * cannot rewind what we already recorded.
 *
 * @param pg the database context
 * @param job_name name of the job the shard belongs to
 * @param start_row inclusive start row of the shard
 * @param end_row exclusive end row of the shard
 * @param progress_row row up to which (exclusive) the work is done and
 *          committed; pass @a end_row to complete the shard
 * @param lease for how much longer we intend to hold the shard
 * @return transaction status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if
 *          the shard is not in the table (someone else finished it)
 */
enum GNUNET_DB_QueryStatus
TALER_EXCHANGEDB_update_shard_progress (
  struct TALER_EXCHANGEDB_PostgresContext *pg,
  const char *job_name,
  uint64_t start_row,
  uint64_t end_row,
  uint64_t progress_row,
  struct GNUNET_TIME_Relative lease);

#endif
