GroupThreadManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file GroupThreadManager.cpp
22  */
23 
24 // Terralib
25 #include "../core/logger/Logger.h"
26 #include "../common/progress/TaskProgress.h"
27 
28 #include "../dataaccess/dataset/DataSet.h"
29 #include "../dataaccess/dataset/DataSetType.h"
30 
31 #include "../memory/DataSet.h"
32 #include "../memory/DataSetItem.h"
33 
34 #include "../dataaccess/utils/Utils.h"
35 
36 #include "../geometry/GeometryProperty.h"
37 
38 #include "GroupThreadManager.h"
39 
40 //Boost
41 #include <boost/thread.hpp>
42 
43 namespace te
44 {
45  namespace vp
46  {
47  GroupThreadManager::GroupThreadManager( const std::map<std::string, std::vector<int> >& groups
48  , te::da::DataSet* dataSet
49  , te::da::DataSetType* dataSetType
50  , te::mem::DataSet* outputDataSet
51  , te::da::DataSetType* outputDataSetType
52  , te::da::DataSource* outputDataSource
53  , std::map<std::string, te::dt::AbstractData*> specificParams)
54  : m_groups(groups)
55  , m_savedCount(0)
56  , m_dataSet(dataSet)
57  , m_dataSetType(dataSetType)
58  , m_outputDataSet(outputDataSet)
59  , m_outputDataSetType(outputDataSetType)
60  , m_outputDataSource(outputDataSource)
61  , m_specificParams(specificParams)
62  {
63  m_groupsIterator = m_groups.begin();
64  }
65 
66 
67  bool GroupThreadManager::getNextGroup(std::vector< te::mem::DataSetItem*>& nextGroup)
68  {
69  boost::lock_guard<boost::mutex> lock(m_mtx);
70 
71  if (m_groupsIterator == m_groups.end())
72  {
73  return false;
74  }
75 
76  nextGroup.clear();
77 
78  for (std::size_t i = 0; i < m_groupsIterator->second.size(); ++i)
79  {
80  m_dataSet->move(m_groupsIterator->second[i]);
82 
83  for (std::size_t j = 0; j < m_dataSetType->size(); ++j)
84  {
85  if (!m_dataSet->isNull(j))
86  item->setValue(j, m_dataSet->getValue(j).release());
87  }
88 
89  nextGroup.push_back(item);
90  }
91 
93 
94  return true;
95  }
96 
97  bool GroupThreadManager::getNextOutput(std::vector< te::mem::DataSetItem*>& nextOutput)
98  {
99  boost::lock_guard<boost::mutex> lock(m_mtxOutput);
100 
101  if (m_outputQueue.empty())
102  {
103  if (m_savedCount == m_groups.size())
104  {
105  return false;
106  }
107 
108  return true;
109  }
110 
111  nextOutput = *m_outputQueue.begin();
112 
113  m_outputQueue.erase(m_outputQueue.begin());
114 
115  ++m_savedCount;
116 
117  return true;
118  }
119 
121  {
122  return m_dataSetType;
123  }
124 
126  {
127  return m_outputDataSource;
128  }
129 
131  {
132  return m_outputDataSetType;
133  }
134 
135  std::map<std::string, te::dt::AbstractData*> GroupThreadManager::getSpecificParameters()
136  {
137  return m_specificParams;
138  }
139 
141  {
143 
144  return m_outputDataSet;
145  }
146 
147  std::vector<std::string> GroupThreadManager::getWarnings()
148  {
149  return m_warnings;
150  }
151 
153  {
155 
156  return item;
157  }
158 
159  void GroupThreadManager::addOutput(std::vector<te::mem::DataSetItem*>& itemGroup)
160  {
161  boost::lock_guard<boost::mutex> lock(m_mtxOutput);
162 
163  m_outputQueue.push_back(itemGroup);
164  }
165 
166  void GroupThreadManager::addWarning(const std::string& warning, const bool& appendIfExists)
167  {
168  boost::lock_guard<boost::mutex> lock(m_mtxWarning);
169 
170  if (!appendIfExists)
171  {
172  std::vector<std::string>::iterator it = std::find(m_warnings.begin(), m_warnings.end(), warning);
173  if (it == m_warnings.end())
174  m_warnings.push_back(warning);
175  }
176  else
177  {
178  m_warnings.push_back(warning);
179  }
180 
181 #ifdef TERRALIB_LOGGER_ENABLED
182  TE_CORE_LOG_DEBUG("vp", warning);
183 #endif // TERRALIB_LOGGER_ENABLED
184  }
185 
187  {
188  }
189 
191  {
192  return *this;
193  }
194  }// end namespace vp
195 }// end namespace te
te::da::DataSetType * getDataSetType()
void addOutput(std::vector< te::mem::DataSetItem * > &itemGroup)
te::mem::DataSetItem * createOutputItem()
A class that models the description of a dataset.
Definition: DataSetType.h:72
te::mem::DataSet * m_outputDataSet
#define TE_CORE_LOG_DEBUG(channel, message)
Use this tag in order to log a message to a specified logger with the DEBUG level.
Definition: Logger.h:225
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
An abstract class for data providers like a DBMS, Web Services or a regular file. ...
bool getNextOutput(std::vector< te::mem::DataSetItem * > &nextOutput)
Class to manager group values in threads.
virtual bool move(std::size_t i)=0
It moves the dataset internal pointer to a given position.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
te::da::DataSetType * m_outputDataSetType
URI C++ Library.
Definition: Attributes.h:37
std::vector< std::string > m_warnings
std::map< std::string, std::vector< int > > m_groups
GroupThreadManager(const std::map< std::string, std::vector< int > > &groups, te::da::DataSet *dataSet, te::da::DataSetType *dataSetType, te::mem::DataSet *outputDataSet, te::da::DataSetType *outputDataSetType, te::da::DataSource *outputDataSource, std::map< std::string, te::dt::AbstractData * > specificParams)
std::size_t size() const
It returns the number of properties of the CompositeProperty.
te::da::DataSource * m_outputDataSource
std::vector< std::string > getWarnings()
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
te::da::DataSetType * getOutputDataSetType()
virtual std::unique_ptr< te::dt::AbstractData > getValue(std::size_t i) const
Method for retrieving any other type of data value stored in the data source.
bool getNextGroup(std::vector< te::mem::DataSetItem * > &nextGroup)
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
void clear()
It clears all the dataset items.
te::mem::DataSet * getClearOutputDataSet()
void addWarning(const std::string &warning, const bool &appendIfExists=false)
te::da::DataSetType * m_dataSetType
std::vector< std::vector< te::mem::DataSetItem * > > m_outputQueue
te::da::DataSource * getOutputDataSource()
std::map< std::string, std::vector< int > >::iterator m_groupsIterator
const GroupThreadManager & operator=(const GroupThreadManager &)
std::map< std::string, te::dt::AbstractData * > getSpecificParameters()
std::map< std::string, te::dt::AbstractData * > m_specificParams