src/terralib/stmemory/DataSource.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 terralib/stmemory/DataSource.cpp
22 
23  \brief Implements a DataSource that contains st memory DataSets indexed by space and time.
24 */
25 
26 // TerraLib
27 #include "../common/STLUtils.h"
28 #include "../common/StringUtils.h"
29 #include "../core/translator/Translator.h"
30 #include "../core/uri/URI.h"
31 #include "../core/uri/Utils.h"
32 #include "../dataaccess/dataset/DataSet.h"
33 #include "../dataaccess/dataset/DataSetType.h"
34 #include "../dataaccess/dataset/CheckConstraint.h"
35 #include "../dataaccess/dataset/ForeignKey.h"
36 #include "../dataaccess/dataset/Index.h"
37 #include "../dataaccess/dataset/PrimaryKey.h"
38 #include "../dataaccess/dataset/Sequence.h"
39 #include "../dataaccess/dataset/UniqueKey.h"
40 #include "../datatype/DateTimePeriod.h"
41 
42 #include "DataSet.h"
43 #include "DataSource.h"
44 #include "Transactor.h"
45 #include "Exception.h"
46 #include "Globals.h"
47 
48 // Boost
49 #include <boost/format.hpp>
50 #include <boost/lexical_cast.hpp>
51 
54 
55 te::stmem::DataSource::DataSource(const std::string& connInfo)
56  : te::da::DataSource(connInfo),
57 
58  m_maxdatasets(TE_STMEMORY_DRIVER_MAX_DATASETS),
59  m_isOpened(false),
60  m_deepCopy(false)
61 
62 {
63 }
64 
66  : te::da::DataSource(uri),
67 
69  m_isOpened(false),
70  m_deepCopy(false)
71 
72 {
73 }
74 
75 
77 {
78  //delete the pointers
79  std::map<std::string, DataSet* >::iterator it = m_datasets.begin();
80  while(it!=m_datasets.end())
81  {
82  delete(it->second);
83  ++it;
84  }
85  m_datasets.clear();
86 
87  //delete the pointers
88  std::map<std::string, te::da::DataSetType* >::iterator it2 = m_schemas.begin();
89  while(it2!=m_schemas.end())
90  {
91  delete(it2->second);
92  ++it2;
93  }
94  m_schemas.clear();
95 }
96 
97 std::string te::stmem::DataSource::getType() const
98 {
100 }
101 
102 std::unique_ptr<te::da::DataSourceTransactor> te::stmem::DataSource::getTransactor()
103 {
104  return std::unique_ptr<te::da::DataSourceTransactor>(new Transactor(this));
105 }
106 
108 {
109 // assure we are in a closed state
110  close();
111 
112  m_transactor.reset(new Transactor(this));
113 
114  std::map<std::string, std::string> kvp = te::core::Expand(m_uri.query());
115  std::map<std::string, std::string>::const_iterator it = kvp.begin();
116  std::map<std::string, std::string>::const_iterator itend = kvp.end();
117 
118 // check if it is required a different dataset limit
119  it = kvp.find("MAX_DATASETS");
120  if(it != itend && !it->second.empty())
121  m_maxdatasets = boost::lexical_cast<std::size_t>(it->second);
122 
123 // check operation mode
124  it = kvp.find("OPERATION_MODE");
125  if((it != itend && !it->second.empty()) && (te::common::Convert2UCase(it->second) == "NON-SHARED"))
126  m_deepCopy = true;
127 
128  m_isOpened = true;
129 }
130 
132 {
133  if(!m_isOpened)
134  return;
135 
136  //delete the pointers
137  std::map<std::string, DataSet* >::iterator it = m_datasets.begin();
138  while(it!=m_datasets.end())
139  {
140  delete(it->second);
141  ++it;
142  }
143  m_datasets.clear();
144 
145  //delete the pointers
146  std::map<std::string, te::da::DataSetType* >::iterator it2 = m_schemas.begin();
147  while(it2!=m_schemas.end())
148  {
149  delete(it2->second);
150  ++it2;
151  }
152  m_schemas.clear();
153 
155 
156  m_isOpened = false;
157 
158  m_deepCopy = false;
159 
160  m_transactor.reset(0);
161 }
162 
164 {
165  return m_isOpened;
166 }
167 
169 {
170  return m_isOpened;
171 }
172 
174 {
175  return sm_capabilities;
176 }
177 
179 {
180  return &sm_dialect;
181 }
182 
183 void te::stmem::DataSource::add(const std::string& name, te::da::DataSetType* t, DataSet* d)
184 {
185  m_transactor->add(name, t, d);
186 }
187 
188 std::unique_ptr<te::da::DataSet> te::stmem::DataSource::getDataSet(const std::string& name, const te::dt::DateTime* dt, te::dt::TemporalRelation r,
189  te::common::TraverseType travType, bool connected,
190  const te::common::AccessPolicy accessPolicy)
191 {
192  return m_transactor->getDataSet(name, dt, r, travType, connected, accessPolicy);
193 }
194 
195 std::unique_ptr<te::da::DataSet> te::stmem::DataSource::getDataSet(const std::string& name,
198  te::common::TraverseType travType, bool connected,
199  const te::common::AccessPolicy accessPolicy)
200 {
201  return m_transactor->getDataSet(name, geom, sr, dt, tr, travType, connected, accessPolicy);
202 }
203 
204 std::unique_ptr<te::da::DataSet> te::stmem::DataSource::getDataSet(const std::string& name,
207  te::common::TraverseType travType, bool connected,
208  const te::common::AccessPolicy accessPolicy)
209 {
210  return m_transactor->getDataSet(name, e, sr, dt, tr, travType, connected, accessPolicy);
211 }
212 
213 std::unique_ptr<te::dt::DateTimePeriod>
215 {
216  return m_transactor->getTemporalExtent(name);
217 }
218 
219 ///protected Methods
220 void te::stmem::DataSource::create(const std::string& /*connInfo*/ /*dsInfo*/)
221 {
222  return;
223 }
224 
225 void te::stmem::DataSource::drop(const std::string& /*connInfo*/ /*dsInfo*/)
226 {
227  return;
228 }
229 
230 bool te::stmem::DataSource::exists(const std::string& /*connInfo*/ /*dsInfo*/)
231 {
232  return false;
233 }
234 
236  const std::string& /*connInfo*/ /*dsInfo*/)
237 {
238  return std::vector<std::string>();
239 }
void create(const std::string &connInfo)
protected Methods
#define TE_STMEMORY_DRIVER_MAX_DATASETS
The maximum number of datasets to be handled by a data source.
static const std::string sm_driverIdentifier
The STMEMORY driver identifier.
A static class with global definitions for the TerraLib ST In-memory driver.
bool m_deepCopy
If true each dataset is cloned in the getDataSet method.
A class that models the description of a dataset.
Definition: DataSetType.h:72
Implements a DataSource that contains In-Memory DataSets indexed by space and time.
TemporalRelation
Temporal relations between date and time (Source: Allen, 1991).
It represents the SQL query dialect accepted by a given data source.
Definition: SQLDialect.h:55
void add(const std::string &name, te::da::DataSetType *t, DataSet *d)
It adds a new DataSet and DataSetType into the DataSource.
std::string Convert2UCase(const std::string &value)
It converts a string to upper case.
Definition: StringUtils.h:168
SpatialRelation
Spatial relations between geometric objects.
A class that represents the known capabilities of a specific data source, i.e. this class informs all...
bool isOpened() const
It returns true if the data source is opened, otherwise it returns false.
std::map< std::string, te::da::DataSetType * > m_schemas
The set of dataset schemas.
std::vector< std::string > getDataSourceNames(const std::string &connInfo)
It gets the data source names available in a driver.
bool isValid() const
It checks if the data source is valid (available for using).
std::string getType() const
It returns the data source type name (in UPPER CASE). Ex: POSTGIS, SQLITE, WFS, WMS, or MYSQL.
void open()
It opens the data source and makes it ready for using.
bool m_isOpened
A flag to control the state of the data source.
std::string query() const
Retrieving the query.
Definition: URI.cpp:123
std::unique_ptr< Transactor > m_transactor
A transactor.
std::unique_ptr< te::dt::DateTimePeriod > getTemporalExtent(const std::string &name)
It returns the temporal extent associated to a DataSet.
AccessPolicy
Supported data access policies (can be used as bitfield).
TraverseType
A dataset can be traversed in two ways:
bool exists(const std::string &connInfo)
Check the existence of a data source in a driver.
static te::da::DataSourceCapabilities sm_capabilities
The Memory data source capabilities.
An Envelope defines a 2D rectangular region.
URI C++ Library.
Definition: Attributes.h:37
const te::da::SQLDialect * getDialect() const
It returns the data source SQL dialect, if there is one.
static te::dt::DateTime d(2010, 8, 9, 15, 58, 39)
static te::dt::TimeDuration dt(20, 30, 50, 11)
std::map< std::string, DataSet * > m_datasets
The set of datasets stored in memory.
An implementation of DataSourceTransactor class for the ST In-memory driver.
std::unique_ptr< te::da::DataSourceTransactor > getTransactor()
It returns the set of parameters used to set up the access channel to the underlying repository...
void drop(const std::string &connInfo)
It removes the data source with the connection information from a driver.
DataSource(const std::string &connInfo)
Constructor.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
A class for representing an Uniform Resource Identifier (URI).
Definition: URI.h:49
Implementation of a in-memory data set that contains spatiotemporal observations indexed by time and ...
std::unique_ptr< te::da::DataSet > getDataSet(const std::string &name, const te::dt::DateTime *dt, te::dt::TemporalRelation r=te::dt::DURING, te::common::TraverseType travType=te::common::FORWARDONLY, bool connected=false, const te::common::AccessPolicy accessPolicy=te::common::RAccess)
It returns a data set with observations whose phenomenon times satisfy a given temporal relation...
An exception class for the TerraLib ST memory driver.
te::core::URI m_uri
The URI used to describe the datasource connection;.
TECOREEXPORT std::map< std::string, std::string > Expand(const std::string &query_str)
Split a query string into its components.
const te::da::DataSourceCapabilities & getCapabilities() const
It returns the known capabilities of the data source.
Implements a DataSource that contains st memory DataSets indexed by space and time.
static const te::da::SQLDialect sm_dialect
A dummy dialect.
A dataset is the unit of information manipulated by the data access module of TerraLib.
std::size_t m_maxdatasets
The maximum number of datasets to be handled by the data source.
void close()
It closes the data source and clears all the resources used by its internal communication channel...