comparison Plugin/Cache/CacheManager.cpp @ 296:d179f3928342

removed "using namespace Orthanc"
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 30 Jan 2021 12:26:50 +0100
parents fc57bf7c0c83
children fb7d62e3235e
comparison
equal deleted inserted replaced
295:fc57bf7c0c83 296:d179f3928342
182 void CacheManager::MakeRoom(Bundle& bundle, 182 void CacheManager::MakeRoom(Bundle& bundle,
183 std::list<std::string>& toRemove, 183 std::list<std::string>& toRemove,
184 int bundleIndex, 184 int bundleIndex,
185 const BundleQuota& quota) 185 const BundleQuota& quota)
186 { 186 {
187 using namespace Orthanc;
188
189 toRemove.clear(); 187 toRemove.clear();
190 188
191 // Make room in the bundle 189 // Make room in the bundle
192 while (!quota.IsSatisfied(bundle)) 190 while (!quota.IsSatisfied(bundle))
193 { 191 {
194 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? ORDER BY seq"); 192 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? ORDER BY seq");
195 s.BindInt(0, bundleIndex); 193 s.BindInt(0, bundleIndex);
196 194
197 if (s.Step()) 195 if (s.Step())
198 { 196 {
199 SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?"); 197 Orthanc::SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?");
200 t.BindInt64(0, s.ColumnInt64(0)); 198 t.BindInt64(0, s.ColumnInt64(0));
201 t.Run(); 199 t.Run();
202 200
203 toRemove.push_back(s.ColumnString(1)); 201 toRemove.push_back(s.ColumnString(1));
204 bundle.Remove(s.ColumnInt64(2)); 202 bundle.Remove(s.ColumnInt64(2));
214 212
215 213
216 void CacheManager::EnsureQuota(int bundleIndex, 214 void CacheManager::EnsureQuota(int bundleIndex,
217 const BundleQuota& quota) 215 const BundleQuota& quota)
218 { 216 {
219 using namespace Orthanc;
220
221 // Remove the cached files that exceed the quota 217 // Remove the cached files that exceed the quota
222 std::unique_ptr<SQLite::Transaction> transaction(new SQLite::Transaction(pimpl_->db_)); 218 std::unique_ptr<Orthanc::SQLite::Transaction> transaction(new Orthanc::SQLite::Transaction(pimpl_->db_));
223 transaction->Begin(); 219 transaction->Begin();
224 220
225 Bundle bundle = GetBundle(bundleIndex); 221 Bundle bundle = GetBundle(bundleIndex);
226 222
227 std::list<std::string> toRemove; 223 std::list<std::string> toRemove;
239 235
240 236
241 237
242 void CacheManager::ReadBundleStatistics() 238 void CacheManager::ReadBundleStatistics()
243 { 239 {
244 using namespace Orthanc;
245
246 pimpl_->bundles_.clear(); 240 pimpl_->bundles_.clear();
247 241
248 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT bundle,COUNT(*),SUM(fileSize) FROM Cache GROUP BY bundle"); 242 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT bundle,COUNT(*),SUM(fileSize) FROM Cache GROUP BY bundle");
249 while (s.Step()) 243 while (s.Step())
250 { 244 {
251 int index = s.ColumnInt(0); 245 int index = s.ColumnInt(0);
252 Bundle bundle(static_cast<uint32_t>(s.ColumnInt(1)), 246 Bundle bundle(static_cast<uint32_t>(s.ColumnInt(1)),
253 static_cast<uint64_t>(s.ColumnInt64(2))); 247 static_cast<uint64_t>(s.ColumnInt64(2)));
262 if (!pimpl_->sanityCheck_) 256 if (!pimpl_->sanityCheck_)
263 { 257 {
264 return; 258 return;
265 } 259 }
266 260
267 using namespace Orthanc; 261 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT bundle,COUNT(*),SUM(fileSize) FROM Cache GROUP BY bundle");
268
269 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT bundle,COUNT(*),SUM(fileSize) FROM Cache GROUP BY bundle");
270 while (s.Step()) 262 while (s.Step())
271 { 263 {
272 const Bundle& bundle = GetBundle(s.ColumnInt(0)); 264 const Bundle& bundle = GetBundle(s.ColumnInt(0));
273 if (bundle.GetCount() != static_cast<uint32_t>(s.ColumnInt(1)) || 265 if (bundle.GetCount() != static_cast<uint32_t>(s.ColumnInt(1)) ||
274 bundle.GetSpace() != static_cast<uint64_t>(s.ColumnInt64(2))) 266 bundle.GetSpace() != static_cast<uint64_t>(s.ColumnInt64(2)))
340 { 332 {
341 // Cannot store such a large instance into the cache, forget about it 333 // Cannot store such a large instance into the cache, forget about it
342 return; 334 return;
343 } 335 }
344 336
345 using namespace Orthanc; 337 std::unique_ptr<Orthanc::SQLite::Transaction> transaction(new Orthanc::SQLite::Transaction(pimpl_->db_));
346
347 std::unique_ptr<SQLite::Transaction> transaction(new SQLite::Transaction(pimpl_->db_));
348 transaction->Begin(); 338 transaction->Begin();
349 339
350 Bundle bundle = GetBundle(bundleIndex); 340 Bundle bundle = GetBundle(bundleIndex);
351 341
352 std::list<std::string> toRemove; 342 std::list<std::string> toRemove;
353 bundle.Add(content.size()); 343 bundle.Add(content.size());
354 MakeRoom(bundle, toRemove, bundleIndex, quota); 344 MakeRoom(bundle, toRemove, bundleIndex, quota);
355 345
356 // Store the cached content on the disk 346 // Store the cached content on the disk
357 const char* data = content.size() ? &content[0] : NULL; 347 const char* data = content.size() ? &content[0] : NULL;
358 std::string uuid = Toolbox::GenerateUuid(); 348 std::string uuid = Orthanc::Toolbox::GenerateUuid();
359 pimpl_->storage_.Create(uuid, data, content.size(), Orthanc::FileContentType_Unknown); 349 pimpl_->storage_.Create(uuid, data, content.size(), Orthanc::FileContentType_Unknown);
360 350
361 // Remove the previous cached value. This might happen if the same 351 // Remove the previous cached value. This might happen if the same
362 // item is accessed very quickly twice: Another factory could have 352 // item is accessed very quickly twice: Another factory could have
363 // been cached a value before the check for existence in Access(). 353 // been cached a value before the check for existence in Access().
364 { 354 {
365 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? AND item=?"); 355 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? AND item=?");
366 s.BindInt(0, bundleIndex); 356 s.BindInt(0, bundleIndex);
367 s.BindString(1, item); 357 s.BindString(1, item);
368 if (s.Step()) 358 if (s.Step())
369 { 359 {
370 SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?"); 360 Orthanc::SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?");
371 t.BindInt64(0, s.ColumnInt64(0)); 361 t.BindInt64(0, s.ColumnInt64(0));
372 t.Run(); 362 t.Run();
373 363
374 toRemove.push_back(s.ColumnString(1)); 364 toRemove.push_back(s.ColumnString(1));
375 bundle.Remove(s.ColumnInt64(2)); 365 bundle.Remove(s.ColumnInt64(2));
376 } 366 }
377 } 367 }
378 368
379 { 369 {
380 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "INSERT INTO Cache VALUES(NULL, ?, ?, ?, ?)"); 370 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "INSERT INTO Cache VALUES(NULL, ?, ?, ?, ?)");
381 s.BindInt(0, bundleIndex); 371 s.BindInt(0, bundleIndex);
382 s.BindString(1, item); 372 s.BindString(1, item);
383 s.BindString(2, uuid); 373 s.BindString(2, uuid);
384 s.BindInt64(3, content.size()); 374 s.BindInt64(3, content.size());
385 375
410 bool CacheManager::LocateInCache(std::string& uuid, 400 bool CacheManager::LocateInCache(std::string& uuid,
411 uint64_t& size, 401 uint64_t& size,
412 int bundle, 402 int bundle,
413 const std::string& item) 403 const std::string& item)
414 { 404 {
415 using namespace Orthanc; 405 SanityCheck();
416 SanityCheck(); 406
417 407 std::unique_ptr<Orthanc::SQLite::Transaction> transaction(new Orthanc::SQLite::Transaction(pimpl_->db_));
418 std::unique_ptr<SQLite::Transaction> transaction(new SQLite::Transaction(pimpl_->db_));
419 transaction->Begin(); 408 transaction->Begin();
420 409
421 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? AND item=?"); 410 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? AND item=?");
422 s.BindInt(0, bundle); 411 s.BindInt(0, bundle);
423 s.BindString(1, item); 412 s.BindString(1, item);
424 if (!s.Step()) 413 if (!s.Step())
425 { 414 {
426 return false; 415 return false;
429 int64_t seq = s.ColumnInt64(0); 418 int64_t seq = s.ColumnInt64(0);
430 uuid = s.ColumnString(1); 419 uuid = s.ColumnString(1);
431 size = s.ColumnInt64(2); 420 size = s.ColumnInt64(2);
432 421
433 // Touch the cache to fulfill the LRU scheme. 422 // Touch the cache to fulfill the LRU scheme.
434 SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?"); 423 Orthanc::SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?");
435 t.BindInt64(0, seq); 424 t.BindInt64(0, seq);
436 if (t.Run()) 425 if (t.Run())
437 { 426 {
438 SQLite::Statement u(pimpl_->db_, SQLITE_FROM_HERE, "INSERT INTO Cache VALUES(NULL, ?, ?, ?, ?)"); 427 Orthanc::SQLite::Statement u(pimpl_->db_, SQLITE_FROM_HERE, "INSERT INTO Cache VALUES(NULL, ?, ?, ?, ?)");
439 u.BindInt(0, bundle); 428 u.BindInt(0, bundle);
440 u.BindString(1, item); 429 u.BindString(1, item);
441 u.BindString(2, uuid); 430 u.BindString(2, uuid);
442 u.BindInt64(3, size); 431 u.BindInt64(3, size);
443 if (u.Run()) 432 if (u.Run())
502 491
503 492
504 void CacheManager::Invalidate(int bundleIndex, 493 void CacheManager::Invalidate(int bundleIndex,
505 const std::string& item) 494 const std::string& item)
506 { 495 {
507 using namespace Orthanc; 496 SanityCheck();
508 SanityCheck(); 497
509 498 std::unique_ptr<Orthanc::SQLite::Transaction> transaction(new Orthanc::SQLite::Transaction(pimpl_->db_));
510 std::unique_ptr<SQLite::Transaction> transaction(new SQLite::Transaction(pimpl_->db_));
511 transaction->Begin(); 499 transaction->Begin();
512 500
513 Bundle bundle = GetBundle(bundleIndex); 501 Bundle bundle = GetBundle(bundleIndex);
514 502
515 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? AND item=?"); 503 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT seq, fileUuid, fileSize FROM Cache WHERE bundle=? AND item=?");
516 s.BindInt(0, bundleIndex); 504 s.BindInt(0, bundleIndex);
517 s.BindString(1, item); 505 s.BindString(1, item);
518 if (s.Step()) 506 if (s.Step())
519 { 507 {
520 int64_t seq = s.ColumnInt64(0); 508 int64_t seq = s.ColumnInt64(0);
521 const std::string uuid = s.ColumnString(1); 509 const std::string uuid = s.ColumnString(1);
522 uint64_t expectedSize = s.ColumnInt64(2); 510 uint64_t expectedSize = s.ColumnInt64(2);
523 bundle.Remove(expectedSize); 511 bundle.Remove(expectedSize);
524 512
525 SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?"); 513 Orthanc::SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE seq=?");
526 t.BindInt64(0, seq); 514 t.BindInt64(0, seq);
527 if (t.Run()) 515 if (t.Run())
528 { 516 {
529 transaction->Commit(); 517 transaction->Commit();
530 pimpl_->bundles_[bundleIndex] = bundle; 518 pimpl_->bundles_[bundleIndex] = bundle;
549 } 537 }
550 538
551 void CacheManager::SetDefaultQuota(uint32_t maxCount, 539 void CacheManager::SetDefaultQuota(uint32_t maxCount,
552 uint64_t maxSpace) 540 uint64_t maxSpace)
553 { 541 {
554 using namespace Orthanc;
555 SanityCheck(); 542 SanityCheck();
556 543
557 pimpl_->defaultQuota_ = BundleQuota(maxCount, maxSpace); 544 pimpl_->defaultQuota_ = BundleQuota(maxCount, maxSpace);
558 545
559 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT DISTINCT bundle FROM Cache"); 546 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT DISTINCT bundle FROM Cache");
560 while (s.Step()) 547 while (s.Step())
561 { 548 {
562 EnsureQuota(s.ColumnInt(0), pimpl_->defaultQuota_); 549 EnsureQuota(s.ColumnInt(0), pimpl_->defaultQuota_);
563 } 550 }
564 551
566 } 553 }
567 554
568 555
569 void CacheManager::Clear() 556 void CacheManager::Clear()
570 { 557 {
571 using namespace Orthanc; 558 SanityCheck();
572 SanityCheck(); 559
573 560 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT fileUuid FROM Cache");
574 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT fileUuid FROM Cache");
575 while (s.Step()) 561 while (s.Step())
576 { 562 {
577 pimpl_->storage_.Remove(s.ColumnString(0), Orthanc::FileContentType_Unknown); 563 pimpl_->storage_.Remove(s.ColumnString(0), Orthanc::FileContentType_Unknown);
578 } 564 }
579 565
580 SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache"); 566 Orthanc::SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache");
581 t.Run(); 567 t.Run();
582 568
583 ReadBundleStatistics(); 569 ReadBundleStatistics();
584 SanityCheck(); 570 SanityCheck();
585 } 571 }
586 572
587 573
588 574
589 void CacheManager::Clear(int bundle) 575 void CacheManager::Clear(int bundle)
590 { 576 {
591 using namespace Orthanc; 577 SanityCheck();
592 SanityCheck(); 578
593 579 Orthanc::SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT fileUuid FROM Cache WHERE bundle=?");
594 SQLite::Statement s(pimpl_->db_, SQLITE_FROM_HERE, "SELECT fileUuid FROM Cache WHERE bundle=?");
595 s.BindInt(0, bundle); 580 s.BindInt(0, bundle);
596 while (s.Step()) 581 while (s.Step())
597 { 582 {
598 pimpl_->storage_.Remove(s.ColumnString(0), Orthanc::FileContentType_Unknown); 583 pimpl_->storage_.Remove(s.ColumnString(0), Orthanc::FileContentType_Unknown);
599 } 584 }
600 585
601 SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE bundle=?"); 586 Orthanc::SQLite::Statement t(pimpl_->db_, SQLITE_FROM_HERE, "DELETE FROM Cache WHERE bundle=?");
602 t.BindInt(0, bundle); 587 t.BindInt(0, bundle);
603 t.Run(); 588 t.Run();
604 589
605 ReadBundleStatistics(); 590 ReadBundleStatistics();
606 SanityCheck(); 591 SanityCheck();