//<----------------------Load database from an XML file.
        XElement load_database <Key, Data>(XElement request_msg, DBEngine <Key, DBElement <Key, Data> > db)
        {
            Console.Write("\n  Request to Load Database from XML file at: ");
            try
            {
                //string pat = request_msg.Element("Pattern").Value;
                //Console.WriteLine(" with pattern - \"{0}\" present in metadata.", pat);
                PersistEngine pe        = new PersistEngine();
                string        file_name = request_msg.Element("File_Name").Value;
                Console.WriteLine("{0}", file_name.Substring(13));
                pe.loadDB <Key, DBElement <Key, Data>, Data>(db, file_name);

                XElement response_msg = new XElement("Query_Response");
                XElement type         = new XElement("Query_Type", "Load Database");

                string   reply  = "Loaded database from XML file saved at " + file_name.Substring(13);
                XElement result = new XElement("Result", reply);
                response_msg.Add(type);
                response_msg.Add(result);

                return(result);
            }
            catch (Exception)
            {
                Console.WriteLine("\n  XML file for request message is not in the expected format. A particular tag could not be found.");
                XElement response_msg = new XElement("Result", "Could not insert element into database");
                return(request_msg);
            }
        }
示例#2
0
 public void performOperations(DBEngine <string, DBElement <string, List <string> > > testDict, DBElement <string, List <string> > value)
 {       /*----------Perform operations as per the input given in the XML document--------------*/
     if (value.operation == "addition")
     {
         testDict.insert(value.key, value);          //insert the key/value pairs to the main database
         string s = "Database after inserting key " + value.key + " is";
         printDatabase(testDict, s);
     }
     if (value.operation == "edit")
     {
         testDict.saveValue(value.key, value);       //edit the value for the given key
         string s = "Database after editing key " + value.key + " is";
         printDatabase(testDict, s);
     }
     if (value.operation == "delete")
     {
         testDict.delete(value.key);                 //delete the key/value pair
         string s = "Database after deleting key " + value.key + " is";
         printDatabase(testDict, s);
     }
     if (value.operation == "persist database")
     {
         PersistEngine <string, DBElement <string, List <string> > > persist = new PersistEngine <string, DBElement <string, List <string> > >(testDict);
         var keys = testDict.Keys();
         persist.persistToXMLListPayload(keys);
         printDatabase(testDict, "Persisted database is:");
     }
     if (value.operation == "Query value")
     {
         DBElement <string, List <string> > valueOfKey = testDict.getValueOfKey(value.key);
         printQuery("Querying the database for value of key " + value.key + " is");
         Console.WriteLine("\n\nThe value of the Key {0} is:\n", value.key);
         valueOfKey.showEnumerableElement();
     }
     if (value.operation == "Query children")
     {
         QueryEngine <string, DBElement <string, List <string> > > qEngine = new QueryEngine <string, DBElement <string, List <string> > >(testDict);
         printQuery("Querying the database for value of key " + value.key + " is");
         List <string> children = qEngine.getChildrenOfKey(value.key);
         Console.WriteLine("\nThe children of the Key {0} are:\n", value.key);
         displayChildren(children);
     }
     if (value.operation == "Augment database")
     {
         PersistEngine <string, DBElement <string, List <string> > > persist = new PersistEngine <string, DBElement <string, List <string> > >(testDict);
         string fileName = "C:\\Users\\rakeshh91\\Documents\\Rakesh Documents\\Class Materials\\SMA\\Assignments\\Assignment 4 - Implementation\\CommPrototype\\augmentDatabase.xml";
         persist.augmentDatabaseFromXMLStringList(testDict, fileName);
         printDatabase(testDict, "Database after augmenting is:");
     }
 }
示例#3
0
        static void Main(string[] args)
        {
            "Testing PersistEngine Package".title('=');
            WriteLine();
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();

            "\nSave to an XML file".title();
            PersistEngineTest p1 = new PersistEngineTest();

            p1.insertData(db);
            dynamic allKeys = db.Keys();
            PersistEngine <int, DBElement <int, string> > pEngine = new PersistEngine <int, DBElement <int, string> >(db);

            pEngine.persistToXML(allKeys);
            WriteLine("\n\nAbove database is stored as XML file in local machine");
            WriteLine();

            WriteLine("\nThe persisted XML file along with new key/value pairs are augmented to the database.\n");
            WriteLine("Below shown key/value pairs are augmented to the database.\n");
            pEngine.augmentDatabaseFromXML(db);                                         //Augment the persisted database along with new values to the main database
            pEngine.persistToXML(allKeys);
            db.showDB();
            WriteLine();
            WriteLine();

            "\nPersist database every 5 seconds until its cancelled".title();
            WriteLine();

            pEngine.scheduledSaveDatabase();
            WriteLine();
            WriteLine();

            "\nProject dependancy and realtionships".title();
            WriteLine();
            DBEngine <string, DBElement <string, List <string> > >      dependancyDb  = new DBEngine <string, DBElement <string, List <string> > >();
            PersistEngine <string, DBElement <string, List <string> > > pEngineString = new PersistEngine <string, DBElement <string, List <string> > >(dependancyDb);

            try
            {
                Console.WriteLine("\nBelow details provide information on dependancy of every package in the project\n");
                pEngine.displayDependancy();
                dependancyDb.showEnumerableDB();
                WriteLine();
            }
            catch (Exception e)
            {
                WriteLine("\n" + e.Message + "\n");
            }
        }
        static void Main(string[] args)
        {
            //test stun for persist engine
            "Testing PersistEngine Package".title();
            WriteLine();
            PersistEngine <int, string> persistEngine = new PersistEngine <int, string>();

            "Testing Persist Engine Package".title('=');
            DBElement <int, string> elem = new DBElement <int, string>();
            DBEngine <int, DBElement <int, string> > dbEngine = new DBEngine <int, DBElement <int, string> >();

            //creating metdata
            "Metadata with data dictionary is ".title();
            elem.name      = "X";
            elem.descr     = "description";
            elem.timeStamp = DateTime.Parse("09/20/2015 11:36:58 PM");
            elem.children  = new List <int> {
                2, 3, 4
            };
            elem.payload = "payload";
            dbEngine.insert(1, elem);
            elem           = new DBElement <int, string>();
            elem.name      = "XX";
            elem.descr     = "description";
            elem.timeStamp = DateTime.Now;
            elem.children  = new List <int> {
                5, 6, 7
            };
            elem.payload = "payload";
            dbEngine.insert(12, elem);
            dbEngine.showDB();
            //persisting metadata into XML
            "Write in-memory database in XML file, please check DataPersistTest.xml file in Path:./PersistEngine/bin/debug.".title('-');
            persistEngine.persistXML(dbEngine);
            "Deleting in-memory database".title('-');
            dbEngine.Dictionary.Clear();
            "Database restored or augmented from an existing XML (DataPersistTest.xml) file".title('-');
            persistEngine.retrieveDataFromXMLTypeTwo(dbEngine, "DataPersistTest.xml", 10);
            dbEngine.showDB();
            WriteLine();
            Write("\n\n");
        }
        static void Main(string[] args)
        {
            "Testing DBExtensions Package".title('=');
            WriteLine();

            DBElement <int, string> elem1 = new DBElement <int, string>("Element-7", "Description of Element-7");

            elem1.payload = "Payload of element-7.";
            elem1.children.AddRange(new List <int> {
                8, 9
            });
            DBElement <int, string> elem2 = new DBElement <int, string>("Element-8", "Description of Element-8");
            //elem2.payload = "Payload of element-8.";
            DBElement <int, string> elem3 = new DBElement <int, string>("Element-9", "Description of Element-9");

            elem3.payload = "Payload of element-3.";

            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();

            db.insert(7, elem1);
            db.insert(8, elem2);
            db.insert(9, elem3);

            Write("  Created a new dataBase  with following contents:");
            db.showDB();
            WriteLine("\n\n  Now going to persist the database contents to an XML file.");
            PersistEngine pe        = new PersistEngine();
            string        file_name = pe.persistDB <int, DBElement <int, string>, string>(db);

            WriteLine("  Database contents are saved as - {0}", file_name);
            WriteLine("\n  Going to remove all DB elements and load the xml file saved above.");
            db.remove(7);
            db.remove(8);
            db.remove(9);
            WriteLine("  DB contents before calling load-DB");
            db.showDB();
            WriteLine("  Now send command to load database from the XML file.");
            pe.loadDB <int, DBElement <int, string>, string>(db, file_name);
            Write("  DB contents after calling load-DB ");
            db.showDB();

            DBEngine <string, DBElement <string, List <string> > > db2 = new DBEngine <string, DBElement <string, List <string> > >();

            Write("\n\n --- DBElement<string,List<string>> ---");
            DBElement <string, List <string> > elem_str = new DBElement <string, List <string> >();

            elem_str.name      = "Element-One";
            elem_str.descr     = "DB Element whose key type is string and payload type is List of strings.";
            elem_str.timeStamp = DateTime.Now;
            elem_str.children  = new List <string> {
                "Two", "Three", "Four"
            };
            elem_str.payload = new List <string> {
                "Element payload is of type List of strings.", "This is string two.", "And third"
            };
            elem_str.showEnumerableElement();

            db2.insert("One", elem_str);
            Write("\n\n --- DBEngine<string,List<string>> ---");
            db2.showEnumerableDB();

            XElement noSqlDb     = new XElement("NoSqlDb");
            XElement keyType     = new XElement("KeyType", typeof(int));
            XElement payloadType = new XElement("PayloadType", typeof(string));

            XElement request_msg = new XElement("Request_Message");
            XElement req_type    = new XElement("Request_Type", "Insert");
            XElement key         = new XElement("Key", 45);
            XElement name        = new XElement("Name", "element_one");
            XElement descr       = new XElement("Description", "Descr of element_one");
            XElement payload     = new XElement("Payload", "This the payload of element one.");

            request_msg.Add(req_type);
            request_msg.Add(key);
            request_msg.Add(name);
            request_msg.Add(descr);
            request_msg.Add(payload);

            Console.WriteLine("Message = \n{0}", request_msg.ToString());

            XElement Rmsg  = XElement.Parse(request_msg.ToString());
            string   Rtype = Rmsg.Element("Request_Type").Value;

            Console.WriteLine("Request type = {0}", Rtype);

            XElement abc    = new XElement("dummy");
            XElement result = new XElement("Result", "Success");

            //Console.WriteLine("abc= {0}\nresult = {1}\n", abc.ToString(), result.ToString());
            //abc = result;
            Console.WriteLine("abc= {0}\nresult = {1}", abc.ToString(), result.ToString());

            int    k  = 5;
            string ts = "Could not find element with key " + k.ToString() + " in database";
            //XElement tst = new XElement("Result", "Could not find element with key {0} in  database", k);
            XElement tst = new XElement("Result", ts);

            Console.WriteLine("tst = {0}", tst.ToString());

            XElement qry = new XElement("Query_Response");
            XElement par = new XElement("Partial", "This is \njust payload");

            qry.Add(tst);
            qry.Add(par);
            Console.WriteLine("qry = \n{0}", qry.ToString());
            Console.WriteLine("value of qry response =\n{0}", qry.Value.ToString());
            //noSqlDb.Add(keyType);
            //noSqlDb.Add(payloadType);
            //Console.WriteLine("\nString mesage = \n{0}", noSqlDb.ToString());
            //string xml_string = noSqlDb.ToString();

            //XElement abc = XElement.Parse(xml_string);
            //XElement root = abc.Element("KeyType");

            //Console.WriteLine("abc= {0}", abc.ToString());
            //Console.WriteLine("root= {0}", root.ToString());

            //elem.name = xml_dbElement.Element("Name").Value;
            //string par = root.Value;
            //Console.WriteLine("par = {0}", par);
        }