/* * The PoolRemoteQuery QuickStart Example. * This examples creates pool using locator. * This example takes the following steps: * * 1. Connect to a GemFire Distributed System. * 2. Create a GemFire Cache. * 3. Get the example Region from the Cache. * 4. Populate some query objects on the Region. * 5. Get the pool, to get the Query Service. Pool is define in clientRemoteQueryWithPool.xml. Pool has locator to get the server. Apart from that pool is bind to server group "ServerGroup1". * 6. Execute a query that returns a Result Set. * 7. Execute a query that returns a Struct Set. * 8. Execute the region shortcut/convenience query methods. * 9. Close the Cache. * 10. Disconnect from the Distributed System. * */ // Include the GemFire library. #include // Include the Query headers. #include #include // Include our Query objects, viz. Portfolio and Position. #include "queryobjects/Portfolio.hpp" #include "queryobjects/Position.hpp" // Use the "gemfire" namespace. using namespace gemfire; // Use the "testobject" namespace for the query objects. using namespace testobject; // The PoolRemoteQuery QuickStart example. int main(int argc, char ** argv) { try { // Connect to the GemFire Distributed System using the settings from the gfcpp.properties file by default. DistributedSystemPtr dSysPtr = DistributedSystem::connect("ExampleDistributedSystem"); LOGINFO("Connected to the GemFire Distributed System"); // Create a GemFire Cache with the "clientRemoteQueryWithPool.xml" Cache XML file. CachePtr cachePtr = CacheFactory::create("ExampleCache", dSysPtr, "XMLs/clientPoolRemoteQuery.xml"); LOGINFO("Created the GemFire Cache"); // Get the example Region from the Cache which is declared in the Cache XML file. RegionPtr regionPtr = cachePtr->getRegion("/root/Portfolios"); LOGINFO("Obtained the Region from the Cache"); // Register our Serializable/Cacheable Query objects, viz. Portfolio and Position. Serializable::registerType( Portfolio::createDeserializable); Serializable::registerType( Position::createDeserializable); LOGINFO("Registered Serializable Query Objects"); // Populate the Region with some Portfolio objects. PortfolioPtr port1Ptr(new Portfolio(1 /*ID*/, 10 /*size*/)); PortfolioPtr port2Ptr(new Portfolio(2 /*ID*/, 20 /*size*/)); PortfolioPtr port3Ptr(new Portfolio(3 /*ID*/, 30 /*size*/)); regionPtr->put("Key1", port1Ptr); regionPtr->put("Key2", port2Ptr); regionPtr->put("Key3", port3Ptr); LOGINFO("Populated some Portfolio Objects"); //if finds the pool PoolPtr poolPtr = PoolManager::find("examplePool"); // Get the QueryService from the Pool. QueryServicePtr qrySvcPtr = poolPtr->getQueryService(); LOGINFO("Got the QueryService from the Pool"); // Execute a Query which returns a ResultSet. QueryPtr qryPtr = qrySvcPtr->newQuery("SELECT DISTINCT * FROM /root/Portfolios"); SelectResultsPtr resultsPtr = qryPtr->execute(); LOGINFO("ResultSet Query returned %d rows", resultsPtr->size()); // Execute a Query which returns a StructSet. qryPtr = qrySvcPtr->newQuery("SELECT DISTINCT ID, status FROM /root/Portfolios WHERE ID > 1"); resultsPtr = qryPtr->execute(); LOGINFO("StructSet Query returned %d rows", resultsPtr->size()); // Iterate through the rows of the query result. int rowCount = 0; SelectResultsIterator iter = resultsPtr->getIterator(); while (iter.hasNext()) { rowCount++; Struct * psi = dynamic_cast( iter.next().ptr() ); LOGINFO("Row %d Column 1 is named %s, value is %s", rowCount, psi->getFieldName(0), (*psi)[(size_t) 0]->toString()->asChar()); LOGINFO("Row %d Column 2 is named %s, value is %s", rowCount, psi->getFieldName(1), (*psi)[(size_t) 1]->toString()->asChar()); } // Execute a Region Shortcut Query (convenience method). resultsPtr = regionPtr->query("ID = 2"); LOGINFO("Region Query returned %d rows", resultsPtr->size()); // Execute the Region selectValue() API. SerializablePtr resultPtr = regionPtr->selectValue("ID = 3"); PortfolioPtr portPtr = dynCast(resultPtr); LOGINFO("Region selectValue() returned an item:\n %s", portPtr->toString()->asChar()); // Execute the Region existsValue() API. bool existsValue = regionPtr->existsValue("ID = 4"); LOGINFO("Region existsValue() returned %s", existsValue ? "true" : "false"); // Close the GemFire Cache. cachePtr->close(); LOGINFO("Closed the GemFire Cache"); // Disconnect from the GemFire Distributed System. dSysPtr->disconnect(); LOGINFO("Disconnected from the GemFire Distributed System"); } // An exception should not occur catch(const Exception & gemfireExcp) { LOGERROR("PoolRemoteQuery GemFire Exception: %s", gemfireExcp.getMessage()); } }