The ScaleOut StateServer service allows you to query a named cache for Protocol Buffer objects whose indexed fields satisfy a specified filter. The NamedCache::query method returns a smart pointer to a query result object (boost::shared_ptr<sosscli::QueryResult>), which in turn contains a vector of keys to matching objects.
#include "soss_client/named_protobuf_cache.h" #include "soss_client/reference_comparand.h" // #include "soss_client/value_comparand.h" #include "StockQuote.pb.h" using namespace NativeClientSample; int main(int argc, char* argv[]) { sosscli::NamedProtobufCache<StockQuote> quote_cache("Stock Quotes"); // Create two StockQuote protobuf objects and insert them into the StateServer service: auto goog_quote = boost::make_shared<StockQuote>(); goog_quote->set_ticker("GOOG"); goog_quote->set_price(1054.48f); goog_quote->set_volume(1373206); quote_cache.put("GOOG", goog_quote); auto msft_quote = boost::make_shared<StockQuote>(); msft_quote->set_ticker("MSFT"); msft_quote->set_price(38.45f); msft_quote->set_volume(42927723); quote_cache.put("MSFT", msft_quote); // Query for stocks whose price is less than $100 and whose volume // is greater than 10 million shares traded: auto query_result = quote_cache.query(sosscli::ReferenceComparand("price") < 100.0f && // sosscli::ReferenceComparand("volume") > 10000000); // Print query results: for (sosscli::SossKey key : query_result->keys()) { // Retrieve the object from the named cache so we can get its ticker symbol: auto get_result = quote_cache.get(key); auto retrieved_quote = get_result.object_ptr(); // a boost::shared_ptr std::cout << retrieved_quote->ticker() << std::endl; } return 0; } // Output: // MSFT
|
|
The ReferenceComparand class is used to create a filter for a query—pass field names from your protobuf classes into the ReferenceComparand’s constructor when building up a filter expression for your query. Note that filters can be composed using intuitive operators, which have been overloaded in the ReferenceComparand and Filter classes. |
The NamedCache::query method that’s illustrated in the example above accepts a sosscli::Filter object as a parameter. Filter objects are not intended to be created directly—instead, operator overloads from comparison expressions act as factories for creating a Filter objects. Those filters may then be logically combined with other filter clauses using overloaded logical operators (&&, ||) to create a single Filter object that is sent to the StateServer service. These operator overloads allow multiple search predicates to be sent to the StateServer service using straightforward C++ syntax.
Note | |
---|---|
Query operations are very resource-intensive compared to normal key-based operations: queries involve every host in the distributed data grid, and all of the hosts must coordinate to merge the results before returning to the caller. Favor key-based operations whenever possible, as they will run faster by orders of magnitude. |