OmniSciDB  471d68cefb
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
import_export::RenderGroupAnalyzer Class Reference

#include <RenderGroupAnalyzer.h>

Public Member Functions

 RenderGroupAnalyzer ()
 
void seedFromExistingTableContents (const Catalog_Namespace::Catalog &cat, const std::string &tableName, const std::string &geoColumnBaseName)
 
int insertBoundsAndReturnRenderGroup (const std::vector< double > &bounds)
 

Private Types

using Point = boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian >
 
using BoundingBox = boost::geometry::model::box< Point >
 
using Node = std::pair< BoundingBox, int >
 
using RTree = boost::geometry::index::rtree< Node, boost::geometry::index::quadratic< 16 >>
 

Private Attributes

std::unique_ptr< RTree_rtree
 
std::mutex _rtreeMutex
 
int _numRenderGroups
 

Detailed Description

Definition at line 36 of file RenderGroupAnalyzer.h.

Member Typedef Documentation

using import_export::RenderGroupAnalyzer::BoundingBox = boost::geometry::model::box<Point>
private

Definition at line 46 of file RenderGroupAnalyzer.h.

using import_export::RenderGroupAnalyzer::Node = std::pair<BoundingBox, int>
private

Definition at line 47 of file RenderGroupAnalyzer.h.

using import_export::RenderGroupAnalyzer::Point = boost::geometry::model::point<double, 2, boost::geometry::cs::cartesian>
private

Definition at line 45 of file RenderGroupAnalyzer.h.

using import_export::RenderGroupAnalyzer::RTree = boost::geometry::index::rtree<Node, boost::geometry::index::quadratic<16>>
private

Definition at line 49 of file RenderGroupAnalyzer.h.

Constructor & Destructor Documentation

import_export::RenderGroupAnalyzer::RenderGroupAnalyzer ( )
inline

Definition at line 38 of file RenderGroupAnalyzer.h.

38 : _rtree(std::make_unique<RTree>()), _numRenderGroups(0) {}

Member Function Documentation

int import_export::RenderGroupAnalyzer::insertBoundsAndReturnRenderGroup ( const std::vector< double > &  bounds)

Definition at line 184 of file RenderGroupAnalyzer.cpp.

References _numRenderGroups, _rtree, _rtreeMutex, and CHECK.

Referenced by GeoPolygonValueConverter::convertToColumnarFormat(), and GeoMultiPolygonValueConverter::convertToColumnarFormat().

185  {
186  // validate
187  CHECK(bounds.size() == 4);
188 
189  // get bounds
190  BoundingBox bounding_box;
191  boost::geometry::assign_inverse(bounding_box);
192  boost::geometry::expand(bounding_box, Point(bounds[0], bounds[1]));
193  boost::geometry::expand(bounding_box, Point(bounds[2], bounds[3]));
194 
195  // remainder under mutex to allow this to be multi-threaded
196  std::lock_guard<std::mutex> guard(_rtreeMutex);
197 
198  // get the intersecting nodes
199  std::vector<Node> intersects;
200  _rtree->query(boost::geometry::index::intersects(bounding_box),
201  std::back_inserter(intersects));
202 
203  // build bitset of render groups of the intersecting rectangles
204  // clear bit means available, allows use of find_first()
205  boost::dynamic_bitset<> bits(_numRenderGroups);
206  bits.set();
207  for (const auto& intersection : intersects) {
208  CHECK(intersection.second < _numRenderGroups);
209  bits.reset(intersection.second);
210  }
211 
212  // find first available group
213  int firstAvailableRenderGroup = 0;
214  size_t firstSetBit = bits.find_first();
215  if (firstSetBit == boost::dynamic_bitset<>::npos) {
216  // all known groups represented, add a new one
217  firstAvailableRenderGroup = _numRenderGroups;
219  } else {
220  firstAvailableRenderGroup = (int)firstSetBit;
221  }
222 
223  // insert new node
224  _rtree->insert(std::make_pair(bounding_box, firstAvailableRenderGroup));
225 
226  // return it
227  return firstAvailableRenderGroup;
228 }
boost::geometry::model::box< Point > BoundingBox
#define CHECK(condition)
Definition: Logger.h:209
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point

+ Here is the caller graph for this function:

void import_export::RenderGroupAnalyzer::seedFromExistingTableContents ( const Catalog_Namespace::Catalog cat,
const std::string &  tableName,
const std::string &  geoColumnBaseName 
)

Definition at line 47 of file RenderGroupAnalyzer.cpp.

References _numRenderGroups, _rtree, CHECK, ChunkIter_get_nth(), DEBUG_RENDER_GROUP_ANALYZER, StorageType::FOREIGN_TABLE, getChunkAccessorTable(), getChunkItersAndRowOffset(), Catalog_Namespace::Catalog::getMetadataForColumn(), Catalog_Namespace::Catalog::getMetadataForTable(), logger::INFO, kARRAY, kDOUBLE, kINT, LOG, VarlenDatum::pointer, timer_start(), timer_stop(), and to_string().

50  {
51  // start timer
52  auto seedTimer = timer_start();
53 
54  // start with a fresh tree
55  _rtree = nullptr;
56  _numRenderGroups = 0;
57 
58  // get the table descriptor
59  auto const* td = cat.getMetadataForTable(tableName);
60  CHECK(td);
61 
62  // foreign tables not supported
63  if (td->storageType == StorageType::FOREIGN_TABLE) {
65  LOG(INFO) << "DEBUG: Table is a foreign table";
66  }
67  _rtree = std::make_unique<RTree>();
68  CHECK(_rtree);
69  return;
70  }
71 
72  // if the table is empty, just make an empty tree
73  CHECK(td->fragmenter);
74  if (td->fragmenter->getFragmentsForQuery().getPhysicalNumTuples() == 0) {
76  LOG(INFO) << "DEBUG: Table is empty!";
77  }
78  _rtree = std::make_unique<RTree>();
79  CHECK(_rtree);
80  return;
81  }
82 
83  // no seeding possible without these two columns
84  const auto cd_bounds =
85  cat.getMetadataForColumn(td->tableId, geoColumnBaseName + "_bounds");
86  const auto cd_render_group =
87  cat.getMetadataForColumn(td->tableId, geoColumnBaseName + "_render_group");
88  if (!cd_bounds || !cd_render_group) {
89  throw std::runtime_error("RenderGroupAnalyzer: Table " + tableName +
90  " doesn't have bounds or render_group columns!");
91  }
92 
93  // and validate their types
94  if (cd_bounds->columnType.get_type() != kARRAY ||
95  cd_bounds->columnType.get_subtype() != kDOUBLE) {
96  throw std::runtime_error("RenderGroupAnalyzer: Table " + tableName +
97  " bounds column is wrong type!");
98  }
99  if (cd_render_group->columnType.get_type() != kINT) {
100  throw std::runtime_error("RenderGroupAnalyzer: Table " + tableName +
101  " render_group column is wrong type!");
102  }
103 
104  // get chunk accessor table
105  auto chunkAccessorTable = getChunkAccessorTable(
106  cat, td, {geoColumnBaseName + "_bounds", geoColumnBaseName + "_render_group"});
107  const auto table_count = std::get<0>(chunkAccessorTable.back());
108 
110  LOG(INFO) << "DEBUG: Scanning existing table geo column set '" << geoColumnBaseName
111  << "'";
112  }
113 
114  std::vector<Node> nodes;
115  try {
116  nodes.resize(table_count);
117  } catch (const std::exception& e) {
118  throw std::runtime_error("RenderGroupAnalyzer failed to reserve memory for " +
119  std::to_string(table_count) + " rows");
120  }
121 
122  for (size_t row = 0; row < table_count; row++) {
123  ArrayDatum ad;
124  VarlenDatum vd;
125  bool is_end;
126 
127  // get ChunkIters and fragment row offset
128  size_t rowOffset = 0;
129  auto& chunkIters = getChunkItersAndRowOffset(chunkAccessorTable, row, rowOffset);
130  auto& boundsChunkIter = chunkIters[0];
131  auto& renderGroupChunkIter = chunkIters[1];
132 
133  // get bounds values
134  ChunkIter_get_nth(&boundsChunkIter, row - rowOffset, &ad, &is_end);
135  CHECK(!is_end);
136  CHECK(ad.pointer);
137  int numBounds = (int)(ad.length / sizeof(double));
138  CHECK(numBounds == 4);
139 
140  // convert to bounding box
141  double* bounds = reinterpret_cast<double*>(ad.pointer);
142  BoundingBox bounding_box;
143  boost::geometry::assign_inverse(bounding_box);
144  boost::geometry::expand(bounding_box, Point(bounds[0], bounds[1]));
145  boost::geometry::expand(bounding_box, Point(bounds[2], bounds[3]));
146 
147  // get render group
148  ChunkIter_get_nth(&renderGroupChunkIter, row - rowOffset, false, &vd, &is_end);
149  CHECK(!is_end);
150  CHECK(vd.pointer);
151  int renderGroup = *reinterpret_cast<int32_t*>(vd.pointer);
152 
153  // skip rows with invalid render groups (e.g. EMPTY geometry)
154  if (renderGroup < 0) {
155  continue;
156  }
157 
158  // store
159  nodes[row] = std::make_pair(bounding_box, renderGroup);
160 
161  // how many render groups do we have now?
162  if (renderGroup >= _numRenderGroups) {
163  _numRenderGroups = renderGroup + 1;
164  }
165 
167  LOG(INFO) << "DEBUG: Existing row " << row << " has Render Group " << renderGroup;
168  }
169  }
170 
171  // bulk-load the tree
172  auto bulk_load_timer = timer_start();
173  _rtree = std::make_unique<RTree>(nodes);
174  CHECK(_rtree);
175  LOG(INFO) << "Scanning render groups of poly column '" << geoColumnBaseName
176  << "' of table '" << tableName << "' took " << timer_stop(seedTimer) << "ms ("
177  << timer_stop(bulk_load_timer) << " ms for tree)";
178 
180  LOG(INFO) << "DEBUG: Done! Now have " << _numRenderGroups << " Render Groups";
181  }
182 }
ChunkAccessorTable getChunkAccessorTable(const Catalog_Namespace::Catalog &cat, const TableDescriptor *td, const std::vector< std::string > &columnNames)
#define LOG(tag)
Definition: Logger.h:203
TypeR::rep timer_stop(Type clock_begin)
Definition: measure.h:48
DEVICE void ChunkIter_get_nth(ChunkIter *it, int n, bool uncompress, VarlenDatum *result, bool *is_end)
Definition: ChunkIter.cpp:181
std::string to_string(char const *&&v)
int8_t * pointer
Definition: sqltypes.h:152
std::conditional_t< is_cuda_compiler(), DeviceArrayDatum, HostArrayDatum > ArrayDatum
Definition: sqltypes.h:208
ChunkIterVector & getChunkItersAndRowOffset(ChunkAccessorTable &table, size_t rowid, size_t &rowOffset)
const ColumnDescriptor * getMetadataForColumn(int tableId, const std::string &colName) const
boost::geometry::model::box< Point > BoundingBox
#define DEBUG_RENDER_GROUP_ANALYZER
#define CHECK(condition)
Definition: Logger.h:209
Definition: sqltypes.h:45
const TableDescriptor * getMetadataForTable(const std::string &tableName, const bool populateFragmenter=true) const
Returns a pointer to a const TableDescriptor struct matching the provided tableName.
static constexpr char const * FOREIGN_TABLE
Type timer_start()
Definition: measure.h:42
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point

+ Here is the call graph for this function:

Member Data Documentation

int import_export::RenderGroupAnalyzer::_numRenderGroups
private
std::unique_ptr<RTree> import_export::RenderGroupAnalyzer::_rtree
private
std::mutex import_export::RenderGroupAnalyzer::_rtreeMutex
private

Definition at line 51 of file RenderGroupAnalyzer.h.

Referenced by insertBoundsAndReturnRenderGroup().


The documentation for this class was generated from the following files: