comparison OrthancServer/Sources/ServerIndex.cpp @ 4567:b812a5f2cef3 db-changes

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 08 Mar 2021 14:41:29 +0100
parents f52d0bc19e07
children a3e6aa2b07b0
comparison
equal deleted inserted replaced
4566:f52d0bc19e07 4567:b812a5f2cef3
598 assert(listener_.get() != NULL); 598 assert(listener_.get() != NULL);
599 listener_->SignalChange(change); 599 listener_->SignalChange(change);
600 } 600 }
601 601
602 602
603 uint64_t ServerIndex::IncrementGlobalSequenceInternal(GlobalProperty property)
604 {
605 std::string oldValue;
606
607 if (db_.LookupGlobalProperty(oldValue, property))
608 {
609 uint64_t oldNumber;
610
611 try
612 {
613 oldNumber = boost::lexical_cast<uint64_t>(oldValue);
614 }
615 catch (boost::bad_lexical_cast&)
616 {
617 LOG(ERROR) << "Cannot read the global sequence "
618 << boost::lexical_cast<std::string>(property) << ", resetting it";
619 oldNumber = 0;
620 }
621
622 db_.SetGlobalProperty(property, boost::lexical_cast<std::string>(oldNumber + 1));
623 return oldNumber + 1;
624 }
625 else
626 {
627 // Initialize the sequence at "1"
628 db_.SetGlobalProperty(property, "1");
629 return 1;
630 }
631 }
632
633
634 bool ServerIndex::IsUnstableResource(int64_t id) 603 bool ServerIndex::IsUnstableResource(int64_t id)
635 { 604 {
636 return unstableResources_.Contains(id); 605 return unstableResources_.Contains(id);
637 } 606 }
638 607
1234 Recycle(0, ""); 1203 Recycle(0, "");
1235 t.Commit(0); 1204 t.Commit(0);
1236 } 1205 }
1237 1206
1238 1207
1239 void ServerIndex::SetProtectedPatient(const std::string& publicId,
1240 bool isProtected)
1241 {
1242 boost::mutex::scoped_lock lock(mutex_);
1243 Transaction transaction(*this);
1244
1245 // Lookup for the requested resource
1246 int64_t id;
1247 ResourceType type;
1248 if (!db_.LookupResource(id, type, publicId) ||
1249 type != ResourceType_Patient)
1250 {
1251 throw OrthancException(ErrorCode_ParameterOutOfRange);
1252 }
1253
1254 db_.SetProtectedPatient(id, isProtected);
1255 transaction.Commit(0);
1256
1257 if (isProtected)
1258 LOG(INFO) << "Patient " << publicId << " has been protected";
1259 else
1260 LOG(INFO) << "Patient " << publicId << " has been unprotected";
1261 }
1262
1263
1264 void ServerIndex::SetMetadata(const std::string& publicId,
1265 MetadataType type,
1266 const std::string& value)
1267 {
1268 boost::mutex::scoped_lock lock(mutex_);
1269 Transaction t(*this);
1270
1271 ResourceType rtype;
1272 int64_t id;
1273 if (!db_.LookupResource(id, rtype, publicId))
1274 {
1275 throw OrthancException(ErrorCode_UnknownResource);
1276 }
1277
1278 db_.SetMetadata(id, type, value);
1279
1280 if (IsUserMetadata(type))
1281 {
1282 LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId);
1283 }
1284
1285 t.Commit(0);
1286 }
1287
1288
1289 void ServerIndex::DeleteMetadata(const std::string& publicId,
1290 MetadataType type)
1291 {
1292 boost::mutex::scoped_lock lock(mutex_);
1293 Transaction t(*this);
1294
1295 ResourceType rtype;
1296 int64_t id;
1297 if (!db_.LookupResource(id, rtype, publicId))
1298 {
1299 throw OrthancException(ErrorCode_UnknownResource);
1300 }
1301
1302 db_.DeleteMetadata(id, type);
1303
1304 if (IsUserMetadata(type))
1305 {
1306 LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId);
1307 }
1308
1309 t.Commit(0);
1310 }
1311
1312
1313 uint64_t ServerIndex::IncrementGlobalSequence(GlobalProperty sequence)
1314 {
1315 boost::mutex::scoped_lock lock(mutex_);
1316 Transaction transaction(*this);
1317
1318 uint64_t seq = IncrementGlobalSequenceInternal(sequence);
1319 transaction.Commit(0);
1320
1321 return seq;
1322 }
1323
1324
1325
1326 void ServerIndex::LogChange(ChangeType changeType, 1208 void ServerIndex::LogChange(ChangeType changeType,
1327 const std::string& publicId) 1209 const std::string& publicId)
1328 { 1210 {
1329 boost::mutex::scoped_lock lock(mutex_); 1211 boost::mutex::scoped_lock lock(mutex_);
1330 Transaction transaction(*this); 1212 Transaction transaction(*this);
1335 { 1217 {
1336 throw OrthancException(ErrorCode_UnknownResource); 1218 throw OrthancException(ErrorCode_UnknownResource);
1337 } 1219 }
1338 1220
1339 LogChange(id, changeType, type, publicId); 1221 LogChange(id, changeType, type, publicId);
1340 transaction.Commit(0);
1341 }
1342
1343
1344 void ServerIndex::DeleteChanges()
1345 {
1346 boost::mutex::scoped_lock lock(mutex_);
1347
1348 Transaction transaction(*this);
1349 db_.ClearChanges();
1350 transaction.Commit(0);
1351 }
1352
1353 void ServerIndex::DeleteExportedResources()
1354 {
1355 boost::mutex::scoped_lock lock(mutex_);
1356
1357 Transaction transaction(*this);
1358 db_.ClearExportedResources();
1359 transaction.Commit(0); 1222 transaction.Commit(0);
1360 } 1223 }
1361 1224
1362 1225
1363 void ServerIndex::UnstableResourcesMonitorThread(ServerIndex* that, 1226 void ServerIndex::UnstableResourcesMonitorThread(ServerIndex* that,
1513 LogChange(id, ChangeType_UpdatedAttachment, rtype, publicId); 1376 LogChange(id, ChangeType_UpdatedAttachment, rtype, publicId);
1514 } 1377 }
1515 1378
1516 t.Commit(0); 1379 t.Commit(0);
1517 } 1380 }
1518
1519
1520 void ServerIndex::SetGlobalProperty(GlobalProperty property,
1521 const std::string& value)
1522 {
1523 boost::mutex::scoped_lock lock(mutex_);
1524
1525 Transaction transaction(*this);
1526 db_.SetGlobalProperty(property, value);
1527 transaction.Commit(0);
1528 }
1529
1530
1531 1381
1532 1382
1533 void ServerIndex::ReconstructInstance(const ParsedDicomFile& dicom) 1383 void ServerIndex::ReconstructInstance(const ParsedDicomFile& dicom)
1534 { 1384 {
1535 DicomMap summary; 1385 DicomMap summary;
1860 readOperations->Apply(t); 1710 readOperations->Apply(t);
1861 } 1711 }
1862 else 1712 else
1863 { 1713 {
1864 assert(writeOperations != NULL); 1714 assert(writeOperations != NULL);
1865 ReadWriteTransaction t(db_); 1715 ReadWriteTransaction t(db_, *this);
1866 writeOperations->Apply(t, *listener_); 1716 writeOperations->Apply(t, *listener_);
1867 } 1717 }
1868 1718
1869 transaction.Commit(0); 1719 transaction.Commit(0);
1870 1720
1922 ResourceType level) 1772 ResourceType level)
1923 { 1773 {
1924 class Operations : public ReadOnlyOperationsT4<bool&, Json::Value&, const std::string&, ResourceType> 1774 class Operations : public ReadOnlyOperationsT4<bool&, Json::Value&, const std::string&, ResourceType>
1925 { 1775 {
1926 private: 1776 private:
1927 ServerIndex& index_; 1777 ServerIndex& index_; // TODO - REMOVE
1928 1778
1929 public: 1779 public:
1930 explicit Operations(ServerIndex& index) : 1780 explicit Operations(ServerIndex& index) :
1931 index_(index) 1781 index_(index)
1932 { 1782 {
3292 }; 3142 };
3293 3143
3294 Operations operations(publicId, remoteModality); 3144 Operations operations(publicId, remoteModality);
3295 Apply(operations); 3145 Apply(operations);
3296 } 3146 }
3147
3148
3149 void ServerIndex::SetProtectedPatient(const std::string& publicId,
3150 bool isProtected)
3151 {
3152 class Operations : public IReadWriteOperations
3153 {
3154 private:
3155 const std::string& publicId_;
3156 bool isProtected_;
3157
3158 public:
3159 Operations(const std::string& publicId,
3160 bool isProtected) :
3161 publicId_(publicId),
3162 isProtected_(isProtected)
3163 {
3164 }
3165
3166 virtual void Apply(ReadWriteTransaction& transaction,
3167 Listener& listener) ORTHANC_OVERRIDE
3168 {
3169 // Lookup for the requested resource
3170 int64_t id;
3171 ResourceType type;
3172 if (!transaction.LookupResource(id, type, publicId_) ||
3173 type != ResourceType_Patient)
3174 {
3175 throw OrthancException(ErrorCode_ParameterOutOfRange);
3176 }
3177 else
3178 {
3179 transaction.SetProtectedPatient(id, isProtected_);
3180 }
3181 }
3182 };
3183
3184 Operations operations(publicId, isProtected);
3185 Apply(operations);
3186
3187 if (isProtected)
3188 {
3189 LOG(INFO) << "Patient " << publicId << " has been protected";
3190 }
3191 else
3192 {
3193 LOG(INFO) << "Patient " << publicId << " has been unprotected";
3194 }
3195 }
3196
3197
3198 void ServerIndex::SetMetadata(const std::string& publicId,
3199 MetadataType type,
3200 const std::string& value)
3201 {
3202 class Operations : public IReadWriteOperations
3203 {
3204 private:
3205 const std::string& publicId_;
3206 MetadataType type_;
3207 const std::string& value_;
3208
3209 public:
3210 Operations(const std::string& publicId,
3211 MetadataType type,
3212 const std::string& value) :
3213 publicId_(publicId),
3214 type_(type),
3215 value_(value)
3216 {
3217 }
3218
3219 virtual void Apply(ReadWriteTransaction& transaction,
3220 Listener& listener) ORTHANC_OVERRIDE
3221 {
3222 ResourceType rtype;
3223 int64_t id;
3224 if (!transaction.LookupResource(id, rtype, publicId_))
3225 {
3226 throw OrthancException(ErrorCode_UnknownResource);
3227 }
3228 else
3229 {
3230 transaction.SetMetadata(id, type_, value_);
3231
3232 if (IsUserMetadata(type_))
3233 {
3234 transaction.LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId_);
3235 }
3236 }
3237 }
3238 };
3239
3240 Operations operations(publicId, type, value);
3241 Apply(operations);
3242 }
3243
3244
3245 void ServerIndex::DeleteMetadata(const std::string& publicId,
3246 MetadataType type)
3247 {
3248 class Operations : public IReadWriteOperations
3249 {
3250 private:
3251 const std::string& publicId_;
3252 MetadataType type_;
3253
3254 public:
3255 Operations(const std::string& publicId,
3256 MetadataType type) :
3257 publicId_(publicId),
3258 type_(type)
3259 {
3260 }
3261
3262 virtual void Apply(ReadWriteTransaction& transaction,
3263 Listener& listener) ORTHANC_OVERRIDE
3264 {
3265 ResourceType rtype;
3266 int64_t id;
3267 if (!transaction.LookupResource(id, rtype, publicId_))
3268 {
3269 throw OrthancException(ErrorCode_UnknownResource);
3270 }
3271 else
3272 {
3273 transaction.DeleteMetadata(id, type_);
3274
3275 if (IsUserMetadata(type_))
3276 {
3277 transaction.LogChange(id, ChangeType_UpdatedMetadata, rtype, publicId_);
3278 }
3279 }
3280 }
3281 };
3282
3283 Operations operations(publicId, type);
3284 Apply(operations);
3285 }
3286
3287
3288 uint64_t ServerIndex::IncrementGlobalSequence(GlobalProperty sequence)
3289 {
3290 class Operations : public IReadWriteOperations
3291 {
3292 private:
3293 uint64_t newValue_;
3294 GlobalProperty sequence_;
3295
3296 public:
3297 Operations(GlobalProperty sequence) :
3298 sequence_(sequence)
3299 {
3300 }
3301
3302 uint64_t GetNewValue() const
3303 {
3304 return newValue_;
3305 }
3306
3307 virtual void Apply(ReadWriteTransaction& transaction,
3308 Listener& listener) ORTHANC_OVERRIDE
3309 {
3310 std::string oldString;
3311
3312 if (transaction.LookupGlobalProperty(oldString, sequence_))
3313 {
3314 uint64_t oldValue;
3315
3316 try
3317 {
3318 oldValue = boost::lexical_cast<uint64_t>(oldString);
3319 }
3320 catch (boost::bad_lexical_cast&)
3321 {
3322 LOG(ERROR) << "Cannot read the global sequence "
3323 << boost::lexical_cast<std::string>(sequence_) << ", resetting it";
3324 oldValue = 0;
3325 }
3326
3327 newValue_ = oldValue + 1;
3328 }
3329 else
3330 {
3331 // Initialize the sequence at "1"
3332 newValue_ = 1;
3333 }
3334
3335 transaction.SetGlobalProperty(sequence_, boost::lexical_cast<std::string>(newValue_));
3336 }
3337 };
3338
3339 Operations operations(sequence);
3340 Apply(operations);
3341 return operations.GetNewValue();
3342 }
3343
3344
3345 void ServerIndex::DeleteChanges()
3346 {
3347 class Operations : public IReadWriteOperations
3348 {
3349 public:
3350 virtual void Apply(ReadWriteTransaction& transaction,
3351 Listener& listener) ORTHANC_OVERRIDE
3352 {
3353 transaction.ClearChanges();
3354 }
3355 };
3356
3357 Operations operations;
3358 Apply(operations);
3359 }
3360
3361
3362 void ServerIndex::DeleteExportedResources()
3363 {
3364 class Operations : public IReadWriteOperations
3365 {
3366 public:
3367 virtual void Apply(ReadWriteTransaction& transaction,
3368 Listener& listener) ORTHANC_OVERRIDE
3369 {
3370 transaction.ClearExportedResources();
3371 }
3372 };
3373
3374 Operations operations;
3375 Apply(operations);
3376 }
3377
3378
3379 void ServerIndex::SetGlobalProperty(GlobalProperty property,
3380 const std::string& value)
3381 {
3382 class Operations : public IReadWriteOperations
3383 {
3384 private:
3385 GlobalProperty property_;
3386 const std::string& value_;
3387
3388 public:
3389 Operations(GlobalProperty property,
3390 const std::string& value) :
3391 property_(property),
3392 value_(value)
3393 {
3394 }
3395
3396 virtual void Apply(ReadWriteTransaction& transaction,
3397 Listener& listener) ORTHANC_OVERRIDE
3398 {
3399 transaction.SetGlobalProperty(property_, value_);
3400 }
3401 };
3402
3403 Operations operations(property, value);
3404 Apply(operations);
3405 }
3297 } 3406 }