Example #1
0
        static void Main(string[] args)
        {
            try
            {
                // Create a Geode Cache Programmatically.
                CacheFactory cacheFactory = CacheFactory.CreateCacheFactory();
                Cache        cache        = cacheFactory.SetSubscriptionEnabled(true)
                                            .AddServer("localhost", 50505)
                                            .Create();

                Console.WriteLine("Created the Geode Cache");

                RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.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, object> qrySvc = cache.GetQueryService <string, object>();

                Console.WriteLine("Got the QueryService from the Cache");

                //create CqAttributes with listener
                CqAttributesFactory <string, object> cqFac    = new CqAttributesFactory <string, object>();
                ICqListener <string, object>         cqLstner = new MyCqListener <string, object>();
                cqFac.AddCqListener(cqLstner);
                CqAttributes <string, object> cqAttr = cqFac.Create();

                //create a new cqQuery
                CqQuery <string, object> qry = qrySvc.NewCq("MyCq", "select * from /Portfolios" + "  p where p.ID!=2", cqAttr, false);

                // Execute a CqQuery with Initial Results
                ICqResults <object> results = qry.ExecuteWithInitialResults();

                Console.WriteLine("ResultSet Query returned {0} rows", results.Size);
                //make changes to generate cq events
                region["Key2"] = port1;
                region["Key3"] = port2;
                region["Key1"] = port3;

                SelectResultsIterator <object> iter = results.GetIterator();

                while (iter.HasNext)
                {
                    object item = iter.Next();

                    if (item != null)
                    {
                        Struct st = item as Struct;

                        string key = st["key"] as string;

                        Console.WriteLine("Got key " + key);

                        Portfolio port = st["value"] as Portfolio;

                        if (port == null)
                        {
                            Position pos = st["value"] as Position;
                            if (pos == null)
                            {
                                string cs = st["value"] as string;
                                if (cs == null)
                                {
                                    Console.WriteLine("Query got other/unknown object.");
                                }
                                else
                                {
                                    Console.WriteLine("Query got string : {0}.", cs);
                                }
                            }
                            else
                            {
                                Console.WriteLine("Query got Position object with secId {0}, shares {1}.", pos.SecId, pos.SharesOutstanding);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Query got Portfolio object with ID {0}, pkid {1}.", port.ID, port.Pkid);
                        }
                    }
                }
                //Stop the cq
                qry.Stop();

                //Close the cq
                qry.Close();

                // Close the Geode Cache.
                cache.Close();

                Console.WriteLine("Closed the Geode Cache");
            }
            // An exception should not occur
            catch (GeodeException gfex)
            {
                Console.WriteLine("CqQuery Geode Exception: {0}", gfex.Message);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            try
            {
                //Create CacheFactory using the user specified properties or from the gfcpp.properties file by default.
                Properties <string, string> prp = Properties <string, string> .Create <string, string>();

                prp.Insert("cache-xml-file", "XMLs/clientPoolCqQuery.xml");
                CacheFactory cacheFactory = CacheFactory.CreateCacheFactory(prp);

                Console.WriteLine("Created CacheFactory");

                // Create a Geode Cache with the "clientPoolCqQuery.xml" Cache XML file.
                Cache cache = cacheFactory.Create();

                Console.WriteLine("Created the Geode Cache");

                // Get the Portfolios Region from the Cache which is declared in the Cache XML file.
                IRegion <string, Portfolio> region = cache.GetRegion <string, Portfolio>("Portfolios");

                Console.WriteLine("Obtained the Region from the Cache");

                // 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");

                Pool pp = PoolManager.Find("examplePool");

                // Get the QueryService from the Pool
                QueryService <string, object> qrySvc = pp.GetQueryService <string, object>();

                Console.WriteLine("Got the QueryService from the Cache");

                //create CqAttributes with listener
                CqAttributesFactory <string, object> cqFac    = new CqAttributesFactory <string, object>();
                ICqListener <string, object>         cqLstner = new MyCqListener <string, object>();
                cqFac.AddCqListener(cqLstner);
                CqAttributes <string, object> cqAttr = cqFac.Create();

                //create a new cqQuery
                CqQuery <string, object> qry = qrySvc.NewCq("MyCq", "select * from /Portfolios" + "  p where p.ID!=2", cqAttr, false);

                // Execute a CqQuery with Initial Results
                ICqResults <object> results = qry.ExecuteWithInitialResults();

                Console.WriteLine("ResultSet Query returned {0} rows", results.Size);
                //make changes to generate cq events
                region["Key2"] = port1;
                region["Key3"] = port2;
                region["Key1"] = port3;

                SelectResultsIterator <object> iter = results.GetIterator();

                while (iter.HasNext)
                {
                    object item = iter.Next();

                    if (item != null)
                    {
                        Struct st  = item as Struct;
                        string key = st["key"] as string;;
                        Console.WriteLine("Got key " + key);
                        Portfolio port = st["value"] as Portfolio;
                        if (port == null)
                        {
                            Position pos = st["value"] as Position;
                            if (pos == null)
                            {
                                string cs = st["value"] as string;
                                if (cs == null)
                                {
                                    Console.WriteLine("Query got other/unknown object.");
                                }
                                else
                                {
                                    Console.WriteLine("Query got string : {0}.", cs);
                                }
                            }
                            else
                            {
                                Console.WriteLine("Query got Position object with secId {0}, shares {1}.", pos.SecId, pos.SharesOutstanding);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Query got Portfolio object with ID {0}, pkid {1}.", port.ID, port.Pkid);
                        }
                    }
                }
                //Stop the cq
                qry.Stop();

                //Close the cq
                qry.Close();

                // Close the Geode Cache.
                cache.Close();

                Console.WriteLine("Closed the Geode Cache");
            }
            // An exception should not occur
            catch (GeodeException gfex)
            {
                Console.WriteLine("PoolCqQuery Geode Exception: {0}", gfex.Message);
            }
        }
        public void StepOnePdxQE(string locators)
        {
            CacheHelper.CreateTCRegion_Pool <object, object>(QERegionName, true, true,
                                                             null, locators, "__TESTPOOL1_", true);
            IRegion <object, object> region = CacheHelper.GetVerifyRegion <object, object>(QERegionName);
            PortfolioPdx             p1     = new PortfolioPdx(1, 100);
            PortfolioPdx             p2     = new PortfolioPdx(2, 100);
            PortfolioPdx             p3     = new PortfolioPdx(3, 100);
            PortfolioPdx             p4     = new PortfolioPdx(4, 100);

            region["1"] = p1;
            region["2"] = p2;
            region["3"] = p3;
            region["4"] = p4;

            var qs = CacheHelper.DCache.GetPoolManager().Find("__TESTPOOL1_").GetQueryService();
            CqAttributesFactory <object, object> cqFac    = new CqAttributesFactory <object, object>();
            ICqListener <object, object>         cqLstner = new MyCqListener <object, object>();

            cqFac.AddCqListener(cqLstner);
            CqAttributes <object, object> cqAttr = cqFac.Create();
            CqQuery <object, object>      qry    = qs.NewCq(CqName, "select * from /" + QERegionName + "  p where p.ID!=2", cqAttr, false);

            qry.Execute();
            Thread.Sleep(18000); // sleep 0.3min to allow server c query to complete
            region["4"] = p1;
            region["3"] = p2;
            region["2"] = p3;
            region["1"] = p4;
            Thread.Sleep(18000); // sleep 0.3min to allow server c query to complete

            qry = qs.GetCq <object, object>(CqName);

            CqServiceStatistics cqSvcStats = qs.GetCqStatistics();

            Assert.AreEqual(1, cqSvcStats.numCqsActive());
            Assert.AreEqual(1, cqSvcStats.numCqsCreated());
            Assert.AreEqual(1, cqSvcStats.numCqsOnClient());

            cqAttr = qry.GetCqAttributes();
            ICqListener <object, object>[] vl = cqAttr.getCqListeners();
            Assert.IsNotNull(vl);
            Assert.AreEqual(1, vl.Length);
            cqLstner = vl[0];
            Assert.IsNotNull(cqLstner);
            MyCqListener <object, object> myLisner = (MyCqListener <object, object>)cqLstner;// as MyCqListener<object, object>;

            Util.Log("event count:{0}, error count {1}.", myLisner.getEventCountBefore(), myLisner.getErrorCountBefore());

            CqStatistics cqStats = qry.GetStatistics();

            Assert.AreEqual(cqStats.numEvents(), myLisner.getEventCountBefore());
            if (myLisner.getEventCountBefore() + myLisner.getErrorCountBefore() == 0)
            {
                Assert.Fail("cq before count zero");
            }
            qry.Stop();
            Assert.AreEqual(1, cqSvcStats.numCqsStopped());
            qry.Close();
            Assert.AreEqual(1, cqSvcStats.numCqsClosed());
            // Bring down the region
            region.GetLocalView().DestroyRegion();
        }
Example #4
0
        public void StepOneQE(string locators)
        {
            CacheHelper.CreateTCRegion_Pool <object, object>(QERegionName, true, true,
                                                             null, locators, "__TESTPOOL1_", true);

            IRegion <object, object> region = CacheHelper.GetVerifyRegion <object, object>(QERegionName);
            Portfolio p1 = new Portfolio(1, 100);
            Portfolio p2 = new Portfolio(2, 100);
            Portfolio p3 = new Portfolio(3, 100);
            Portfolio p4 = new Portfolio(4, 100);

            region["1"] = p1;
            region["2"] = p2;
            region["3"] = p3;
            region["4"] = p4;

            var qs = CacheHelper.DCache.GetPoolManager().Find("__TESTPOOL1_").GetQueryService();
            CqAttributesFactory <object, object> cqFac    = new CqAttributesFactory <object, object>();
            ICqListener <object, object>         cqLstner = new MyCqListener <object, object>();

            cqFac.AddCqListener(cqLstner);
            CqAttributes <object, object> cqAttr  = cqFac.Create();
            CqQuery <object, object>      qry     = qs.NewCq(CqName, "select * from /" + QERegionName + "  p where p.ID!=2", cqAttr, false);
            ICqResults <object>           results = qry.ExecuteWithInitialResults();

            Thread.Sleep(18000); // sleep 0.3min to allow server c query to complete
            region["4"] = p1;
            region["3"] = p2;
            region["2"] = p3;
            region["1"] = p4;
            Thread.Sleep(18000); // sleep 0.3min to allow server c query to complete
            Util.Log("Results size {0}.", results.Size);

            foreach (var item in results)
            {
                if (item != null)
                {
                    Struct st = item as Struct;

                    string key = st["key"] as string;

                    Assert.IsNotNull(key, "key is null");

                    Portfolio port = st["value"] as Portfolio;

                    if (port == null)
                    {
                        Position pos = st["value"] as Position;
                        if (pos == null)
                        {
                            string cs = item as string;

                            if (cs == null)
                            {
                                Assert.Fail("value is null");
                                Util.Log("Query got other/unknown object.");
                            }
                            else
                            {
                                Util.Log("Query got string : {0}.", cs);
                            }
                        }
                        else
                        {
                            Util.Log("Query got Position object with secId {0}, shares {1}.", pos.SecId, pos.SharesOutstanding);
                        }
                    }
                    else
                    {
                        Util.Log("Query got Portfolio object with ID {0}, pkid {1}.", port.ID, port.Pkid);
                    }
                }
            }
            qry = qs.GetCq <object, object>(CqName);
            qry.Stop();
            qry.Close();
            // Bring down the region
            region.GetLocalView().DestroyRegion();
        }