summaryrefslogtreecommitdiffstats
path: root/common/storage/entitystore.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-01-23 11:03:03 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-01-23 11:03:03 +0100
commit6003ae63e45485d1f1c76ea378900bc5242465cd (patch)
treeb03327d733714a0dab7272c0deb9a44075ac29e4 /common/storage/entitystore.cpp
parent752f0907574debe9d7d139a117b2efac80636e93 (diff)
downloadsink-6003ae63e45485d1f1c76ea378900bc5242465cd.tar.gz
sink-6003ae63e45485d1f1c76ea378900bc5242465cd.zip
Debug output
Diffstat (limited to 'common/storage/entitystore.cpp')
-rw-r--r--common/storage/entitystore.cpp50
1 files changed, 24 insertions, 26 deletions
diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp
index 96c8ccd..6c341e2 100644
--- a/common/storage/entitystore.cpp
+++ b/common/storage/entitystore.cpp
@@ -37,8 +37,6 @@
37using namespace Sink; 37using namespace Sink;
38using namespace Sink::Storage; 38using namespace Sink::Storage;
39 39
40SINK_DEBUG_AREA("entitystore");
41
42class EntityStore::Private { 40class EntityStore::Private {
43public: 41public:
44 Private(const ResourceContext &context, const Sink::Log::Context &ctx) : resourceContext(context), logCtx(ctx.subContext("entitystore")) {} 42 Private(const ResourceContext &context, const Sink::Log::Context &ctx) : resourceContext(context), logCtx(ctx.subContext("entitystore")) {}
@@ -112,7 +110,7 @@ EntityStore::EntityStore(const ResourceContext &context, const Log::Context &ctx
112 110
113void EntityStore::startTransaction(Sink::Storage::DataStore::AccessMode accessMode) 111void EntityStore::startTransaction(Sink::Storage::DataStore::AccessMode accessMode)
114{ 112{
115 SinkTraceCtx(d->logCtx) << "Starting transaction"; 113 SinkTraceCtx(d->logCtx) << "Starting transaction: " << accessMode;
116 Sink::Storage::DataStore store(Sink::storageLocation(), d->resourceContext.instanceId(), accessMode); 114 Sink::Storage::DataStore store(Sink::storageLocation(), d->resourceContext.instanceId(), accessMode);
117 d->transaction = store.createTransaction(accessMode); 115 d->transaction = store.createTransaction(accessMode);
118 Q_ASSERT(d->transaction.validateNamedDatabases()); 116 Q_ASSERT(d->transaction.validateNamedDatabases());
@@ -136,7 +134,7 @@ void EntityStore::copyBlobs(ApplicationDomain::ApplicationDomainType &entity, qi
136{ 134{
137 const auto directory = d->entityBlobStoragePath(entity.identifier()); 135 const auto directory = d->entityBlobStoragePath(entity.identifier());
138 if (!QDir().mkpath(directory)) { 136 if (!QDir().mkpath(directory)) {
139 SinkWarning() << "Failed to create the directory: " << directory; 137 SinkWarningCtx(d->logCtx) << "Failed to create the directory: " << directory;
140 } 138 }
141 139
142 for (const auto &property : entity.changedProperties()) { 140 for (const auto &property : entity.changedProperties()) {
@@ -151,10 +149,10 @@ void EntityStore::copyBlobs(ApplicationDomain::ApplicationDomainType &entity, qi
151 QFile::remove(filePath); 149 QFile::remove(filePath);
152 QFile origFile(oldPath); 150 QFile origFile(oldPath);
153 if (!origFile.open(QIODevice::ReadWrite)) { 151 if (!origFile.open(QIODevice::ReadWrite)) {
154 SinkWarning() << "Failed to open the original file with write rights: " << origFile.errorString(); 152 SinkWarningCtx(d->logCtx) << "Failed to open the original file with write rights: " << origFile.errorString();
155 } 153 }
156 if (!origFile.rename(filePath)) { 154 if (!origFile.rename(filePath)) {
157 SinkWarning() << "Failed to move the file from: " << oldPath << " to " << filePath << ". " << origFile.errorString(); 155 SinkWarningCtx(d->logCtx) << "Failed to move the file from: " << oldPath << " to " << filePath << ". " << origFile.errorString();
158 } 156 }
159 origFile.close(); 157 origFile.close();
160 entity.setProperty(property, QVariant::fromValue(ApplicationDomain::BLOB{filePath})); 158 entity.setProperty(property, QVariant::fromValue(ApplicationDomain::BLOB{filePath}));
@@ -166,7 +164,7 @@ void EntityStore::copyBlobs(ApplicationDomain::ApplicationDomainType &entity, qi
166bool EntityStore::add(const QByteArray &type, const ApplicationDomain::ApplicationDomainType &entity_, bool replayToSource, const PreprocessCreation &preprocess) 164bool EntityStore::add(const QByteArray &type, const ApplicationDomain::ApplicationDomainType &entity_, bool replayToSource, const PreprocessCreation &preprocess)
167{ 165{
168 if (entity_.identifier().isEmpty()) { 166 if (entity_.identifier().isEmpty()) {
169 SinkWarning() << "Can't write entity with an empty identifier"; 167 SinkWarningCtx(d->logCtx) << "Can't write entity with an empty identifier";
170 return false; 168 return false;
171 } 169 }
172 170
@@ -197,7 +195,7 @@ bool EntityStore::add(const QByteArray &type, const ApplicationDomain::Applicati
197 195
198 DataStore::mainDatabase(d->transaction, type) 196 DataStore::mainDatabase(d->transaction, type)
199 .write(DataStore::assembleKey(entity.identifier(), newRevision), BufferUtils::extractBuffer(fbb), 197 .write(DataStore::assembleKey(entity.identifier(), newRevision), BufferUtils::extractBuffer(fbb),
200 [&](const DataStore::Error &error) { SinkWarning() << "Failed to write entity" << entity.identifier() << newRevision; }); 198 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << entity.identifier() << newRevision; });
201 DataStore::setMaxRevision(d->transaction, newRevision); 199 DataStore::setMaxRevision(d->transaction, newRevision);
202 DataStore::recordRevision(d->transaction, newRevision, entity.identifier(), type); 200 DataStore::recordRevision(d->transaction, newRevision, entity.identifier(), type);
203 SinkTraceCtx(d->logCtx) << "Wrote entity: " << entity.identifier() << type << newRevision; 201 SinkTraceCtx(d->logCtx) << "Wrote entity: " << entity.identifier() << type << newRevision;
@@ -209,7 +207,7 @@ bool EntityStore::modify(const QByteArray &type, const ApplicationDomain::Applic
209 auto changeset = diff.changedProperties(); 207 auto changeset = diff.changedProperties();
210 const auto current = readLatest(type, diff.identifier()); 208 const auto current = readLatest(type, diff.identifier());
211 if (current.identifier().isEmpty()) { 209 if (current.identifier().isEmpty()) {
212 SinkWarning() << "Failed to read current version: " << diff.identifier(); 210 SinkWarningCtx(d->logCtx) << "Failed to read current version: " << diff.identifier();
213 return false; 211 return false;
214 } 212 }
215 213
@@ -263,7 +261,7 @@ bool EntityStore::modify(const QByteArray &type, const ApplicationDomain::Applic
263 261
264 DataStore::mainDatabase(d->transaction, type) 262 DataStore::mainDatabase(d->transaction, type)
265 .write(DataStore::assembleKey(newEntity.identifier(), newRevision), BufferUtils::extractBuffer(fbb), 263 .write(DataStore::assembleKey(newEntity.identifier(), newRevision), BufferUtils::extractBuffer(fbb),
266 [&](const DataStore::Error &error) { SinkWarning() << "Failed to write entity" << newEntity.identifier() << newRevision; }); 264 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << newEntity.identifier() << newRevision; });
267 DataStore::setMaxRevision(d->transaction, newRevision); 265 DataStore::setMaxRevision(d->transaction, newRevision);
268 DataStore::recordRevision(d->transaction, newRevision, newEntity.identifier(), type); 266 DataStore::recordRevision(d->transaction, newRevision, newEntity.identifier(), type);
269 SinkTraceCtx(d->logCtx) << "Wrote modified entity: " << newEntity.identifier() << type << newRevision; 267 SinkTraceCtx(d->logCtx) << "Wrote modified entity: " << newEntity.identifier() << type << newRevision;
@@ -287,14 +285,14 @@ bool EntityStore::remove(const QByteArray &type, const QByteArray &uid, bool rep
287 } 285 }
288 return false; 286 return false;
289 }, 287 },
290 [](const DataStore::Error &error) { SinkWarning() << "Failed to read old revision from storage: " << error.message; }); 288 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read old revision from storage: " << error.message; });
291 289
292 if (!found) { 290 if (!found) {
293 SinkWarning() << "Remove: Failed to find entity " << uid; 291 SinkWarningCtx(d->logCtx) << "Remove: Failed to find entity " << uid;
294 return false; 292 return false;
295 } 293 }
296 if (alreadyRemoved) { 294 if (alreadyRemoved) {
297 SinkWarning() << "Remove: Entity is already removed " << uid; 295 SinkWarningCtx(d->logCtx) << "Remove: Entity is already removed " << uid;
298 return false; 296 return false;
299 } 297 }
300 298
@@ -320,7 +318,7 @@ bool EntityStore::remove(const QByteArray &type, const QByteArray &uid, bool rep
320 318
321 DataStore::mainDatabase(d->transaction, type) 319 DataStore::mainDatabase(d->transaction, type)
322 .write(DataStore::assembleKey(uid, newRevision), BufferUtils::extractBuffer(fbb), 320 .write(DataStore::assembleKey(uid, newRevision), BufferUtils::extractBuffer(fbb),
323 [&](const DataStore::Error &error) { SinkWarning() << "Failed to write entity" << uid << newRevision; }); 321 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to write entity" << uid << newRevision; });
324 DataStore::setMaxRevision(d->transaction, newRevision); 322 DataStore::setMaxRevision(d->transaction, newRevision);
325 DataStore::recordRevision(d->transaction, newRevision, uid, type); 323 DataStore::recordRevision(d->transaction, newRevision, uid, type);
326 return true; 324 return true;
@@ -336,7 +334,7 @@ void EntityStore::cleanupEntityRevisionsUntil(qint64 revision)
336 [&](const QByteArray &key, const QByteArray &data) -> bool { 334 [&](const QByteArray &key, const QByteArray &data) -> bool {
337 EntityBuffer buffer(const_cast<const char *>(data.data()), data.size()); 335 EntityBuffer buffer(const_cast<const char *>(data.data()), data.size());
338 if (!buffer.isValid()) { 336 if (!buffer.isValid()) {
339 SinkWarning() << "Read invalid buffer from disk"; 337 SinkWarningCtx(d->logCtx) << "Read invalid buffer from disk";
340 } else { 338 } else {
341 const auto metadata = flatbuffers::GetRoot<Metadata>(buffer.metadataBuffer()); 339 const auto metadata = flatbuffers::GetRoot<Metadata>(buffer.metadataBuffer());
342 const qint64 rev = metadata->revision(); 340 const qint64 rev = metadata->revision();
@@ -350,7 +348,7 @@ void EntityStore::cleanupEntityRevisionsUntil(qint64 revision)
350 const auto directory = d->entityBlobStoragePath(uid); 348 const auto directory = d->entityBlobStoragePath(uid);
351 QDir dir(directory); 349 QDir dir(directory);
352 if (!dir.removeRecursively()) { 350 if (!dir.removeRecursively()) {
353 SinkError() << "Failed to cleanup: " << directory; 351 SinkErrorCtx(d->logCtx) << "Failed to cleanup: " << directory;
354 } 352 }
355 } 353 }
356 //Don't cleanup more than specified 354 //Don't cleanup more than specified
@@ -361,7 +359,7 @@ void EntityStore::cleanupEntityRevisionsUntil(qint64 revision)
361 359
362 return true; 360 return true;
363 }, 361 },
364 [](const DataStore::Error &error) { SinkWarning() << "Error while reading: " << error.message; }, true); 362 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error while reading: " << error.message; }, true);
365 DataStore::setCleanedUpRevision(d->transaction, revision); 363 DataStore::setCleanedUpRevision(d->transaction, revision);
366} 364}
367 365
@@ -407,7 +405,7 @@ QVector<QByteArray> EntityStore::fullScan(const QByteArray &type)
407 keys << uid; 405 keys << uid;
408 return true; 406 return true;
409 }, 407 },
410 [](const DataStore::Error &error) { SinkWarning() << "Error during query: " << error.message; }); 408 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during query: " << error.message; });
411 409
412 SinkTraceCtx(d->logCtx) << "Full scan retrieved " << keys.size() << " results."; 410 SinkTraceCtx(d->logCtx) << "Full scan retrieved " << keys.size() << " results.";
413 return keys.toList().toVector(); 411 return keys.toList().toVector();
@@ -446,7 +444,7 @@ void EntityStore::indexLookup(const QByteArray &type, const QByteArray &property
446 /* callback(sinkId); */ 444 /* callback(sinkId); */
447 /* }, */ 445 /* }, */
448 /* [&](const Index::Error &error) { */ 446 /* [&](const Index::Error &error) { */
449 /* SinkWarning() << "Error in index: " << error.message << property; */ 447 /* SinkWarningCtx(d->logCtx) << "Error in index: " << error.message << property; */
450 /* }); */ 448 /* }); */
451} 449}
452 450
@@ -458,7 +456,7 @@ void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, cons
458 callback(DataStore::uidFromKey(key), Sink::EntityBuffer(value.data(), value.size())); 456 callback(DataStore::uidFromKey(key), Sink::EntityBuffer(value.data(), value.size()));
459 return false; 457 return false;
460 }, 458 },
461 [&](const DataStore::Error &error) { SinkWarning() << "Error during query: " << error.message << uid; }); 459 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during query: " << error.message << uid; });
462} 460}
463 461
464void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomain::ApplicationDomainType &)> callback) 462void EntityStore::readLatest(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomain::ApplicationDomainType &)> callback)
@@ -494,7 +492,7 @@ void EntityStore::readEntity(const QByteArray &type, const QByteArray &key, cons
494 callback(DataStore::uidFromKey(key), Sink::EntityBuffer(value.data(), value.size())); 492 callback(DataStore::uidFromKey(key), Sink::EntityBuffer(value.data(), value.size()));
495 return false; 493 return false;
496 }, 494 },
497 [&](const DataStore::Error &error) { SinkWarning() << "Error during query: " << error.message << key; }); 495 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during query: " << error.message << key; });
498} 496}
499 497
500void EntityStore::readEntity(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomain::ApplicationDomainType &)> callback) 498void EntityStore::readEntity(const QByteArray &type, const QByteArray &uid, const std::function<void(const ApplicationDomain::ApplicationDomainType &)> callback)
@@ -524,7 +522,7 @@ void EntityStore::readAll(const QByteArray &type, const std::function<void(const
524 callback(d->createApplicationDomainType(type, uid, DataStore::maxRevision(d->getTransaction()), buffer)); 522 callback(d->createApplicationDomainType(type, uid, DataStore::maxRevision(d->getTransaction()), buffer));
525 return true; 523 return true;
526 }, 524 },
527 [&](const DataStore::Error &error) { SinkWarning() << "Error during query: " << error.message; }); 525 [&](const DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during query: " << error.message; });
528} 526}
529 527
530void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedType, const std::function<void(const QByteArray &key)> &callback) 528void EntityStore::readRevisions(qint64 baseRevision, const QByteArray &expectedType, const std::function<void(const QByteArray &key)> &callback)
@@ -561,7 +559,7 @@ void EntityStore::readPrevious(const QByteArray &type, const QByteArray &uid, qi
561 } 559 }
562 return true; 560 return true;
563 }, 561 },
564 [](const Sink::Storage::DataStore::Error &error) { SinkWarning() << "Failed to read current value from storage: " << error.message; }, true); 562 [&](const Sink::Storage::DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; }, true);
565 return readEntity(type, Sink::Storage::DataStore::assembleKey(uid, latestRevision), callback); 563 return readEntity(type, Sink::Storage::DataStore::assembleKey(uid, latestRevision), callback);
566} 564}
567 565
@@ -591,7 +589,7 @@ void EntityStore::readAllUids(const QByteArray &type, const std::function<void(c
591 callback(Sink::Storage::DataStore::uidFromKey(key)); 589 callback(Sink::Storage::DataStore::uidFromKey(key));
592 return true; 590 return true;
593 }, 591 },
594 [](const Sink::Storage::DataStore::Error &error) { SinkWarning() << "Failed to read current value from storage: " << error.message; }); 592 [&](const Sink::Storage::DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Failed to read current value from storage: " << error.message; });
595} 593}
596 594
597bool EntityStore::contains(const QByteArray &type, const QByteArray &uid) 595bool EntityStore::contains(const QByteArray &type, const QByteArray &uid)
@@ -615,10 +613,10 @@ qint64 EntityStore::maxRevision()
615/* Sink::Storage::DataStore storage(Sink::storageLocation(), mResourceInstanceIdentifier); */ 613/* Sink::Storage::DataStore storage(Sink::storageLocation(), mResourceInstanceIdentifier); */
616/* if (!storage.exists()) { */ 614/* if (!storage.exists()) { */
617/* //This is not an error if the resource wasn't started before */ 615/* //This is not an error if the resource wasn't started before */
618/* SinkLog() << "Store doesn't exist: " << mResourceInstanceIdentifier; */ 616/* SinkLogCtx(d->logCtx) << "Store doesn't exist: " << mResourceInstanceIdentifier; */
619/* return Sink::Storage::DataStore::Transaction(); */ 617/* return Sink::Storage::DataStore::Transaction(); */
620/* } */ 618/* } */
621/* storage.setDefaultErrorHandler([this](const Sink::Storage::DataStore::Error &error) { SinkWarning() << "Error during query: " << error.store << error.message; }); */ 619/* storage.setDefaultErrorHandler([this](const Sink::Storage::DataStore::Error &error) { SinkWarningCtx(d->logCtx) << "Error during query: " << error.store << error.message; }); */
622/* transaction = storage.createTransaction(Sink::Storage::DataStore::ReadOnly); */ 620/* transaction = storage.createTransaction(Sink::Storage::DataStore::ReadOnly); */
623/* } */ 621/* } */
624 622