public Action<Message>edit(DBEngine<int, DBElement<int, string>> db)
 {
     Action<Message> Edit = (msg) =>
       {
           XDocument xml = XDocument.Parse(msg.content.ToString());
           XElement element = xml.Descendants("Msg").ElementAt(0).Descendants("Data").ElementAt(0);
           int key = int.Parse(element.Descendants("key").ElementAt(0).Value);
           DBElement<int, string> elem = new DBElement<int, string>();
           Console.Write("\n\n --- edit a element which key ={0}--- ",key);
           Write("\n\n ---Database before edit---");
           db.showDB();
           if (!db.getValue(key, out elem))
           {
               msg.content = "edit fail";
               return;
           }
           elem.name = element.Descendants("value").Descendants("name").ElementAt(0).Value;
           elem.descr = element.Descendants("value").Descendants("descr").ElementAt(0).Value;
           elem.timeStamp = (DateTime)element.Descendants("value").Descendants("timestamp").ElementAt(0);
           IEnumerable<XElement> children = element.Descendants("value").Descendants("children").Descendants("key");
           elem.children = new List<int>();
           foreach (var child in children)
               elem.children.Add(int.Parse(child.Value));
           IEnumerable<XElement> items = element.Descendants("value").Descendants("payload");
           if (items.Count() == 1)
               elem.payload = items.ElementAt(0).Value;
           msg.content = "edit success";
           Utilities.swapUrls(ref msg);
           Console.Write("\n\n --- Database after edit ---");
           db.showDB();
       };
     return Edit;
 }
        static void Main(string[] args)
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            ItemFactory <int, string> itemFactory       = new ItemFactory <int, string>();

            "Demonstrating Requirement #2".title();
            DBElement <int, string> elem = itemFactory.Create();

            elem.name      = "element";
            elem.descr     = "test element";
            elem.timeStamp = DateTime.Now.AddDays(-4);
            elem.children.AddRange(new List <int> {
                1, 2, 3
            });
            elem.payload = "elem's payload";
            WriteLine("\n Item to be inserted.. \n");
            elem.showElement();
            db.insert(1, elem);
            db.showDB();
            WriteLine();

            WriteLine("\n Inserting second element into DB :");
            DBElement <int, string> elem2 = new DBElement <int, string>();

            elem2.name      = "element2";
            elem2.descr     = "test element2";
            elem2.timeStamp = DateTime.Now;
            elem2.children.AddRange(new List <int> {
                1, 2, 3, 4
            });
            elem2.payload = "elem2's payload";
            WriteLine("\nItem to be inserted.. \n");
            elem2.showElement();
            db.insert(2, elem2);
            WriteLine("\n\n DB after insertion:");
            db.showDB();
            "Adding relationship :".title('.');
            WriteLine("\n Elements in DB :");
            db.showDB();
            int key      = 2;
            var children = new List <int> {
                5, 6
            };

            WriteLine("\n Add children for key {0} : ", key);
            foreach (var child in children)
            {
                Write("{0}, ", child);
            }
            WriteLine();
            db.addRelationship(key, children);
            WriteLine("\n\n DB after the change :");
            db.showDB();
            WriteLine();
            "".demarcation();
            ItemEditorTest editorTest = new ItemEditorTest();

            editorTest.test();
        }
示例#3
0
        static void Main(string[] args)
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            DBEngine <string, DBElement <string, List <string> > > dbString = new DBEngine <string, DBElement <string, List <string> > >();
            ItemFactory <int, string> itemFactory = new ItemFactory <int, string>();

            DBElement <int, string> elem = itemFactory.Create();

            elem.name      = "element";
            elem.descr     = "test element";
            elem.timeStamp = DateTime.Now.AddDays(-4);
            elem.children.AddRange(new List <int> {
                1, 2, 3
            });
            elem.payload = "elem's payload";
            WriteLine("\n Item to be inserted.. \n");
            elem.showElement();
            db.insert(1, elem);
            db.showDB();
            WriteLine();

            WriteLine("\n Inserting second element into DB :");
            DBElement <int, string> elem2 = new DBElement <int, string>();

            elem2.name      = "element2";
            elem2.descr     = "test element2";
            elem2.timeStamp = DateTime.Now;
            elem2.children.AddRange(new List <int> {
                1, 2, 3, 4
            });
            elem2.payload = "elem2's payload";
            WriteLine("\nItem to be inserted.. \n");
            elem2.showElement();
            db.insert(2, elem2);
            WriteLine("\n\n DB after insertion:");
            db.showDB();

            //Inserting element 3
            WriteLine("\n Inserting third element into DB :");
            DBElement <int, string> elem3 = new DBElement <int, string>();

            elem3.name      = "element3";
            elem3.descr     = "test element3";
            elem3.timeStamp = DateTime.Now.AddDays(-5);
            elem3.children.AddRange(new List <int> {
                5, 8, 10
            });
            elem3.payload = "elem3's payload";
            WriteLine("\nItem to be inserted.. \n");
            elem3.showElement();
            db.insert(3, elem3);
            WriteLine("\n\n DB after insertion:");
            db.showDB();
            QueryEngineTest engineTest = new QueryEngineTest();

            engineTest.test1();
            engineTest.test2();
            engineTest.test3();
        }
        static void Main(string[] args)
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            ItemFactory <int, string> itemFactory       = new ItemFactory <int, string>();

            "Demonstrating Requirement #2".title();
            DBElement <int, string> elem = itemFactory.Create();

            elem.name      = "element";
            elem.descr     = "test element";
            elem.timeStamp = DateTime.Now.AddDays(-4);
            elem.children.AddRange(new List <int> {
                1, 2, 3
            });
            elem.payload = "elem's payload";
            WriteLine("\n Item to be inserted.. \n");
            elem.showElement();
            db.insert(1, elem);
            db.showDB();
            WriteLine("\n Inserting second element into DB :");
            DBElement <int, string> elem2 = new DBElement <int, string>();

            elem2.name      = "element2";
            elem2.descr     = "test element2";
            elem2.timeStamp = DateTime.Now;
            elem2.children.AddRange(new List <int> {
                1, 2, 3, 4
            });
            elem2.payload = "elem2's payload";
            WriteLine("\nItem to be inserted.. \n");
            elem2.showElement();
            db.insert(2, elem2);
            WriteLine("\n\n DB after insertion:");
            db.showDB();
            WriteLine("\n\n DB after insertion:");
            db.showDB();
            var dict = new Dictionary <int, DBElement <int, string> >();
            var keys = db.searchForTimeStamp(DateTime.Now.AddDays(-6), DateTime.Now.AddDays(-3));

            Console.WriteLine("\nRunning a query on database..\n");
            Console.WriteLine("\n\nThe result are the following keys:\n\n");
            foreach (var item in keys)
            {
                dynamic result = db.searchValue(item);
                Console.Write("{0}, ", item);
                dict.Add(item, result);
            }
            DBFactory <int, DBElement <int, string> > dbFactory = new DBFactory <int, DBElement <int, string> >(dict);

            "Immutable database:".title('.');
            Console.WriteLine("\n\nImmutable database is of type DBFactory, which is immutable (Found in DBFactory.cs).");
        }
        private void test()
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            ItemFactory <int, string> itemFactory       = new ItemFactory <int, string>();
            int key = 0;

            //-----------Modify Metadata--------------------------
            "Modify metadata".title('.');
            WriteLine("\n Elements in DB :");
            db.showDB();
            key = 2;
            string name        = "Nitish";
            string description = "MS in CE";

            WriteLine("\n\n\n New Details for metadata of key {0} :", key);
            WriteLine("\n Name : {0}", name);
            WriteLine("\n Description : {0}", description);
            WriteLine();
            db.modifyMetadata(key, name, description);
            WriteLine("\n\n DB after the change :");
            db.showDB();
            WriteLine();
            "".demarcation();
            //-----------Modify Metadata--------------------------

            //-------------------Replace Value instance-----------------------
            "Replace value instance".title('.');
            WriteLine("\n\n Elements in DB :");
            DBElement <int, string> elem = itemFactory.Create();

            db.showDB();
            key = 2;
            //DBElement<int, string> elem = new DBElement<int, string>();
            elem.name      = "new element";
            elem.descr     = "new test element";
            elem.timeStamp = DateTime.Now;
            elem.children.AddRange(new List <int> {
                7, 8, 9
            });
            elem.payload = "new elem's payload";
            WriteLine("\n\n New instance for replacement for key {0}:\n", key);
            elem.showElement();
            WriteLine("\n\n Replace instance of a key :");
            db.replaceKeyInstance(key, elem);
            WriteLine("\n\n DB after the change :");
            db.showDB();
            WriteLine();
            "".demarcation();
            //-------------------Replace Value instance-----------------------
        }
        static void Main(string[] args)
        {
            "Testing ItemEditor Package".title('=');
            WriteLine();
            ItemEditorTest iET = new ItemEditorTest();

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

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

            "\n1) Editing metadata".title();
            "\nBefore editing metadata for key 1".title();
            db.showDB();
            "\nAfter editing metadata for key 1".title();
            iEditor = new ItemEditor <int, DBElement <int, string> >(db);
            iEditor.editMetaData(1, "Sachin Tendulkar", "Cricket player");                     //send the values to be edited to the editMetaData() function
            db.showDB();
            WriteLine();

            "\n2) Adding children".title();
            "\nBefore adding relationship(children) for key 1".title();
            db.showDB();
            "\nAfter adding relationship(children) for Key 1".title();
            iEditor.addrelationships(1, new List <int> {
                3
            });                                                                              //send a new list with children to be added to a key
            db.showDB();
            WriteLine();

            "\n3) Deleting children".title();
            "\nBefore deleting relationship(children) for key 1".title();
            db.showDB();
            WriteLine();
            "\nAfter deleting relationship(children) to Key 1".title();                           //send a new list with children to be deleted from a key
            iEditor.deleteRelationships(1, new List <int> {
                3
            });
            db.showDB();
            WriteLine();

            "\n4) Replacing value instance".title();
            DBElement <int, string> elem = new DBElement <int, string>();                         //create a new element for replacing value

            elem.name    = "Messi";
            elem.payload = "Plays for Argentina";
            elem.descr   = "Football player";
            elem.children.AddRange(new List <int> {
                2
            });
            elem.timeStamp = DateTime.Now;

            "\nBefore replacing the value instance for key 2".title();
            db.showDB();
            WriteLine();
            "\nAfter replacing the value instance for key 2".title();
            iEditor.replaceValueInstance(2, elem);                                              //send value to be replaced for a key
            db.showDB();
        }
        public void insertData(DBEngine <int, DBElement <int, string> > db)
        {
            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.name      = "Usain Bolt";
            elem1.descr     = "Athelte";
            elem1.timeStamp = DateTime.Now;
            elem1.children.AddRange(new List <int> {
                2
            });
            elem1.payload = "Fastest in the world";
            db.insert(1, elem1);

            DBElement <int, string> elem2 = new DBElement <int, string>();

            elem2.name      = "Saina Nehwal";
            elem2.descr     = "Badminton Player";
            elem2.timeStamp = DateTime.Now;
            elem2.children.AddRange(new List <int> {
                1
            });
            elem2.payload = "Famous badminton player";
            db.insert(2, elem2);
            db.showDB();
            WriteLine();
        }
示例#8
0
        static void Main(string[] args)
        {
            //test stub for Query Processing
            // "Testing QueryProcessing Package".title('=');
            WriteLine();
            QueryProcessing <int, string> query = new QueryProcessing <int, string>();
            DBElement <int, string>       elem  = new DBElement <int, string>();
            DBElement <int, string>       elemResults;
            List <int> childrens;
            DBEngine <int, DBElement <int, string> > dbEngine = new DBEngine <int, DBElement <int, string> >();

            //  "Metadata with data dictionary is ".subTitle('-');
            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();

            /*query.DBEngine = dbEngine;
             * Write("\n\n");
             * //testing different kinds of function by querying different kinds of data
             * "Testing func: valueQuery() by getting following metdata from key = 1".subTitle();
             * query.valueQuery(1, out elemResults);
             * elemResults.showElement();
             * Write("\n\n");
             * "Testing func: childrenQuery() by getting following childrens from key = 1".subTitle();
             * query.childrensQuery(1, out childrens);
             * childrens.showList();
             * Write("\n\n");
             * "Testing func: processKeyMatchQuery() and defineQueryKeySearch() by getting keys with pattern 1".subTitle();
             * List<int> keys1 = query.processMatchQuery(query.defineQueryKeySearch("1"));
             * keys1.showList();
             * Write("\n\n");
             * "Testing func: processKeyMatchQuery() and defineQueryValueSearch() by getting keys with values having 'X' in their pattern".subTitle();
             * List<int> keys2 = query.processMatchQuery(query.defineQueryValueSearch("X"));
             * keys2.showList();
             * Write("\n\n");
             * "Testing func: processKeyMatchQuery() and defineQueryTimestampSearch() by getting keys with timestamp between 09/20/2015 11:36:58 PM to 09/22/2015 11:36:58 PM".subTitle();
             * List<int> keys3 = query.processMatchQuery(query.defineQueryTimestamp(DateTime.Parse("09/20/2015 11:36:58 PM"), DateTime.Parse("09/22/2015 11:36:58 PM")));
             * keys3.showList();
             * Write("\n\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");
        }
 public Action<Message>delete(DBEngine<int, DBElement<int, string>> db)
 {
     Action<Message> Delete = (msg) =>
        {
            XDocument doc = XDocument.Parse(msg.content.ToString());
            int key = int.Parse(doc.Descendants("Msg").Descendants("Data").Descendants("key").ElementAt(0).Value);
            Console.Write("\n\n delete an element which key = {0}", key);
            Write("\n\n --- Database before delete ---");
            db.showDB();
            if (db.delete(key))
            {
                msg.content = "Delete success";
                Console.Write("\n\n --- Database after delete---", key);
                db.showDB();
            }                       
            else
                msg.content = "Delete fail";
            Utilities.swapUrls(ref msg);                  
        };
     return Delete;
 }
示例#11
0
        private void test1()
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            DBEngine <string, DBElement <string, List <string> > > dbString = new DBEngine <string, DBElement <string, List <string> > >();
            ItemFactory <int, string> itemFactory = new ItemFactory <int, string>();

            //------------Search for value---------------------
            "Search for a value".title('.');
            WriteLine("\n\n Elements in DB :");
            db.showDB();
            int key = 2;

            WriteLine("\n\nSearching for value of Key {0}\n\n", key);
            var value = db.searchValue <int, string>(key);

            if (value != null)
            {
                value.showElement();
            }
            else
            {
                WriteLine("\nKey not found..!\n\n");
            }
            //------------Search for value---------------------

            //------------Search for children---------------------
            "Search for children:".title('.');
            WriteLine("\n\n Elements in DB :");
            db.showDB();
            key = 3;
            WriteLine("\n\nSearching for Children of Key {0}\n", key);
            dynamic children = db.searchChildren(key);

            foreach (var child in children)
            {
                Write("{0}, ", child);
            }
            //------------Search for children---------------------
        }
示例#12
0
        static void Main(string[] args)
        {
            "Testing Persist Engine Package".title('=');
            DBEngine <int, DBElement <int, ListOfStrings> > db1 = new DBEngine <int, DBElement <int, ListOfStrings> >();
            PersistData <int, ListOfStrings> persistEngine1     = new PersistData <int, ListOfStrings>();

            "Write in-memory database in XML file, please check DataPersistTest.xml file.".title('-');
            persistEngine1.loadXmlFile("DataPersistTest.xml", typeof(int).Name, typeof(ListOfStrings).Name);
            if (!persistEngine1.addRecord(1, db1.Dictionary[1]))
            {
                WriteLine("Key 1 is already in xml file.");
            }
            else
            {
                WriteLine("Key 1 is inserted in xml file.");
            }
            if (!persistEngine1.addRecord(2, db1.Dictionary[2]))
            {
                WriteLine("Key 2 is already in xml file.");
            }
            else
            {
                WriteLine("Key 2 is inserted in xml file.");
            }
            if (!persistEngine1.addRecord(4, db1.Dictionary[4]))
            {
                WriteLine("Key 4 is already in xml file.");
            }
            else
            {
                WriteLine("Key 4 is inserted in xml file.");
            }
            if (!persistEngine1.addRecord(2, db1.Dictionary[4]))
            {
                WriteLine("Key 4 is already in xml file.");
            }
            else
            {
                WriteLine("Key 4 is inserted in xml file.");
            }

            "Database restored or augmented from an existing XML (Data1.xml) file".title('-');

            DBEngine <int, DBElement <int, ListOfStrings> > augmentData = new DBEngine <int, DBElement <int, ListOfStrings> >();

            persistEngine1.retrieveDataFromXML(augmentData, "DataPersistTest.xml");
            augmentData.showDB();

            WriteLine();
        }
示例#13
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");
            }
        }
示例#14
0
        public void insertData(DBEngine <int, DBElement <int, string> > db)
        {
            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.name      = "India";
            elem1.descr     = "Country";
            elem1.timeStamp = DateTime.Now;
            elem1.children.AddRange(new List <int> {
                2, 3
            });
            elem1.payload = "Famous cricket player";
            db.insert(1, elem1);

            DBElement <int, string> elem2 = new DBElement <int, string>();

            elem2.name      = "Roger federer";
            elem2.descr     = "Tennis player";
            elem2.timeStamp = DateTime.Now.AddDays(-15);
            elem2.children.AddRange(new List <int> {
                3
            });
            elem2.payload = "Famous tennis player";
            db.insert(2, elem2);

            DBElement <int, string> elem3 = new DBElement <int, string>();

            elem3.name      = "Usain Bolt";
            elem3.descr     = "Athelte";
            elem3.timeStamp = DateTime.Now;
            elem3.children.AddRange(new List <int> {
                1
            });
            elem3.payload = "Fastest in the world";
            db.insert(3, elem3);

            DBElement <int, string> elem4 = new DBElement <int, string>();

            elem4.name      = "Saina Nehwal";
            elem4.descr     = "Badminton Player";
            elem4.timeStamp = DateTime.Now;
            elem4.children.AddRange(new List <int> {
                2
            });
            elem4.payload = "Famous badminton player";
            db.insert(4, elem4);
            db.showDB();
            WriteLine();
        }
示例#15
0
        private void test3()
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            DBEngine <string, DBElement <string, List <string> > > dbString = new DBEngine <string, DBElement <string, List <string> > >();
            ItemFactory <int, string> itemFactory = new ItemFactory <int, string>();

            //---------------Search for data with specified time interval---------------
            "Search for data with specified time interval:".title('.');
            WriteLine("\n\n Elements in DB :");
            var elem = new DBElement <int, string>();

            elem.name      = "old element";
            elem.descr     = "old test element";
            elem.timeStamp = DateTime.Now.AddDays(-3);
            elem.children.AddRange(new List <int> {
                85, 27, 65
            });
            elem.payload = "old elem's payload";

            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.name      = "old2 element";
            elem1.descr     = "old2 test element";
            elem1.timeStamp = DateTime.Now.AddDays(-2);
            elem1.children.AddRange(new List <int> {
                11, 12, 13
            });
            elem1.payload = "old2 elem's payload";

            WriteLine("\n\n Search for Key which is created between {0} and {1}:\n\n", DateTime.Now.AddDays(-5).Date, DateTime.Now.AddDays(-1).Date);
            db.insert(99, elem);
            db.insert(50, elem1);
            db.showDB();
            WriteLine();
            var resultkeys = db.searchForTimeStamp(DateTime.Now.AddDays(-5), DateTime.Now.AddDays(-1));

            WriteLine("\n\nThe result is:\n\n");
            foreach (var item in resultkeys)
            {
                Write("{0}, ", item);
            }
            //---------------Search for data with specified time interval---------------
        }
        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);
        }
示例#17
0
        static void Main(string[] args)
        {
            "Testing DBEngine Package".title('=');;
            WriteLine();

            "Test db of scalar elements".title();
            WriteLine();

            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.payload = "a payload";

            DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord");

            elem2.payload = "The Empire strikes back!";

            var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot");

            elem3.payload = "X-Wing fighter in swamp - Oh oh!";

            if (verbose)
            {
                Write("\n --- Test DBElement<int,string> ---");
                WriteLine();
                elem1.showElement();
                WriteLine();
                elem2.showElement();
                WriteLine();
                elem3.showElement();
                WriteLine();

                /* ElementFormatter is not ready for prime time yet */
                //Write(ElementFormatter.formatElement(elem1.showElement<int, string>(), false));
            }

            Write("\n --- Test DBEngine<int,DBElement<int,string>> ---");
            WriteLine();

            int        key    = 0;
            Func <int> keyGen = () => { ++key; return(key); };

            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            bool p1 = db.insert(keyGen(), elem1);
            bool p2 = db.insert(keyGen(), elem2);
            bool p3 = db.insert(keyGen(), elem3);

            if (p1 && p2 && p3)
            {
                Write("\n  all inserts succeeded");
            }
            else
            {
                Write("\n  at least one insert failed");
            }
            db.showDB();
            WriteLine();

            "Test db of enumerable elements".title();
            WriteLine();

            DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >();

            newelem1.name    = "newelem1";
            newelem1.descr   = "test new type";
            newelem1.payload = new List <string> {
                "one", "two", "three"
            };

            DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >();

            newerelem1.name    = "newerelem1";
            newerelem1.descr   = "better formatting";
            newerelem1.payload = new List <string> {
                "alpha", "beta", "gamma"
            };
            newerelem1.payload.Add("delta");
            newerelem1.payload.Add("epsilon");

            DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >();

            newerelem2.name  = "newerelem2";
            newerelem2.descr = "better formatting";
            newerelem2.children.AddRange(new List <string> {
                "first", "second"
            });
            newerelem2.payload = new List <string> {
                "a", "b", "c"
            };
            newerelem2.payload.Add("d");
            newerelem2.payload.Add("e");

            if (verbose)
            {
                Write("\n --- Test DBElement<string,List<string>> ---");
                WriteLine();
                newelem1.showEnumerableElement();
                WriteLine();
                newerelem1.showEnumerableElement();
                WriteLine();
                newerelem2.showEnumerableElement();
                WriteLine();
            }

            Write("\n --- Test DBEngine<string,DBElement<string,List<string>>> ---");

            int           seed    = 0;
            string        skey    = seed.ToString();
            Func <string> skeyGen = () => {
                ++seed;
                skey = "string" + seed.ToString();
                skey = skey.GetHashCode().ToString();
                return(skey);
            };

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

            newdb.insert(skeyGen(), newelem1);
            newdb.insert(skeyGen(), newerelem1);
            newdb.insert(skeyGen(), newerelem2);
            newdb.showEnumerableDB();
            Write("\n\n");
        }
示例#18
0
        static void Main(string[] args)
        {
            "Testing QueryEngine Package".title('=');
            WriteLine();
            QueryEngine sendQuery = new QueryEngine();

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

            elem1.payload = "Payload of element-1.";
            elem1.children.AddRange(new List <int> {
                9, 10, 11, 79
            });
            DBElement <int, string> elem2 = new DBElement <int, string>("Element-2", "Description of Element-2");

            elem2.payload = "Payload of element-2.";
            DBElement <int, string> elem3 = new DBElement <int, string>("Element-3", "Description of Element-3");

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

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

            db.insert(1, elem1);
            db.insert(2, elem2);
            db.insert(3, elem3);
            db.showDB();

            DBElement <int, string> result_elem  = new DBElement <int, string>();
            DBElement <int, string> result_elem2 = new DBElement <int, string>();

            //DBElement<int, string> result_elem = new DBElement<int, string>();
            Write("\n\n  Send query -> Find element with key = 2");

            result_elem = sendQuery.findValue <int, DBElement <int, string>, string>(db, 2);
            if (result_elem == null)
            {
                WriteLine("\n  Element with Key= 2 was not found in the database.");
            }
            else
            {
                result_elem.showElement();
            }

            Write("\n\n  Send query -> Find element with key = 5");
            result_elem = sendQuery.findValue <int, DBElement <int, string>, string>(db, 5);
            if (result_elem == null)
            {
                WriteLine("\n  Element with Key= 5 was not found in the database.");
            }
            else
            {
                result_elem.showElement();
            }

            Write("\n  Send query -> Get children of element with key = 1");
            List <int> x1 = sendQuery.getChildren <int, DBElement <int, string>, string>(db, 1);

            Write("\n  Children of element with key=1 are ");
            foreach (int i in x1)
            {
                Write("{0} ", i);
            }

            Write("\n\n  Send query -> Get children of element with key = 3");
            List <int> y1 = sendQuery.getChildren <int, DBElement <int, string>, string>(db, 3);

            if (y1.Count() > 0)
            {
                Write("\n  Children of element with key=3 are ");
                foreach (int i in y1)
                {
                    Write("{0} ", i);
                }
            }
            else
            {
                WriteLine("\n  Element with key=3 has no children.");
            }

            WriteLine("\n  Send query -> Get keys with string -\"blah\" in metadata section.");
            List <int> m1 = sendQuery.get_keys_with_metadata <int, DBElement <int, string>, string>(db, "18779");

            if (m1.Count() > 0)
            {
                Write("  Keys that contain the string \"ent-2\" in their metadata section are: ");
                foreach (int i in m1)
                {
                    Write("{0} ", i);
                }
            }
            else
            {
                WriteLine("  No keys found having \"ent-2\" in their metadata section.");
            }

            //WriteLine("Type of x = {0}", x.ToString());



            //----< Test query methods for DBEngine<string, DBElement<string, List<string>>>  >----------
            Write("\n\n --- Test Query methods for DBEngine<string, DBElement<string,List<string>>> ---");
            DBElement <string, List <string> > new_elem1 = new DBElement <string, List <string> >("Element-One", "Description of Element-One");

            new_elem1.payload = new List <string> {
                "First string in payload of Element-One", "Second string in payload of Element-One", "Third string"
            };
            new_elem1.children.AddRange(new List <string> {
                "Nine", "Ten", "Eleven"
            });
            System.Threading.Thread.Sleep(1200);
            DateTime tm1 = DateTime.Now;

            System.Threading.Thread.Sleep(1200);
            DBElement <string, List <string> > new_elem2 = new DBElement <string, List <string> >("Element-Two", "Description of Element-Two");

            new_elem2.payload = new List <string> {
                "First string in payload of Element-Two", "Mars", "Venus"
            };
            System.Threading.Thread.Sleep(1200);
            DBElement <string, List <string> > new_elem3 = new DBElement <string, List <string> >("Element-Three", "Description of Element-Three");

            new_elem3.payload = new List <string> {
                "First string in payload of element-3", "Beta", "Gamma"
            };

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

            new_db.insert("One", new_elem1);
            new_db.insert("Two", new_elem2);
            new_db.insert("Three", new_elem3);

            DateTime tm2 = DateTime.Now;

            new_db.showEnumerableDB();

            Write("\n\n  Send query -> Find element with key = One");
            if (sendQuery.findValue <string, DBElement <string, List <string> >, List <string> >(new_db, "One") == null)
            {
                WriteLine("\n  Element with Key= \"One\" was not found in the database.");
            }
            else
            {
                sendQuery.findValue <string, DBElement <string, List <string> >, List <string> >(new_db, "One").showEnumerableElement();
            }

            Write("\n\n  Send query -> Find element with key = Five");
            if (sendQuery.findValue <string, DBElement <string, List <string> >, List <string> >(new_db, "Five") == null)
            {
                WriteLine("\n  Element with Key= \"Five\" was not found in the database.");
            }
            else
            {
                sendQuery.findValue <string, DBElement <string, List <string> >, List <string> >(new_db, "Five").showEnumerableElement();
            }

            Write("\n  Send query -> Get children of element with key = \"One\"");
            //List<int> x = sendQuery.getChildren<int, DBElement<int, string>, string>(db, 1);
            List <string> x = sendQuery.getChildren <string, DBElement <string, List <string> >, List <string> >(new_db, "One");

            if (x.Count() > 0)
            {
                Write("\n  Children of element with key=\"One\" are ");
                foreach (string i in x)
                {
                    Write("{0} ", i);
                }
            }
            else
            {
                WriteLine("\n  Element with key=\"One\"  has no children.");
            }


            Write("\n\n  Send query -> Get children list of element with key = \"Three\"");
            //List<int> x = sendQuery.getChildren<int, DBElement<int, string>, string>(db, 1);
            List <string> y = sendQuery.getChildren <string, DBElement <string, List <string> >, List <string> >(new_db, "Three");

            if (y.Count > 0)
            {
                Write("\n  Children of element with key=\"Three\" are ");
                foreach (string i in y)
                {
                    Write("{0} ", i);
                }
            }
            else
            {
                WriteLine("\n  Element with key=\"Three\"  has no children.");
            }

            List <string> search_pat = new List <string> {
                "UnKnown", "lement"
            };

            foreach (string pattern_in_metadata in search_pat)
            {
                WriteLine("\n  Send query -> Get all keys containing string - \"{0}\" in their metadata section.", pattern_in_metadata);
                List <string> res1 = sendQuery.get_keys_with_metadata <string, DBElement <string, List <string> >, List <string> >(new_db, pattern_in_metadata);
                if (res1.Count > 0)
                {
                    Write("  Keys containing string \"{0}\" in their metadata section are: ", pattern_in_metadata);
                    foreach (string i in res1)
                    {
                        Write("{0} ", i);
                    }
                }
                else
                {
                    WriteLine("  No keys found containing pattern \"{0}\" in their metadata section.", pattern_in_metadata);
                }
            }

            WriteLine("\n\n  Send query -> Get all keys written within time intervals \"{0}\" and \"{1}\"", tm1, tm2);
            List <string> result1 = sendQuery.get_keys_within_timeInterval <string, DBElement <string, List <string> >, List <string> >(new_db, tm1);

            if (result1.Count > 0)
            {
                Write("  Keys written within specified time intervals are: ");
                //Write("  Keys are: ");
                foreach (string i in result1)
                {
                    Write("{0} ", i);
                }
            }
            else
            {
                WriteLine("  No keys found written withing specified time interevals tm1 and tm2.");
            }
            //WriteLine("tm1 = {0}\ntm2 = {1}", tm1, tm2);
        }
示例#19
0
        static void Main(string[] args)
        {
            DBProcessor db_process = new DBProcessor();
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();

            /*
             * for (int i=0;i<100;i++)
             * {
             * // int tm = DateTime.Now
             *  Thread.Sleep(1100);
             *  Console.WriteLine("tm = {0}", DateTime.Now);
             *  Console.WriteLine("tm1 = {0}", DateTime.Now.ToString("M / dd / yyyy h: mm:ss.fff tt"));
             *  //DateTime.Parse
             *  lkfs
             * }*/
            string tm1 = DateTime.Now.ToString("M / dd / yyyy h: mm:ss.fff tt");

            Thread.Sleep(150);
            string   tm2     = DateTime.Now.ToString("M / dd / yyyy h: mm:ss.fff tt");
            DateTime tma     = DateTime.Parse(tm1);
            DateTime tmb     = DateTime.Parse(tm2);
            TimeSpan ts      = tmb - tma;
            long     ts_tick = ts.Ticks;

            Console.WriteLine("tm1 = {0}\ntm2 = {1}", tm1, tm2);
            Console.WriteLine("ts = {0}\nts_tick = {1}", ts, ts.TotalMilliseconds);

            DateTime tm1_t = DateTime.Now;

            Thread.Sleep(160);
            DateTime tm2_t = DateTime.Now;
            TimeSpan a     = tm2_t - tm1_t;
            long     ms    = a.Ticks;

            Console.WriteLine("tm1 = {0}\ntm2 = {1}", tm1_t, tm2_t);
            Console.WriteLine("a = {0}\nms = {1}", a, (tm2_t - tm1_t).TotalMilliseconds);


            //------<Test inserting Element into database >----------
            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 children = new XElement("Children");
            XElement k1       = new XElement("Key", 55);
            XElement k2       = new XElement("Key", 65);

            children.Add(k1);
            children.Add(k2);
            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(children);
            request_msg.Add(payload);
            string msg_content = request_msg.ToString();

            Console.WriteLine("Going to call show db:");
            db.showDB();
            Console.WriteLine("Going to to call process query with xml = \n{0}", request_msg.ToString());

            //  string reply = db_process.process_msg<int, DBElement<int, string>, string>(msg_content, db);
            // Console.WriteLine("Reply from processing = {0}", reply);
            db.showDB();

            XElement req_msg3  = new XElement("Request_Message");
            XElement req_type3 = new XElement("Request_Type", "Edit Name");
            XElement req_elem  = new XElement("Key", 45);
            XElement new_name  = new XElement("Name", "This is new name for element.");

            req_msg3.Add(req_type3);
            req_msg3.Add(req_elem);
            req_msg3.Add(new_name);
            Console.WriteLine("Going to to call process query with xml = \n{0}", req_msg3.ToString());
            //  string reply3 = db_process.process_msg<int, DBElement<int, string>, string>(req_msg3.ToString(),db);
            db.showDB();

            XElement req_msg4  = new XElement("Request_Message");
            XElement req_type4 = new XElement("Request_Type", "Edit Description");
            XElement req_key   = new XElement("Key", 45);
            XElement new_descr = new XElement("Description", "New decription for element.");

            req_msg4.Add(req_type4);
            req_msg4.Add(req_key);
            req_msg4.Add(new_descr);
            Console.WriteLine("Going to to call process query with xml = \n{0}", req_msg4.ToString());
            // string reply4 = db_process.process_msg<int, DBElement<int, string>, string>(req_msg4.ToString(), db);
            db.showDB();
            Console.WriteLine("Testing XML");
            XElement rep    = new XElement("Reply_msg");
            XElement result = new XElement("Result", "Success");
            XElement query  = new XElement("Query");
            XElement qres   = new XElement("Type", "File Name");

            rep.Add(qres);
            if (rep.Element("Result") == null)
            {
                Console.Write("Hello");
            }
        }
示例#20
0
        private void test2()
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            DBEngine <string, DBElement <string, List <string> > > dbString = new DBEngine <string, DBElement <string, List <string> > >();
            ItemFactory <int, string> itemFactory = new ItemFactory <int, string>();

            //------------Search for Keys with pattern---------------------
            "Search for keys with a pattern:".title('.');
            WriteLine("\n\n Elements in DB :");
            DBElement <string, List <string> > elemString = new DBElement <string, List <string> >();

            elemString.name      = "element";
            elemString.descr     = "test element";
            elemString.timeStamp = DateTime.Now;
            elemString.children.AddRange(new List <string> {
                "Martin", "Nitish", "Michelle"
            });
            elemString.payload = new List <string> {
                "elem's payload", "sample payload"
            };
            dbString.insert("Name", elemString);

            DBElement <string, List <string> > elemString1 = new DBElement <string, List <string> >();

            elemString1.name      = "element";
            elemString1.descr     = "test element";
            elemString1.timeStamp = DateTime.Now;
            elemString1.children.AddRange(new List <string> {
                "Syracuse", "Boston", "Mysore"
            });
            elemString1.payload = new List <string> {
                "elem's payload", "sample payload"
            };
            dbString.insert("Cities", elemString1);
            dbString.showEnumerableDB();
            string keyPattern = "^C";

            WriteLine("\n\nSearching for Keys with pattern: {0}\n", keyPattern);
            dynamic keys = dbString.searchForKeyPattern(keyPattern);

            WriteLine("\n\nMatched keys : \n");
            foreach (var item in keys)
            {
                Write("{0}, ", item);
            }
            //------------Search for Keys with pattern---------------------

            //------------Search for specified string in metadata---------------------
            "Search for specified string in metadata:".title('.');
            WriteLine("\n\n Elements in DB :");
            db.showDB();
            string searchString = "element3";

            WriteLine("\n\n\nSearch string : {0}", searchString);
            WriteLine("\n\nSearching for specified string in metadata: \n\n", searchString);
            var matchedKeys = db.searchMetadata(searchString);

            foreach (var matchedKey in matchedKeys)
            {
                Write("{0}, ", matchedKey);
            }
            //------------Search for specified string in metadata---------------------
        }
示例#21
0
        static void Main(string[] args)
        {
            "Testing ItemEditor Package".title('=');
            WriteLine();

            Write("\n --- Test DBElement<int,string> ---");

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

            elem1.payload = "Payload of element-1.";
            elem1.children.AddRange(new List <int> {
                9, 10, 11
            });
            DBElement <int, string> elem2 = new DBElement <int, string>("Element-2", "Description of Element-2");

            elem2.payload = "Payload of element-2.";
            DBElement <int, string> elem3 = new DBElement <int, string>("Element-3", "Description of Element-3");

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

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

            db.insert(1, elem1);
            db.insert(2, elem2);
            db.insert(3, elem3);
            db.showDB();

            Write("\n\n  Going to test adding of relationship to DB-elements: Element-1 and Element-2");
            bool add1 = db.addRelation <int, DBElement <int, string>, string>(1, 2);
            bool add2 = db.addRelation <int, DBElement <int, string>, string>(2, 3);
            bool add3 = db.addRelation <int, DBElement <int, string>, string>(2, 17);

            //add3 will be equal to false because we cannot add a child key(17) if it is not present in the database.

            db.showDB();
            if (add1 && add2 && add3)
            {
                Write("\n  Adding relationship to all items successded.");
            }
            else
            {
                Write("\n  Adding relationship failed in one of the cases.");
            }

            Write("\n\n  Now going to test removing of relationship in DB-element: Element-1.");
            bool rem1 = db.removeRelation <int, DBElement <int, string>, string>(1, 10);
            bool rem2 = db.removeRelation <int, DBElement <int, string>, string>(1, 15);

            //The above case will fail because key-15 is not present in children list of Element-1.

            db.showDB();
            if (rem1 && rem2)
            {
                Write("\n  Deleting of relationship to both items successded.");
            }
            else
            {
                Write("\n  Deleting of relationship failed in one of the cases.");
            }


            Write("\n\n  Now going to test edition of name, description and replacing instance of payload with new instance in element1.");
            bool ed_name1 = db.editName <int, DBElement <int, string>, string>(1, "Elemen1_Renamed.");
            bool ed_descr = db.editDescr <int, DBElement <int, string>, string>(1, "New description for element 1.");
            bool ed_inst  = db.editInstance <int, DBElement <int, string>, string>(1, "New instance of payload for element-1.");

            db.showDB();

            Write("\n\n --- Test DBElement<string,List<string>> ---");
            DBElement <string, List <string> > new_elem1 = new DBElement <string, List <string> >("Element-One", "Description of Element-One");

            new_elem1.payload = new List <string> {
                "First string in payload of Element-One", "Second string in payload of Element-One", "Third string"
            };
            new_elem1.children.AddRange(new List <string> {
                "Nine", "Ten", "Eleven"
            });
            DBElement <string, List <string> > new_elem2 = new DBElement <string, List <string> >("Element-Two", "Description of Element-Two");

            new_elem2.payload = new List <string> {
                "First string in payload of Element-Two", "Mars", "Venus"
            };
            DBElement <string, List <string> > new_elem3 = new DBElement <string, List <string> >("Element-Three", "Description of Element-Three");

            new_elem3.payload = new List <string> {
                "First string in payload of element-3", "Beta", "Gamma"
            };

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

            new_db.insert("One", new_elem1);
            new_db.insert("Two", new_elem2);
            new_db.insert("Three", new_elem3);
            new_db.showEnumerableDB();

            Write("\n\n  Going to test adding of relationship to DB-elements: Element-One and Element-Two");
            bool a1 = new_db.addRelation <string, DBElement <string, List <string> >, List <string> >("One", "Two");
            bool a2 = new_db.addRelation <string, DBElement <string, List <string> >, List <string> >("Two", "Three");
            bool a3 = new_db.addRelation <string, DBElement <string, List <string> >, List <string> >("Three", "Not_presnet");

            //a3 will be false because we are trying to add a key that is not present in the database.

            new_db.showEnumerableDB();
            if (a1 && a2 && a3)
            {
                Write("\n  Adding relationship to all items successded.");
            }
            else
            {
                Write("\n  Adding relationship failed in one of the cases.");
            }


            Write("\n\n  Now going to test removing of relationships in DB-element: Element-One");
            bool r1 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "Nine");
            bool r2 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "Not_present");

            //r2 will be false because we are trying to remove a relationship in element one which is not even presnet.

            new_db.showEnumerableDB();
            if (r1 && r2)
            {
                Write("\n  Deleting of relationships successded in both cases.");
            }
            else
            {
                Write("\n  Deleting of relationship failed in one of the cases.");
            }


            Write("\n\n  Now going to test edition of name, description and replacing instance of payload with new instance in Element-One.");
            new_db.editName <string, DBElement <string, List <string> >, List <string> >("One", "Edited name for Element-One");
            new_db.editDescr <string, DBElement <string, List <string> >, List <string> >("One", "New description for Element-One");
            new_db.editInstance <string, DBElement <string, List <string> >, List <string> >("One", new List <string> {
                "New payload - String One", "New payload - String Two", "New payload - String Three"
            });
            new_db.showEnumerableDB();
            WriteLine();
        }
示例#22
0
        public static void Main(string[] args)
        {
            Write("\n --- Test DBElement<int,string> ---");
            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.payload = "a payload";
            WriteLine();

            DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord");

            elem2.payload = "The Empire strikes back!";
            WriteLine();

            var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot");

            elem3.children.AddRange(new List <int> {
                1, 5, 23
            });
            elem3.payload = "X-Wing fighter in swamp - Oh oh!";
            WriteLine();

            Write("\n --- Test DBEngine<int,DBElement<int,string>> ---");

            int        key    = 0;
            Func <int> keyGen = () => { ++key; return(key); };  // anonymous function to generate keys

            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            bool p1 = db.insert(keyGen(), elem1);
            bool p2 = db.insert(keyGen(), elem2);
            bool p3 = db.insert(keyGen(), elem3);

            if (p1 && p2 && p3)
            {
                Write("\n  all inserts succeeded");
            }
            else
            {
                Write("\n  at least one insert failed");
            }
            db.showDB();
            WriteLine();

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

            newelem1.name    = "newelem1";
            newelem1.descr   = "test new type";
            newelem1.payload = new List <string> {
                "one", "two", "three"
            };
            Write(newelem1.showElement <string, List <string> >());
            WriteLine();

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

            newerelem1.name    = "newerelem1";
            newerelem1.descr   = "better formatting";
            newerelem1.payload = new List <string> {
                "alpha", "beta", "gamma"
            };
            newerelem1.payload.Add("delta");
            newerelem1.payload.Add("epsilon");
            Write(newerelem1.showElement <string, List <string>, string>());
            WriteLine();

            DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >();

            newerelem2.name  = "newerelem2";
            newerelem2.descr = "better formatting";
            newerelem1.children.AddRange(new[] { "first", "second" });
            newerelem2.payload = new List <string> {
                "a", "b", "c"
            };
            newerelem2.payload.Add("d");
            newerelem2.payload.Add("e");
            Write(newerelem2.showElement <string, List <string>, string>());
            WriteLine();

            Write("\n --- Test DBEngine<string,DBElement<string,List<string>>> ---");

            int           seed    = 0;
            string        skey    = seed.ToString();
            Func <string> skeyGen = () =>
            {
                ++seed;
                skey = "string" + seed.ToString();
                skey = skey.GetHashCode().ToString();
                return(skey);
            };

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

            newdb.insert(skeyGen(), newerelem1);
            newdb.insert(skeyGen(), newerelem2);
            newdb.showEnumerableDB();
            WriteLine();
            WriteLine("Persisting int-string XML into Database");
            pe.createStringXML(db, false);
            WriteLine("Persisting string-List<string> XML into Database");
            pe.createListStringXML(newdb);
        }