Remove templating of Continuation

custom
jacqueline 2 years ago
parent 53cf476876
commit 20d1c280a7
  1. 2
      src/app_console/app_console.cpp
  2. 49
      src/database/database.cpp
  3. 19
      src/database/include/database.hpp
  4. 6
      src/playlist/source.cpp
  5. 4
      src/ui/screen_track_browser.cpp
  6. 2
      src/ui/ui_fsm.cpp

@ -194,7 +194,7 @@ int CmdDbTracks(int argc, char** argv) {
} }
if (res->next_page()) { if (res->next_page()) {
auto continuation = res->next_page().value(); auto continuation = res->next_page().value();
res.reset(db->GetPage(&continuation).get()); res.reset(db->GetPage<database::Track>(&continuation).get());
} else { } else {
break; break;
} }

@ -355,23 +355,23 @@ auto Database::GetTracksByIndex(const IndexInfo& index, std::size_t page_size)
.components_hash = 0, .components_hash = 0,
}; };
OwningSlice prefix = EncodeIndexPrefix(header); OwningSlice prefix = EncodeIndexPrefix(header);
Continuation<IndexRecord> c{.prefix = prefix.data, Continuation c{.prefix = prefix.data,
.start_key = prefix.data, .start_key = prefix.data,
.forward = true, .forward = true,
.was_prev_forward = true, .was_prev_forward = true,
.page_size = page_size}; .page_size = page_size};
return dbGetPage(c); return dbGetPage<IndexRecord>(c);
}); });
} }
auto Database::GetTracks(std::size_t page_size) -> std::future<Result<Track>*> { auto Database::GetTracks(std::size_t page_size) -> std::future<Result<Track>*> {
return worker_task_->Dispatch<Result<Track>*>([=, this]() -> Result<Track>* { return worker_task_->Dispatch<Result<Track>*>([=, this]() -> Result<Track>* {
Continuation<Track> c{.prefix = EncodeDataPrefix().data, Continuation c{.prefix = EncodeDataPrefix().data,
.start_key = EncodeDataPrefix().data, .start_key = EncodeDataPrefix().data,
.forward = true, .forward = true,
.was_prev_forward = true, .was_prev_forward = true,
.page_size = page_size}; .page_size = page_size};
return dbGetPage(c); return dbGetPage<Track>(c);
}); });
} }
@ -379,28 +379,27 @@ auto Database::GetDump(std::size_t page_size)
-> std::future<Result<std::pmr::string>*> { -> std::future<Result<std::pmr::string>*> {
return worker_task_->Dispatch<Result<std::pmr::string>*>( return worker_task_->Dispatch<Result<std::pmr::string>*>(
[=, this]() -> Result<std::pmr::string>* { [=, this]() -> Result<std::pmr::string>* {
Continuation<std::pmr::string> c{.prefix = "", Continuation c{.prefix = "",
.start_key = "", .start_key = "",
.forward = true, .forward = true,
.was_prev_forward = true, .was_prev_forward = true,
.page_size = page_size}; .page_size = page_size};
return dbGetPage(c); return dbGetPage<std::pmr::string>(c);
}); });
} }
template <typename T> template <typename T>
auto Database::GetPage(Continuation<T>* c) -> std::future<Result<T>*> { auto Database::GetPage(Continuation* c) -> std::future<Result<T>*> {
Continuation<T> copy = *c; Continuation copy = *c;
return worker_task_->Dispatch<Result<T>*>( return worker_task_->Dispatch<Result<T>*>(
[=, this]() -> Result<T>* { return dbGetPage(copy); }); [=, this]() -> Result<T>* { return dbGetPage<T>(copy); });
} }
template auto Database::GetPage<Track>(Continuation<Track>* c) template auto Database::GetPage<Track>(Continuation* c)
-> std::future<Result<Track>*>; -> std::future<Result<Track>*>;
template auto Database::GetPage<IndexRecord>(Continuation<IndexRecord>* c) template auto Database::GetPage<IndexRecord>(Continuation* c)
-> std::future<Result<IndexRecord>*>; -> std::future<Result<IndexRecord>*>;
template auto Database::GetPage<std::pmr::string>( template auto Database::GetPage<std::pmr::string>(Continuation* c)
Continuation<std::pmr::string>* c)
-> std::future<Result<std::pmr::string>*>; -> std::future<Result<std::pmr::string>*>;
auto Database::dbMintNewTrackId() -> TrackId { auto Database::dbMintNewTrackId() -> TrackId {
@ -477,7 +476,7 @@ auto Database::dbCreateIndexesForTrack(const Track& track) -> void {
} }
template <typename T> template <typename T>
auto Database::dbGetPage(const Continuation<T>& c) -> Result<T>* { auto Database::dbGetPage(const Continuation& c) -> Result<T>* {
// Work out our starting point. Sometimes this will already done. // Work out our starting point. Sometimes this will already done.
std::unique_ptr<leveldb::Iterator> it{ std::unique_ptr<leveldb::Iterator> it{
db_->NewIterator(leveldb::ReadOptions{})}; db_->NewIterator(leveldb::ReadOptions{})};
@ -524,13 +523,13 @@ auto Database::dbGetPage(const Continuation<T>& c) -> Result<T>* {
} }
// Work out the new continuations. // Work out the new continuations.
std::optional<Continuation<T>> next_page; std::optional<Continuation> next_page;
if (c.forward) { if (c.forward) {
if (it != nullptr) { if (it != nullptr) {
// We were going forward, and now we want the next page. // We were going forward, and now we want the next page.
std::pmr::string key{it->key().data(), it->key().size(), std::pmr::string key{it->key().data(), it->key().size(),
&memory::kSpiRamResource}; &memory::kSpiRamResource};
next_page = Continuation<T>{ next_page = Continuation{
.prefix = c.prefix, .prefix = c.prefix,
.start_key = key, .start_key = key,
.forward = true, .forward = true,
@ -543,7 +542,7 @@ auto Database::dbGetPage(const Continuation<T>& c) -> Result<T>* {
// We were going backwards, and now we want the next page. This is a // We were going backwards, and now we want the next page. This is a
// reversal, to set the start key to the first record we saw and mark that // reversal, to set the start key to the first record we saw and mark that
// it's off by one. // it's off by one.
next_page = Continuation<T>{ next_page = Continuation{
.prefix = c.prefix, .prefix = c.prefix,
.start_key = *first_key, .start_key = *first_key,
.forward = true, .forward = true,
@ -552,11 +551,11 @@ auto Database::dbGetPage(const Continuation<T>& c) -> Result<T>* {
}; };
} }
std::optional<Continuation<T>> prev_page; std::optional<Continuation> prev_page;
if (c.forward) { if (c.forward) {
// We were going forwards, and now we want the previous page. Set the search // We were going forwards, and now we want the previous page. Set the search
// key to the first result we saw, and mark that it's off by one. // key to the first result we saw, and mark that it's off by one.
prev_page = Continuation<T>{ prev_page = Continuation{
.prefix = c.prefix, .prefix = c.prefix,
.start_key = *first_key, .start_key = *first_key,
.forward = false, .forward = false,
@ -568,7 +567,7 @@ auto Database::dbGetPage(const Continuation<T>& c) -> Result<T>* {
// We were going backwards, and we still want to go backwards. // We were going backwards, and we still want to go backwards.
std::pmr::string key{it->key().data(), it->key().size(), std::pmr::string key{it->key().data(), it->key().size(),
&memory::kSpiRamResource}; &memory::kSpiRamResource};
prev_page = Continuation<T>{ prev_page = Continuation{
.prefix = c.prefix, .prefix = c.prefix,
.start_key = key, .start_key = key,
.forward = false, .forward = false,
@ -582,10 +581,10 @@ auto Database::dbGetPage(const Continuation<T>& c) -> Result<T>* {
return new Result<T>(std::move(records), next_page, prev_page); return new Result<T>(std::move(records), next_page, prev_page);
} }
template auto Database::dbGetPage<Track>(const Continuation<Track>& c) template auto Database::dbGetPage<Track>(const Continuation& c)
-> Result<Track>*; -> Result<Track>*;
template auto Database::dbGetPage<std::pmr::string>( template auto Database::dbGetPage<std::pmr::string>(const Continuation& c)
const Continuation<std::pmr::string>& c) -> Result<std::pmr::string>*; -> Result<std::pmr::string>*;
template <> template <>
auto Database::ParseRecord<IndexRecord>(const leveldb::Slice& key, auto Database::ParseRecord<IndexRecord>(const leveldb::Slice& key,
@ -668,13 +667,13 @@ auto IndexRecord::track() const -> std::optional<TrackId> {
} }
auto IndexRecord::Expand(std::size_t page_size) const auto IndexRecord::Expand(std::size_t page_size) const
-> std::optional<Continuation<IndexRecord>> { -> std::optional<Continuation> {
if (track_) { if (track_) {
return {}; return {};
} }
IndexKey::Header new_header = ExpandHeader(key_.header, key_.item); IndexKey::Header new_header = ExpandHeader(key_.header, key_.item);
OwningSlice new_prefix = EncodeIndexPrefix(new_header); OwningSlice new_prefix = EncodeIndexPrefix(new_header);
return Continuation<IndexRecord>{ return Continuation{
.prefix = new_prefix.data, .prefix = new_prefix.data,
.start_key = new_prefix.data, .start_key = new_prefix.data,
.forward = true, .forward = true,

@ -31,7 +31,6 @@
namespace database { namespace database {
template <typename T>
struct Continuation { struct Continuation {
std::pmr::string prefix; std::pmr::string prefix;
std::pmr::string start_key; std::pmr::string start_key;
@ -52,12 +51,12 @@ class Result {
return values_; return values_;
} }
auto next_page() -> std::optional<Continuation<T>>& { return next_page_; } auto next_page() -> std::optional<Continuation>& { return next_page_; }
auto prev_page() -> std::optional<Continuation<T>>& { return prev_page_; } auto prev_page() -> std::optional<Continuation>& { return prev_page_; }
Result(const std::vector<std::shared_ptr<T>>&& values, Result(const std::vector<std::shared_ptr<T>>&& values,
std::optional<Continuation<T>> next, std::optional<Continuation> next,
std::optional<Continuation<T>> prev) std::optional<Continuation> prev)
: values_(values), next_page_(next), prev_page_(prev) {} : values_(values), next_page_(next), prev_page_(prev) {}
Result(const Result&) = delete; Result(const Result&) = delete;
@ -65,8 +64,8 @@ class Result {
private: private:
std::vector<std::shared_ptr<T>> values_; std::vector<std::shared_ptr<T>> values_;
std::optional<Continuation<T>> next_page_; std::optional<Continuation> next_page_;
std::optional<Continuation<T>> prev_page_; std::optional<Continuation> prev_page_;
}; };
class IndexRecord { class IndexRecord {
@ -78,7 +77,7 @@ class IndexRecord {
auto text() const -> std::optional<std::pmr::string>; auto text() const -> std::optional<std::pmr::string>;
auto track() const -> std::optional<TrackId>; auto track() const -> std::optional<TrackId>;
auto Expand(std::size_t) const -> std::optional<Continuation<IndexRecord>>; auto Expand(std::size_t) const -> std::optional<Continuation>;
private: private:
IndexKey key_; IndexKey key_;
@ -120,7 +119,7 @@ class Database {
auto GetDump(std::size_t page_size) -> std::future<Result<std::pmr::string>*>; auto GetDump(std::size_t page_size) -> std::future<Result<std::pmr::string>*>;
template <typename T> template <typename T>
auto GetPage(Continuation<T>* c) -> std::future<Result<T>*>; auto GetPage(Continuation* c) -> std::future<Result<T>*>;
Database(const Database&) = delete; Database(const Database&) = delete;
Database& operator=(const Database&) = delete; Database& operator=(const Database&) = delete;
@ -153,7 +152,7 @@ class Database {
auto dbCreateIndexesForTrack(const Track& track) -> void; auto dbCreateIndexesForTrack(const Track& track) -> void;
template <typename T> template <typename T>
auto dbGetPage(const Continuation<T>& c) -> Result<T>*; auto dbGetPage(const Continuation& c) -> Result<T>*;
template <typename T> template <typename T>
auto ParseRecord(const leveldb::Slice& key, const leveldb::Slice& val) auto ParseRecord(const leveldb::Slice& key, const leveldb::Slice& val)

@ -68,7 +68,7 @@ auto IndexRecordSource::Advance() -> std::optional<database::TrackId> {
return {}; return {};
} }
current_page_.reset(db->GetPage(&*next_page).get()); current_page_.reset(db->GetPage<database::IndexRecord>(&*next_page).get());
current_item_ = 0; current_item_ = 0;
} }
@ -92,7 +92,7 @@ auto IndexRecordSource::Previous() -> std::optional<database::TrackId> {
return {}; return {};
} }
current_page_.reset(db->GetPage(&*prev_page).get()); current_page_.reset(db->GetPage<database::IndexRecord>(&*prev_page).get());
current_item_ = current_page_->values().size() - 1; current_item_ = current_page_->values().size() - 1;
} }
@ -124,7 +124,7 @@ auto IndexRecordSource::Peek(std::size_t n, std::vector<database::TrackId>* out)
} }
// TODO(jacqueline): It would probably be a good idea to hold onto these // TODO(jacqueline): It would probably be a good idea to hold onto these
// peeked pages, to avoid needing to look them up again later. // peeked pages, to avoid needing to look them up again later.
working_page.reset(db->GetPage(&*next_page).get()); working_page.reset(db->GetPage<database::IndexRecord>(&*next_page).get());
working_item = 0; working_item = 0;
} }

@ -262,7 +262,7 @@ auto TrackBrowser::FetchNewPage(Position pos) -> void {
return; return;
} }
std::optional<database::Continuation<database::IndexRecord>> cont; std::optional<database::Continuation> cont;
switch (pos) { switch (pos) {
case START: case START:
cont = current_pages_.front()->prev_page(); cont = current_pages_.front()->prev_page();
@ -294,7 +294,7 @@ auto TrackBrowser::FetchNewPage(Position pos) -> void {
} }
loading_pos_ = pos; loading_pos_ = pos;
loading_page_ = db->GetPage(&cont.value()); loading_page_ = db->GetPage<database::IndexRecord>(&cont.value());
} }
auto TrackBrowser::GetNumRecords() -> std::size_t { auto TrackBrowser::GetNumRecords() -> std::size_t {

@ -299,7 +299,7 @@ void Browse::react(const internal::RecordSelected& ev) {
if (!cont) { if (!cont) {
return; return;
} }
auto query = db->GetPage(&cont.value()); auto query = db->GetPage<database::IndexRecord>(&cont.value());
std::pmr::string title = record->text().value_or("TODO"); std::pmr::string title = record->text().value_or("TODO");
PushScreen(std::make_shared<screens::TrackBrowser>( PushScreen(std::make_shared<screens::TrackBrowser>(
sTopBarModel, sServices->database(), title, std::move(query))); sTopBarModel, sServices->database(), title, std::move(query)));

Loading…
Cancel
Save