// Method to retrieve database from XML file
 public void XMLProcessing()
 {
     "  Retrieving key value pairs from the persisted database XML file and adding them back to <int, string> database".Wrap();
     IntKeyDB = XMLtoIntDB();
     "  Adding elements back to the dictionary from XML file".Wrap();
     "  Displaying current state of the databases".Wrap();
 }
示例#2
0
 // Code Analyzer shows that its complexity is 17 but it doesn't look like to me. Please comment if there is any scope of reducing the complexity
 public static string AddElementSL(this DatabaseDictionary <string, Element <string, List <string> > > Temp) //Method to add a random generated elment in a DB
 {
     try
     {
         string        Key    = Temp.SKeygenerator();
         Random        Random = new Random();
         List <string> Names  = new List <string> {
             "Megadeath", "Pink Floyd", "Porcupine Tree", "Bahramji", "RHCP"
         };
         List <List <string> > Relation = new List <List <string> > {
             new List <string> {
                 "Key1"
             }, new List <string> {
                 "Key2", "Key3"
             }, new List <string> {
                 "Key3", "Key1"
             }, new List <string> {
                 "Key4"
             }, new List <string> {
                 "Key4", "Key2"
             }
         };
         List <List <string> > Data = new List <List <string> > {
             new List <string> {
                 "Opeth"
             }, new List <string> {
                 "Animals"
             }, new List <string> {
                 "Sunset"
             }, new List <string> {
                 "Chillout"
             }, new List <string> {
                 "New Era"
             }
         };
         if (Key != "" || Key != null)
         {
             Element <string, List <string> > TempE = new Element <string, List <string> >();
             TempE.NAME        = Names[Random.Next(0, Names.Count)];
             TempE.DESCRIPTION = "Rockband";
             TempE.TIMESTAMP   = DateTime.Now;
             TempE.RELATIONS   = Relation[Random.Next(0, Relation.Count)];
             TempE.DATA        = Data[Random.Next(0, Data.Count)];
             Temp.AddPair(Key, TempE);
             Console.WriteLine("  New element added");
             return("New element added");
         }
         Console.WriteLine("  New element was not added");
         return("Element was not added");
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return(e.Message);
     }
 }
 void DemoR2()
 {
     "\n  Demonstrating Requirement #2".Wrap();
     "  Persisting previously created databases from XML files".Wrap();
     "  Displaying contents of XML file of persisted database of type Int-String".Wrap();
     DisplayXMLfile();
     "  Displaying current state of the database".Wrap();
     IntKeyDB = XMLtoIntDB();
     Console.WriteLine(IntKeyDB.DisplayKVP <int, Element <int, string>, string>());
 }
示例#4
0
        public static string EditElementSL(this DatabaseDictionary <string, Element <string, List <string> > > Temp, List <string> ParameterSelected, List <string> ParameterValue)  //Method to edit any information in an elment of DB
        {
            try
            {
                string Param;
                if (ParameterSelected.Last().StartsWith("Metadata"))
                {
                    Param = ParameterSelected.Last().Substring(8);
                }
                else
                {
                    Param = ParameterSelected.Last();
                }

                string Key = ParameterValue.First();
                if (Key != null)
                {
                    Element <string, List <string> > TempE;
                    if (Temp.RetrieveValue(Key, out TempE))
                    {
                        if (Param == "Name")
                        {
                            TempE.NAME = ParameterValue[1];
                        }
                        if (Param == "Description")
                        {
                            TempE.DESCRIPTION = ParameterValue[1];
                        }
                        if (Param == "Children")
                        {
                            List <string> relations = new List <string>();
                            relations.Add(ParameterValue[1]);
                            TempE.RELATIONS = relations;
                        }
                        if (Param == "Data")
                        {
                            List <string> values = new List <string>();
                            values.Add(ParameterValue[1]);
                            TempE.DATA = values;
                        }
                    }
                    return("Modified the element");
                }
                else
                {
                    return("Couldn't modify the element. Key not found");
                }
            }
            catch (Exception)
            {
                return("Couldn't process the query");
            }
        }
示例#5
0
 public static string EditElementIS(this DatabaseDictionary <int, Element <int, string> > Temp, List <string> ParameterSelected, List <string> ParameterValue)   //Method to edit any information in an elment of DB
 {
     try
     {
         int    Key = int.Parse(ParameterValue.First());
         string Param;
         if (ParameterSelected.Last().StartsWith("Metadata"))
         {
             Param = ParameterSelected.Last().Substring(8);
         }
         else
         {
             Param = ParameterSelected.Last();
         }
         if (Key != 0)
         {
             Element <int, string> TempE;
             if (Temp.RetrieveValue(Key, out TempE))
             {
                 if (Param == "Name")
                 {
                     TempE.NAME = ParameterValue[1];
                 }
                 if (Param == "Description")
                 {
                     TempE.DESCRIPTION = ParameterValue[1];
                 }
                 if (Param == "Children")
                 {
                     List <int> relations = new List <int>();
                     relations.Add(int.Parse(ParameterValue[1]));
                     TempE.RELATIONS = relations;
                 }
                 if (Param == "Data")
                 {
                     TempE.DATA = ParameterValue[1];
                 }
             }
             Console.WriteLine("  Modified the element");
             return("Modified the element");
         }
         else
         {
             Console.WriteLine("  Couldn't modify the element");
             return("Couldn't modify the element. Key not found");
         }
     }
     catch (Exception)
     {
         Console.WriteLine("  Couldn't modify the element");
         return("Couldn't process the query");
     }
 }
示例#6
0
        public static List <int> FindMatchingKeys(this DatabaseDictionary <int, Element <int, string> > Temp, string K) //Method to retrieve keys using a pattern
        {
            List <int> result = new List <int>();

            foreach (var key in Temp.Keys().ToList())
            {
                if (key.ToString().Contains(K))
                {
                    result.Add(key);
                }
            }
            return(result);
        }
示例#7
0
        public static List <int> FindKeysbyData(this DatabaseDictionary <int, Element <int, string> > Temp, string V)  ////Method to retrieve keys using data parameter
        {
            List <int> LK = new List <int>();

            foreach (var k in Temp.Keys())
            {
                if (Temp.RetrieveValue(k).DATA.Contains(V))
                {
                    LK.Add(k);
                }
            }
            return(LK);
        }
        public void DisplayDBafterProcessing()
        {
            "\n\n  Database is persisted at the end of every read client automatically.".Wrap();
            "\n  Although order and number of queries to persist database can be changed by editing XML file of MessageStream in the respective client folders".Wrap();
            "\n  Once all processing is done, XML of persisted database is created in the parent folder of project titled as PersistedDB.xml".Wrap();
            "\n  In order to demonstrate the requirement to persist and reload database from XML, this project persists the latest state of database and reloads it".Wrap();
            "\n  Press any key to retrieve the last state of processed database".Wrap();
            Console.ReadKey();
            DatabaseDictionary <int, Element <int, string> > AfterProcessing = new DatabaseDictionary <int, Element <int, string> >();

            AfterProcessing = XMLtoDBAfterProcessing();
            Console.WriteLine(AfterProcessing.DisplayKVP <int, Element <int, string>, string>());
        }
示例#9
0
        public static string DisplayKVP <Key, Value, Data, T>(this DatabaseDictionary <Key, Value> TempDB) where Data : IEnumerable <T>   // Method to display the state of a DB if Data is inemurable
        {
            StringBuilder KVP = new StringBuilder();

            foreach (Key k in TempDB.Keys())
            {
                KVP.Append("  Key: " + k);
                Value V = TempDB.RetrieveValue(k);
                Element <Key, Data> Tempelement = V as Element <Key, Data>;
                KVP.Append("\n" + Tempelement.DisplayData <Key, Data, T>());
                KVP.Append("\n\n");
            }
            return(KVP.ToString());
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("Testing Database Dictionary class" + "\n");
            DatabaseDictionary <int, Element <int, string> > d = new DatabaseDictionary <int, Element <int, string> >();
            Element <int, string> IKElement1 = new Element <int, string>();

            IKElement1.NAME = "Pink Floyd"; IKElement1.DESCRIPTION = "Rockband"; IKElement1.TIMESTAMP = DateTime.Now; IKElement1.RELATIONS = new List <int> {
                1, 2
            }; IKElement1.DATA = "Wish you were here";
            d.AddPair(1, IKElement1);
            //Above code is there to show how coding of adding an element in a database would look like.

            Console.WriteLine("Databases can't be tested in this project as most of the testing depends on ExtensionMethod which contain all extension methods.");
            Console.WriteLine("To avoid circular dependency, all tests are performed in DatabaseDictionaryTest project");
        }
示例#11
0
        public static List <int> FindKeysbyMetadata(this DatabaseDictionary <int, Element <int, string> > Temp, string Parameter, string V)  ////Method to retrieve keys using a metadata parameter
        {
            List <int> LK = new List <int>();

            foreach (var k in Temp.Keys())
            {
                if (Parameter == "Name")
                {
                    if (Temp.RetrieveValue(k).NAME.Equals(V))
                    {
                        LK.Add(k);
                    }
                }

                if (Parameter == "Description")
                {
                    if (Temp.RetrieveValue(k).DESCRIPTION.Equals(V))
                    {
                        LK.Add(k);
                    }
                }

                if (Parameter == "Timestamp")
                {
                    try
                    {
                        DateTime V1 = Convert.ToDateTime(V);
                        if ((Temp.RetrieveValue(k).TIMESTAMP.CompareTo(V1) >= 0) && (Temp.RetrieveValue(k).TIMESTAMP.CompareTo(DateTime.Now) <= 0))
                        {
                            LK.Add(k);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error in converting datetime. Enter correct format" + e.Message);
                    }
                }

                if (Parameter == "Children")
                {
                    if (Temp.RetrieveValue(k).RELATIONS.Contains(int.Parse(V)))
                    {
                        LK.Add(k);
                    }
                }
            }
            return(LK);
        }
        // To persist int-string database
        public static void IntKeyDBtoXML(ref DatabaseDictionary <int, Element <int, string> > TempDB, ref List <string> Results)
        {
            "Persisting the database".Wrap();
            XDocument IntDBXML = new XDocument();

            IntDBXML.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
            XComment comment = new XComment("Persisting int-string database");

            IntDBXML.Add(comment);
            XElement Root = new XElement("KeyValuePairs");

            IntDBXML.Add(Root);
            foreach (int K in TempDB.Keys())
            {
                XElement KVP = new XElement("KeyValuePair");
                Root.Add(KVP);
                Element <int, string> Val = TempDB.RetrieveValue(K);
                XElement Key = new XElement("Key", new XAttribute("IntKey", K.ToString()));
                KVP.Add(Key);
                XElement Value = new XElement("Value", new XAttribute("DatabaseElement", K.ToString()));
                KVP.Add(Value);
                XElement Name = new XElement("Name", Val.NAME);
                Value.Add(Name);
                XElement Description = new XElement("Description", Val.DESCRIPTION.ToString());
                Value.Add(Description);
                XElement Timestamp = new XElement("Timestamp", Val.TIMESTAMP.ToString());
                Value.Add(Timestamp);
                XElement Relations = new XElement("Relations");
                if (Val.RELATIONS != null)
                {
                    foreach (int R in Val.RELATIONS)
                    {
                        XElement Relation = new XElement("Relation", R.ToString());
                        Relations.Add(Relation);
                    }
                    Value.Add(Relations);
                }
                XElement Data = new XElement("Data", Val.DATA);
                Value.Add(Data);
            }
            "Persisting the contents of the dictionary into an XML file".Wrap();
            IntDBXML.Save("PersistedDB.xml");
            Results.Add("Database has been persisted");
        }
示例#13
0
        //Method to get a set of keys containing the string as their data in values or among other strings in the data of respective values
        public static List <string> FindKeysbyData(this DatabaseDictionary <string, Element <string, List <string> > > Temp, string V) //Method to retrieve keys using data as a parameter
        {
            List <string>         LK1    = new List <string>();
            List <string>         result = new List <string>();
            List <List <string> > LK     = new List <List <string> >();

            foreach (string k in Temp.Keys())
            {
                LK.Add(Temp.RetrieveValue(k).DATA);
                foreach (List <string> TempL in LK)
                {
                    if (TempL.Contains(V))
                    {
                        result.Add(k);
                    }
                }
            }
            return(result);
        }
示例#14
0
        static void Main(string[] args)
        {
            DatabaseDictionary <int, Element <int, string> > IntKeyDB = new DatabaseDictionary <int, Element <int, string> >();
            Element <int, string> IKElement1 = new Element <int, string>();

            IKElement1.NAME = "The Beatles"; IKElement1.DESCRIPTION = "Rockband"; IKElement1.TIMESTAMP = DateTime.Now; IKElement1.RELATIONS = new List <int> {
                5, 2
            }; IKElement1.DATA = "Revolver";
            Element <int, string> IKElement2 = new Element <int, string>();

            IKElement2.NAME = "Pink Floyd"; IKElement2.DESCRIPTION = "Rockband"; IKElement2.TIMESTAMP = DateTime.Now; IKElement2.RELATIONS = new List <int> {
                1, 2
            }; IKElement2.DATA = "Wish you were here";
            IntKeyDB.AddPair(1, IKElement2);
            IntKeyDB.AddPair(2, IKElement1);
            Console.Write("Following key or keys contain THE BEATLES as the NAME in their values (Elements)\nKey: ");
            Console.Write("\n\nFollowing key or keys contain WISH YOU WERE HERE as the DATA in their values (Elements)\nKey: ");
            foreach (int a in IntKeyDB.FindKeysbyData("Wish you were here"))
            {
                Console.Write("{0} ", a.ToString());
            }
            Console.Write("\n\nFollowing key or keys have been updated in the database today till now\nKey: ");
            Console.WriteLine();
        }