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

#include <Importer.h>

Public Member Functions

 RenderGroupAnalyzer ()
 
void seedFromExistingTableContents (const std::unique_ptr< Loader > &loader, 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 712 of file Importer.h.

Member Typedef Documentation

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

Definition at line 721 of file Importer.h.

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

Definition at line 722 of file Importer.h.

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

Definition at line 720 of file Importer.h.

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

Definition at line 724 of file Importer.h.

Constructor & Destructor Documentation

import_export::RenderGroupAnalyzer::RenderGroupAnalyzer ( )
inline

Definition at line 714 of file Importer.h.

714 : _rtree(std::make_unique<RTree>()), _numRenderGroups(0) {}
std::unique_ptr< RTree > _rtree
Definition: Importer.h:725

Member Function Documentation

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

Definition at line 5120 of file Importer.cpp.

References _numRenderGroups, _rtree, _rtreeMutex, and CHECK.

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

5121  {
5122  // validate
5123  CHECK(bounds.size() == 4);
5124 
5125  // get bounds
5126  BoundingBox bounding_box;
5127  boost::geometry::assign_inverse(bounding_box);
5128  boost::geometry::expand(bounding_box, Point(bounds[0], bounds[1]));
5129  boost::geometry::expand(bounding_box, Point(bounds[2], bounds[3]));
5130 
5131  // remainder under mutex to allow this to be multi-threaded
5132  std::lock_guard<std::mutex> guard(_rtreeMutex);
5133 
5134  // get the intersecting nodes
5135  std::vector<Node> intersects;
5136  _rtree->query(boost::geometry::index::intersects(bounding_box),
5137  std::back_inserter(intersects));
5138 
5139  // build bitset of render groups of the intersecting rectangles
5140  // clear bit means available, allows use of find_first()
5141  boost::dynamic_bitset<> bits(_numRenderGroups);
5142  bits.set();
5143  for (const auto& intersection : intersects) {
5144  CHECK(intersection.second < _numRenderGroups);
5145  bits.reset(intersection.second);
5146  }
5147 
5148  // find first available group
5149  int firstAvailableRenderGroup = 0;
5150  size_t firstSetBit = bits.find_first();
5151  if (firstSetBit == boost::dynamic_bitset<>::npos) {
5152  // all known groups represented, add a new one
5153  firstAvailableRenderGroup = _numRenderGroups;
5154  _numRenderGroups++;
5155  } else {
5156  firstAvailableRenderGroup = (int)firstSetBit;
5157  }
5158 
5159  // insert new node
5160  _rtree->insert(std::make_pair(bounding_box, firstAvailableRenderGroup));
5161 
5162  // return it
5163  return firstAvailableRenderGroup;
5164 }
boost::geometry::model::box< Point > BoundingBox
Definition: Importer.h:721
std::unique_ptr< RTree > _rtree
Definition: Importer.h:725
#define CHECK(condition)
Definition: Logger.h:197
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point
Definition: Importer.h:720

+ Here is the caller graph for this function:

void import_export::RenderGroupAnalyzer::seedFromExistingTableContents ( const std::unique_ptr< Loader > &  loader,
const std::string &  geoColumnBaseName 
)

Definition at line 4983 of file Importer.cpp.

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

4985  {
4986  // start timer
4987  auto seedTimer = timer_start();
4988 
4989  // start with a fresh tree
4990  _rtree = nullptr;
4991  _numRenderGroups = 0;
4992 
4993  // get the table descriptor
4994  const auto& cat = loader->getCatalog();
4995  if (loader->getTableDesc()->storageType == StorageType::FOREIGN_TABLE) {
4997  LOG(INFO) << "DEBUG: Table is a foreign table";
4998  }
4999  _rtree = std::make_unique<RTree>();
5000  CHECK(_rtree);
5001  return;
5002  }
5003 
5004  const std::string& tableName = loader->getTableDesc()->tableName;
5005  const auto td = cat.getMetadataForTable(tableName);
5006  CHECK(td);
5007  CHECK(td->fragmenter);
5008 
5009  // if the table is empty, just make an empty tree
5010  if (td->fragmenter->getFragmentsForQuery().getPhysicalNumTuples() == 0) {
5012  LOG(INFO) << "DEBUG: Table is empty!";
5013  }
5014  _rtree = std::make_unique<RTree>();
5015  CHECK(_rtree);
5016  return;
5017  }
5018 
5019  // no seeding possible without these two columns
5020  const auto cd_bounds =
5021  cat.getMetadataForColumn(td->tableId, geoColumnBaseName + "_bounds");
5022  const auto cd_render_group =
5023  cat.getMetadataForColumn(td->tableId, geoColumnBaseName + "_render_group");
5024  if (!cd_bounds || !cd_render_group) {
5025  throw std::runtime_error("RenderGroupAnalyzer: Table " + tableName +
5026  " doesn't have bounds or render_group columns!");
5027  }
5028 
5029  // and validate their types
5030  if (cd_bounds->columnType.get_type() != kARRAY ||
5031  cd_bounds->columnType.get_subtype() != kDOUBLE) {
5032  throw std::runtime_error("RenderGroupAnalyzer: Table " + tableName +
5033  " bounds column is wrong type!");
5034  }
5035  if (cd_render_group->columnType.get_type() != kINT) {
5036  throw std::runtime_error("RenderGroupAnalyzer: Table " + tableName +
5037  " render_group column is wrong type!");
5038  }
5039 
5040  // get chunk accessor table
5041  auto chunkAccessorTable = getChunkAccessorTable(
5042  cat, td, {geoColumnBaseName + "_bounds", geoColumnBaseName + "_render_group"});
5043  const auto table_count = std::get<0>(chunkAccessorTable.back());
5044 
5046  LOG(INFO) << "DEBUG: Scanning existing table geo column set '" << geoColumnBaseName
5047  << "'";
5048  }
5049 
5050  std::vector<Node> nodes;
5051  try {
5052  nodes.resize(table_count);
5053  } catch (const std::exception& e) {
5054  throw std::runtime_error("RenderGroupAnalyzer failed to reserve memory for " +
5055  std::to_string(table_count) + " rows");
5056  }
5057 
5058  for (size_t row = 0; row < table_count; row++) {
5059  ArrayDatum ad;
5060  VarlenDatum vd;
5061  bool is_end;
5062 
5063  // get ChunkIters and fragment row offset
5064  size_t rowOffset = 0;
5065  auto& chunkIters = getChunkItersAndRowOffset(chunkAccessorTable, row, rowOffset);
5066  auto& boundsChunkIter = chunkIters[0];
5067  auto& renderGroupChunkIter = chunkIters[1];
5068 
5069  // get bounds values
5070  ChunkIter_get_nth(&boundsChunkIter, row - rowOffset, &ad, &is_end);
5071  CHECK(!is_end);
5072  CHECK(ad.pointer);
5073  int numBounds = (int)(ad.length / sizeof(double));
5074  CHECK(numBounds == 4);
5075 
5076  // convert to bounding box
5077  double* bounds = reinterpret_cast<double*>(ad.pointer);
5078  BoundingBox bounding_box;
5079  boost::geometry::assign_inverse(bounding_box);
5080  boost::geometry::expand(bounding_box, Point(bounds[0], bounds[1]));
5081  boost::geometry::expand(bounding_box, Point(bounds[2], bounds[3]));
5082 
5083  // get render group
5084  ChunkIter_get_nth(&renderGroupChunkIter, row - rowOffset, false, &vd, &is_end);
5085  CHECK(!is_end);
5086  CHECK(vd.pointer);
5087  int renderGroup = *reinterpret_cast<int32_t*>(vd.pointer);
5088 
5089  // skip rows with invalid render groups (e.g. EMPTY geometry)
5090  if (renderGroup < 0) {
5091  continue;
5092  }
5093 
5094  // store
5095  nodes[row] = std::make_pair(bounding_box, renderGroup);
5096 
5097  // how many render groups do we have now?
5098  if (renderGroup >= _numRenderGroups) {
5099  _numRenderGroups = renderGroup + 1;
5100  }
5101 
5103  LOG(INFO) << "DEBUG: Existing row " << row << " has Render Group " << renderGroup;
5104  }
5105  }
5106 
5107  // bulk-load the tree
5108  auto bulk_load_timer = timer_start();
5109  _rtree = std::make_unique<RTree>(nodes);
5110  CHECK(_rtree);
5111  LOG(INFO) << "Scanning render groups of poly column '" << geoColumnBaseName
5112  << "' of table '" << tableName << "' took " << timer_stop(seedTimer) << "ms ("
5113  << timer_stop(bulk_load_timer) << " ms for tree)";
5114 
5116  LOG(INFO) << "DEBUG: Done! Now have " << _numRenderGroups << " Render Groups";
5117  }
5118 }
ChunkAccessorTable getChunkAccessorTable(const Catalog_Namespace::Catalog &cat, const TableDescriptor *td, const std::vector< std::string > &columnNames)
std::string cat(Ts &&...args)
#define LOG(tag)
Definition: Logger.h:188
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:143
std::conditional_t< is_cuda_compiler(), DeviceArrayDatum, HostArrayDatum > ArrayDatum
Definition: sqltypes.h:199
ChunkIterVector & getChunkItersAndRowOffset(ChunkAccessorTable &table, size_t rowid, size_t &rowOffset)
#define DEBUG_RENDER_GROUP_ANALYZER
Definition: Importer.cpp:144
boost::geometry::model::box< Point > BoundingBox
Definition: Importer.h:721
std::unique_ptr< RTree > _rtree
Definition: Importer.h:725
#define CHECK(condition)
Definition: Logger.h:197
Definition: sqltypes.h:44
static constexpr char const * FOREIGN_TABLE
Type timer_start()
Definition: measure.h:42
boost::geometry::model::point< double, 2, boost::geometry::cs::cartesian > Point
Definition: Importer.h:720

+ Here is the call graph for this function:

Member Data Documentation

int import_export::RenderGroupAnalyzer::_numRenderGroups
private

Definition at line 727 of file Importer.h.

Referenced by insertBoundsAndReturnRenderGroup(), and seedFromExistingTableContents().

std::unique_ptr<RTree> import_export::RenderGroupAnalyzer::_rtree
private

Definition at line 725 of file Importer.h.

Referenced by insertBoundsAndReturnRenderGroup(), and seedFromExistingTableContents().

std::mutex import_export::RenderGroupAnalyzer::_rtreeMutex
private

Definition at line 726 of file Importer.h.

Referenced by insertBoundsAndReturnRenderGroup().


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