Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions plugins/bkpr/bookkeeper.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ static void
parse_and_log_channel_move(struct command *cmd,
const char *buf,
const jsmntok_t *channelmove,
struct refresh_info *rinfo);
struct refresh_info *rinfo,
bool log);

static struct command_result *datastore_done(struct command *cmd,
const char *method,
Expand Down Expand Up @@ -120,8 +121,15 @@ static struct command_result *listchannelmoves_done(struct command *cmd,
be64 be_index;

moves = json_get_member(buf, result, "channelmoves");
if (moves->size > 2) {
plugin_log(cmd->plugin, LOG_DBG,
"%u channelmoves, only logging first and last",
moves->size);
}

json_for_each_arr(i, t, moves)
parse_and_log_channel_move(cmd, buf, t, rinfo);
parse_and_log_channel_move(cmd, buf, t, rinfo,
i == 0 || i == moves->size - 1);

be_index = cpu_to_be64(bkpr->channelmoves_index);
jsonrpc_set_datastore_binary(cmd, "bookkeeper/channelmoves_index",
Expand Down Expand Up @@ -1277,7 +1285,8 @@ static void
parse_and_log_channel_move(struct command *cmd,
const char *buf,
const jsmntok_t *channelmove,
struct refresh_info *rinfo)
struct refresh_info *rinfo,
bool log)
{
struct channel_event *e = tal(cmd, struct channel_event);
struct account *acct;
Expand Down Expand Up @@ -1324,11 +1333,12 @@ parse_and_log_channel_move(struct command *cmd,
err = tal_free(err);
}

plugin_log(cmd->plugin, LOG_DBG, "coin_move 2 (%s) %s -%s %s %"PRIu64,
e->tag,
fmt_amount_msat(tmpctx, e->credit),
fmt_amount_msat(tmpctx, e->debit),
CHANNEL_MOVE, e->timestamp);
if (log)
plugin_log(cmd->plugin, LOG_DBG, "coin_move 2 (%s) %s -%s %s %"PRIu64,
e->tag,
fmt_amount_msat(tmpctx, e->credit),
fmt_amount_msat(tmpctx, e->debit),
CHANNEL_MOVE, e->timestamp);

/* Go find the account for this event */
acct = find_account(bkpr, acct_name);
Expand Down
50 changes: 30 additions & 20 deletions plugins/sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ struct table_desc {
bool is_subobject;
/* Do we use created_index as primary key? Otherwise we create rowid. */
bool has_created_index;
/* Have we created our sql indexes yet? */
bool indices_created;
/* function to refresh it. */
struct command_result *(*refresh)(struct command *cmd,
const struct table_desc *td,
Expand Down Expand Up @@ -487,6 +489,28 @@ static struct command_result *refresh_complete(struct command *cmd,
return command_finished(cmd, ret);
}

static void init_indices(struct plugin *plugin, const struct table_desc *td)
{
for (size_t i = 0; i < ARRAY_SIZE(indices); i++) {
char *errmsg, *cmd;
int err;

if (!streq(indices[i].tablename, td->name))
continue;

cmd = tal_fmt(tmpctx, "CREATE INDEX %s_%zu_idx ON %s (%s",
indices[i].tablename, i,
indices[i].tablename,
indices[i].fields[0]);
if (indices[i].fields[1])
tal_append_fmt(&cmd, ", %s", indices[i].fields[1]);
tal_append_fmt(&cmd, ");");
err = sqlite3_exec(db, cmd, NULL, NULL, &errmsg);
if (err != SQLITE_OK)
plugin_err(plugin, "Failed '%s': %s", cmd, errmsg);
}
}

/* Recursion */
static struct command_result *refresh_tables(struct command *cmd,
struct db_query *dbq);
Expand All @@ -502,6 +526,11 @@ static struct command_result *one_refresh_done(struct command *cmd,
assert(td->refreshing);
td->refreshing = false;

if (!td->indices_created) {
init_indices(cmd->plugin, td);
td->indices_created = 1;
}

/* Transfer refresh waiters onto local list */
list_head_init(&waiters);
list_append_list(&waiters, &td->refresh_waiters);
Expand Down Expand Up @@ -1524,6 +1553,7 @@ static struct table_desc *new_table_desc(const tal_t *ctx,
td->last_created_index = 0;
td->has_created_index = false;
td->refreshing = false;
td->indices_created = false;
list_head_init(&td->refresh_waiters);

/* Only top-levels have refresh functions */
Expand Down Expand Up @@ -1704,25 +1734,6 @@ static void init_tablemap(struct plugin *plugin)
}
}

static void init_indices(struct plugin *plugin)
{
for (size_t i = 0; i < ARRAY_SIZE(indices); i++) {
char *errmsg, *cmd;
int err;

cmd = tal_fmt(tmpctx, "CREATE INDEX %s_%zu_idx ON %s (%s",
indices[i].tablename, i,
indices[i].tablename,
indices[i].fields[0]);
if (indices[i].fields[1])
tal_append_fmt(&cmd, ", %s", indices[i].fields[1]);
tal_append_fmt(&cmd, ");");
err = sqlite3_exec(db, cmd, NULL, NULL, &errmsg);
if (err != SQLITE_OK)
plugin_err(plugin, "Failed '%s': %s", cmd, errmsg);
}
}

static void memleak_mark_tablemap(struct plugin *p, struct htable *memtable)
{
memleak_ptr(memtable, dbfilename);
Expand All @@ -1735,7 +1746,6 @@ static const char *init(struct command *init_cmd,
struct plugin *plugin = init_cmd->plugin;
db = sqlite_setup(plugin);
init_tablemap(plugin);
init_indices(plugin);

plugin_set_memleak_handler(plugin, memleak_mark_tablemap);
return NULL;
Expand Down
Loading