Exemple #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);
            }
        }
Exemple #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);
            }
        }