comparison OrthancServer/ServerIndex.cpp @ 3105:2e1711f80f74 db-changes

More consistent handling of the "Last" field returned by the "/changes" URI
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 10 Jan 2019 18:02:34 +0100
parents 61da3c9b4121
children 8ea7c4546c3a
comparison
equal deleted inserted replaced
3101:b2b6db5ad9a5 3105:2e1711f80f74
1306 template <typename T> 1306 template <typename T>
1307 static void FormatLog(Json::Value& target, 1307 static void FormatLog(Json::Value& target,
1308 const std::list<T>& log, 1308 const std::list<T>& log,
1309 const std::string& name, 1309 const std::string& name,
1310 bool done, 1310 bool done,
1311 int64_t since) 1311 int64_t since,
1312 bool hasLast,
1313 int64_t last)
1312 { 1314 {
1313 Json::Value items = Json::arrayValue; 1315 Json::Value items = Json::arrayValue;
1314 for (typename std::list<T>::const_iterator 1316 for (typename std::list<T>::const_iterator
1315 it = log.begin(); it != log.end(); ++it) 1317 it = log.begin(); it != log.end(); ++it)
1316 { 1318 {
1321 1323
1322 target = Json::objectValue; 1324 target = Json::objectValue;
1323 target[name] = items; 1325 target[name] = items;
1324 target["Done"] = done; 1326 target["Done"] = done;
1325 1327
1326 int64_t last = (log.empty() ? since : log.back().GetSeq()); 1328 if (!hasLast)
1329 {
1330 // Best-effort guess of the last index in the sequence
1331 if (log.empty())
1332 {
1333 last = since;
1334 }
1335 else
1336 {
1337 last = log.back().GetSeq();
1338 }
1339 }
1340
1327 target["Last"] = static_cast<int>(last); 1341 target["Last"] = static_cast<int>(last);
1328 } 1342 }
1329 1343
1330 1344
1331 void ServerIndex::GetChanges(Json::Value& target, 1345 void ServerIndex::GetChanges(Json::Value& target,
1332 int64_t since, 1346 int64_t since,
1333 unsigned int maxResults) 1347 unsigned int maxResults)
1334 { 1348 {
1335 std::list<ServerIndexChange> changes; 1349 std::list<ServerIndexChange> changes;
1336 bool done; 1350 bool done;
1351 bool hasLast = false;
1352 int64_t last = 0;
1337 1353
1338 { 1354 {
1339 boost::mutex::scoped_lock lock(mutex_); 1355 boost::mutex::scoped_lock lock(mutex_);
1340 1356
1341 // Fix wrt. Orthanc <= 1.3.2: A transaction was missing, as 1357 // Fix wrt. Orthanc <= 1.3.2: A transaction was missing, as
1342 // "GetLastChange()" involves calls to "GetPublicId()" 1358 // "GetLastChange()" involves calls to "GetPublicId()"
1343 Transaction transaction(*this); 1359 Transaction transaction(*this);
1360
1344 db_.GetChanges(changes, done, since, maxResults); 1361 db_.GetChanges(changes, done, since, maxResults);
1362 if (changes.empty())
1363 {
1364 last = db_.GetLastChangeIndex();
1365 hasLast = true;
1366 }
1367
1345 transaction.Commit(0); 1368 transaction.Commit(0);
1346 } 1369 }
1347 1370
1348 FormatLog(target, changes, "Changes", done, since); 1371 FormatLog(target, changes, "Changes", done, since, hasLast, last);
1349 } 1372 }
1350 1373
1351 1374
1352 void ServerIndex::GetLastChange(Json::Value& target) 1375 void ServerIndex::GetLastChange(Json::Value& target)
1353 { 1376 {
1354 std::list<ServerIndexChange> changes; 1377 std::list<ServerIndexChange> changes;
1378 bool hasLast = false;
1379 int64_t last = 0;
1355 1380
1356 { 1381 {
1357 boost::mutex::scoped_lock lock(mutex_); 1382 boost::mutex::scoped_lock lock(mutex_);
1358 1383
1359 // Fix wrt. Orthanc <= 1.3.2: A transaction was missing, as 1384 // Fix wrt. Orthanc <= 1.3.2: A transaction was missing, as
1360 // "GetLastChange()" involves calls to "GetPublicId()" 1385 // "GetLastChange()" involves calls to "GetPublicId()"
1361 Transaction transaction(*this); 1386 Transaction transaction(*this);
1387
1362 db_.GetLastChange(changes); 1388 db_.GetLastChange(changes);
1389 if (changes.empty())
1390 {
1391 last = db_.GetLastChangeIndex();
1392 hasLast = true;
1393 }
1394
1363 transaction.Commit(0); 1395 transaction.Commit(0);
1364 } 1396 }
1365 1397
1366 FormatLog(target, changes, "Changes", true, 0); 1398 FormatLog(target, changes, "Changes", true, 0, hasLast, last);
1367 } 1399 }
1368 1400
1369 1401
1370 void ServerIndex::LogExportedResource(const std::string& publicId, 1402 void ServerIndex::LogExportedResource(const std::string& publicId,
1371 const std::string& remoteModality) 1403 const std::string& remoteModality)
1467 { 1499 {
1468 boost::mutex::scoped_lock lock(mutex_); 1500 boost::mutex::scoped_lock lock(mutex_);
1469 db_.GetExportedResources(exported, done, since, maxResults); 1501 db_.GetExportedResources(exported, done, since, maxResults);
1470 } 1502 }
1471 1503
1472 FormatLog(target, exported, "Exports", done, since); 1504 FormatLog(target, exported, "Exports", done, since, false, -1);
1473 } 1505 }
1474 1506
1475 1507
1476 void ServerIndex::GetLastExportedResource(Json::Value& target) 1508 void ServerIndex::GetLastExportedResource(Json::Value& target)
1477 { 1509 {
1480 { 1512 {
1481 boost::mutex::scoped_lock lock(mutex_); 1513 boost::mutex::scoped_lock lock(mutex_);
1482 db_.GetLastExportedResource(exported); 1514 db_.GetLastExportedResource(exported);
1483 } 1515 }
1484 1516
1485 FormatLog(target, exported, "Exports", true, 0); 1517 FormatLog(target, exported, "Exports", true, 0, false, -1);
1486 } 1518 }
1487 1519
1488 1520
1489 bool ServerIndex::IsRecyclingNeeded(uint64_t instanceSize) 1521 bool ServerIndex::IsRecyclingNeeded(uint64_t instanceSize)
1490 { 1522 {