comparison OrthancServer/ServerIndex.cpp @ 1432:0ac74fa21db8

rename IServerIndexListener as IDatabaseListener
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 30 Jun 2015 12:51:29 +0200
parents b22ba8c5edbe
children c0bdc47165ef
comparison
equal deleted inserted replaced
1431:3e53edcd0120 1432:0ac74fa21db8
52 52
53 static const uint64_t MEGA_BYTES = 1024 * 1024; 53 static const uint64_t MEGA_BYTES = 1024 * 1024;
54 54
55 namespace Orthanc 55 namespace Orthanc
56 { 56 {
57 namespace Internals 57 class ServerIndex::Listener : public IDatabaseListener
58 { 58 {
59 class ServerIndexListener : public IServerIndexListener 59 private:
60 struct FileToRemove
60 { 61 {
61 private: 62 private:
62 struct FileToRemove 63 std::string uuid_;
63 { 64 FileContentType type_;
64 private: 65
65 std::string uuid_; 66 public:
66 FileContentType type_; 67 FileToRemove(const FileInfo& info) : uuid_(info.GetUuid()),
67 68 type_(info.GetContentType())
68 public: 69 {
69 FileToRemove(const FileInfo& info) : uuid_(info.GetUuid()), 70 }
70 type_(info.GetContentType()) 71
72 const std::string& GetUuid() const
73 {
74 return uuid_;
75 }
76
77 FileContentType GetContentType() const
78 {
79 return type_;
80 }
81 };
82
83 ServerContext& context_;
84 bool hasRemainingLevel_;
85 ResourceType remainingType_;
86 std::string remainingPublicId_;
87 std::list<FileToRemove> pendingFilesToRemove_;
88 std::list<ServerIndexChange> pendingChanges_;
89 uint64_t sizeOfFilesToRemove_;
90 bool insideTransaction_;
91
92 void Reset()
93 {
94 sizeOfFilesToRemove_ = 0;
95 hasRemainingLevel_ = false;
96 pendingFilesToRemove_.clear();
97 pendingChanges_.clear();
98 }
99
100 public:
101 Listener(ServerContext& context) : context_(context),
102 insideTransaction_(false)
103 {
104 Reset();
105 assert(ResourceType_Patient < ResourceType_Study &&
106 ResourceType_Study < ResourceType_Series &&
107 ResourceType_Series < ResourceType_Instance);
108 }
109
110 void StartTransaction()
111 {
112 Reset();
113 insideTransaction_ = true;
114 }
115
116 void EndTransaction()
117 {
118 insideTransaction_ = false;
119 }
120
121 uint64_t GetSizeOfFilesToRemove()
122 {
123 return sizeOfFilesToRemove_;
124 }
125
126 void CommitFilesToRemove()
127 {
128 for (std::list<FileToRemove>::const_iterator
129 it = pendingFilesToRemove_.begin();
130 it != pendingFilesToRemove_.end(); ++it)
131 {
132 context_.RemoveFile(it->GetUuid(), it->GetContentType());
133 }
134 }
135
136 void CommitChanges()
137 {
138 for (std::list<ServerIndexChange>::const_iterator
139 it = pendingChanges_.begin();
140 it != pendingChanges_.end(); ++it)
141 {
142 context_.SignalChange(*it);
143 }
144 }
145
146 virtual void SignalRemainingAncestor(ResourceType parentType,
147 const std::string& publicId)
148 {
149 VLOG(1) << "Remaining ancestor \"" << publicId << "\" (" << parentType << ")";
150
151 if (hasRemainingLevel_)
152 {
153 if (parentType < remainingType_)
71 { 154 {
72 }
73
74 const std::string& GetUuid() const
75 {
76 return uuid_;
77 }
78
79 FileContentType GetContentType() const
80 {
81 return type_;
82 }
83 };
84
85 ServerContext& context_;
86 bool hasRemainingLevel_;
87 ResourceType remainingType_;
88 std::string remainingPublicId_;
89 std::list<FileToRemove> pendingFilesToRemove_;
90 std::list<ServerIndexChange> pendingChanges_;
91 uint64_t sizeOfFilesToRemove_;
92 bool insideTransaction_;
93
94 void Reset()
95 {
96 sizeOfFilesToRemove_ = 0;
97 hasRemainingLevel_ = false;
98 pendingFilesToRemove_.clear();
99 pendingChanges_.clear();
100 }
101
102 public:
103 ServerIndexListener(ServerContext& context) : context_(context),
104 insideTransaction_(false)
105 {
106 Reset();
107 assert(ResourceType_Patient < ResourceType_Study &&
108 ResourceType_Study < ResourceType_Series &&
109 ResourceType_Series < ResourceType_Instance);
110 }
111
112 void StartTransaction()
113 {
114 Reset();
115 insideTransaction_ = true;
116 }
117
118 void EndTransaction()
119 {
120 insideTransaction_ = false;
121 }
122
123 uint64_t GetSizeOfFilesToRemove()
124 {
125 return sizeOfFilesToRemove_;
126 }
127
128 void CommitFilesToRemove()
129 {
130 for (std::list<FileToRemove>::const_iterator
131 it = pendingFilesToRemove_.begin();
132 it != pendingFilesToRemove_.end(); ++it)
133 {
134 context_.RemoveFile(it->GetUuid(), it->GetContentType());
135 }
136 }
137
138 void CommitChanges()
139 {
140 for (std::list<ServerIndexChange>::const_iterator
141 it = pendingChanges_.begin();
142 it != pendingChanges_.end(); ++it)
143 {
144 context_.SignalChange(*it);
145 }
146 }
147
148 virtual void SignalRemainingAncestor(ResourceType parentType,
149 const std::string& publicId)
150 {
151 VLOG(1) << "Remaining ancestor \"" << publicId << "\" (" << parentType << ")";
152
153 if (hasRemainingLevel_)
154 {
155 if (parentType < remainingType_)
156 {
157 remainingType_ = parentType;
158 remainingPublicId_ = publicId;
159 }
160 }
161 else
162 {
163 hasRemainingLevel_ = true;
164 remainingType_ = parentType; 155 remainingType_ = parentType;
165 remainingPublicId_ = publicId; 156 remainingPublicId_ = publicId;
166 }
167 }
168
169 virtual void SignalFileDeleted(const FileInfo& info)
170 {
171 assert(Toolbox::IsUuid(info.GetUuid()));
172 pendingFilesToRemove_.push_back(FileToRemove(info));
173 sizeOfFilesToRemove_ += info.GetCompressedSize();
174 }
175
176 virtual void SignalChange(const ServerIndexChange& change)
177 {
178 VLOG(1) << "Change related to resource " << change.GetPublicId() << " of type "
179 << EnumerationToString(change.GetResourceType()) << ": "
180 << EnumerationToString(change.GetChangeType());
181
182 if (insideTransaction_)
183 {
184 pendingChanges_.push_back(change);
185 } 157 }
186 else 158 }
187 { 159 else
188 context_.SignalChange(change); 160 {
189 } 161 hasRemainingLevel_ = true;
190 } 162 remainingType_ = parentType;
191 163 remainingPublicId_ = publicId;
192 bool HasRemainingLevel() const 164 }
193 { 165 }
194 return hasRemainingLevel_; 166
195 } 167 virtual void SignalFileDeleted(const FileInfo& info)
196 168 {
197 ResourceType GetRemainingType() const 169 assert(Toolbox::IsUuid(info.GetUuid()));
198 { 170 pendingFilesToRemove_.push_back(FileToRemove(info));
199 assert(HasRemainingLevel()); 171 sizeOfFilesToRemove_ += info.GetCompressedSize();
200 return remainingType_; 172 }
201 } 173
202 174 virtual void SignalChange(const ServerIndexChange& change)
203 const std::string& GetRemainingPublicId() const 175 {
204 { 176 VLOG(1) << "Change related to resource " << change.GetPublicId() << " of type "
205 assert(HasRemainingLevel()); 177 << EnumerationToString(change.GetResourceType()) << ": "
206 return remainingPublicId_; 178 << EnumerationToString(change.GetChangeType());
207 } 179
208 }; 180 if (insideTransaction_)
209 } 181 {
182 pendingChanges_.push_back(change);
183 }
184 else
185 {
186 context_.SignalChange(change);
187 }
188 }
189
190 bool HasRemainingLevel() const
191 {
192 return hasRemainingLevel_;
193 }
194
195 ResourceType GetRemainingType() const
196 {
197 assert(HasRemainingLevel());
198 return remainingType_;
199 }
200
201 const std::string& GetRemainingPublicId() const
202 {
203 assert(HasRemainingLevel());
204 return remainingPublicId_;
205 }
206 };
210 207
211 208
212 class ServerIndex::Transaction 209 class ServerIndex::Transaction
213 { 210 {
214 private: 211 private:
547 done_(false), 544 done_(false),
548 db_(db), 545 db_(db),
549 maximumStorageSize_(0), 546 maximumStorageSize_(0),
550 maximumPatients_(0) 547 maximumPatients_(0)
551 { 548 {
552 listener_.reset(new Internals::ServerIndexListener(context)); 549 listener_.reset(new Listener(context));
553 db_.SetListener(*listener_); 550 db_.SetListener(*listener_);
554 551
555 currentStorageSize_ = db_.GetTotalCompressedSize(); 552 currentStorageSize_ = db_.GetTotalCompressedSize();
556 553
557 // Initial recycling if the parameters have changed since the last 554 // Initial recycling if the parameters have changed since the last