From 46bf82eaec0a30281d0b8deaf1ffbd06030eb997 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 3 Jan 2018 11:36:00 +0100 Subject: Since we only support incremental fetching for flat lists a boolean is enough. --- common/modelresult.cpp | 65 ++++++++++++++++++-------------------------------- common/modelresult.h | 7 +++--- 2 files changed, 26 insertions(+), 46 deletions(-) diff --git a/common/modelresult.cpp b/common/modelresult.cpp index 5f8fc8a..9685e3c 100644 --- a/common/modelresult.cpp +++ b/common/modelresult.cpp @@ -259,8 +259,8 @@ bool ModelResult::hasChildren(const QModelIndex &parent) const template bool ModelResult::canFetchMore(const QModelIndex &parent) const { - const auto id = parent.internalId(); - if (mEntityAllChildrenFetched.contains(id)) { + //We fetch trees immediately so can never fetch more + if (parent.isValid() || mFetchedAll) { return false; } return true; @@ -270,8 +270,23 @@ template void ModelResult::fetchMore(const QModelIndex &parent) { SinkTraceCtx(mLogCtx) << "Fetching more: " << parent; - if (!parent.isValid()) { - fetchEntities(); + Q_ASSERT(QThread::currentThread() == this->thread()); + //We only suppor fetchMore for flat lists + if (parent.isValid()) { + return; + } + //There is already a fetch in progress, don't fetch again. + if (mFetchInProgress) { + SinkTraceCtx(mLogCtx) << "A fetch is already in progress."; + return; + } + mFetchInProgress = true; + mFetchComplete = false; + SinkTraceCtx(mLogCtx) << "Fetching more."; + if (loadEntities) { + loadEntities(); + } else { + SinkWarningCtx(mLogCtx) << "No way to fetch entities"; } } @@ -280,11 +295,6 @@ void ModelResult::add(const Ptr &value) { const auto childId = qHash(*value); const auto pId = parentId(value); - // Ignore updates we get before the initial fetch is done - // if (!mEntityChildrenFetched.contains(id)) { - // SinkTraceCtx(mLogCtx) << "Too early" << id; - // return; - // } if (mEntities.contains(childId)) { SinkWarningCtx(mLogCtx) << "Entity already in model: " << value->identifier(); return; @@ -332,26 +342,6 @@ void ModelResult::remove(const Ptr &value) } } -template -void ModelResult::fetchEntities() -{ - Q_ASSERT(QThread::currentThread() == this->thread()); - const auto id = getIdentifier({}); - //There is already a fetch in progress, don't fetch again. - if (mEntityChildrenFetched.contains(id) && !mEntityChildrenFetchComplete.contains(id)) { - SinkTraceCtx(mLogCtx) << "A fetch is already in progress."; - return; - } - mEntityChildrenFetchComplete.remove(id); - mEntityChildrenFetched.insert(id); - SinkTraceCtx(mLogCtx) << "Loading child entities of parent " << id; - if (loadEntities) { - loadEntities(); - } else { - SinkWarningCtx(mLogCtx) << "No way to fetch entities"; - } -} - template void ModelResult::setFetcher(const std::function &fetcher) { @@ -391,14 +381,9 @@ void ModelResult::setEmitter(const typename Sink::ResultEmitter::Pt SinkTraceCtx(mLogCtx) << "Initial result set complete. Fetched all: " << fetchedAll; Q_ASSERT(guard); Q_ASSERT(QThread::currentThread() == this->thread()); - - // const qint64 parentId = parent ? qHash(*parent) : 0; - // const auto parentIndex = createIndexFromId(parentId); - mEntityChildrenFetchComplete.insert(0); - if (fetchedAll) { - mEntityAllChildrenFetched.insert(0); - } - // emit dataChanged(parentIndex, parentIndex, QVector() << ChildrenFetchedRole); + mFetchInProgress = false; + mFetchedAll = fetchedAll; + mFetchComplete = true; emit dataChanged({}, {}, QVector() << ChildrenFetchedRole); }); mEmitter = emitter; @@ -407,7 +392,7 @@ void ModelResult::setEmitter(const typename Sink::ResultEmitter::Pt template bool ModelResult::childrenFetched(const QModelIndex &index) const { - return mEntityChildrenFetchComplete.contains(getIdentifier(index)); + return mFetchComplete; } template @@ -421,10 +406,6 @@ void ModelResult::modify(const Ptr &value) return; } auto id = parentId(value); - // Ignore updates we get before the initial fetch is done - // if (!mEntityChildrenFetched.contains(id)) { - // return; - // } auto parent = createIndexFromId(id); SinkTraceCtx(mLogCtx) << "Modified entity:" << value->identifier() << ", id: " << childId; auto i = mTree[id].indexOf(childId); diff --git a/common/modelresult.h b/common/modelresult.h index a4def81..55a95b9 100644 --- a/common/modelresult.h +++ b/common/modelresult.h @@ -75,17 +75,16 @@ private: qint64 parentId(const Ptr &value); QModelIndex createIndexFromId(const qint64 &id) const; - void fetchEntities(); Sink::Log::Context mLogCtx; // TODO we should be able to directly use T as index, with an appropriate hash function, and thus have a QMap and QList QMap mEntities; QMap /* child entity id*/> mTree; QMap mParents; - QSet mEntityChildrenFetched; - QSet mEntityChildrenFetchComplete; - QSet mEntityAllChildrenFetched; QMap mEntityStatus; + bool mFetchInProgress{false}; + bool mFetchedAll{false}; + bool mFetchComplete{false}; QList mPropertyColumns; Sink::Query mQuery; std::function loadEntities; -- cgit v1.2.3