All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GraphCache.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 GraphCache.cpp
22 
23  \brief Class used to manager the graph data elements.
24  This class uses a cache policy to control the elements in memory.
25  If a element was requested and not found in cache, the
26  GraphDataManager is used to loaded a new GraphData.
27 */
28 
29 // Terralib Includes
30 #include "../../common/STLUtils.h"
31 #include "../cache/AbstractCachePolicy.h"
32 #include "../cache/AbstractCachePolicyFactory.h"
33 #include "../loader/AbstractGraphLoaderStrategy.h"
34 #include "GraphCache.h"
35 #include "GraphData.h"
36 #include "GraphDataManager.h"
37 
38 // STL Includes
39 #include <iostream>
40 
42 {
43  m_graphDataCounter = 0; // initializate the graph data counter
44 
46 
47  if(m_policy == 0)
48  {
50  }
51 }
52 
54 {
55  clearCache();
56 
57  delete m_policy;
58 }
59 
61 {
62  //check local cache
63  std::map<int, GraphData*>::iterator itMap = m_graphDataMap.begin();
64 
65  while(itMap != m_graphDataMap.end())
66  {
67  te::graph::GraphData* d = itMap->second;
68 
69  te::graph::GraphData::VertexMap::iterator it = d->getVertexMap().find(id);
70 
71  if(it != d->getVertexMap().end())
72  {
73  m_policy->accessed(d->getId());
74 
75  return d;
76  }
77 
78  ++itMap;
79  }
80 
81  //if not found
82  if(m_dataManager != 0)
83  {
84  m_dataManager->loadGraphDataByVertexId(id, this);
85 
86  return checkCacheByVertexId(id);
87  }
88 
89  return 0;
90 }
91 
93 {
94  //check local cache
95  std::map<int, GraphData*>::iterator itMap = m_graphDataMap.begin();
96 
97  while(itMap != m_graphDataMap.end())
98  {
99  te::graph::GraphData* d = itMap->second;
100 
101  te::graph::GraphData::EdgeMap::iterator it = d->getEdgeMap().find(id);
102 
103  if(it != d->getEdgeMap().end())
104  {
105  m_policy->accessed(d->getId());
106 
107  return d;
108  }
109 
110  ++itMap;
111  }
112 
113  //if not found
114  if(m_dataManager != 0)
115  {
116  m_dataManager->loadGraphDataByEdgeId(id, this);
117 
118  return checkCacheByEdgeId(id);
119  }
120 
121  return 0;
122 }
123 
125 {
126  //cache is empty, return a new graph data
127  if(m_graphDataMap.empty())
128  {
129  return createGraphData();
130  }
131 
132  //verify if has a incomplete graph data in cache and return
133  std::map<int, GraphData*>::iterator itMap = m_graphDataMap.begin();
134 
135  int gdId = -1;
136  size_t maxSize = m_metadata->m_maxCacheSize - 1;
137 
138  while(itMap != m_graphDataMap.end())
139  {
140  te::graph::GraphData* d = itMap->second;
141 
142  if(d->getVertexMap().size() < maxSize && d->getEdgeMap().size() < maxSize)
143  {
144  gdId = d->getId();
145 
146  maxSize = d->getVertexMap().size();
147  }
148  ++itMap;
149  }
150 
151  if(gdId != -1)
152  {
153  m_policy->update(gdId);
154 
155  return m_graphDataMap[gdId];
156  }
157 
158  //if cache is not full create a new graph data... add to cache and return
159  if(m_graphDataMap.size() < m_metadata->m_maxVecCacheSize)
160  {
161  return createGraphData();
162  }
163 
164  //remove the a graph data following the cache policy and create a new graph data
165  if(m_graphDataMap.size() == m_metadata->m_maxVecCacheSize)
166  {
167  int idxToRemove = 0;
168 
169  m_policy->toRemove(idxToRemove);
170 
171  te::graph::GraphData* d = m_graphDataMap[idxToRemove];
172 
173  saveGraphData(d);
174 
175  removeGraphData(d->getId());
176 
177  return createGraphData();
178  }
179 
180  return 0;
181 }
182 
184 {
185  if(m_graphDataMap.size() == m_metadata->m_maxVecCacheSize)
186  {
187  return 0;
188  }
189 
190  te::graph::GraphData* d = new te::graph::GraphData(getGraphDataId());
191 
192  m_graphDataMap.insert(std::map<int, GraphData*>::value_type(d->getId(), d));
193 
194  m_policy->added(d->getId());
195 
196  return d;
197 }
198 
200 {
201  std::map<int, GraphData*>::iterator it = m_graphDataMap.find(idx);
202 
203  if(it == m_graphDataMap.end())
204  {
205  return;
206  }
207 
208  te::graph::GraphData* data =it->second;
209  delete data;
210 
211  m_graphDataMap.erase(it);
212 }
213 
215 {
216  if(m_dataManager)
217  {
218  m_dataManager->saveGraphData(data);
219  }
220 }
221 
223 {
224  std::map<int, GraphData*>::iterator it = m_graphDataMap.begin();
225 
226  while(it != m_graphDataMap.end())
227  {
228  te::graph::GraphData* d = it->second;
229 
230  if(d->isDirty())
231  {
232  if(m_dataManager)
233  {
234  m_dataManager->saveGraphData(d);
235  }
236  }
237 
238  ++it;
239  }
240 
241  te::common::FreeContents(m_graphDataMap);
242 
243  m_graphDataMap.clear();
244 }
245 
247 {
248  //if not found check in cache
249  std::map<int, GraphData*>::iterator itMap = m_graphDataMap.begin();
250 
251  while(itMap != m_graphDataMap.end())
252  {
253  te::graph::GraphData* d = itMap->second;
254 
255  te::graph::GraphData::VertexMap::iterator it = d->getVertexMap().find(id);
256 
257  if(it != d->getVertexMap().end())
258  {
259  m_policy->accessed(d->getId());
260 
261  return d;
262  }
263 
264  ++itMap;
265  }
266 
267  return 0;
268 }
269 
271 {
272  //if not found check in cache
273  std::map<int, GraphData*>::iterator itMap = m_graphDataMap.begin();
274 
275  while(itMap != m_graphDataMap.end())
276  {
277  te::graph::GraphData* d = itMap->second;
278 
279  te::graph::GraphData::EdgeMap::iterator it = d->getEdgeMap().find(id);
280 
281  if(it != d->getEdgeMap().end())
282  {
283  m_policy->accessed(d->getId());
284 
285  return d;
286  }
287 
288  ++itMap;
289  }
290 
291  return 0;
292 }
293 
295 {
296  int id = m_graphDataCounter;
297 
298  ++m_graphDataCounter;
299 
300  return id;
301 }
void clearCache()
Used to remove from memory all elements loaded.
Definition: GraphCache.cpp:222
~GraphCache()
Default destructor.
Definition: GraphCache.cpp:53
AbstractCachePolicy * m_policy
Cache policy to control the cache in memory.
Definition: GraphCache.h:179
te::graph::GraphMetadata * getMetadata()
It returns a pointer to a class that describes the graph metadata.
VertexMap & getVertexMap()
It returns the the vertex map.
Definition: GraphData.cpp:89
GraphData * getGraphDataByEdgeId(int id)
Get a graph data from vector using a edge id information. if not found a new graph data has to be loa...
Definition: GraphCache.cpp:92
int getGraphDataId()
Protected function used to define a new value of Id to a graph data.
Definition: GraphCache.cpp:294
int getId()
Get data identifier.
Definition: GraphData.cpp:50
void removeGraphData(int idx)
Used to remove a graph data from cache.
Definition: GraphCache.cpp:199
GraphData * checkCacheByVertexId(int id)
This functions check in cache if the vertex element with a given id was alredy in memory...
Definition: GraphCache.cpp:246
This class define a important struct used to group a map of vertex and edges. A flag is used to indic...
GraphMetadata * m_metadata
Graph metadata information.
Definition: GraphCache.h:183
AbstractGraphLoaderStrategy * getLoaderStrategy()
Function used to get the current loader strategy.
This class is used to set the main functions of a cache policy.
GraphData * getGraphData()
Get a graph data.
Definition: GraphCache.cpp:124
This class defines a interface to access the graph elements inside a data source. Its use a implement...
GraphData * createGraphData()
Creates a new graph data structure.
Definition: GraphCache.cpp:183
void saveGraphData(GraphData *data)
Save the graph data structure inside a data source.
Definition: GraphCache.cpp:214
EdgeMap & getEdgeMap()
It returns the the edge map.
Definition: GraphData.cpp:133
Class used to manager the graph data elements. This class uses a cache policy to control the elements...
GraphData * checkCacheByEdgeId(int id)
This functions check in cache if the edge element with a given id was alredy in memory.
Definition: GraphCache.cpp:270
#define TE_DEFAULT_CACHE_POLICY_TYPE
This definition is used to set the default cache policy.
Definition: Config.h:58
This class define a important struct used to group a map of vertex and edges. A flag is used to indic...
Definition: GraphData.h:55
bool isDirty()
Used to check the graph data state.
Definition: GraphData.cpp:148
GraphDataManager * m_dataManager
Used to load and save GraphData information from a DataSource.
Definition: GraphCache.h:181
GraphData * getGraphDataByVertexId(int id)
Get a graph data from vector using a vertex id information. if not found a new graph data has to be l...
Definition: GraphCache.cpp:60
GraphCache(AbstractCachePolicy *cp, GraphDataManager *dm)
Default constructor.
Definition: GraphCache.cpp:41
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
int m_graphDataCounter
Graph data identifier counter.
Definition: GraphCache.h:185
static AbstractCachePolicy * make()
It creates and returns default cache policy.