| 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 | /** Delivers result for a finished job.
 | 
|---|
| 142 |  *
 | 
|---|
| 143 |  * \note we throw assertion if not present
 | 
|---|
| 144 |  *
 | 
|---|
| 145 |  * \param jobid id of job
 | 
|---|
| 146 |  * \return result for job of given \a jobid
 | 
|---|
| 147 |  */
 | 
|---|
| 148 | FragmentResult::ptr FragmentQueue::getResult(JobId_t jobid)
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   ResultMap::iterator iter = results.find(jobid);
 | 
|---|
| 151 |   ASSERT(iter != results.end(),
 | 
|---|
| 152 |       "FragmentQueue::pushResult() - job "+toString(jobid)+" is not known to us.");
 | 
|---|
| 153 |   ASSERT(*iter->second != *NoResult,
 | 
|---|
| 154 |       "FragmentQueue::pushResult() - job "+toString(jobid)+" has not been request for calculation yet.");
 | 
|---|
| 155 |   ASSERT(*iter->second != *NoResultQueued,
 | 
|---|
| 156 |       "FragmentQueue::pushResult() - job "+toString(jobid)+"'s calculation is underway but not result has arrived yet.");
 | 
|---|
| 157 |   ASSERT(*iter->second != *ResultDelivered,
 | 
|---|
| 158 |       "FragmentQueue::pushResult() - job "+toString(jobid)+"'s result has already been delivered.");
 | 
|---|
| 159 |   /// store result
 | 
|---|
| 160 |   FragmentResult::ptr _result = iter->second;
 | 
|---|
| 161 |   /// mark as delivered in map
 | 
|---|
| 162 |   iter->second = ResultDelivered;
 | 
|---|
| 163 |   /// and return result
 | 
|---|
| 164 |   return _result;
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | std::vector<FragmentResult::ptr> FragmentQueue::getAllResults()
 | 
|---|
| 168 | {
 | 
|---|
| 169 |   std::vector<FragmentResult::ptr> returnresults;
 | 
|---|
| 170 |   for (ResultMap::iterator iter = results.begin();
 | 
|---|
| 171 |       iter != results.end(); ++iter) {
 | 
|---|
| 172 |     if (isPresentResult(iter->second)) {
 | 
|---|
| 173 |       returnresults.push_back(getResult(iter->first));
 | 
|---|
| 174 |       iter = results.begin();
 | 
|---|
| 175 |     }
 | 
|---|
| 176 |   }
 | 
|---|
| 177 | 
 | 
|---|
| 178 |   return returnresults;
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | /** Pushes a result for a finished job.
 | 
|---|
| 182 |  *
 | 
|---|
| 183 |  * \note we throw assertion if job already has result or is not known.
 | 
|---|
| 184 |  *
 | 
|---|
| 185 |  * \param result result of job to store
 | 
|---|
| 186 |  */
 | 
|---|
| 187 | void FragmentQueue::pushResult(FragmentResult::ptr &_result)
 | 
|---|
| 188 | {
 | 
|---|
| 189 |   /// check for presence
 | 
|---|
| 190 |   ResultMap::iterator iter = results.find(_result->getId());
 | 
|---|
| 191 |   ASSERT(iter != results.end(),
 | 
|---|
| 192 |       "FragmentQueue::pushResult() - job "+toString(_result->getId())+" is not known to us.");
 | 
|---|
| 193 |   ASSERT(*iter->second == *NoResultQueued,
 | 
|---|
| 194 |       "FragmentQueue::pushResult() - is not waiting for the result of job "+toString(_result->getId())+".");
 | 
|---|
| 195 |   /// and overwrite NoResult in found entry
 | 
|---|
| 196 |   iter->second = _result;
 | 
|---|
| 197 | }
 | 
|---|
| 198 | 
 | 
|---|