static void Main(string[] args) { try { // Create a Geode Cache Programmatically. CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(); Cache cache = cacheFactory.SetSubscriptionEnabled(true).Create(); Console.WriteLine("Created the Geode Cache"); RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY); // Create the example Region programmatically. IRegion <string, Portfolio> region = regionFactory.Create <string, Portfolio>("Portfolios"); Console.WriteLine("Created the Region Programmatically."); // Register our Serializable/Cacheable Query objects, viz. Portfolio and Position. Serializable.RegisterTypeGeneric(Portfolio.CreateDeserializable); Serializable.RegisterTypeGeneric(Position.CreateDeserializable); Console.WriteLine("Registered Serializable Query Objects"); // Populate the Region with some Portfolio objects. Portfolio port1 = new Portfolio(1 /*ID*/, 10 /*size*/); Portfolio port2 = new Portfolio(2 /*ID*/, 20 /*size*/); Portfolio port3 = new Portfolio(3 /*ID*/, 30 /*size*/); region["Key1"] = port1; region["Key2"] = port2; region["Key3"] = port3; Console.WriteLine("Populated some Portfolio Objects"); // Get the QueryService from the Cache. QueryService <string, Portfolio> qrySvc = cache.GetQueryService <string, Portfolio>(); Console.WriteLine("Got the QueryService from the Cache"); // Execute a Query which returns a ResultSet. Query <Portfolio> qry = qrySvc.NewQuery("SELECT DISTINCT * FROM /Portfolios"); ISelectResults <Portfolio> results = qry.Execute(); Console.WriteLine("ResultSet Query returned {0} rows", results.Size); // Execute a Query which returns a StructSet. QueryService <string, Struct> qrySvc1 = cache.GetQueryService <string, Struct>(); Query <Struct> qry1 = qrySvc1.NewQuery("SELECT DISTINCT ID, status FROM /Portfolios WHERE ID > 1"); ISelectResults <Struct> results1 = qry1.Execute(); Console.WriteLine("StructSet Query returned {0} rows", results1.Size); // Iterate through the rows of the query result. int rowCount = 0; foreach (Struct si in results1) { rowCount++; Console.WriteLine("Row {0} Column 1 is named {1}, value is {2}", rowCount, si.Set.GetFieldName(0), si[0].ToString()); Console.WriteLine("Row {0} Column 2 is named {1}, value is {2}", rowCount, si.Set.GetFieldName(0), si[1].ToString()); } // Execute a Region Shortcut Query (convenience method). results = region.Query <Portfolio>("ID = 2"); Console.WriteLine("Region Query returned {0} rows", results.Size); // Execute the Region selectValue() API. object result = region.SelectValue("ID = 3"); Console.WriteLine("Region selectValue() returned an item:\n {0}", result.ToString()); // Execute the Region existsValue() API. bool existsValue = region.ExistsValue("ID = 4"); Console.WriteLine("Region existsValue() returned {0}", existsValue ? "true" : "false"); //Execute the parameterized query //Populate the parameter list (paramList) for the query. //TODO:remove once query service is generic QueryService <string, Struct> pqrySvc = cache.GetQueryService <string, Struct>(); Query <Struct> pquery = pqrySvc.NewQuery("SELECT DISTINCT ID, status FROM /Portfolios WHERE ID > $1 and status=$2"); object[] paramList = new object[2]; paramList[0] = 1; //param-1 paramList[1] = "active"; //param-2 ISelectResults <Struct> pqresults = pquery.Execute(paramList); Console.WriteLine("Parameterized Query returned {0} rows", pqresults.Size); // Iterate through the rows of the query result. rowCount = 0; foreach (Struct st in pqresults) { rowCount++; Console.WriteLine("Row {0} Column 1 is named {1}, value is {2}", rowCount, st.Set.GetFieldName(0), st[0].ToString()); Console.WriteLine("Row {0} Column 2 is named {1}, value is {2}", rowCount, st.Set.GetFieldName(0), st[1].ToString()); } // Close the Geode Cache. cache.Close(); Console.WriteLine("Closed the Geode Cache"); } // An exception should not occur catch (GeodeException gfex) { Console.WriteLine("RemoteQuery Geode Exception: {0}", gfex.Message); } }
public void StepFourRQ() { bool ErrorOccurred = false; IRegion <object, object> region = CacheHelper.GetRegion <object, object>(QueryRegionNames[0]); int qryIdx = 0; foreach (QueryStrings qrystr in QueryStatics.RegionQueries) { if (qrystr.Category == QueryCategory.Unsupported) { Util.Log("Skipping query index {0} because it is unsupported.", qryIdx); qryIdx++; continue; } Util.Log("Evaluating query index {0}.{1}", qryIdx, qrystr.Query); bool existsValue = region.ExistsValue(qrystr.Query); bool expectedResult = QueryStatics.RegionQueryRowCounts[qryIdx] > 0 ? true : false; if (existsValue != expectedResult) { ErrorOccurred = true; Util.Log("FAIL: Query # {0} existsValue expected is {1}, actual is {2}", qryIdx, expectedResult ? "true" : "false", existsValue ? "true" : "false"); qryIdx++; continue; } qryIdx++; } Assert.IsFalse(ErrorOccurred, "One or more query validation errors occurred."); try { bool existsValue = region.ExistsValue(""); Assert.Fail("Expected IllegalArgumentException exception for empty predicate"); } catch (IllegalArgumentException ex) { Util.Log("got expected IllegalArgumentException exception for empty predicate:"); Util.Log(ex.Message); } try { bool existsValue = region.ExistsValue(QueryStatics.RegionQueries[0].Query, TimeSpan.FromSeconds(2200000)); Assert.Fail("Expected IllegalArgumentException exception for invalid timeout"); } catch (IllegalArgumentException ex) { Util.Log("got expected IllegalArgumentException exception for invalid timeout:"); Util.Log(ex.Message); } try { bool existsValue = region.ExistsValue("bad predicate"); Assert.Fail("Expected QueryException exception for wrong predicate"); } catch (QueryException ex) { Util.Log("got expected QueryException exception for wrong predicate:"); Util.Log(ex.Message); } }
static void Main(string[] args) { try { CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(); Console.WriteLine("Connected to the Geode Distributed System"); // Create a Geode Cache with the "clientPdxRemoteQuery.xml" Cache XML file. Cache cache = cacheFactory.Set("cache-xml-file", "XMLs/clientPdxRemoteQuery.xml").Create(); Console.WriteLine("Created the Geode Cache"); // Get the example Region from the Cache which is declared in the Cache XML file. IRegion <string, PortfolioPdx> region = cache.GetRegion <string, PortfolioPdx>("Portfolios"); Console.WriteLine("Obtained the Region from the Cache"); // Register our Serializable/Cacheable Query objects, viz. Portfolio and Position. Serializable.RegisterPdxType(PortfolioPdx.CreateDeserializable); Serializable.RegisterPdxType(PositionPdx.CreateDeserializable); Console.WriteLine("Registered Serializable Query Objects"); // Populate the Region with some PortfolioPdx objects. PortfolioPdx port1 = new PortfolioPdx(1 /*ID*/, 10 /*size*/); PortfolioPdx port2 = new PortfolioPdx(2 /*ID*/, 20 /*size*/); PortfolioPdx port3 = new PortfolioPdx(3 /*ID*/, 30 /*size*/); region["Key1"] = port1; region["Key2"] = port2; region["Key3"] = port3; Console.WriteLine("Populated some PortfolioPdx Objects"); //find the pool Pool pool = PoolManager.Find("examplePool"); // Get the QueryService from the pool QueryService <string, PortfolioPdx> qrySvc = pool.GetQueryService <string, PortfolioPdx>(); Console.WriteLine("Got the QueryService from the Pool"); // Execute a Query which returns a ResultSet. Query <PortfolioPdx> qry = qrySvc.NewQuery("SELECT DISTINCT * FROM /Portfolios"); ISelectResults <PortfolioPdx> results = qry.Execute(); Console.WriteLine("ResultSet Query returned {0} rows", results.Size); // Execute a Query which returns a StructSet. QueryService <string, Struct> qrySvc1 = pool.GetQueryService <string, Struct>(); Query <Struct> qry1 = qrySvc1.NewQuery("SELECT DISTINCT id, status FROM /Portfolios WHERE id > 1"); ISelectResults <Struct> results1 = qry1.Execute(); Console.WriteLine("StructSet Query returned {0} rows", results1.Size); // Iterate through the rows of the query result. int rowCount = 0; foreach (Struct si in results1) { rowCount++; Console.WriteLine("Row {0} Column 1 is named {1}, value is {2}", rowCount, si.Set.GetFieldName(0), si[0].ToString()); Console.WriteLine("Row {0} Column 2 is named {1}, value is {2}", rowCount, si.Set.GetFieldName(1), si[1].ToString()); } // Execute a Region Shortcut Query (convenience method). results = region.Query <PortfolioPdx>("id = 2"); Console.WriteLine("Region Query returned {0} rows", results.Size); // Execute the Region selectValue() API. object result = region.SelectValue("id = 3"); Console.WriteLine("Region selectValue() returned an item:\n {0}", result.ToString()); // Execute the Region existsValue() API. bool existsValue = region.ExistsValue("id = 4"); Console.WriteLine("Region existsValue() returned {0}", existsValue ? "true" : "false"); // Close the Geode Cache. cache.Close(); Console.WriteLine("Closed the Geode Cache"); } // An exception should not occur catch (GeodeException gfex) { Console.WriteLine("PdxRemoteQuery Geode Exception: {0}", gfex.Message); } }