Ejemplo n.º 1
0
        // Waits for input of a command (chrgn, get, put, putAll, exec, query,
        // existsvalue, selectvalue or quit), then does just that.
        public static void DoCommand()
        {
            string myCmd = string.Empty;
            string myKey;
            string myValue;

            while (myCmd != "quit")
            {
                Console.Write("{0}: chrgn, lsrgn, get, put, putAll, run, exec, query, " +
                              "existsvalue, selectvalue, quit: ", m_region.FullPath);

                string   strIn    = Console.ReadLine().Trim();
                string[] strSplit = strIn.Split(' ');

                myCmd = strSplit[0];
                myCmd = myCmd.ToLower();

                if (myCmd == "q")
                {
                    myCmd = "quit";
                }

                switch (myCmd)
                {
                case "chrgn":
                    if (strSplit.Length == 2)
                    {
                        string regionPath = strSplit[1].Trim();
                        Region region     = null;
                        try
                        {
                            region = m_cache.GetRegion(regionPath);
                        }
                        catch
                        {
                        }
                        if (region == null)
                        {
                            Console.WriteLine("No region {0} found.", regionPath);
                            Console.WriteLine("usage: chrgn region-path");
                        }
                        else
                        {
                            SetRegion(region);
                        }
                    }
                    else
                    {
                        Console.WriteLine("usage: chrgn region-path \t change the " +
                                          "current region to the given region; the region-path can " +
                                          "be absolute or relative to the current region");
                    }
                    break;

                case "lsrgn":
                    Region[] rootRegions = m_cache.RootRegions();
                    Console.Write("\nNumber of regions in Cache: {0}\n", rootRegions.Length);
                    int count = 1;
                    for (int rgnCnt = 0; rgnCnt < rootRegions.Length; rgnCnt++)
                    {
                        Console.Write("Region Name {0}: {1}\n", count++, rootRegions[rgnCnt].Name);
                    }
                    break;

                case "run":
                    if (strSplit.Length == 3)
                    {
                        string myNumber = strSplit[1];
                        string mySize   = strSplit[2];
                        int    number   = int.Parse(myNumber);
                        int    size     = int.Parse(mySize);
                        byte[] payload  = new byte[size];

                        CacheableHashMap map = new CacheableHashMap();
                        int ts1 = getTS(System.DateTime.Now);
                        for (int i = 0; i < number; i++)
                        {
                            if (size == 0)
                            {
                                map.Add(new CacheableInt32(i), new ExampleObject(i));
                            }
                            else
                            {
                                map.Add(CacheableInt32.Create(i),
                                        CacheableBytes.Create(payload));
                            }
                        }
                        m_region.PutAll(map);
                        int    ts2   = getTS(System.DateTime.Now);
                        Double run_t = (ts2 - ts1) / 1000.0;
                        Console.WriteLine("Ops/sec: {0}, {1}, {2}", number / run_t, ts1, ts2);
                    }
                    else
                    {
                        Console.WriteLine("usage: run numOfKeys entrySize");
                    }
                    break;

                case "putall":
                    if (strSplit.Length == 3)
                    {
                        myKey   = strSplit[1];
                        myValue = strSplit[2];
                        int num = int.Parse(myValue);

                        //byte[] payload = new byte[10];
                        //for (int index = 0; index < 10; index++)
                        //{
                        //payload[index] = 1;
                        //}

                        Console.WriteLine("putAll: " + myKey + ":" + num);

                        CacheableHashMap map = new CacheableHashMap();
                        map.Clear();
                        for (int i = 0; i < num; i++)
                        {
                            string key = myKey + i;
                            // map.Add(new CacheableString(key), (CacheableBytes)(payload));
                            map.Add(new CacheableString(key), new ExampleObject(i));
                            // map.Add(new CacheableString(key), new Position(i));
                        }
                        m_region.PutAll(map);
                    }
                    else
                    {
                        Console.WriteLine("usage: putAll keyBase numOfKeys");
                    }
                    break;

                case "put":
                    if (strSplit.Length == 3)
                    {
                        myKey   = strSplit[1];
                        myValue = strSplit[2];

                        // Check to see if value is ComplexNumber or String
                        ComplexNumber cNum = ComplexNumber.Parse(myValue);

                        if (cNum != null)
                        {
                            // Put the key and value
                            PutComplex(myKey, cNum);
                        }
                        else
                        {
                            // Put the key and value
                            PutStr(myKey, myValue);
                        }
                    }
                    else if (strSplit.Length == 4)
                    {
                        myKey   = strSplit[1];
                        myValue = strSplit[2];
                        string type = strSplit[3];
                        type = type.ToLower();

                        switch (type)
                        {
                        case "str":
                            PutStr(myKey, myValue);
                            break;

                        case "bytes":
                            PutBytes(myKey, myValue);
                            break;

                        case "complex":
                            ComplexNumber cNum = ComplexNumber.Parse(myValue);
                            if (cNum != null)
                            {
                                PutComplex(myKey, cNum);
                            }
                            else
                            {
                                Console.WriteLine("Could not parse the complex number.");
                            }
                            break;

                        case "double":
                            double dval = 0.0;
                            try
                            {
                                dval = double.Parse(myValue);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Double value [{0}] not valid: {1}.", myValue, ex);
                                break;
                            }
                            PutObject(myKey, new CacheableDouble(dval));
                            break;

                        case "float":
                            float fval = 0.0F;
                            try
                            {
                                fval = float.Parse(myValue);
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Float value [{0}] not valid: {1}.", myValue, ex);
                                break;
                            }
                            PutObject(myKey, new CacheableFloat(fval));
                            break;

                        case "obj":
                            string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                            xmlStr += "<IndexMessage xmlns=\"urn:example.com:FinanceManager.Messages:v1.0\">";
                            xmlStr += "<Keys>11</Keys><Keys>22</Keys><Keys>33</Keys></IndexMessage>";

                            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
                            xd.LoadXml(xmlStr);
                            PutObject(myKey, CacheableObjectXml.Create(xd));
                            break;

                        case "port":
                            int id = 0;
                            try
                            {
                                id = int.Parse(myValue);
                            }
                            catch
                            {
                                Console.WriteLine("Portfolio value [{0}] not a valid"
                                                  + " integer ID.", myValue);
                                break;
                            }
                            PutPortfolio(myKey, id);
                            break;

                        case "pos":
                            int id2 = 0;
                            try
                            {
                                id2 = int.Parse(myValue);
                            }
                            catch
                            {
                                Console.WriteLine("Position value [{0}] not a valid"
                                                  + " integer ID.", myValue);
                                break;
                            }
                            PutPosition(myKey, id2);
                            break;

                        default:
                            Console.WriteLine("usage: put key value [str|bytes|complex|double|float|obj|port|pos]");
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("usage: put key value [str|bytes|complex|double|float|obj|port|pos]");
                    }
                    break;

                case "get":
                    if (strSplit.Length == 2)
                    {
                        myKey = strSplit[1];

                        // Get the value
                        string xStr = GetStr(myKey);
                    }
                    else
                    {
                        Console.WriteLine("usage: get key");
                    }

                    break;

                case "exec":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        RunQuery(query, false);
                    }
                    else
                    {
                        Console.WriteLine("usage: exec <select query string>");
                    }

                    break;

                case "query":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        RunQuery(query, true);
                    }
                    else
                    {
                        Console.WriteLine("usage: query <query predicate>");
                    }

                    break;

                case "existsvalue":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        ExistsValue(query);
                    }
                    else
                    {
                        Console.WriteLine("usage: existsvalue <query predicate>");
                    }

                    break;

                case "selectvalue":
                    if (strSplit.Length > 1)
                    {
                        string query = string.Empty;
                        for (int index = 1; index < strSplit.Length; index++)
                        {
                            query += strSplit[index] + ' ';
                        }
                        query = query.Trim();
                        SelectValue(query);
                    }
                    else
                    {
                        Console.WriteLine("usage: selectvalue <query predicate>");
                    }

                    break;
                }
            }
        }
Ejemplo n.º 2
0
 // Puts a string key and complex number value
 public static void PutComplex(string key, ComplexNumber value)
 {
     m_region.Put(key, value);
     Console.WriteLine("Put complex -- key: " + key + " value: " + value);
 }