1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2011 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * FragmentQueue.cpp
|
---|
10 | *
|
---|
11 | * Created on: Oct 19, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "FragmentQueue.hpp"
|
---|
23 |
|
---|
24 | #include "CodePatterns/Assert.hpp"
|
---|
25 |
|
---|
26 | FragmentResult::ptr FragmentQueue::NoResult( new FragmentResult(-1) );
|
---|
27 | FragmentResult::ptr FragmentQueue::NoResultQueued( new FragmentResult(-2) );
|
---|
28 | FragmentResult::ptr FragmentQueue::ResultDelivered( new FragmentResult(-3) );
|
---|
29 |
|
---|
30 | /** Constructor for class FragmentQueue.
|
---|
31 | *
|
---|
32 | */
|
---|
33 | FragmentQueue::FragmentQueue()
|
---|
34 | {}
|
---|
35 |
|
---|
36 | /** Destructor for class FragmentQueue.
|
---|
37 | *
|
---|
38 | */
|
---|
39 | FragmentQueue::~FragmentQueue()
|
---|
40 | {
|
---|
41 | jobs.clear();
|
---|
42 | results.clear();
|
---|
43 | }
|
---|
44 |
|
---|
45 | /** Checks whether there are jobs in the queue at all.
|
---|
46 | * \return true - jobs present, false - queue is empty
|
---|
47 | */
|
---|
48 | bool FragmentQueue::isJobPresent() const
|
---|
49 | {
|
---|
50 | return !jobs.empty();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /** Pushes a FragmentJob into the internal queue for delivery to server.
|
---|
54 | *
|
---|
55 | * \note we throw assertion when jobid has already been used.
|
---|
56 | *
|
---|
57 | * \param job job to enter into queue
|
---|
58 | */
|
---|
59 | void FragmentQueue::pushJob(FragmentJob::ptr job)
|
---|
60 | {
|
---|
61 | ASSERT(job->getId() != JobId::IllegalJob,
|
---|
62 | "FragmentQueue::pushJob() - job to push has IllegalJob id.");
|
---|
63 | ASSERT(!results.count(job->getId()),
|
---|
64 | "FragmentQueue::pushJob() - job id "+toString(job->getId())+" has already been used.");
|
---|
65 | results.insert( std::make_pair(job->getId(), NoResult));
|
---|
66 | jobs.push_back(job);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Pushes a bunch of FragmentJob's into the internal queue for delivery to server.
|
---|
70 | *
|
---|
71 | * \note we throw assertion when jobid has already been used.
|
---|
72 | *
|
---|
73 | * \sa pushJob()
|
---|
74 | *
|
---|
75 | * \param _jobs jobs to enter into queue
|
---|
76 | */
|
---|
77 | void FragmentQueue::pushJobs(std::vector<FragmentJob::ptr> &_jobs)
|
---|
78 | {
|
---|
79 | for (std::vector<FragmentJob::ptr>::iterator iter = _jobs.begin();
|
---|
80 | iter != _jobs.end(); ++iter)
|
---|
81 | pushJob(*iter);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /** Pops top-most FragmentJob from internal queue.
|
---|
85 | *
|
---|
86 | * From here on, we expect a result in FragmentQueue::results.
|
---|
87 | *
|
---|
88 | * \return job topmost job from queue
|
---|
89 | */
|
---|
90 | FragmentJob::ptr FragmentQueue::popJob()
|
---|
91 | {
|
---|
92 | ASSERT(jobs.size(),
|
---|
93 | "FragmentQueue::popJob() - there are no jobs on the queue.");
|
---|
94 | FragmentJob::ptr job = jobs.front();
|
---|
95 | ResultMap::iterator iter = results.find(job->getId());
|
---|
96 | ASSERT(iter != results.end(),
|
---|
97 | "FragmentQueue::popJob() - for job "+toString(job->getId())+" no result place has been stored.");
|
---|
98 | iter->second = NoResultQueued;
|
---|
99 | jobs.pop_front();
|
---|
100 | return job;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Internal function to check whether result is not one of static entities.
|
---|
104 | *
|
---|
105 | * @param result result to check against
|
---|
106 | * @return true - result is a present, valid result, false - result is one of the statics
|
---|
107 | */
|
---|
108 | bool FragmentQueue::isPresentResult(const FragmentResult::ptr result) const
|
---|
109 | {
|
---|
110 | return (*result != *NoResult)
|
---|
111 | && (*result != *NoResultQueued)
|
---|
112 | && (*result != *ResultDelivered);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /** Queries whether a job has already been finished and the result is present.
|
---|
116 | *
|
---|
117 | * \param jobid id of job to query
|
---|
118 | * \return true - result is present, false - result is not present
|
---|
119 | */
|
---|
120 | bool FragmentQueue::isResultPresent(JobId_t jobid) const
|
---|
121 | {
|
---|
122 | ResultMap::const_iterator iter = results.find(jobid);
|
---|
123 | return ((iter != results.end())
|
---|
124 | && isPresentResult(iter->second));
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Counts the number of jobs for which we have a calculated result present.
|
---|
128 | *
|
---|
129 | * \return number of calculated results
|
---|
130 | */
|
---|
131 | size_t FragmentQueue::getDoneJobs() const
|
---|
132 | {
|
---|
133 | size_t doneJobs = 0;
|
---|
134 | for (ResultMap::const_iterator iter = results.begin();
|
---|
135 | iter != results.end(); ++iter)
|
---|
136 | if (isPresentResult(iter->second))
|
---|
137 | ++doneJobs;
|
---|
138 | return doneJobs;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /** Counts the number of jobs for which still have to be calculated.
|
---|
142 | *
|
---|
143 | * \return number of jobs to be calculated
|
---|
144 | */
|
---|
145 | size_t FragmentQueue::getPresentJobs() const
|
---|
146 | {
|
---|
147 | const size_t presentJobs = jobs.size();
|
---|
148 | return presentJobs;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Delivers result for a finished job.
|
---|
152 | *
|
---|
153 | * \note we throw assertion if not present
|
---|
154 | *
|
---|
155 | * \param jobid id of job
|
---|
156 | * \return result for job of given \a jobid
|
---|
157 | */
|
---|
158 | FragmentResult::ptr FragmentQueue::getResult(JobId_t jobid)
|
---|
159 | {
|
---|
160 | ResultMap::iterator iter = results.find(jobid);
|
---|
161 | ASSERT(iter != results.end(),
|
---|
162 | "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us.");
|
---|
163 | ASSERT(*iter->second != *NoResult,
|
---|
164 | "FragmentQueue::pushResult() - job "+toString(jobid)+" has not been request for calculation yet.");
|
---|
165 | ASSERT(*iter->second != *NoResultQueued,
|
---|
166 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s calculation is underway but not result has arrived yet.");
|
---|
167 | ASSERT(*iter->second != *ResultDelivered,
|
---|
168 | "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has already been delivered.");
|
---|
169 | /// store result
|
---|
170 | FragmentResult::ptr _result = iter->second;
|
---|
171 | /// mark as delivered in map
|
---|
172 | iter->second = ResultDelivered;
|
---|
173 | /// and return result
|
---|
174 | return _result;
|
---|
175 | }
|
---|
176 |
|
---|
177 | std::vector<FragmentResult::ptr> FragmentQueue::getAllResults()
|
---|
178 | {
|
---|
179 | std::vector<FragmentResult::ptr> returnresults;
|
---|
180 | for (ResultMap::iterator iter = results.begin();
|
---|
181 | iter != results.end(); ++iter) {
|
---|
182 | if (isPresentResult(iter->second)) {
|
---|
183 | returnresults.push_back(getResult(iter->first));
|
---|
184 | iter = results.begin();
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | return returnresults;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /** Pushes a result for a finished job.
|
---|
192 | *
|
---|
193 | * \note we throw assertion if job already has result or is not known.
|
---|
194 | *
|
---|
195 | * \param result result of job to store
|
---|
196 | */
|
---|
197 | void FragmentQueue::pushResult(FragmentResult::ptr &_result)
|
---|
198 | {
|
---|
199 | /// check for presence
|
---|
200 | ResultMap::iterator iter = results.find(_result->getId());
|
---|
201 | ASSERT(iter != results.end(),
|
---|
202 | "FragmentQueue::pushResult() - job "+toString(_result->getId())+" is not known to us.");
|
---|
203 | ASSERT(*iter->second == *NoResultQueued,
|
---|
204 | "FragmentQueue::pushResult() - is not waiting for the result of job "+toString(_result->getId())+".");
|
---|
205 | /// and overwrite NoResult in found entry
|
---|
206 | iter->second = _result;
|
---|
207 | }
|
---|
208 |
|
---|