Skip to content

Commit

Permalink
feat: implement a paginate in reverse flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fusion44 committed Apr 1, 2023
1 parent e3312f2 commit 41aae43
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
10 changes: 8 additions & 2 deletions common/json_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -1107,8 +1107,9 @@ struct command_result *param_paginator(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct jsonrpc_paginator **paginator)
{
const jsmntok_t *batch_tok, *offset_tok, *limit_tok;
const jsmntok_t *batch_tok, *offset_tok, *limit_tok, *reverse_tok;
u64 *limit, *offset;
bool *reverse;
const char **batch;

batch = NULL;
Expand All @@ -1126,8 +1127,13 @@ struct command_result *param_paginator(struct command *cmd, const char *name,
if (limit_tok)
json_to_u64(buffer, limit_tok, limit);

reverse = tal(cmd, bool);
reverse_tok = json_get_member(buffer, tok, "reverse");
if (reverse_tok)
json_to_bool(buffer, reverse_tok, reverse);

if (batch || (limit && offset)) {
*paginator = new_paginator(cmd, batch, limit, offset);
*paginator = new_paginator(cmd, batch, limit, offset, reverse);
assert(paginator);
return NULL;
}
Expand Down
8 changes: 6 additions & 2 deletions common/jsonrpc_paginator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @offset: a position (u64) that determines the number of element (in SQL row)
* returned by request.
* @limit: a position (u64) that give the number of element in [0,..,offset - 1] to skip skip.
* @reverse: a boolean that reverse the order of the result.
*/
struct jsonrpc_paginator {
/** reall usefult for access to gossip map */
Expand All @@ -50,6 +51,7 @@ struct jsonrpc_paginator {
* query please use the sql plugins */
const u64 *offset;
const u64 *limit;
const bool *reverse;
/* FIXME: more smarter one? like sort_by = "json key"
* but this required to have a mapping between json_keys and sql keys
* maybe we had already somethings in the sql plugin? */
Expand All @@ -62,15 +64,17 @@ struct jsonrpc_paginator {
*
* BTW: I love C Macros, really!
*/
static inline struct jsonrpc_paginator *new_paginator(const tal_t *ctx, const char **batch,
const u64 *limit, const u64 *offset)
static inline struct jsonrpc_paginator *
new_paginator(const tal_t *ctx, const char **batch, const u64 *limit,
const u64 *offset, const bool *reverse)
{
struct jsonrpc_paginator *paginator = NULL;
if (batch || (limit && offset)) {
paginator = tal(ctx, struct jsonrpc_paginator);
paginator->batch = batch;
paginator->limit = limit;
paginator->offset = offset;
paginator->reverse = reverse;
return paginator;
}
return NULL;
Expand Down
31 changes: 26 additions & 5 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include <stdio.h>
#include <bitcoin/script.h>
#include <ccan/array_size/array_size.h>
#include <ccan/cast/cast.h>
Expand Down Expand Up @@ -4538,8 +4539,8 @@ const struct forwarding *wallet_forwarded_payments_get(struct wallet *w,
// placeholder for any parameter, the value doesn't matter because it's discarded by sql
const int any = -1;

if (paginator) {
stmt = db_prepare_v2(
if (paginator && *paginator->reverse == true) {
stmt = db_prepare_v2(
w->db,
SQL("SELECT"
" state"
Expand All @@ -4554,11 +4555,31 @@ const struct forwarding *wallet_forwarded_payments_get(struct wallet *w,
", failcode "
", forward_style "
"FROM forwards "
"ORDER BY ROWID DESC "
"WHERE (1 = ? OR state = ?) AND "
"(1 = ? OR in_channel_scid = ?) AND "
"(1 = ? OR out_channel_scid = ?)"
"LIMIT ? OFFSET ?"));
} else {
"(1 = ? OR out_channel_scid = ?)"));
} else if (paginator && *paginator->reverse == false) {
stmt = db_prepare_v2(
w->db,
SQL("SELECT"
" state"
", in_msatoshi"
", out_msatoshi"
", in_channel_scid"
", out_channel_scid"
", in_htlc_id"
", out_htlc_id"
", received_time"
", resolved_time"
", failcode "
", forward_style "
"FROM forwards "
"WHERE (1 = ? OR state = ?) AND "
"(1 = ? OR in_channel_scid = ?) AND "
"(1 = ? OR out_channel_scid = ?)"));
}
else {
stmt = db_prepare_v2(
w->db,
SQL("SELECT"
Expand Down

0 comments on commit 41aae43

Please sign in to comment.