diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-10-18 12:57:52 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-10-21 09:18:49 +0200 |
commit | da1c86b80f230c3a2023f97c0048020a12e38de4 (patch) | |
tree | 922fd3ae8ae4feb0977f8e636f01f96073fb4de3 /common/entityreader.cpp | |
parent | 71a0167e0216f84588b492c84e92667847fbe5a5 (diff) | |
download | sink-da1c86b80f230c3a2023f97c0048020a12e38de4.tar.gz sink-da1c86b80f230c3a2023f97c0048020a12e38de4.zip |
Removed EntityReader
Diffstat (limited to 'common/entityreader.cpp')
-rw-r--r-- | common/entityreader.cpp | 207 |
1 files changed, 0 insertions, 207 deletions
diff --git a/common/entityreader.cpp b/common/entityreader.cpp deleted file mode 100644 index c49d1f7..0000000 --- a/common/entityreader.cpp +++ /dev/null | |||
@@ -1,207 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Lesser General Public | ||
6 | * License as published by the Free Software Foundation; either | ||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||
8 | * later version accepted by the membership of KDE e.V. (or its | ||
9 | * successor approved by the membership of KDE e.V.), which shall | ||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
19 | */ | ||
20 | #include "entityreader.h" | ||
21 | |||
22 | #include "resultset.h" | ||
23 | #include "storage.h" | ||
24 | #include "query.h" | ||
25 | #include "datastorequery.h" | ||
26 | |||
27 | SINK_DEBUG_AREA("entityreader") | ||
28 | |||
29 | using namespace Sink; | ||
30 | |||
31 | /* QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityReaderUtils::getLatest(const Sink::Storage::DataStore::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) */ | ||
32 | /* { */ | ||
33 | /* QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; */ | ||
34 | /* db.findLatest(uid, */ | ||
35 | /* [¤t, &adaptorFactory, &retrievedRevision](const QByteArray &key, const QByteArray &data) -> bool { */ | ||
36 | /* Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); */ | ||
37 | /* if (!buffer.isValid()) { */ | ||
38 | /* SinkWarning() << "Read invalid buffer from disk"; */ | ||
39 | /* } else { */ | ||
40 | /* SinkTrace() << "Found value " << key; */ | ||
41 | /* current = adaptorFactory.createAdaptor(buffer.entity()); */ | ||
42 | /* retrievedRevision = Sink::Storage::DataStore::revisionFromKey(key); */ | ||
43 | /* } */ | ||
44 | /* return false; */ | ||
45 | /* }, */ | ||
46 | /* [](const Sink::Storage::DataStore::Error &error) { SinkWarning() << "Failed to read current value from storage: " << error.message; }); */ | ||
47 | /* return current; */ | ||
48 | /* } */ | ||
49 | |||
50 | /* QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityReaderUtils::get(const Sink::Storage::DataStore::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) */ | ||
51 | /* { */ | ||
52 | /* QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; */ | ||
53 | /* db.scan(key, */ | ||
54 | /* [¤t, &adaptorFactory, &retrievedRevision](const QByteArray &key, const QByteArray &data) -> bool { */ | ||
55 | /* Sink::EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); */ | ||
56 | /* if (!buffer.isValid()) { */ | ||
57 | /* SinkWarning() << "Read invalid buffer from disk"; */ | ||
58 | /* } else { */ | ||
59 | /* current = adaptorFactory.createAdaptor(buffer.entity()); */ | ||
60 | /* retrievedRevision = Sink::Storage::DataStore::revisionFromKey(key); */ | ||
61 | /* } */ | ||
62 | /* return false; */ | ||
63 | /* }, */ | ||
64 | /* [](const Sink::Storage::DataStore::Error &error) { SinkWarning() << "Failed to read current value from storage: " << error.message; }); */ | ||
65 | /* return current; */ | ||
66 | /* } */ | ||
67 | |||
68 | /* QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> EntityReaderUtils::getPrevious(const Sink::Storage::DataStore::NamedDatabase &db, const QByteArray &uid, qint64 revision, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision) */ | ||
69 | /* { */ | ||
70 | /* QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> current; */ | ||
71 | /* qint64 latestRevision = 0; */ | ||
72 | /* db.scan(uid, */ | ||
73 | /* [¤t, &latestRevision, revision](const QByteArray &key, const QByteArray &) -> bool { */ | ||
74 | /* auto foundRevision = Sink::Storage::DataStore::revisionFromKey(key); */ | ||
75 | /* if (foundRevision < revision && foundRevision > latestRevision) { */ | ||
76 | /* latestRevision = foundRevision; */ | ||
77 | /* } */ | ||
78 | /* return true; */ | ||
79 | /* }, */ | ||
80 | /* [](const Sink::Storage::DataStore::Error &error) { SinkWarning() << "Failed to read current value from storage: " << error.message; }, true); */ | ||
81 | /* return get(db, Sink::Storage::DataStore::assembleKey(uid, latestRevision), adaptorFactory, retrievedRevision); */ | ||
82 | /* } */ | ||
83 | |||
84 | /* template <class DomainType> */ | ||
85 | /* EntityReader<DomainType>::EntityReader(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier, Sink::Storage::DataStore::Transaction &transaction) */ | ||
86 | /* : mResourceInstanceIdentifier(resourceInstanceIdentifier), */ | ||
87 | /* mTransaction(transaction), */ | ||
88 | /* mDomainTypeAdaptorFactoryPtr(Sink::AdaptorFactoryRegistry::instance().getFactory<DomainType>(resourceType)), */ | ||
89 | /* mDomainTypeAdaptorFactory(*mDomainTypeAdaptorFactoryPtr) */ | ||
90 | /* { */ | ||
91 | /* Q_ASSERT(!resourceType.isEmpty()); */ | ||
92 | /* Q_ASSERT(mDomainTypeAdaptorFactoryPtr); */ | ||
93 | /* } */ | ||
94 | |||
95 | /* template <class DomainType> */ | ||
96 | /* EntityReader<DomainType>::EntityReader(DomainTypeAdaptorFactoryInterface &domainTypeAdaptorFactory, const QByteArray &resourceInstanceIdentifier, Sink::Storage::DataStore::Transaction &transaction) */ | ||
97 | /* : mResourceInstanceIdentifier(resourceInstanceIdentifier), */ | ||
98 | /* mTransaction(transaction), */ | ||
99 | /* mDomainTypeAdaptorFactory(domainTypeAdaptorFactory) */ | ||
100 | /* { */ | ||
101 | |||
102 | /* } */ | ||
103 | |||
104 | template <class DomainType> | ||
105 | EntityReader<DomainType>::EntityReader(Storage::EntityStore &entityStore) | ||
106 | : mEntityStore(entityStore) | ||
107 | { | ||
108 | |||
109 | } | ||
110 | |||
111 | template <class DomainType> | ||
112 | DomainType EntityReader<DomainType>::read(const QByteArray &identifier) const | ||
113 | { | ||
114 | auto typeName = ApplicationDomain::getTypeName<DomainType>(); | ||
115 | return mEntityStore.readLatest<DomainType>(identifier); | ||
116 | } | ||
117 | |||
118 | template <class DomainType> | ||
119 | DomainType EntityReader<DomainType>::readFromKey(const QByteArray &key) const | ||
120 | { | ||
121 | /* auto typeName = ApplicationDomain::getTypeName<DomainType>(); */ | ||
122 | /* auto mainDatabase = Storage::DataStore::mainDatabase(mTransaction, typeName); */ | ||
123 | /* qint64 retrievedRevision = 0; */ | ||
124 | /* auto bufferAdaptor = EntityReaderUtils::get(mainDatabase, key, mDomainTypeAdaptorFactory, retrievedRevision); */ | ||
125 | /* const auto identifier = Storage::DataStore::uidFromKey(key); */ | ||
126 | /* if (!bufferAdaptor) { */ | ||
127 | /* return DomainType(); */ | ||
128 | /* } */ | ||
129 | /* return DomainType(mResourceInstanceIdentifier, identifier, retrievedRevision, bufferAdaptor); */ | ||
130 | return mEntityStore.readEntity<DomainType>(key); | ||
131 | } | ||
132 | |||
133 | template <class DomainType> | ||
134 | DomainType EntityReader<DomainType>::readPrevious(const QByteArray &uid, qint64 revision) const | ||
135 | { | ||
136 | return mEntityStore.readPrevious<DomainType>(uid, revision); | ||
137 | } | ||
138 | |||
139 | template <class DomainType> | ||
140 | void EntityReader<DomainType>::query(const Sink::Query &query, const std::function<bool(const DomainType &)> &callback) | ||
141 | { | ||
142 | executeInitialQuery(query, 0, 0, | ||
143 | [&callback](const typename DomainType::Ptr &value, Sink::Operation operation, const QMap<QByteArray, QVariant> &) -> bool { | ||
144 | Q_ASSERT(operation == Sink::Operation_Creation); | ||
145 | return callback(*value); | ||
146 | }); | ||
147 | } | ||
148 | |||
149 | template <class DomainType> | ||
150 | QPair<qint64, qint64> EntityReader<DomainType>::executeInitialQuery(const Sink::Query &query, int offset, int batchsize, const ResultCallback &callback) | ||
151 | { | ||
152 | QTime time; | ||
153 | time.start(); | ||
154 | |||
155 | auto preparedQuery = ApplicationDomain::TypeImplementation<DomainType>::prepareQuery(query, Storage::EntityStore::Ptr(&mEntityStore, [](Storage::EntityStore *){})); | ||
156 | auto resultSet = preparedQuery->execute(); | ||
157 | |||
158 | SinkTrace() << "Filtered set retrieved. " << Log::TraceTime(time.elapsed()); | ||
159 | auto replayedEntities = replaySet(resultSet, offset, batchsize, callback); | ||
160 | |||
161 | SinkTrace() << "Initial query took: " << Log::TraceTime(time.elapsed()); | ||
162 | return qMakePair(mEntityStore.maxRevision(), replayedEntities); | ||
163 | } | ||
164 | |||
165 | template <class DomainType> | ||
166 | QPair<qint64, qint64> EntityReader<DomainType>::executeIncrementalQuery(const Sink::Query &query, qint64 lastRevision, const ResultCallback &callback) | ||
167 | { | ||
168 | QTime time; | ||
169 | time.start(); | ||
170 | const qint64 baseRevision = lastRevision + 1; | ||
171 | |||
172 | auto preparedQuery = ApplicationDomain::TypeImplementation<DomainType>::prepareQuery(query, Storage::EntityStore::Ptr(&mEntityStore, [](Storage::EntityStore *){})); | ||
173 | auto resultSet = preparedQuery->update(baseRevision); | ||
174 | |||
175 | SinkTrace() << "Filtered set retrieved. " << Log::TraceTime(time.elapsed()); | ||
176 | auto replayedEntities = replaySet(resultSet, 0, 0, callback); | ||
177 | |||
178 | SinkTrace() << "Incremental query took: " << Log::TraceTime(time.elapsed()); | ||
179 | return qMakePair(mEntityStore.maxRevision(), replayedEntities); | ||
180 | } | ||
181 | |||
182 | template <class DomainType> | ||
183 | qint64 EntityReader<DomainType>::replaySet(ResultSet &resultSet, int offset, int batchSize, const ResultCallback &callback) | ||
184 | { | ||
185 | SinkTrace() << "Skipping over " << offset << " results"; | ||
186 | resultSet.skip(offset); | ||
187 | int counter = 0; | ||
188 | /* while (!batchSize || (counter < batchSize)) { */ | ||
189 | /* const bool ret = */ | ||
190 | /* resultSet.next([this, &counter, callback](const ResultSet::Result &result) -> bool { */ | ||
191 | /* counter++; */ | ||
192 | /* auto adaptor = mResourceContext.adaptorFactory<DomainType>().createAdaptor(result.buffer.entity()); */ | ||
193 | /* Q_ASSERT(adaptor); */ | ||
194 | /* return callback(QSharedPointer<DomainType>::create(mResourceContext, result.uid, result.buffer.revision(), adaptor), result.operation, result.aggregateValues); */ | ||
195 | /* }); */ | ||
196 | /* if (!ret) { */ | ||
197 | /* break; */ | ||
198 | /* } */ | ||
199 | /* }; */ | ||
200 | SinkTrace() << "Replayed " << counter << " results." | ||
201 | << "Limit " << batchSize; | ||
202 | return counter; | ||
203 | } | ||
204 | |||
205 | template class Sink::EntityReader<Sink::ApplicationDomain::Folder>; | ||
206 | template class Sink::EntityReader<Sink::ApplicationDomain::Mail>; | ||
207 | template class Sink::EntityReader<Sink::ApplicationDomain::Event>; | ||