comparison OrthancServer/ServerIndex.cpp @ 201:bee20e978835

refactoring of delete
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Nov 2012 17:36:19 +0100
parents 9c58b2b03cf0
children 1650557bd81a
comparison
equal deleted inserted replaced
200:9c58b2b03cf0 201:bee20e978835
51 51
52 namespace Orthanc 52 namespace Orthanc
53 { 53 {
54 namespace Internals 54 namespace Internals
55 { 55 {
56 class ServerIndexListenerTodo : public IServerIndexListener
57 {
58 private:
59 FileStorage& fileStorage_;
60 bool hasRemainingLevel_;
61 ResourceType remainingType_;
62 std::string remainingPublicId_;
63
64 public:
65 ServerIndexListenerTodo(FileStorage& fileStorage) :
66 fileStorage_(fileStorage),
67 hasRemainingLevel_(false)
68 {
69 assert(ResourceType_Patient < ResourceType_Study &&
70 ResourceType_Study < ResourceType_Series &&
71 ResourceType_Series < ResourceType_Instance);
72 }
73
74 void Reset()
75 {
76 hasRemainingLevel_ = false;
77 }
78
79 virtual void SignalRemainingAncestor(ResourceType parentType,
80 const std::string& publicId)
81 {
82 LOG(INFO) << "Remaining ancestor \"" << publicId << "\" (" << parentType << ")";
83
84 if (hasRemainingLevel_)
85 {
86 if (parentType < remainingType_)
87 {
88 remainingType_ = parentType;
89 remainingPublicId_ = publicId;
90 }
91 }
92 else
93 {
94 hasRemainingLevel_ = true;
95 remainingType_ = parentType;
96 remainingPublicId_ = publicId;
97 }
98 }
99
100 virtual void SignalFileDeleted(const std::string& fileUuid)
101 {
102 assert(Toolbox::IsUuid(fileUuid));
103 fileStorage_.Remove(fileUuid);
104 }
105
106 bool HasRemainingLevel() const
107 {
108 return hasRemainingLevel_;
109 }
110
111 ResourceType GetRemainingType() const
112 {
113 assert(HasRemainingLevel());
114 return remainingType_;
115 }
116
117 const std::string& GetRemainingPublicId() const
118 {
119 assert(HasRemainingLevel());
120 return remainingPublicId_;
121 }
122 };
123
124
56 class DeleteFromFileStorageFunction : public SQLite::IScalarFunction 125 class DeleteFromFileStorageFunction : public SQLite::IScalarFunction
57 { 126 {
58 private: 127 private:
59 std::auto_ptr<FileStorage> fileStorage_; 128 FileStorage& fileStorage_;
60 129
61 public: 130 public:
62 DeleteFromFileStorageFunction(const std::string& path) 131 DeleteFromFileStorageFunction(FileStorage& fileStorage) :
63 { 132 fileStorage_(fileStorage)
64 if (path != ":memory:") 133 {
65 {
66 fileStorage_.reset(new FileStorage(path));
67 }
68 } 134 }
69 135
70 virtual const char* GetName() const 136 virtual const char* GetName() const
71 { 137 {
72 return "DeleteFromFileStorage"; 138 return "DeleteFromFileStorage";
77 return 1; 143 return 1;
78 } 144 }
79 145
80 virtual void Compute(SQLite::FunctionContext& context) 146 virtual void Compute(SQLite::FunctionContext& context)
81 { 147 {
82 if (fileStorage_.get() == NULL)
83 {
84 // In-memory index, for unit tests
85 return;
86 }
87
88 std::string fileUuid = context.GetStringValue(0); 148 std::string fileUuid = context.GetStringValue(0);
89 LOG(INFO) << "Removing file [" << fileUuid << "]"; 149 LOG(INFO) << "Removing file [" << fileUuid << "]";
90 150
91 if (Toolbox::IsUuid(fileUuid)) 151 if (Toolbox::IsUuid(fileUuid))
92 { 152 {
93 fileStorage_->Remove(fileUuid); 153 fileStorage_.Remove(fileUuid);
94 } 154 }
95 } 155 }
96 }; 156 };
97 157
98 158
348 const std::string& uuid, 408 const std::string& uuid,
349 const std::string& tableName) 409 const std::string& tableName)
350 { 410 {
351 boost::mutex::scoped_lock scoped_lock(mutex_); 411 boost::mutex::scoped_lock scoped_lock(mutex_);
352 412
413
414 {
415 listener2_->Reset();
416
417 std::auto_ptr<SQLite::Transaction> t(db2_->StartTransaction());
418 t->Begin();
419
420 int64_t id;
421 ResourceType type;
422 if (!db2_->LookupResource(uuid, id, type))
423 {
424 return false;
425 }
426
427 db2_->DeleteResource(id);
428
429 if (listener2_->HasRemainingLevel())
430 {
431 ResourceType type = listener2_->GetRemainingType();
432 const std::string& uuid = listener2_->GetRemainingPublicId();
433
434 target["RemainingAncestor"] = Json::Value(Json::objectValue);
435 target["RemainingAncestor"]["Path"] = std::string(GetBasePath(type)) + "/" + uuid;
436 target["RemainingAncestor"]["Type"] = ToString(type);
437 target["RemainingAncestor"]["ID"] = uuid;
438 }
439 else
440 {
441 target["RemainingAncestor"] = Json::nullValue;
442 }
443
444 std::cout << target << std::endl;
445
446 t->Commit();
447
448 return true;
449 }
450
451
452
353 deletedLevels_->Clear(); 453 deletedLevels_->Clear();
354 454
355 SQLite::Statement s(db_, "DELETE FROM " + tableName + " WHERE uuid=?"); 455 SQLite::Statement s(db_, "DELETE FROM " + tableName + " WHERE uuid=?");
356 s.BindString(0, uuid); 456 s.BindString(0, uuid);
357 457
383 483
384 return true; 484 return true;
385 } 485 }
386 486
387 487
388 namespace Internals 488 ServerIndex::ServerIndex(FileStorage& fileStorage,
389 { 489 const std::string& dbPath)
390 class ServerIndexListenerTodo : public IServerIndexListener 490 {
391 { 491 listener2_.reset(new Internals::ServerIndexListenerTodo(fileStorage));
392 public: 492
393 virtual void SignalRemainingAncestor(ResourceType parentType, 493 if (dbPath == ":memory:")
394 const std::string& publicId)
395 {
396 LOG(INFO) << "Remaning ancestor \"" << publicId << "\" (" << parentType << ")";
397 }
398
399 virtual void SignalFileDeleted(const std::string& fileUuid)
400 {
401 LOG(INFO) << "Deleted file " << fileUuid;
402 }
403
404 };
405 }
406
407
408 ServerIndex::ServerIndex(const std::string& storagePath)
409 {
410 listener2_.reset(new Internals::ServerIndexListenerTodo);
411
412 if (storagePath == ":memory:")
413 { 494 {
414 db_.OpenInMemory(); 495 db_.OpenInMemory();
415 db2_.reset(new DatabaseWrapper(*listener2_)); 496 db2_.reset(new DatabaseWrapper(*listener2_));
416 } 497 }
417 else 498 else
418 { 499 {
419 boost::filesystem::path p = storagePath; 500 boost::filesystem::path p = dbPath;
420 501
421 try 502 try
422 { 503 {
423 boost::filesystem::create_directories(storagePath); 504 boost::filesystem::create_directories(p);
424 } 505 }
425 catch (boost::filesystem::filesystem_error) 506 catch (boost::filesystem::filesystem_error)
426 { 507 {
427 } 508 }
428 509
430 511
431 p /= "index"; 512 p /= "index";
432 db_.Open(p.string()); 513 db_.Open(p.string());
433 } 514 }
434 515
435 db_.Register(new Internals::DeleteFromFileStorageFunction(storagePath)); 516 db_.Register(new Internals::DeleteFromFileStorageFunction(fileStorage));
436 deletedLevels_ = (Internals::SignalDeletedLevelFunction*) 517 deletedLevels_ = (Internals::SignalDeletedLevelFunction*)
437 db_.Register(new Internals::SignalDeletedLevelFunction); 518 db_.Register(new Internals::SignalDeletedLevelFunction);
438 519
439 if (!db_.DoesTableExist("GlobalProperties")) 520 if (!db_.DoesTableExist("GlobalProperties"))
440 { 521 {