diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-06-23 15:32:11 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-06-23 15:32:11 +1000 |
| commit | 245d9ff4b9cde1f487beed76085a52f3f2d6d26c (patch) | |
| tree | 0730e6cda4c03a92c0d5e6b2e31fe27bfa021f69 /src/app_console | |
| parent | aee0474191aa6b4e4505e3f5a74b4ac8cc48063b (diff) | |
| download | tangara-fw-245d9ff4b9cde1f487beed76085a52f3f2d6d26c.tar.gz | |
add indexing to the database
idk man i wrote most of this in a fugue state whilst high on the couch
with my cat
Diffstat (limited to 'src/app_console')
| -rw-r--r-- | src/app_console/app_console.cpp | 86 |
1 files changed, 85 insertions, 1 deletions
diff --git a/src/app_console/app_console.cpp b/src/app_console/app_console.cpp index 1a549653..539fea00 100644 --- a/src/app_console/app_console.cpp +++ b/src/app_console/app_console.cpp @@ -8,10 +8,12 @@ #include <dirent.h> +#include <algorithm> #include <cstdint> #include <cstdio> #include <cstdlib> #include <iostream> +#include <ostream> #include <sstream> #include <string> @@ -22,6 +24,8 @@ #include "esp_log.h" #include "event_queue.hpp" #include "ff.h" +#include "index.hpp" +#include "track.hpp" namespace console { @@ -158,7 +162,8 @@ int CmdDbTracks(int argc, char** argv) { db->GetTracks(20).get()); while (true) { for (database::Track s : res->values()) { - std::cout << s.tags().title.value_or("[BLANK]") << std::endl; + std::cout << s.tags()[database::Tag::kTitle].value_or("[BLANK]") + << std::endl; } if (res->next_page()) { auto continuation = res->next_page().value(); @@ -180,6 +185,84 @@ void RegisterDbTracks() { esp_console_cmd_register(&cmd); } +int CmdDbIndex(int argc, char** argv) { + std::cout << std::endl; + vTaskDelay(1); + static const std::string usage = "usage: db_index [id] [choices ...]"; + + auto db = AppConsole::sDatabase.lock(); + if (!db) { + std::cout << "no database open" << std::endl; + return 1; + } + + auto indexes = db->GetIndexes(); + if (argc <= 1) { + std::cout << usage << std::endl; + std::cout << "available indexes:" << std::endl; + std::cout << "id\tname" << std::endl; + for (const database::IndexInfo& info : indexes) { + std::cout << static_cast<int>(info.id) << '\t' << info.name << std::endl; + } + return 0; + } + + int index_id = std::atoi(argv[1]); + auto index = std::find_if(indexes.begin(), indexes.end(), + [=](const auto& i) { return i.id == index_id; }); + if (index == indexes.end()) { + std::cout << "bad index id" << std::endl; + return -1; + } + + std::unique_ptr<database::Result<database::IndexRecord>> res( + db->GetTracksByIndex(*index, 20).get()); + int choice_index = 2; + + if (res->values().empty()) { + std::cout << "no entries for this index" << std::endl; + return 1; + } + + while (choice_index < argc) { + int choice = std::atoi(argv[choice_index]); + if (choice >= res->values().size()) { + std::cout << "choice out of range" << std::endl; + return -1; + } + auto cont = res->values().at(choice).Expand(20); + if (!cont) { + std::cout << "more choices than levels" << std::endl; + return 0; + } + res.reset(db->GetPage<database::IndexRecord>(&*cont).get()); + choice_index++; + } + + for (database::IndexRecord r : res->values()) { + std::cout << r.text().value_or("<unknown>"); + if (r.track()) { + std::cout << "\t(id:" << r.track()->data().id() << ")"; + } + std::cout << std::endl; + } + + if (res->next_page()) { + std::cout << "(more results not shown)" << std::endl; + } + + return 0; +} + +void RegisterDbIndex() { + esp_console_cmd_t cmd{.command = "db_index", + .help = "queries the database by index", + .hint = NULL, + .func = &CmdDbIndex, + .argtable = NULL}; + esp_console_cmd_register(&cmd); +} + int CmdDbDump(int argc, char** argv) { static const std::string usage = "usage: db_dump"; if (argc != 1) { @@ -232,6 +315,7 @@ auto AppConsole::RegisterExtraComponents() -> void { */ RegisterDbInit(); RegisterDbTracks(); + RegisterDbIndex(); RegisterDbDump(); } |
