From 50eda73d7d3ad1e52c8a7bf4108978a8ee55dd96 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 24 Sep 2017 17:59:23 -0700 Subject: [PATCH] ircd::db: Consistent row validity tests for cursor. --- include/ircd/db/cursor.h | 61 +++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/include/ircd/db/cursor.h b/include/ircd/db/cursor.h index 94aba9eb5..1cf8bb694 100644 --- a/include/ircd/db/cursor.h +++ b/include/ircd/db/cursor.h @@ -84,7 +84,7 @@ struct ircd::db::cursor::const_iterator_base if(!stale) return v; - assign(v, row, idx->first); + assign(v, row, row_key()); stale = false; return v; } @@ -95,6 +95,8 @@ struct ircd::db::cursor::const_iterator_base } protected: + string_view row_key() const; + bool row_valid() const; bool seek_row(); public: @@ -171,11 +173,20 @@ ircd::db::cursor::const_iterator_base::const_iterator_ ,idx{std::move(idx)} ,row { - *d, bool(this->idx)? this->idx->first : string_view{}, tuple{}, opts + *d, + bool(this->idx) && this->idx->second? this->idx->second: + bool(this->idx)? this->idx->first: + string_view{}, + tuple{}, + opts +} +,stale +{ + true } ,invalid { - !this->idx || !row.valid(this->idx->first) + !this->idx || !row_valid() } { if(invalid) @@ -220,7 +231,7 @@ template bool ircd::db::cursor::const_iterator_base::seek_row() { - if(!db::seek(row, idx->first)) + if(!db::seek(row, row_key())) return false; stale = true; @@ -230,6 +241,33 @@ ircd::db::cursor::const_iterator_base::seek_row() return true; } +template +template +bool +ircd::db::cursor::const_iterator_base::row_valid() +const +{ + return row.valid(row_key()); +} + +template +template +ircd::string_view +ircd::db::cursor::const_iterator_base::row_key() +const +{ + if(!idx) + return {}; + + if(idx->second) + return idx->second; + + assert(bool(idx->first)); + return idx->first; +} + template template @@ -253,7 +291,7 @@ const if(!idx) return false; - return row.valid(idx->first); + return row_valid(); } template::const_iterator_base::operator==(const const_iterator_base &o) const { - if(!row.valid() && !o.row.valid()) - return true; - - if(!row.valid() || !o.row.valid()) + if(row_key() != o.row_key()) return false; - return idx->first == o.idx->first; + if(!row_valid() && !o.row_valid()) + return true; + + if(!row_valid() || !o.row_valid()) + return false; + + return true; }