Remove templating of Continuation

custom
jacqueline 2 years ago
parent 53cf476876
commit 20d1c280a7
  1. 2
      src/app_console/app_console.cpp
  2. 73
      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()) {
auto continuation = res->next_page().value();
res.reset(db->GetPage(&continuation).get());
res.reset(db->GetPage<database::Track>(&continuation).get());
} else {
break;
}

@ -355,23 +355,23 @@ auto Database::GetTracksByIndex(const IndexInfo& index, std::size_t page_size)
.components_hash = 0,
};
OwningSlice prefix = EncodeIndexPrefix(header);
Continuation<IndexRecord> c{.prefix = prefix.data,
.start_key = prefix.data,
.forward = true,
.was_prev_forward = true,
.page_size = page_size};
return dbGetPage(c);
Continuation c{.prefix = prefix.data,
.start_key = prefix.data,
.forward = true,
.was_prev_forward = true,
.page_size = page_size};
return dbGetPage<IndexRecord>(c);
});
}
auto Database::GetTracks(std::size_t page_size) -> std::future<Result<Track>*> {
return worker_task_->Dispatch<Result<Track>*>([=, this]() -> Result<Track>* {
Continuation<Track> c{.prefix = EncodeDataPrefix().data,
.start_key = EncodeDataPrefix().data,
.forward = true,
.was_prev_forward = true,
.page_size = page_size};
return dbGetPage(c);
Continuation c{.prefix = EncodeDataPrefix().data,
.start_key = EncodeDataPrefix().data,
.forward = true,
.was_prev_forward = true,
.page_size = page_size};
return dbGetPage<Track>(c);
});
}
@ -379,28 +379,27 @@ auto Database::GetDump(std::size_t page_size)
-> std::future<Result<std::pmr::string>*> {
return worker_task_->Dispatch<Result<std::pmr::string>*>(
[=, this]() -> Result<std::pmr::string>* {
Continuation<std::pmr::string> c{.prefix = "",
.start_key = "",
.forward = true,
.was_prev_forward = true,
.page_size = page_size};
return dbGetPage(c);
Continuation c{.prefix = "",
.start_key = "",
.forward = true,
.was_prev_forward = true,
.page_size = page_size};
return dbGetPage<std::pmr::string>(c);
});
}
template <typename T>
auto Database::GetPage(Continuation<T>* c) -> std::future<Result<T>*> {
Continuation<T> copy = *c;
auto Database::GetPage(Continuation* c) -> std::future<Result<T>*> {
Continuation copy = *c;
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>*>;
template auto Database::GetPage<IndexRecord>(Continuation<IndexRecord>* c)
template auto Database::GetPage<IndexRecord>(Continuation* c)
-> std::future<Result<IndexRecord>*>;
template auto Database::GetPage<std::pmr::string>(
Continuation<std::pmr::string>* c)
template auto Database::GetPage<std::pmr::string>(Continuation* c)
-> std::future<Result<std::pmr::string>*>;
auto Database::dbMintNewTrackId() -> TrackId {
@ -477,7 +476,7 @@ auto Database::dbCreateIndexesForTrack(const Track& track) -> void {
}
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.
std::unique_ptr<leveldb::Iterator> it{
db_->NewIterator(leveldb::ReadOptions{})};
@ -524,13 +523,13 @@ auto Database::dbGetPage(const Continuation<T>& c) -> Result<T>* {
}
// Work out the new continuations.
std::optional<Continuation<T>> next_page;
std::optional<Continuation> next_page;
if (c.forward) {
if (it != nullptr) {
// We were going forward, and now we want the next page.
std::pmr::string key{it->key().data(), it->key().size(),
&memory::kSpiRamResource};
next_page = Continuation<T>{
next_page = Continuation{
.prefix = c.prefix,
.start_key = key,
.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
// reversal, to set the start key to the first record we saw and mark that
// it's off by one.
next_page = Continuation<T>{
next_page = Continuation{
.prefix = c.prefix,
.start_key = *first_key,
.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) {
// 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.
prev_page = Continuation<T>{
prev_page = Continuation{
.prefix = c.prefix,
.start_key = *first_key,
.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.
std::pmr::string key{it->key().data(), it->key().size(),
&memory::kSpiRamResource};
prev_page = Continuation<T>{
prev_page = Continuation{
.prefix = c.prefix,
.start_key = key,
.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);
}
template auto Database::dbGetPage<Track>(const Continuation<Track>& c)
template auto Database::dbGetPage<Track>(const Continuation& c)
-> Result<Track>*;
template auto Database::dbGetPage<std::pmr::string>(
const Continuation<std::pmr::string>& c) -> Result<std::pmr::string>*;
template auto Database::dbGetPage<std::pmr::string>(const Continuation& c)
-> Result<std::pmr::string>*;
template <>
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
-> std::optional<Continuation<IndexRecord>> {
-> std::optional<Continuation> {
if (track_) {
return {};
}
IndexKey::Header new_header = ExpandHeader(key_.header, key_.item);
OwningSlice new_prefix = EncodeIndexPrefix(new_header);
return Continuation<IndexRecord>{
return Continuation{
.prefix = new_prefix.data,
.start_key = new_prefix.data,
.forward = true,

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

@ -68,7 +68,7 @@ auto IndexRecordSource::Advance() -> std::optional<database::TrackId> {
return {};
}
current_page_.reset(db->GetPage(&*next_page).get());
current_page_.reset(db->GetPage<database::IndexRecord>(&*next_page).get());
current_item_ = 0;
}
@ -92,7 +92,7 @@ auto IndexRecordSource::Previous() -> std::optional<database::TrackId> {
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;
}
@ -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
// 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;
}

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

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

Loading…
Cancel
Save