void TestR2() { "Demonstrating Requirement #2".title(); //creating new element of type int and string DBElement <int, string> element = new DBElement <int, string>(); element.name = "first element of db"; element.descr = "int and string type"; element.timeStamp = DateTime.Now; element.children.AddRange(new List <int> { 0, 2, 4, 8 }); element.payload = "first element's payload"; element.showElement(); db.insert(1, element); db.showDB(); WriteLine(); //creating new element of type string and list of strings DBElement <string, List <string> > element2 = new DBElement <string, List <string> >(); element2.name = "second element of db"; element2.descr = "strings and strings of string types"; element2.timeStamp = DateTime.Now; element2.children.AddRange(new List <string> { "SMA", "OOD", "Project2" }); element2.payload = new List <string> { "second", "SMA", "project" }; element2.showEnumerableElement(); db2.insert("2", element2); db2.showEnumerableDB(); WriteLine(); }
public void persistdb <Key, Value, Data>(DBEngine <Key, Value> db) { XDocument xml = new XDocument(); xml.Declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("NoSQL Database"); //title for XML xml.Add(comment); XElement NoSQLelem = new XElement("NoSQLDB_ELEMENTS"); XElement type = new XElement("numbers", typeof(Key)); XElement payload = new XElement("name", typeof(Data)); xml.Add(NoSQLelem); NoSQLelem.Add(type); NoSQLelem.Add(payload); foreach (Key k1 in db.Keys()) { XElement tags = new XElement("open_close"); XElement key = new XElement("new_Key", k1); //tag for new keys generated tags.Add(key); Value value1; db.getValue(k1, out value1); DBElement <Key, Data> element = value1 as DBElement <Key, Data>; WriteLine(element.showElement()); XElement dbelement = persistdbelement <Key, Data>(element); tags.Add(dbelement); NoSQLelem.Add(tags); xml.Save("XML_FILE_PROJECT4.xml"); //XML file name } }
//----< write details of element with enumerable Data to string >------ public static string showElement <Key, Data, T>(this DBElement <Key, Data> elem) where Data : IEnumerable <T> // constraint clause { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { IEnumerable <object> d = elem.payload as IEnumerable <object>; if (d == null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } else { bool first = true; accum.Append(String.Format("\n payload:\n ")); foreach (var item in elem.payload) // won't compile without constraint clause { if (first) { accum.Append(String.Format("{0}", item)); first = false; } else { accum.Append(String.Format(", {0}", item)); } } } } return(accum.ToString()); }
//----< write metadata to string >------------------------------------- public static string showMetaData <Key, Data>(this DBElement <Key, Data> elem) { StringBuilder accum = new StringBuilder(); accum.Append(String.Format("\n name: {0}", elem.name)); accum.Append(String.Format("\n desc: {0}", elem.descr)); accum.Append(String.Format("\n time: {0}", elem.timeStamp)); if (elem.children.Count() > 0) { // convert to string format accum.Append(String.Format("\n Children: ")); bool first = true; foreach (Key key in elem.children) { if (first) { accum.Append(String.Format("{0}", key.ToString())); first = false; } else { accum.Append(String.Format(", {0}", key.ToString())); } } } return(accum.ToString()); }
// function to insert into database public XElement insert(XElement dbe, DBEngine <int, DBElement <int, string> > db) { DBElement <int, string> elem = new DBElement <int, string>(); Console.WriteLine("\n"); Console.WriteLine("\n----------Insert operation----------"); elem.name = dbe.Element("name").Value; elem.descr = dbe.Element("descr").Value; elem.payload = dbe.Element("payload").Value; List <int> childrenlist = new List <int>(); XElement db1 = dbe.Element("children"); foreach (var v in db1.Elements("dbkey")) { childrenlist.Add(Int32.Parse(v.Value)); } elem.children = childrenlist; bool result = db.insert(Int32.Parse((dbe.Element("key").Value)), elem); db.showDB(); if (result == true) { XElement s = new XElement("result", "\n element inserted successfully "); return(s); } else { XElement f = new XElement("result", "Failure"); return(f); } }
public XElement persistdbelement <Key, Data>(DBElement <Key, Data> elem) { XElement element = new XElement("newelement"); XElement name = new XElement("name", elem.name); // name of elements XElement descr = new XElement("description", elem.descr); // description of elements element.Add(name); element.Add(descr); if (elem.children.Count() > 0) { XElement children = new XElement("Children"); //defines the children of key foreach (Key key in elem.children) { XElement k1 = new XElement("key", key); children.Add(k1); } element.Add(children); } XElement time = new XElement("newtime", elem.timeStamp); //provides the timestamp element.Add(time); XElement payload = new XElement("newpayload", elem.payload); element.Add(payload); return(element); }
{ // Main method static void Main(string[] args) { "Testing persistence Package".title('='); WriteLine(); PersistenceEngine persiseng = new PersistenceEngine(); // creating new databse and new database elements DBEngine <int, DBElement <int, string> > pdb = new DBEngine <int, DBElement <int, string> >(); DBElement <int, string> pel1 = new DBElement <int, string>("first element", "SMA"); DBElement <int, string> pel2 = new DBElement <int, string>("second element ", "OOD"); DBElement <int, string> pel3 = new DBElement <int, string>("third element", "DP"); pel1.payload = "first payload"; pel1.children.AddRange(new List <int> { 111, 112, 113 }); pel2.payload = "second payload"; pel2.children.AddRange(new List <int> { 221, 222, 223 }); pel3.payload = "third payload"; pel3.children.AddRange(new List <int> { 331, 332, 333 }); // inserting elements to database pdb.insert(1, pel1); pdb.insert(2, pel2); pdb.insert(3, pel3); pdb.showDB(); WriteLine(); persiseng.persistdb <int, DBElement <int, string>, string>(pdb); }
//----< write simple db elements out to Console >------------------ public static void show <Key, Value, Data>(this DBEngine <Key, Value> db) { foreach (Key key in db.Keys()) { Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; Write("\n\n -- key = {0} --", key); Write(elem.showElement()); } }
//----< write details of element with simple Data to string >---------- public static string showElement <Key, Data>(this DBElement <Key, Data> elem) { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } return(accum.ToString()); }
// function to perform removal of relationships public static bool removeRelation <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Key key2) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; if (elem.children.Contains(key2)) { elem.children.Remove(key2); } } return(true); }
// function to edit instances public static bool editInstance <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Data new_instance) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.payload = new_instance; return(true); } else { return(false); } }
// Function to perform editing of name in metedata public static bool editName <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, string new_name) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.name = new_name; return(true); } else { return(false); } }
public List <key> querychildren <key, value, Data>(DBEngine <key, value> db, key Key) { value getqueryvalue; bool key_present = db.getValue(Key, out getqueryvalue); // check if key present DBElement <key, Data> temp = getqueryvalue as DBElement <key, Data>; // create new element to store value if (key_present) { return(temp.children); } else { Console.WriteLine("invalid key"); // if key not present error message } return(null); }
//function to getvalue public XElement getvalue(XElement dbe, DBEngine <int, DBElement <int, string> > db, QueryEngine QE) { Console.WriteLine("\n value of the particular key is returned "); DBElement <int, string> dbelem = new DBElement <int, string>(); QE.queryvalue <int, DBElement <int, string>, string>(db, 2); if (QE.Equals(null)) { XElement f = new XElement("result", "\n no key present "); return(f); } else { XElement s = new XElement("result", "\n Value obtained "); return(s); } }
{ // Function to perform addition of relationships public static bool addRelations <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Key key2) { Value val1, val2; bool key1_present = dbedit.getValue(key1, out val1); // check if key1 is present if (key1_present) { bool key2_present = dbedit.getValue(key2, out val2); // check if key2 is present if (key2_present) { DBElement <Key, Data> element = val1 as DBElement <Key, Data>; element.children.Add(key1); } } return(true); }
// function to delete from database public XElement Delete(XElement dbe, DBEngine <int, DBElement <int, string> > db) { DBElement <int, string> elem = new DBElement <int, string>(); Console.WriteLine("\n----------Delete Operation----------"); Console.Write("\n Now deleting the element with key=3"); bool result = db.remove(Int32.Parse((dbe.Element("key").Value))); db.showDB(); if (result == true) { XElement s = new XElement("result", "\n Element deleted successfully "); return(s); } else { XElement f = new XElement("result", "Failure"); return(f); } }
// main method to test DBExtensions package static void Main(string[] args) { "Testing DBExtensions Package".title('='); WriteLine(); // creation of new element Write("\n --- Test DBElement<int,string> ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.payload = "a payload"; Write(elem1.showElement <int, string>()); // creation of new database DBEngine <int, DBElement <int, string> > dbs = new DBEngine <int, DBElement <int, string> >(); dbs.insert(1, elem1); dbs.show <int, DBElement <int, string>, string>(); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); // creation of new element DBElement <string, List <string> > newelem1 = new DBElement <string, List <string> >(); newelem1.name = "newelem1"; newelem1.descr = "test new type"; newelem1.children = new List <string> { "Key1", "Key2" }; newelem1.payload = new List <string> { "one", "two", "three" }; Write(newelem1.showElement <string, List <string>, string>()); DBEngine <string, DBElement <string, List <string> > > dbe = new DBEngine <string, DBElement <string, List <string> > >(); dbe.insert("key1", newelem1); dbe.show <string, DBElement <string, List <string> >, List <string>, string>(); Write("\n\n"); }
void TestR7() { //Demonstrating queries "Demonstrating Requirement #7".title(); WriteLine(); QueryEngine queryv = new QueryEngine(); DBElement <int, string> displayresc = new DBElement <int, string>(); DBElement <int, string> displayres = new DBElement <int, string>(); //demonstrating query 1 Console.WriteLine("\n \n getting value for key 1"); displayres = queryv.queryvalue <int, DBElement <int, string>, string>(db, 1); if (displayres != null) { displayres.showElement(); } DBElement <string, List <string> > valuestring = new DBElement <string, List <string> >(); Console.WriteLine("\n \n getting value for key 2"); valuestring = queryv.queryvalue <string, DBElement <string, List <string> >, string>(db2, "2"); if (valuestring != null) { valuestring.showEnumerableElement(); } // demonstrating query 2 Console.WriteLine("\n \n getting children for key 1"); List <int> tchildlist = new List <int>(); tchildlist = queryv.querychildren <int, DBElement <int, string>, string>(db, 1); if (tchildlist != null) { foreach (int i in tchildlist) { Console.WriteLine(i); } } }
void TestR3() { "Demonstrating Requirement #3".title(); //demonstrating addition and removal of key/value pairs pairs WriteLine("\n Database contents before addition"); db.showDB(); WriteLine(); WriteLine(" \n Database contents after addition-----"); DBElement <int, string> insert1 = new DBElement <int, string>(); insert1.name = "first inserted element"; insert1.descr = "int and string values database"; insert1.timeStamp = DateTime.Now; insert1.children.AddRange(new List <int> { 7, 7, 8, 3, 8, 8, 5, 5, 7 }); insert1.payload = "first inserted elements payload"; db.insert(2, insert1); DBElement <int, string> insert2 = new DBElement <int, string>(); insert2.name = "second inserted element"; insert2.descr = "int and string values"; insert2.timeStamp = DateTime.Now; insert2.children.AddRange(new List <int> { 1, 3, 5, 7 }); insert2.payload = "second inserted elements payload"; db.insert(3, insert2); db.showDB(); WriteLine(); WriteLine(" \n Database before contents are removed"); db.showDB(); WriteLine("\n \n second element to be removed"); db.remove(2); db.showDB(); WriteLine(); }
// test stub for processor // main function static void Main(string[] args) { DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); "Testing the Package".title('='); Console.WriteLine(); Console.WriteLine("\n remote operations are done are and tested"); Console.Write("\n"); DBElement <int, string> elem = new DBElement <int, string>(); Console.WriteLine("\n----------Delete Operation----------"); Console.Write("\n Now deleting the element with key=3"); bool result = db.remove(2); db.showDB(); if (result == true) { XElement s = new XElement("result", "\n Element deleted successfully "); } else { XElement f = new XElement("result", "Failure"); } }
{ // calling function to show elements of type int and strings public static void showElement(this DBElement <int, string> element) { Console.Write(element.showElement <int, string>()); }
//calling function to show elements of type strings and list of strings public static void showEnumerableElement(this DBElement <string, List <string> > enumElement) { Console.Write(enumElement.showElement <string, List <string>, string>()); }
static void Main(string[] args) { "Testing DBEngine Package".title('=');; WriteLine(); "Test db of scalar elements".title(); WriteLine(); //creation of new elements 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(); } Write("\n --- Test DBEngine<int,DBElement<int,string>> ---"); WriteLine(); int key = 0; Func <int> keyGen = () => { ++key; return(key); }; // function to generate keys // new database created 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(); //new element created 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" }; //new element created 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"); //new element created 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"); }
static void Main(string[] args) { "Testing ItemEditor Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); // new database created to test the functionaity DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); // new elements are created and inserted to the database DBElement <int, string> elem1 = new DBElement <int, string>("first element", "first element description"); DBElement <int, string> elem2 = new DBElement <int, string>("second element", "second element description"); DBElement <int, string> elem3 = new DBElement <int, string>("third element", "third element description"); elem1.payload = "Payload of 1"; elem1.children.AddRange(new List <int> { 1, 2, 3 }); elem2.payload = "Payload of 2"; elem2.children.AddRange(new List <int> { 11, 22, 33 }); elem3.payload = "Payload of 3"; elem3.children.AddRange(new List <int> { 111, 222, 333 }); // inserting elements to database db.insert(1, elem1); db.insert(2, elem2); db.insert(3, elem3); db.showDB(); // testing addition of relationships to elements Write("\n\n Testing of addition of new relationships"); bool addr1 = db.addRelations <int, DBElement <int, string>, string>(1, 2); bool addr2 = db.addRelations <int, DBElement <int, string>, string>(2, 3); bool addr3 = db.addRelations <int, DBElement <int, string>, string>(3, 111); db.showDB(); // testing removing relationships to elements Write("\n\n Testing of removal of relations"); bool remr1 = db.removeRelation <int, DBElement <int, string>, string>(2, 11); bool remr2 = db.removeRelation <int, DBElement <int, string>, string>(1, 3); db.showDB(); if (remr1 && remr2) { Write("\n \n Removal succeded"); } // Testing editing of text data Write("\n\n Testing of editing of text data"); bool ed_name1 = db.editName <int, DBElement <int, string>, string>(1, "renaming of element 1"); bool ed_descr = db.editDescr <int, DBElement <int, string>, string>(1, "editing description of element 1"); bool ed_inst = db.editInstance <int, DBElement <int, string>, string>(1, "new instance for element 1"); db.showDB(); Write("\n\n --- Test DBElement<string,List<string>> ---"); // creating elements to new database of type string,List of strings DBElement <string, List <string> > new_elem1 = new DBElement <string, List <string> >("new element 1", "Description of 1"); DBElement <string, List <string> > new_elem2 = new DBElement <string, List <string> >("new element 2", "Description of 2"); DBElement <string, List <string> > new_elem3 = new DBElement <string, List <string> >("new element 3", "Description of 3"); //creating new database DBEngine <string, DBElement <string, List <string> > > new_db = new DBEngine <string, DBElement <string, List <string> > >(); new_elem1.payload = new List <string> { "First data in payload ", "Second data in payload", "Third data in payload" }; new_elem1.children.AddRange(new List <string> { "one", "two", "three" }); new_elem2.payload = new List <string> { "DP", "SMA", "OOD" }; new_elem2.children.AddRange(new List <string> { "four", "five", "six" }); new_elem3.payload = new List <string> { "CE", "CS", "EE" }; new_elem3.children.AddRange(new List <string> { "seven", "eight", "nine" }); // inserting elements to database new_db.insert("One", new_elem1); new_db.insert("Two", new_elem2); new_db.insert("Three", new_elem3); new_db.showEnumerableDB(); // testing addition of relationships to elements Write("\n\n testing addition of relationship "); bool add1 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("One", "two"); bool add2 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("Two", "three"); bool add3 = new_db.addRelations <string, DBElement <string, List <string> >, List <string> >("Three", "one"); new_db.showEnumerableDB(); if (add1 && add2 && add3) { Write("\n \n Adding relationship successful."); } // testing removing relationships to elements Write("\n \n Now going to test removing of relationships in DB-element: Element-One"); bool rem1 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "Nine"); bool rem2 = new_db.removeRelation <string, DBElement <string, List <string> >, List <string> >("One", "two"); new_db.showEnumerableDB(); if (rem1 && rem2) { Write("\n \n Deleting of relationships succeeded "); } // Testing editing of text data 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(); }