22 #include "queryoptimizer.h" 26 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
27 BaseView<DataSetType, PointType, SearchPointType, DistanceType>::BaseView(DataSetType* dataset) :
28 _dataset(dataset), _isViewValid(false), _searchSpacePool(dataset) {
34 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
35 void BaseView<DataSetType, PointType, SearchPointType, DistanceType>::init() {
36 if (!_dataset)
throw GaiaException(
"You need to specify a valid DataSet when creating a view");
38 _dataset->addView(
this);
43 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
44 BaseView<DataSetType, PointType, SearchPointType, DistanceType>::~BaseView() {
46 _dataset->removeView(
this);
50 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
53 _searchSpacePool.indexOn(descriptorName);
57 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
59 G_DEBUG(GView,
"invalidating view");
63 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
65 if (_isViewValid)
return;
67 G_DEBUG(GView,
"validating view");
71 throw GaiaException(
"Cannot revalidate view, as the underlying dataset has been deleted");
75 _searchSpacePool.recreate();
76 G_DEBUG(GView,
"Search space pool updated! View has been revalidated");
82 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
86 while (!resQueue.empty()) {
87 result.append(Result(resQueue.top().ptr->name(),
88 resQueue.top().dist));
92 std::reverse(result.begin(), result.end());
99 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
102 filter->updateVariables();
109 throw GaiaException(
"Cannot use a filter on this query because no reference DataSet has been set.");
111 filter->bindToLayout(refDataSet->
layout());
115 filter->bindToPointAttributes();
119 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
122 QString filterStr = filterString;
123 if (filterString ==
"") filterStr =
"where true";
132 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
134 const DistanceType* dist,
138 bool owns = (filter == 0);
139 ResultSetType result = nnSearch(p, getSearchSpace(0, filter, owns), dist);
141 G_DEBUG(GPerf,
"Total time for the full query (optimization + query): " << timer.
elapsed() * 1000 <<
" ms");
145 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
147 const DistanceType* dist,
148 const QString& filter) {
151 ResultSetType result = nnSearch(p, getSearchSpace(0, filter), dist);
153 G_DEBUG(GPerf,
"Total time for the full query (optimization + query): " << timer.
elapsed() * 1000 <<
" ms");
157 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
160 const DistanceType* dist,
164 bool owns = (filter == 0);
165 ResultSetType result = nnSearch(p, getSearchSpace(inputSpace.searchSpace(), filter, owns), dist);
167 G_DEBUG(GPerf,
"Total time for the full query (optimization + query): " << timer.
elapsed() * 1000 <<
" ms");
171 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
174 const DistanceType* dist,
175 const QString& filter) {
178 ResultSetType result = nnSearch(p, getSearchSpace(inputSpace.searchSpace(), filter), dist);
180 G_DEBUG(GPerf,
"Total time for the full query (optimization + query): " << timer.
elapsed() * 1000 <<
" ms");
187 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
189 const QString& filterString) {
190 return getSearchSpace(sspace, createFilter(filterString),
true);
193 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
196 bool spaceOwnsFilter) {
205 spaceOwnsFilter =
true;
212 G_DEBUG(GView,
"Query on given SearchSpace, skipping optimization part");
215 result->setFilter(filter, spaceOwnsFilter);
219 G_DEBUG(GView,
"Query on full dataset, getting optimized search space");
224 if (!spaceOwnsFilter) filter =
new Filter(*filter);
227 result = qo.
optimize(const_cast<Filter*>(filter));
228 G_DEBUG(GPerf,
"Time to do full optimization: " << timer.
elapsed() * 1000 <<
" ms");
235 template <
typename DataSetType,
typename Po
intType,
typename SearchPo
intType,
typename DistanceType>
238 const DistanceType* dist) {
240 Timer fullTime, timer;
246 bindFilter(sspace->filter(), _dataset);
250 throw GaiaException(
"View: you are trying to compute the nearest-neighbors of a point, " 251 "but you haven't specified any distance to use... Please do so.");
257 G_DEBUG(GPerf,
"Time to (re)validate view: " << timer.
elapsed() <<
" ms");
264 G_DEBUG(GPerf,
"Time to compare layouts (dataset/query point/distance): " 272 G_DEBUG(GPerf,
"Time to compute distances: " << timer.
elapsed() * 1000 <<
" ms");
276 G_DEBUG(GPerf,
"Time to filter and sort distances: " << timer.
elapsed() * 1000 <<
" ms");
279 G_DEBUG(GPerf,
"Time to answer query: " << fullTime.
elapsed() * 1000.0 <<
" ms");
void filterAndSort(int n=1000)
Filters the points in this SearchSpace using the given Filter and sort them by increasing distance...
Definition: searchspace.h:202
bool needsBinding() const
Returns whether this filter needs to be bound to a specific layout.
Definition: filter.cpp:129
double elapsed() const
Returns the cumulative number of seconds elapsed between each call to the start()/stop() pair...
Definition: timer.cpp:51
void computeDistance(const PointType &query, const DistanceType *dist)
Compute the distance from the given query point to all those contained in this SearchSpace, and presort a few points to have near instant access to them.
static Filter * parse(const QString &str)
Parses a given string and returns the newly created Filter object.
Definition: filter.cpp:181
void restart()
Restarts the timer.
Definition: timer.h:62
This class represents a dataset and all related information.
Definition: dataset.h:91
const PointLayout & layout() const
Return the layout of this dataset.
Definition: dataset.h:136
void copyPointsFrom(const BaseSearchSpace< SearchPointType, DataSetType > &other)
Copy all the points from the other SearchSpace into this one.
Definition: searchspace.h:38
The QueryOptimizer class tries to optimize a query by reducing the SearchSpace on which it is suppose...
Definition: parsertypes.h:30
The Filter class allows to check whether a predicate is true for a given Point.
Definition: filter.h:73
void start()
Starts the timer.
Definition: timer.cpp:32
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
SearchSpaceType * optimize(Filter *filter)
Ownership of the SearchSpace is yielded to the caller of the function.
Definition: queryoptimizer_impl.h:255
void init()
Init function that makes sure that all the factories have correctly registered all the types they can...
Definition: gaia.cpp:53
SearchSpaceType * getSearchSpace(SearchSpaceType *sspace=0, const Filter *filter=0, bool spaceOwnsFilter=true)
Returns a valid SearchSpace with associated filter.
Definition: view_impl.h:194
void indexOn(const QString &descriptorName)
This methods creates an index on the given descriptor name, in the hope that future queries using thi...
Definition: view_impl.h:51
The View class contains a specific view on a dataset.
Definition: dataset.h:37
void invalidate()
This function is called whenever the DataSet it points to changes, so it knows it needs to recompute ...
Definition: view_impl.h:58
A SearchSpace is a structure dedicated to the task of storing pointers to Point with an associated di...
Definition: searchspace.h:91
This class serves as a ref-counted wrapper for a SearchSpace, which is much more convenient to deal w...
Definition: searchspace.h:306
void stop()
Stops the timer.
Definition: timer.cpp:42
This class allows you to measure a certain amount of time, for instance if you want to know how long ...
Definition: timer.h:38
Exception class that can take up to 3 arguments of any type, which will be serialized into a QString ...
Definition: gaiaexception.h:46
The SearchSpacePool accomplishes 2 main functions:
Definition: searchspacepool.h:41
void validate()
This function is called to make sure that the View is valid (ie: its sorting structure and filters ac...
Definition: view_impl.h:64
const DataSet * referenceDataSet() const
Return the reference dataset.
Definition: dataset.cpp:295