File indexing completed on 2024-05-12 05:26:04
0001 /* 0002 * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm> 0003 * 0004 * This program is free software; you can redistribute it and/or modify 0005 * it under the terms of the GNU General Public License as published by 0006 * the Free Software Foundation; either version 2 of the License, or 0007 * (at your option) any later version. 0008 * 0009 * This program is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License 0015 * along with this program; if not, write to the 0016 * Free Software Foundation, Inc., 0017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 0018 */ 0019 #include "resultset.h" 0020 0021 #include "log.h" 0022 0023 using Sink::Storage::Identifier; 0024 0025 ResultSet::ResultSet() : mIt(nullptr) 0026 { 0027 } 0028 0029 ResultSet::ResultSet(const ValueGenerator &generator, const SkipValue &skip) : mIt(nullptr), mValueGenerator(generator), mSkip(skip) 0030 { 0031 } 0032 0033 ResultSet::ResultSet(const QVector<Identifier> &resultSet) 0034 : mResultSet(resultSet), 0035 mIt(mResultSet.constBegin()), 0036 mSkip([this]() { 0037 if (mIt != mResultSet.constEnd()) { 0038 mIt++; 0039 } 0040 }), 0041 mFirst(true) 0042 { 0043 } 0044 0045 ResultSet::ResultSet(const ResultSet &other) : mResultSet(other.mResultSet), mIt(nullptr), mFirst(true) 0046 { 0047 if (other.mValueGenerator) { 0048 mValueGenerator = other.mValueGenerator; 0049 mSkip = other.mSkip; 0050 } else { 0051 mResultSet = other.mResultSet; 0052 mIt = mResultSet.constBegin(); 0053 mSkip = [this]() { 0054 if (mIt != mResultSet.constEnd()) { 0055 mIt++; 0056 } 0057 }; 0058 } 0059 } 0060 0061 bool ResultSet::next() 0062 { 0063 Q_ASSERT(!mValueGenerator); 0064 if (mIt) { 0065 if (mIt != mResultSet.constEnd() && !mFirst) { 0066 mIt++; 0067 } 0068 mFirst = false; 0069 return mIt != mResultSet.constEnd(); 0070 } else { 0071 next([](const Result &) { return false; }); 0072 } 0073 return false; 0074 } 0075 0076 bool ResultSet::next(const Callback &callback) 0077 { 0078 Q_ASSERT(mValueGenerator); 0079 return mValueGenerator(callback); 0080 } 0081 0082 void ResultSet::skip(int number) 0083 { 0084 Q_ASSERT(mSkip); 0085 for (int i = 0; i < number; i++) { 0086 mSkip(); 0087 } 0088 } 0089 0090 ResultSet::ReplayResult ResultSet::replaySet(int offset, int batchSize, const Callback &callback) 0091 { 0092 skip(offset); 0093 int counter = 0; 0094 while (!batchSize || (counter < batchSize)) { 0095 const bool ret = next([&counter, callback](const ResultSet::Result &result) { 0096 counter++; 0097 callback(result); 0098 }); 0099 if (!ret) { 0100 return {counter, true}; 0101 } 0102 }; 0103 return {counter, false}; 0104 } 0105 0106 Identifier ResultSet::id() 0107 { 0108 if (mIt) { 0109 if (mIt == mResultSet.constEnd()) { 0110 return {}; 0111 } 0112 Q_ASSERT(mIt != mResultSet.constEnd()); 0113 return *mIt; 0114 } else { 0115 return mCurrentValue; 0116 } 0117 } 0118 0119 bool ResultSet::isEmpty() 0120 { 0121 return mResultSet.isEmpty(); 0122 }