| 286 | } |
| 287 | |
| 288 | static struct command_result *json_datastoreusage(struct command *cmd, |
| 289 | const char *buffer, |
| 290 | const jsmntok_t *obj UNNEEDED, |
| 291 | const jsmntok_t *params) |
| 292 | { |
| 293 | struct json_stream *response; |
| 294 | const char **k, **key; |
| 295 | struct db_stmt *stmt; |
| 296 | const u8 *data; |
| 297 | u64 gen, total_bytes = 0; |
| 298 | |
| 299 | if (!param(cmd, buffer, params, |
| 300 | p_opt("key", param_list_or_string, &key), |
| 301 | NULL)) |
| 302 | return command_param_failed(); |
| 303 | |
| 304 | // We ignore an empty key string or key array. |
| 305 | if (key && *key[0] == '\0') |
| 306 | key = NULL; |
| 307 | |
| 308 | response = json_stream_success(cmd); |
| 309 | json_object_start(response, "datastoreusage"); |
| 310 | json_add_string(response, "key", datastore_key_fmt(tmpctx, key)); |
| 311 | |
| 312 | for (stmt = wallet_datastore_first(cmd, cmd->ld->wallet, key, |
| 313 | &k, &data, &gen); |
| 314 | stmt; |
| 315 | stmt = wallet_datastore_next(cmd, key, stmt, |
| 316 | &k, &data, &gen)) { |
| 317 | |
| 318 | u64 self_bytes = tal_bytelen(data); |
| 319 | /* The key is stored as a binary blob where each string is separated by |
| 320 | * a '\0'. Therefore we add an additional `len(k) - 1`. k is the primary |
| 321 | * key of the table and can not be NULL */ |
| 322 | self_bytes += tal_count(k) - 1; |
| 323 | for (size_t i = 0; i < tal_count(k); i++) { |
| 324 | self_bytes += strlen(k[i]); |
| 325 | }; |
| 326 | total_bytes += self_bytes; |
| 327 | }; |
| 328 | tal_free(stmt); |
| 329 | |
| 330 | json_add_u64(response, "total_bytes", total_bytes); |
| 331 | json_object_end(response); |
| 332 | |
| 333 | return command_success(cmd, response); |
| 334 | } |
| 335 | |
| 336 | static const struct json_command datastore_command = { |
| 337 | "datastore", |
nothing calls this directly
no test coverage detected