Working with data filters.
Working with data filters.
Introduction <br>
RTI Connext supports filtering data either during the exchange from dds::pub::DataWriter
to dds::sub::DataReader
, or after the data has been stored at the dds::sub::DataReader
.
Filtering during the exchange process is performed by a dds::topic::ContentFilteredTopic
, which is created by the dds::sub::DataReader
as a way of specifying a subset of the data samples that it wishes to receive.
Filtering samples that have already been received by the dds::sub::DataReader
is performed by creating a dds::sub::cond::QueryCondition
, which can then used to check for matching samples, be alerted when matching samples arrive, or retrieve matching samples.
Filtering may be performed on any topic, either keyed or un-keyed, except the built-in topics. Filtering may be performed on any field, subset of fields, or combination of fields, subject only to the limitations of the filter syntax.
Code Examples
The following #includes are needed for the examples on this page
#include <iostream>
#include <dds/topic/ddstopic.hpp>
#include <dds/sub/ddssub.hpp>
#include "Foo.hpp"
Filtering with ContentFilteredTopic
- Set up a Subscriber
- Set up a Topic
- Create a ContentFilteredTopic, of user data type
Foo:
std::vector<std::string> cft_parameters(2);
cft_parameters[0] = "1";
cft_parameters[1] = "100";
topic,
"ContentFilteredTopic",
"x > %0 AND x < %1",
cft_parameters));
topic,
"ContentFilteredTopic2",
<<reference-type>> Container for all dds::core::Entity objects.
Definition: TDomainParticipant.hpp:63
<<reference-type>> Specialization of TopicDescription that allows for content-based subscriptions.
Definition: TContentFilteredTopic.hpp:57
Defines the filter to create a ContentFilteredTopic.
Definition: TFilter.hpp:46
<<reference-type>> Topic is the most basic description of the data to be published and subscribed.
Definition: TTopic.hpp:55
- Create a DataReader using the ContentFilteredTopic:
<<reference-type>> Allows the application to: (1) declare the data it wishes to receive (i....
Definition: TDataReader.hpp:73
Once setup, reading samples with a dds::topic::ContentFilteredTopic
is exactly the same as normal reads or takes, as described in DataReader Use Cases.
- Changing filter criteria:
std::vector<std::string> new_cft_parameters(2);
new_cft_parameters[0] = "5";
new_cft_parameters[1] = "9";
cft.filter_parameters(new_cft_parameters.begin(), new_cft_parameters.end());
Filtering with Query Conditions
- Creating a QueryCondition
std::vector<std::string> query_parameters(2);
query_parameters[0] = "1";
query_parameters[1] = "100";
<<value-type>> Encapsulates a query for a dds::sub::cond::QueryCondition.
Definition: Query.hpp:57
<<reference-type>> Specialized ReadCondition that allows applications to also specify a filter on the...
Definition: TQueryCondition.hpp:44
static DataState any_data()
Create a DataState with InstanceState::alive(), ViewState::any(), and SampleState::any()
Definition: status/DataState.hpp:494
- You can use a QueryCondition in a WaitSet: see Waiting for Condition(s) to trigger
- And to query for data in a DataReader: see Selecting what samples to read
- To modify the filter criteria you can use
dds::sub::cond::QueryCondition::parameters
(similar to dds::topic::ContentFilteredTopic::filter_parameters
)
- This example shows how to create a condition with a handler that takes the samples received by a DataReader and selected by the QueryCondition's filter.
QueryCondition query_condition2(
{
auto condition_as_qc =
dds::core::polymorphic_cast<QueryCondition>(
condition);
auto samples = reader.select().condition(condition_as_qc).take();
for (auto& sample : samples) {
std::cout << "Sample received: " << sample << std::endl;
}
});
<<reference-type>> Abstract base class of all the conditions
Definition: TCondition.hpp:58
dds::sub::functors::ConditionManipulatorFunctor condition(const dds::sub::cond::ReadCondition &condition)
Stream manipulator to set a QueryCondition to use during the subsequent read/take operation.
Definition: DataReader.hpp:178
Filtering Performance
Although RTI Connext supports filtering on any field or combination of fields using the SQL syntax of the built-in filter, filters for keyed topics that filter solely on the contents of key fields have the potential for much higher performance. This is because for key-only filters, the dds::sub::DataReader caches the results of the filter (pass or not pass) for each instance. When another sample of the same instance is seen at the dds::sub::DataReader, the filter results are retrieved from the cache, dispensing with the need to call the filter function.
This optimization applies to all filtering using the built-in SQL filter, performed by the dds::sub::DataReader, for either dds::topic::ContentFilteredTopic or dds::sub::cond::QueryCondition. This does not apply to filtering perfomed for dds::topic::ContentFilteredTopic by the dds::pub::DataWriter.