static void Main(string[] args) { WriteLine("Testing DBExtensions Package\n"); WriteLine(); //Insert first element Write("\n --- Test DBElement<int,string> ---"); DBElement<int, string> elem1 = new DBElement<int, string>(); elem1.payload = "a payload"; Write(elem1.showElement<int, string>()); //Insert first element into DB DBEngine<int, DBElement<int, string>> dbs = new DBEngine<int, DBElement<int, string>>(); dbs.insert(1, elem1); dbs.show<int, DBElement<int, string>, string>(); WriteLine(); //Insert first element into String Key/Value DB 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.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"); }
//----< Helper function to persist payload and children info to a xml file>------------------- private static void persist_child_payload <Key, Value, Data, T>(DBElement <Key, Data> elem, XElement elementNode) where Data : IEnumerable <T> { if (elem.children.Count() > 0) { XElement childrensNode = new XElement("children"); elementNode.Add(childrensNode); foreach (Key childrenkeys in elem.children) { XElement childrenkey = new XElement("key"); childrenkey.SetValue(childrenkeys.ToString()); childrensNode.Add(childrenkey); } } if (elem.payload != null) { XElement payLoadNode = new XElement("payload"); IEnumerable <object> d = elem.payload as IEnumerable <object>; if (d == null) { payLoadNode.SetValue(elem.payload.ToString()); } else { foreach (var item in elem.payload) { XElement itemNode = new XElement("item"); itemNode.SetValue(item); payLoadNode.Add(itemNode); } } elementNode.Add(payLoadNode); } }
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(); }
//----< Demonstrating req 3 - addition/deletion of key/value database for collection type db>------------------- public void TestR3_NonPrimitive(DBEngine <int, DBElement <int, string> > dbType1, DBEngine <string, DBElement <string, List <string> > > dbType2, DBItemEditor editor) { "\nDemonstrating Requirement #3 Collection Type".title(); WriteLine("\n\n Addition of Key/value pair"); String movie_name = "3 Idiots"; WriteLine(" Before Adding Key : " + movie_name); dbType2.showEnumerableDB(); DBElement <string, List <string> > newerelem3 = new DBElement <string, List <string> >(); newerelem3.name = "Movie Name: 3 Idiots"; newerelem3.descr = "3 Friends revist the college days and recall memories"; newerelem3.children.AddRange(new[] { "The Good, the Bad and the Ugly", "Django Unchained" }); newerelem3.payload = new List <string> { "Aamir Khan", "Madhavan", "Mona Singh" }; editor.addKeyValyePair <string, List <String>, string>(dbType2, newerelem3, movie_name); WriteLine("\n\n After adding key :" + movie_name); dbType2.showEnumerableDB(); IEnumerable <string> keys = dbType2.Keys(); String first = keys.First(); WriteLine("\n\n Removal of Key/value pair"); WriteLine(" Before removing key :" + first); dbType2.showEnumerableDB(); editor.removeKey <string, List <string>, string>(dbType2, first); WriteLine("\n\n After removing key :" + first); dbType2.showEnumerableDB(); }
void TestR3() { "\n1) Inserting key/value pairs to the database".title(); DBElement<int, string> elem2 = new DBElement<int, string>(); //Add new key/value pairs to the database elem2.name = "Roger federer"; elem2.descr = "Tennis player"; elem2.timeStamp = DateTime.Now; 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(); "\n2) Removing key 4 from the database".title(); db.delete(4); db.showDB(); WriteLine(); }
public void testDBlement() { Write("\n --- Test DBElement<int,string> ---"); DBElement<int, string> elem1 = new DBElement<int, string>(); elem1.payload = "a payload"; Write(elem1.showElement<int, string>()); WriteLine(); DBElement<int, string> elem2 = new DBElement<int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; Write(elem2.showElement<int, string>()); 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!"; Write(elem3.showElement<int, string>()); 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.show<int, DBElement<int, string>, string>(); WriteLine(); }
//----< Demonstrating req 3 - addition/deletion of key/value database for primitive type db>------------------- public void TestR3(DBEngine <int, DBElement <int, string> > dbType1, DBEngine <string, DBElement <string, List <string> > > dbType2, DBItemEditor editor) { "\nDemonstrating Requirement #3 Primitive Type".title(); int key1 = DBElementExtensions.generate_int_key(); WriteLine("\n\n Addition of Key/value pair"); WriteLine(" Before Adding Key : " + key1); dbType1.showDB(); WriteLine("\n\n After adding key :" + key1); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.name = "Titanic"; elem1.descr = "A seventeen-year-old aristocrat falls in love with a kind"; elem1.timeStamp = DateTime.Now; elem1.children.AddRange(new List <int> { 114, 116 }); elem1.payload = "Stars: Leonardo DiCaprio, Kate Winslet, Billy Zane"; editor.addKeyValyePair <int, String>(dbType1, elem1, key1); dbType1.showDB(); IEnumerable <int> keys1 = dbType1.Keys(); int first = keys1.First(); WriteLine("\n\n Removal of Key/value pair"); WriteLine(" Before removing key :" + first); dbType1.showDB(); WriteLine("\n\n After removing key :" + first); editor.removeKey <int, string>(dbType1, first); dbType1.showDB(); }
public Server() { elem = new DBElement<int, string>(); strElem = new DBElement<string, List<string>>(); rh = new RequestHandler(); tot = new Hashtable(); }
// 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; } }
void TestR3()//addition and deletion of key/value pairs { "Demonstrating Requirement #3".title(); WriteLine(); Write("\n --- Test addition of key/value pairs Start---"); WriteLine(); DBElement<int, string> elem1 = new DBElement<int, string>(); elem1.name = "element#1";//add a new key/value pairs elem1.descr = "test element#1"; elem1.timeStamp = DateTime.Now; elem1.children.AddRange(new List<int>{ 6, 8 }); elem1.payload = "elem#1's payload"; elem1.showElement(); db.insert(2, elem1); Write("\n\n Show key/value pairs in data base:\n"); db.showDB(); WriteLine(); Write("\n --- Test addition of key/value pairs End---"); WriteLine(); Write("\n --- Test deletion of key/value pairs Start---"); WriteLine(); db.delete(1);//delete an existing key/value pairs Write("\n\n Show key/value pairs in data base:\n"); db.showDB(); WriteLine(); db.delete(100);//try to delete a key/value pairs that doesn't exist Write("\n --- Test deletion of key/value pairs End---"); WriteLine(); }
public void XMLWrite(DBEngine<int, DBElement<int, string>> db, out string pathname) { pathname = "xmlDoc.xml"; // The root element is nosqldb or nosqldbLOS, which we can say, is the name // of the database of the corresponding type. The declaration is important // from an XML parser point of view. DBElement<int, string> Val = new DBElement<int, string>(); XDocument doc = new XDocument(new XElement("nosqldb")); doc.Declaration = new XDeclaration("1.0", "utf - 8", "yes"); XElement keyType = new XElement("keytype", "int"); XElement payloadType = new XElement("payloadtype", "string"); doc.Root.Add(keyType); doc.Root.Add(payloadType); foreach (var key in db.Keys()) { XElement keyNode = new XElement("key", key); db.getValue(key, out Val); XElement elementNode = new XElement("element"); elementNode.Add(new XElement("name", Val.name)); elementNode.Add(new XElement("descr", Val.descr)); elementNode.Add(new XElement("timeStamp", Val.timeStamp.ToString())); XElement childrenNode = new XElement("children"); //since children is List<Key> foreach (var item in Val.children) { childrenNode.Add(new XElement("key", item)); } elementNode.Add(childrenNode); elementNode.Add(new XElement("payload", Val.payload)); //since payload is string for this type of database doc.Root.Add(keyNode); doc.Root.Add(elementNode); } doc.Save(pathname); }
private static void Test2(out DBElement<string, string> newelem1, out DBElement<string, string> newerelem1, out DBElement<string, string> newerelem2) { newelem1 = new DBElement<string, string>(); newelem1.name = "newelem1"; newelem1.descr = "test new type"; newelem1.payload = "one"; //Load the second element newerelem1 = new DBElement<string, string>(); newerelem1.name = "newerelem1"; newerelem1.descr = "better formatting"; newerelem1.payload = "alpha"; //Load the third element newerelem2 = new DBElement<string, string>(); newerelem2.name = "newerelem2"; newerelem2.descr = "better formatting"; newerelem2.children.AddRange(new List<string> { "first", "second" }); newerelem2.payload = "a"; if (verbose) { Write("\n --- Test DBElement<string,List<string>> ---"); WriteLine(); newelem1.showEnumerableElement(); //Display the element WriteLine(); newerelem1.showEnumerableElement(); //Display the element WriteLine(); newerelem2.showEnumerableElement(); WriteLine(); } }
static void Main(string[] args) { DBEngine <int, DBElement <int, string> > dbType1 = new DBEngine <int, DBElement <int, string> >(); DBEngine <string, DBElement <string, List <string> > > dbCollectionType = new DBEngine <string, DBElement <string, List <string> > >(); DBItemEditor editor = new DBItemEditor(); "\nDemonstrating Requirement #2 - Primitive Type DB".title(); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.name = "Jurassic World"; elem1.descr = "Story on escape from giant creatures"; elem1.timeStamp = DateTime.Now; elem1.payload = "A giant creature attacks the park and becomes a killing machine"; editor.addKeyValyePair <int, String>(dbType1, elem1, DBElementExtensions.generate_int_key()); DBElement <int, string> elem2 = new DBElement <int, string>(); elem2.name = "Cast Away"; elem2.descr = "Story of surviving a crash landing on a deserted island."; elem2.timeStamp = DateTime.Now; elem2.children.AddRange(new List <int> { 4, 5 }); elem2.payload = "Directed by Robert Zemeckis and written by Willian Broyles Jr."; editor.addKeyValyePair <int, String>(dbType1, elem2, DBElementExtensions.generate_int_key()); dbType1.showDB(); int timeInterval = 2000; Scheduler <int, DBElement <int, string>, List <string>, string> sch = new Scheduler <int, DBElement <int, string>, List <string>, string>(dbType1, timeInterval); }
/* Defining a query using lambda function to search specific key */ public void key_value_search(DBEngine<int, DBElement<int, string>> db, IQuery<int, DBElement<int, string>> i_query, QueryEngine<int, DBElement<int, string>> qe ) { "Query for value with specified key (key = 2):".title(); WriteLine(); int key_to_search = 2; Func<int, string, bool> keyValueSearch = (int key, string search) => //lambda function { if (!db.Keys().Contains(key)) return false; else { if (key == int.Parse(search)) { DBElement<int, string> ele = new DBElement<int, string>(); db.getValue(key, out ele); return true; } else { return false; } } }; // pass query to query engine and call simpleQuery to make query on DBEngine qe.simpleQuery(keyValueSearch, key_to_search.ToString(), out i_query); WriteLine(); foreach (var key in i_query.Keys()) { DBElement<int, string> temp = new DBElement<int, string>(); i_query.getValue(key, out temp); WriteLine("key : {0}", key); temp.showElement(); WriteLine(); } }
//----< show metadata of DBElement>---------- 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) { 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()); }
public void TestR2() { "Demonstrating Requirement #2".title('='); "Database with string as payload".title(); DBElement<int, string> elem = new DBElement<int, string>(); elem.name = "element"; elem.descr = "test element"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List<int>{ 1, 2, 3 }); elem.payload = "elem's payload"; elem.showElement(); db.insert(1, elem); db.showDB(); WriteLine(); "database with list of strings as payload".title(); DBElement<string, List<string>> element = new DBElement<string, List<string>>(); element.name = "element2"; element.descr = "test element for list of strings as value"; element.timeStamp = DateTime.Now; element.children.AddRange(new List<string> { "one","two"}); element.payload = new List<string> { "element's payload", "2nd payload" }; element.showEnumerableElement(); enum_db.insert("enum_one", element); enum_db.showEnumerableDB(); WriteLine(); }
static void Main(string[] args) { DBEngine <int, DBElement <int, string> > dbType1 = new DBEngine <int, DBElement <int, string> >(); DBEngine <string, DBElement <string, List <string> > > dbType2 = new DBEngine <string, DBElement <string, List <string> > >(); DBItemEditor editor = new DBItemEditor(); //Demonstrating primitive type DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.name = "Jurassic World"; elem1.descr = "Story on escape from giant creatures"; elem1.timeStamp = DateTime.Now; elem1.payload = "A giant creature attacks the park and becomes a killing machine"; editor.addKeyValyePair <int, String>(dbType1, elem1, DBElementExtensions.generate_int_key()); DBElement <int, string> elem2 = new DBElement <int, string>(); elem2.name = "Cast Away"; elem2.descr = "Story of surviving a crash landing on a deserted island."; elem2.timeStamp = DateTime.Now; elem2.children.AddRange(new List <int> { 4, 5 }); elem2.payload = "Directed by Robert Zemeckis and written by Willian Broyles Jr."; editor.addKeyValyePair <int, String>(dbType1, elem2, DBElementExtensions.generate_int_key()); dbType1.showDB(); QueryEngine <int, string> queryEnginePrimitive = new QueryEngine <int, string>(dbType1); string inputString = "11"; Write("\n\n \n Input Search String :" + inputString); List <int> resultList = queryEnginePrimitive.searchKeyPattern(inputString); foreach (int key in resultList) { Write("\n found \"{0}\" in key \"{1}\"", inputString, key); } string inputString2 = "Movie"; Write("\n\n Input Search String :" + inputString); List <int> resultList2 = queryEnginePrimitive.searchMetadataPattern(inputString2); foreach (int key in resultList2) { Write("\n found \"{0}\" in \"{1}\"", inputString, key); } DateTime startDate = new DateTime(2014, DateTime.Today.Month, DateTime.Today.Day, 00, 00, 01); DateTime endDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 23, 59, 59); List <int> resultList3 = queryEnginePrimitive.searchTimeStamp(startDate); foreach (int key in resultList3) { Write("\n found key within \"{0}\" within range \"{1}\" {2}", key, startDate, endDate); } }
public void writeXML() { if(db1.emptyDictionary()) Write("\n\nThe database is empty.\n"); else{//if the dictionary is not empty store key/value pairs into a XML file DBElement<int, string> temp = new DBElement<int, string>(); IEnumerable<int> keys = db1.Keys(); XDocument xml = new XDocument(); xml.Declaration = new XDeclaration("1.0", "utf-8", "yes"); XElement noSqlDb = new XElement("noSqlDb"); xml.Add(noSqlDb); XElement keytype = new XElement("keytype", "int"); XElement payloadtype = new XElement("payloadtype", "string"); noSqlDb.Add(keytype); noSqlDb.Add(payloadtype); foreach(var i in keys) { db1.getValue(2, out temp); XElement key = new XElement("key", ("key"+i)); noSqlDb.Add(key);//noSqlDb's children XElement element = new XElement("element"); noSqlDb.Add(element);//noSqlDb's children XElement name = new XElement("name", temp.name);//name of the element element.Add(name); XElement descr = new XElement("descr", temp.descr);//description of the element element.Add(descr); XElement timeStamp = new XElement("timeStamp", temp.timeStamp);//timeStamp of the element element.Add(timeStamp); if(temp.children.Count!=0)//children of the element { XElement children = new XElement("children"); element.Add(children); foreach(var j in temp.children) { XElement childkey = new XElement("key", ("key"+j)); children.Add(childkey); } } XElement payload = new XElement("payload", temp.payload);//payload of the element element.Add(payload); } Console.Write("\n{0}\n", xml.Declaration); Console.Write(xml.ToString()); Console.Write("\n\n"); xml.Save(@"C:\Users\lxinghe\Downloads\Project2Starter\Test.xml"); db1.emptyDBEngine();//empty DBEngine } }
public RequestHandler() { sb = new StringBuilder(); db = new DBEngine<int, DBElement<int, string>>(); query = new QueryEngine<int, DBElement<int, string>>(); dbString = new DBEngine<string, DBElement<string, List<string>>>(); strElem = new DBElement<string, List<string>>(); }
static void Main(string[] args) { "Testing DBElement Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); WriteLine(); DBElement <int, string> elem1 = new DBElement <int, string>(); Write(elem1.showElement <int, string>()); WriteLine(); DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; Write(elem2.showElement <int, string>()); WriteLine(); var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.children = new List <int> { 1, 2, 7 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; Write(elem3.showElement <int, string>()); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); 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" }; Write(newelem1.showElement <string, List <string>, string>()); WriteLine(); DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >(); newerelem1.name = "newerelem1"; newerelem1.descr = "same stuff"; newerelem1.children.Add("first_key"); newerelem1.children.Add("second_key"); newerelem1.payload = new List <string> { "alpha", "beta", "gamma" }; newerelem1.payload.AddRange(new[] { "delta", "epsilon" }); Write(newerelem1.showElement <string, List <string>, string>()); WriteLine(); Write("\n\n"); }
//-----------< Fish out the type of query and criteria from the request string >------------- public bool parseRequest(DBEngine <string, DBElement <string, List <string> > > db, string msg, out string reply) { elem = new DBElement <string, List <string> >(); int IndexOfComma = msg.IndexOf(","); message = msg.Substring(IndexOfComma + 1); content = message.Split(','); type = content[0]; query = content[1]; //-----------< make function call and convert response into suitable string >---------- if (call(db, type)) { if (type == "value") { reply = "\n Value of key " + query + ": " + elem.showElement <string, List <string>, string>(); } else if (type == "children") { reply = "\n Children of key " + query + ": " + Utilities.ToString(replyList); } else if (type == "pattern") { reply = "\n List of keys starting with \"" + query + "\": " + Utilities.ToString(replyList); } else if (type == "string") { reply = "\n List of keys containing \"" + query + "\" in their metadata: " + Utilities.ToString(replyList); } else { reply = "\n List of keys entered from " + query + " to present: " + Utilities.ToString(replyList); } return(true); } if (type == "value") { reply = "\n Value of key " + query + " not found"; } else if (type == "children") { reply = "\n Children of key " + query + " not found"; } else if (type == "pattern") { reply = "\n List of keys starting with \"" + query + "\" not found"; } else if (type == "string") { reply = "\n List of keys containing \"" + query + "\" in their metadata not found"; } else { reply = "\n List of keys entered from " + query + " to present not found"; } return(false); }
//----< Persist primitive type db to a xml file>------------------- public static void persist_db <Key, Value, Data>(this DBEngine <Key, Value> db, String fileName) { XDocument xml = new XDocument(); xml.Declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("Demonstrating XML"); xml.Add(comment); XElement root = new XElement("noSqlDb"); xml.Add(root); XElement keyType = new XElement("keytype"); keyType.SetValue(typeof(Key)); root.Add(keyType); XElement payload = new XElement("payloadtype"); payload.SetValue(typeof(Data)); root.Add(payload); foreach (Key key in db.Keys()) //Iterate for each keys { XElement child = new XElement("record"); root.Add(child); XElement keyNode = new XElement("key"); keyNode.SetValue(key); child.Add(keyNode); XElement elementNode = new XElement("element"); child.Add(elementNode); Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; String timestamp = String.Format("{0}", elem.timeStamp); XElement nameNode = new XElement("name"); nameNode.SetValue(elem.name); XElement descrNode = new XElement("descr"); descrNode.SetValue(elem.descr); XElement timestampNode = new XElement("timestamp"); timestampNode.SetValue(timestamp); elementNode.Add(nameNode); elementNode.Add(descrNode); elementNode.Add(timestampNode); persist_child_payload_primtive <Key, Value, Data>(elem, elementNode); } try { xml.Save(fileName); } catch (Exception) { Console.WriteLine("Invalid Directory Specified"); } }
public void writeXML(string path) { if(db1.emptyDictionary()) Write("\n\nThe database is empty.\n"); else{//if the dictionary is not empty store key/value pairs into a XML file DBElement<int, string> temp = new DBElement<int, string>(); IEnumerable<int> keys = db1.Keys(); xml.Declaration = new XDeclaration("1.0", "utf-8", "yes"); XElement noSqlDb = new XElement("noSqlDb"); xml.Add(noSqlDb); XElement keytype = new XElement("keytype", "int"); XElement payloadtype = new XElement("payloadtype", "string"); noSqlDb.Add(keytype); noSqlDb.Add(payloadtype); foreach(var i in keys) { db1.getValue(i, out temp); XElement key = new XElement("key", ("key"+i)); noSqlDb.Add(key);//noSqlDb's children XElement element = new XElement("element"); noSqlDb.Add(element);//noSqlDb's children XElement name = new XElement("name", temp.name);//name of the element element.Add(name); XElement descr = new XElement("descr", temp.descr);//description of the element element.Add(descr); XElement timeStamp = new XElement("timeStamp", temp.timeStamp);//timeStamp of the element element.Add(timeStamp); if(temp.children.Count!=0)//children of the element { XElement children = new XElement("children"); element.Add(children); foreach(var j in temp.children) { XElement childkey = new XElement("key", ("key"+j)); children.Add(childkey); } } XElement payload = new XElement("payload", temp.payload);//payload of the element element.Add(payload); } xml.Save(path); } }
//----< Augument xml file db contents to existing collection type db>------------------- public static void augument_db <Key, Value, Data, T>(this DBEngine <Key, Value> db, string fileName) where Data : List <T> { try { XDocument newDoc = null; newDoc = XDocument.Load(fileName); WriteLine("\n\nInput XML\n"); WriteLine(newDoc.ToString()); XElement keyElement = newDoc.Root.Element("keytype"); XElement payloadElement = newDoc.Root.Element("payloadtype"); String keyType = keyElement.Value; String payLoad = payloadElement.Value; // check the key-type if (typeof(Key).ToString() == keyType && typeof(Data).ToString() == payLoad) { IEnumerable <XElement> elements = newDoc.Root.Elements("record"); foreach (XElement item in elements) //Extract each record { string KeyStringval = item.Element("key").Value; Key key = (Key)Convert.ChangeType(KeyStringval, typeof(Key)); DBElement <Key, Data> dbElement = new DBElement <Key, Data>(); XElement myelement = item.Element("element"); dbElement.name = myelement.Element("name").Value; dbElement.descr = myelement.Element("descr").Value; dbElement.timeStamp = DateTime.Now; IEnumerable <XElement> childrensElements = myelement.Elements("children").Elements("key"); foreach (XElement child in childrensElements) { Key childKey = (Key)Convert.ChangeType(child.Value, typeof(Key)); dbElement.children.AddRange(new[] { childKey }); } IEnumerable <XElement> payloadElements = myelement.Elements("payload").Elements("item"); List <T> payloadlist = new List <T>(); foreach (XElement child in payloadElements) { T payLoadKey = (T)Convert.ChangeType(child.Value, typeof(T)); payloadlist.Add(payLoadKey); } dbElement.payload = payloadlist as Data; Value valueType = (Value)Convert.ChangeType(dbElement, typeof(Value)); db.insert(key, valueType); } } else { Console.WriteLine("Not matching db"); } } catch (Exception) { Console.WriteLine("File Not Found to Read"); } }
static void Main(string[] args) { QueryRequestEngine qre = new QueryRequestEngine(); string reply; DBEngine <string, DBElement <string, List <string> > > db = new DBEngine <string, DBElement <string, List <string> > >(); DBElement <string, List <string> > elem = new DBElement <string, List <string> >(); elem.name = "name0"; elem.descr = "descr0"; elem.children = new List <string>() { "key0", "key1", "key2" }; elem.timeStamp = DateTime.Now; elem.payload = new List <string>() { "item1", "item2", "item3" }; db.insert("key0", elem); elem = new DBElement <string, List <string> >(); elem.name = "name1"; elem.descr = "descr1"; elem.children = new List <string>() { "key3", "key4", "key5" }; elem.timeStamp = DateTime.Now; elem.payload = new List <string>() { "item4", "item5", "item6" }; db.insert("key1", elem); qre.parseRequest(db, "write,value,key0", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,value,key1", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,children,key0", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,children,key1", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,pattern,k", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,pattern,a", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,string,descr0", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,string,descr1", out reply); Console.WriteLine(reply); qre.parseRequest(db, "write,interval,10/7/1999 12:00:00 AM", out reply); Console.WriteLine(reply); }
//----< Demonstrating req 2 - creating generic key/value in-memory database>------------------- public void TestR2(DBEngine <int, DBElement <int, string> > dbType1, DBEngine <string, DBElement <string, List <string> > > dbType2, DBItemEditor editor) { //Demonstrating primitive type "\nDemonstrating Requirement #2 - Primitive Type DB".title(); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.name = "Jurassic World"; elem1.descr = "Story on escape from giant creatures"; elem1.timeStamp = DateTime.Now; elem1.payload = "A giant creature attacks the park and becomes a killing machine"; editor.addKeyValyePair <int, String>(dbType1, elem1, DBElementExtensions.generate_int_key()); DBElement <int, string> elem2 = new DBElement <int, string>(); elem2.name = "Cast Away"; elem2.descr = "Story of surviving a crash landing on a deserted island."; elem2.timeStamp = DateTime.Now; elem2.children.AddRange(new List <int> { 113, 116 }); elem2.payload = "Directed by Robert Zemeckis and written by Willian Broyles Jr."; editor.addKeyValyePair <int, String>(dbType1, elem2, DBElementExtensions.generate_int_key()); dbType1.showDB(); //Demostrating IEnumberable Type "\nDemonstrating Requirement #2 - Collection Type DB".title(); DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >(); newerelem1.name = "Movie Name - The Good the Bad and the Ugly"; newerelem1.descr = "A bounty hunting scam joins two men in an uneasy alliance "; newerelem1.payload = new List <string> { "Clint Eastwood", " Eli Wallach", "Lee Van Cleef" }; String key = "The Good, the Bad and the Ugly"; editor.addKeyValyePair <string, List <String>, string>(dbType2, newerelem1, key); DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >(); newerelem2.name = "Movie Name - Django Unchained"; newerelem2.descr = "With the help of a German hunter, a freed slave sets to rescue"; newerelem2.children.AddRange(new[] { key, "Life Is Beautiful" }); newerelem2.payload = new List <string> { "Jamie Foxx", "Christoph Waltz", "Leonardo DiCaprio" }; newerelem2.payload.Add("Quentin Tarantino"); String key1 = "Django Unchained"; editor.addKeyValyePair <string, List <String>, string>(dbType2, newerelem2, key1); dbType2.showEnumerableDB(); }
/// <summary> /// Editing Data while updating.This returns element to be modified /// </summary> /// <param name="dbStore"></param> /// <param name="key"></param> /// <param name="element"></param> /// <param name="Operation"></param> /// <param name="error"></param> /// <returns></returns> public bool EditData(ref DBEngine<int, DBElement<int, string>> dbStore, int key, out DBElement<int, string> element, string Operation, out string error) { try { DBElement<int, string> elem = new DBElement<int, string>(); error = "success"; bool success = true; success = dbStore.getValue(key, out element); if (success != true) { error = "Key not found in DB"; return false; } elem = element; switch (Operation) { case "EditName": //Modify Name { element.name = elem.name; break; } case "EditDescription": //Modify Description { element.descr = elem.descr; break; } case "EditPayload": //Modify Payload { element.payload = elem.payload; break; } case "EditChildren": //Modify Children { element.children = new List<int>(); element.children = elem.children; break; } case "EditKeyNotValid": //Key editing is not valid { error = "Editing of Key is not allowed"; return false; } default: break; } return true; } catch (Exception ex) { throw new CustomException("error at itemeditor editdata", ex); } }
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:"); } }
static void Main(string[] args) { Console.WriteLine("Started testing of QueryEngineTest\n"); DBElement<int, string> elem = new DBElement<int, string>(); int key = 54; elem.name = "2015 Maserati GranTurismo"; elem.descr = "Ferrari FF new model 2015"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List<int> { 1, 2, 3, 4 }); elem.payload = "Make:Maserati;Model:Maserati GranTurismo;Color:Green;Year:2015;"; key = 54; db.insert(54, elem); bool status = query.getKey(db, key, out elem); Console.WriteLine("Obtained the key successfully\n {0} ,{1}, {2} ,{3} \n", elem.name, elem.payload, elem.timeStamp, elem.descr); }
void TestR2() { "Demonstrating Requirement #2".title(); DBElement<int, string> elem = new DBElement<int, string>(); elem.name = "element"; elem.descr = "test element"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List<int>{ 1, 2, 3 }); elem.payload = "elem's payload"; elem.showElement(); db.insert(1, elem); Write("\n\n Show key/value pairs in data base:\n"); db.showDB(); WriteLine(); }
void TestR3() { WriteLine(); "Demonstrating Requirement #3".title('='); WriteLine(); "Adding Key-Value pair to database".title(); DBElement<int, string> element = new DBElement<int, string>(); element.addElementData("element3", "test element for adding key-value pair to databse with value as string", DateTime.Now, new List<int> { 1, 2 }, "test elemet3's payload"); "element to be added to database".title(); element.showElement(); db.insert(2, element); db.showDB(); WriteLine(); "Adding Key-Value pair to enumerable database".title(); DBElement<string, List<string>> listelement = new DBElement<string, List<string>>(); listelement.addElementData("element4", "test element for adding key-value pair to databse with value as list of string", DateTime.Now, new List<string> { "1", "two" }, new List<string> { "test elemet4's payload" }); "element to be added to database".title(); listelement.showEnumerableElement(); enum_db.insert("enum_two", listelement); enum_db.showEnumerableDB(); WriteLine(); "Deleting Key-Value pair from database".title(); "Element with key=1 will be deleted from database".title(); "Element with key=1:".title(); DBElement<int, string> remove_element = new DBElement<int, string>(); db.getValue(1, out remove_element); remove_element.showElement(); db.remove(1); WriteLine(); "Modified Database: ".title(); db.showDB(); WriteLine(); "Deleting Key-Value pair from enumerable database".title(); "Element with key=enum_one will be deleted from database".title(); "Element with key=enum_one:".title(); DBElement<string, List<string>> remove_enum_element = new DBElement<string, List<string>>(); enum_db.getValue("enum_one", out remove_enum_element); remove_enum_element.showEnumerableElement(); enum_db.remove("enum_one"); WriteLine(); "Modified enumerable Database: ".title(); enum_db.showEnumerableDB(); WriteLine(); }
//----< Demonstrating req 4 - editing metadata,value instance of key/value collection primitive type>------------------- public void TestR4_NonPrimitive(DBEngine <int, DBElement <int, string> > dbType1, DBEngine <string, DBElement <string, List <string> > > dbType2, DBItemEditor editor) { "\nDemonstrating Requirement #4 Updating Metadata - Collection Type DB".title(); IEnumerable <string> db2Keys = dbType2.Keys(); String firstDB2Key = db2Keys.ElementAt(0); String secondDB2Key = db2Keys.ElementAt(1); WriteLine("\n\n Before updating Metadata for key : " + firstDB2Key); dbType2.showEnumerableDB(); WriteLine("\n\n After updating Metadata for key : " + firstDB2Key); editor.updateMetadataInfo <string, List <string>, string>(dbType2, firstDB2Key, "Django Unchained Reborn", " German Hunter helps to resuce a slave wife"); dbType2.showEnumerableDB(); "\nDemonstrating Requirement #4 Editing Value Instance Info - Collection Type DB".title(); WriteLine("\n\n Before updating Value Instance for key : " + secondDB2Key); dbType2.showEnumerableDB(); WriteLine("\n\n After updating Value Instance for key : " + secondDB2Key); DBElement <string, List <string> > newerelem3 = new DBElement <string, List <string> >(); newerelem3.name = "3 Idiots Remade"; newerelem3.descr = " They think differently, even as the rest of the world called them idiots"; newerelem3.children.AddRange(new[] { "Django Unchained Remake" }); newerelem3.payload = new List <string> { "Rajkumar Hirani", "Amir Khan", "Abhijat Joshi" }; editor.updatePayloadInfo <string, List <string>, string>(dbType2, newerelem3, secondDB2Key); dbType2.showEnumerableDB(); "\nDemonstrating Requirement #4 Addition of child instances - Collection DB".title(); WriteLine("\n\n Before adding child Instance :" + secondDB2Key + " to key : " + firstDB2Key); dbType2.showEnumerableDB(); editor.addChildren <string, List <string>, string>(dbType2, firstDB2Key, secondDB2Key); WriteLine("\n\n After adding child Instance :" + secondDB2Key + " to key :" + firstDB2Key); dbType2.showEnumerableDB(); "\nDemonstrating Requirement #4 Removal of child instances - Collection DB".title(); string keyChild = "Django Unchained Remake"; WriteLine("\n\n Before removing child Instance :" + keyChild + " from key :" + secondDB2Key); dbType2.showEnumerableDB(); editor.removeChildren <string, List <string>, string>(dbType2, secondDB2Key, "Django Unchained Remake"); WriteLine("\n\n After removing child Instance :" + keyChild + " from key :" + secondDB2Key); dbType2.showEnumerableDB(); }
// Main Method to test the implementation of DBElement Package static void Main(string[] args) { "Testing DBElement Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); WriteLine(); // new elements for int and string database DBElement<int, string> elem1 = new DBElement<int, string>(); Write(elem1.showElement<int, string>()); WriteLine(); DBElement<int, string> elem2 = new DBElement<int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; Write(elem2.showElement<int, string>()); WriteLine(); var elem3 = new DBElement<int, string>("Luke Skywalker", "Young HotShot"); elem3.children = new List<int> { 1, 2, 7 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; Write(elem3.showElement<int, string>()); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); WriteLine(); //new elements for string and list of strings database 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>, string>()); WriteLine(); DBElement<string, List<string>> newerelem1 = new DBElement<string, List<string>>(); newerelem1.name = "newerelem1"; newerelem1.descr = "same stuff"; newerelem1.children.Add("first_key"); newerelem1.children.Add("second_key"); newerelem1.payload = new List<string> { "alpha", "beta", "gamma" }; newerelem1.payload.AddRange(new[] { "delta", "epsilon" }); Write(newerelem1.showElement<string, List<string>, string>()); WriteLine(); Write("\n\n"); }
//private DBEngine<int, DBElement<int, List<int>>> keysFromQuery = new DBEngine<int, DBElement<int, List<int>>>(); public string query(XDocument message) {//method used to handle queries string reply; List<int> children = new List<int>(); XElement element = message.Element("Message").Element("QueryType"); Query<int, int, string> query1 = new Query<int, int, string>(db); switch(element.Value){ case "the value of specified key": element = message.Element("Message").Element("Key"); DBElement<int, string> elem = new DBElement<int, string>(); query1.checkValueByKey(Int32.Parse(element.Value), out elem); reply = ("The value of specified key " + element.Value + " is\n" + elem.showElement<int, string>()); break; case "the children of specified key": element = message.Element("Message").Element("Key"); children = query1.childrenByKey(Int32.Parse(element.Value)); reply = ("The children of specified key " + element.Value + " is\n"); reply = this.addChildToStr(children, reply); break; case "the keys share a same pattern": element = message.Element("Message").Element("Pattern"); Query<string, int, string> queryString = new Query<string, int, string>(dbString); List<string> keyString = new List<string>(); keyString = queryString.keysWithPattern(dbString,element.Value); reply = ("The keys share a same pattern \"" + element.Value + "\" is\n"); foreach(var key in keyString) reply += (String.Format("{0}\n", key.ToString())); break; case "the keys share same pattern in their metadata": element = message.Element("Message").Element("Pattern"); children = query1.keysSameMdataPattern(element.Value); reply = ("The keys share same pattern " + element.Value + " is\n"); reply = this.addChildToStr(children, reply); break; case "the keys of value created in the same time interval": List<DateTime> dts = new List<DateTime>(); dts = this.getDTS(message); children = query1.keysSameTinterval(dts[0], dts[1]); reply = ("The keys of value created in the same time interval between " + dts[0].ToString() + " and " + dts[1]).ToString()+"\n"; reply = this.addChildToStr(children, reply); break; default: reply = ("Invalid editing type."); break; } return reply; }
//----< Serialize value of a Key >--------------------------------------- public string SerializeValue <Key, Value>(DBElement <Key, Value> element) { string xml = String.Format("<Value>\n<name>{0}</name>\n<descr>{1}</descr>\n<timestamp>{2}</timestamp>\n<payload>{3}</payload>\n", element.name, element.descr, element.timeStamp, element.PayloadToString()); if (element.children.Count > 0) { xml += "<children>\n"; foreach (Key child in element.children) { xml += String.Format("<child>{0}</child>\n", child.ToString()); } xml += "</children>"; } xml += "</Value>"; return(xml); }
//----< Demonstrating req 4 - editing metadata,value instance of key/value database primitive type>------------------- public void TestR4(DBEngine <int, DBElement <int, string> > dbType1, DBEngine <string, DBElement <string, List <string> > > dbType2, DBItemEditor editor) { "\n\nDemonstrating Requirement #4 Updating Metadata - Primitive Type DB".title(); IEnumerable <int> keys = dbType1.Keys(); int firstDB1Key = keys.ElementAt(0); int secondDB1Key = keys.ElementAt(1); WriteLine("\n\n Before updating Metadata for key : " + firstDB1Key); dbType1.showDB(); WriteLine("\n\n After updating Metadata for key : " + firstDB1Key); editor.updateMetadataInfo <int, String>(dbType1, firstDB1Key, "Reborn -Cast Away", "The guy who survived in deserted insland"); dbType1.showDB(); "\nDemonstrating Requirement #4 Editing Value Instance Info - Primitive Type DB".title(); WriteLine("\n\n Before updating Value Instance for key : " + secondDB1Key); dbType1.showDB(); WriteLine("\n\n After updating Value Instance for key : " + secondDB1Key); DBElement <int, string> elem2 = new DBElement <int, string>(); elem2.name = "Titanic Reborn"; elem2.descr = "A new movie directed in 2015 with the same plot line"; elem2.timeStamp = DateTime.Now; elem2.children.AddRange(new List <int> { 114 }); elem2.payload = "The movie will feature same actors but director changes"; editor.updatePayloadInfo <int, String>(dbType1, elem2, secondDB1Key); dbType1.showDB(); "\nDemonstrating Requirement #4 Addition of child instances - Primitive Type DB ".title(); WriteLine("\n\n Before adding child Instance " + secondDB1Key + " to key " + firstDB1Key); dbType1.showDB(); editor.addChildren <int, string>(dbType1, firstDB1Key, secondDB1Key); WriteLine("\n\n After adding child Instance : " + secondDB1Key + " to key " + firstDB1Key); dbType1.showDB(); "\nDemonstrating Requirement #4 Removal of child instances - Primitive DB ".title(); WriteLine("\n\n Before removing child Instance key " + 113 + " from key " + firstDB1Key); dbType1.showDB(); editor.removeChildren <int, string>(dbType1, firstDB1Key, 113); WriteLine("\n\n After removing child Instance key " + 113 + " from key " + firstDB1Key); dbType1.showDB(); }
public void WriteToDBEngine() { List<int> keyList = new List<int>(); var q = from x in xml.Elements("noSqlDb") .Elements("key") select x; foreach (var elem in q) { int key = Int32.Parse(elem.Value.Substring(3)); keyList.Add(key); } int i = 0; var p = from x in xml.Elements("noSqlDb") .Elements("element") select x; foreach (var elem in p) { temp = new DBElement<int, string>(); // add the data to objcet temp.name = (elem as XElement).Descendants("name").First().Value; temp.descr = (elem as XElement).Descendants("descr").First().Value; temp.timeStamp = Convert.ToDateTime((elem as XElement).Descendants("timeStamp").First().Value); temp.payload = (elem as XElement).Descendants("payload").First().Value; var a = from x in (elem as XElement).Elements("children").Descendants() select x; foreach (var child in a) { temp.children.Add(Int32.Parse(child.Value.Substring(3))); } db.insert(keyList[i], temp); i++; } //Console.Write("\n {0}", elem.Value); //Console.Write("\n\n"); }
/* * It is a quirk of the XDocument class that the XML declaration, * a valid element, cannot be added to the XDocument's element * collection. Instead, it must be assigned to the document's * Declaration property. */ /* * we are creating XElements for each DBElement<int, string> in * DBEngine<int, DBElement<int, string>> using XElement object * We are saving XMl file to ~/TestExec/bin/debug/Test_DB.xml */ public static void create_xml_from_db(this DBEngine<int, DBElement<int, string>> db, Boolean persist) { XDocument xml = new XDocument(); xml.Declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("Test DB data to XML"); xml.Add(comment); XElement root = new XElement("noSQL"); xml.Add(root); XElement keytype = new XElement("keytype", "integer"); root.Add(keytype); XElement payloadtype = new XElement("payloadtype", "string"); root.Add(payloadtype); foreach (var db_key in db.Keys()) { DBElement<int, string> ele = new DBElement<int, string>(); db.getValue(db_key, out ele); XElement key = new XElement("key", db_key); root.Add(key); XElement element = new XElement("element"); XElement name = new XElement("name", ele.name); XElement descr = new XElement("descr", ele.descr); XElement timestamp = new XElement("timestamp", ele.timeStamp); XElement children = new XElement("children"); XElement payload = new XElement("payload", ele.payload); foreach (int x in ele.children) { XElement children_key = new XElement("key", x); children.Add(children_key); } element.Add(name); element.Add(descr); element.Add(timestamp); element.Add(children); element.Add(payload); root.Add(element); } WriteLine(); //<--------Writing to XML---------> "Creating XML file using XDocument and writing into Test_DB.xml".title(); xml.Save("Test_DB.xml"); display_xml(xml, persist); WriteLine(); }
void TestR2() { DBElement<int, string> elem = new DBElement<int, string>(); elem.name = "India"; elem.descr = "Country"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List<int> { 2, 3 }); elem.payload = "Famous cricket player"; WriteLine(); "1) The element is as shown below".title(); elem.showElement(); db.insert(1, elem); WriteLine(); "2) The key Value pair is shown below".title(); db.showDB(); WriteLine(); }
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(); }
static void Main(string[] args) { "Testing DBFactory Package".title('='); WriteLine(); DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); DBElement <int, string> elem = new DBElement <int, string>(); elem.name = "Name"; elem.descr = "Description"; elem.timeStamp = DateTime.Now; elem.children.Add(1); elem.payload = "Payload"; db.insert(1, elem); DBFactory <int, DBElement <int, string>, string> dbFact = new DBFactory <int, DBElement <int, string>, string>(new List <int> { 1 }, db); dbFact.showDBF(dbFact); }
public void WriteToDBEngine() { List<int> keyList = new List<int>(); var q = from x in xml.Elements("noSqlDb") .Elements("key") select x; foreach (var elem in q) { int key = Int32.Parse(elem.Value.Substring(3));//read all keys and store them to a list keyList.Add(key); } int i = 0; var p = from x in xml.Elements("noSqlDb") .Elements("element") select x; foreach (var elem in p) { temp = new DBElement<int, string>(); temp.name = (elem as XElement).Descendants("name").First().Value;//read and store name to element temp.descr = (elem as XElement).Descendants("descr").First().Value;//read and store description to element temp.timeStamp = Convert.ToDateTime((elem as XElement).Descendants("timeStamp").First().Value);//read and store timeStamp to element temp.payload = (elem as XElement).Descendants("payload").First().Value;//read payload and store it to element var a = from x in (elem as XElement).Elements("children").Descendants() select x; foreach (var child in a)//store key/value pairs into database { temp.children.Add(Int32.Parse(child.Value.Substring(3))); } db.insert(keyList[i], temp); i++; } }
//----< Helper function to persist payload and children info to a xml file>------------------- private static void persist_child_payload_primtive <Key, Value, Data>(DBElement <Key, Data> elem, XElement elementNode) { if (elem.children.Count() > 0) { XElement childrensNode = new XElement("children"); elementNode.Add(childrensNode); foreach (Key childrenkeys in elem.children) { XElement childrenkey = new XElement("key"); childrenkey.SetValue(childrenkeys.ToString()); childrensNode.Add(childrenkey); } } if (elem.payload != null) { XElement payLoadNode = new XElement("payload"); payLoadNode.SetValue(elem.payload); elementNode.Add(payLoadNode); } }
static void Main(string[] args) { DBElement<int, string> elem = new DBElement<int, string>(); elem.name = "2015 Maserati GranTurismo"; elem.descr = "Ferrari FF new model 2015"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List<int> { 1, 2, 3, 4 }); elem.payload = "Make:Maserati;Model:Maserati GranTurismo;Color:Green;Year:2015;"; db.insert(54, elem); string path = ""; "Demonstrating Requirement #6".title(); Console.WriteLine(); Console.WriteLine("..Showing the Scheduled Persistance Process..\n"); Console.Write("..Wait for 3 seconds..\n\n"); Scheduler sch = new Scheduler(); sch.PersistXML(db); //Persist to XML XmlTextReader textReader = new XmlTextReader("PersistXMLFile.xml"); path = textReader.BaseURI; textReader.Close(); Console.WriteLine("..Persisted Elements successfully to location '{0}'..\n", path); }
static void Main(string[] args) { /* * Create and edit a DBEngine first. * Then create a DBFactory using the DBEngine. */ "Testing DBEngine Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); DBElement<int, string> elem1 = new DBElement<int, string>(); elem1.payload = "a payload"; Write(elem1.showElement<int, string>()); WriteLine(); DBElement<int, string> elem2 = new DBElement<int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; Write(elem2.showElement<int, string>()); WriteLine(); 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); Write("\n --- Create DBFactory<int, DBElement<int, string>> from DBEngine<int,DBElement<int, string>> ---"); DBFactory<int, DBElement<int, string>> dbFactory = new DBFactory<int, DBElement<int, string>>(db); foreach (int dbKey in dbFactory.Keys()) { DBElement<int, string> value; dbFactory.getValue(dbKey, out value); value.showElement(); } dbFactory.showDB(); Write("\n\n"); }
/* Defining a query using lambda function to search specific key */ public bool key_value_search(DBEngine<string, DBElement<string, List<string>>> db, out IQuery<string, DBElement<string, List<string>>> i_query, QueryEngine<string, DBElement<string, List<string>>> qe, string key_to_search = "12345") { "Query for value with specified key (key = element2):".title(); WriteLine(); Func<string, string, bool> keyValueSearch = (string key, string search) => //lambda function { if (!db.Keys().Contains(key)) return false; else { if (key == (search)) { DBElement<string, List<string>> ele = new DBElement<string, List<string>>(); db.getValue(key, out ele); return true; } else { return false; } } }; // pass query to query engine and call simpleQuery to make query on DBEngine if (qe.simpleQuery(keyValueSearch, key_to_search.ToString(), out i_query)) { WriteLine(); foreach (var key in i_query.Keys()) { DBElement<string, List<string>> temp = new DBElement<string, List<string>>(); i_query.getValue(key, out temp); WriteLine("key : {0}", key); temp.showEnumerableElement(); WriteLine(); } return true; } else { return false; } }
void TestR4(){//support editing of value including the addition and/or deletion of relationships, //editing text metadata and replacing an existing value's instance with a new instance "Demonstrating Requirement #4".title(); DBElement<int, string> temp = new DBElement<int, string>(); ItemEditor<int, string> editItem; if (db.containsKey(2)){ db.getValue(2, out temp); Write("\n\n --- value before modified---\n"); temp.showElement(); editItem = new ItemEditor<int, string>(temp); editItem.nameEdit("newName!!");//edit the name of the value with key 2 editItem.descrEdit("new description!!");//edit description editItem.dateTimeEdit();//update timeStamp editItem.addRelationship(18);//add relationship editItem.deleteRelationship(6);//delete relationship editItem.payloadEdit("new payload!!");//modify payload DBElement<int, string> elemNew = new DBElement<int, string>(); editItem.replaceWithInstance(out elemNew);// replace an existing value's instance with a new instance temp = null; Write("\n\n --- value after modified---\n"); elemNew.showElement(); editItem = null; } else Write("Value not found!"); //Write("\n\n Show key/value pairs in data base:\n"); //db.showDB(); WriteLine(); WriteLine(); }
public static void showEnumerableElement(this DBElement <string, List <string> > enumElement) { Console.Write(enumElement.showElement <string, List <string>, string>()); }
public static void showElement(this DBElement <int, string> element) { Console.Write(element.showElement <int, string>()); }
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"); }
//----< add key value pair info for primitive type db >------------------------------------- public bool addKeyValyePair <Key, Data>(DBEngine <Key, DBElement <Key, Data> > dbType2, DBElement <Key, Data> dbElem, Key keyVal) { bool res = dbType2.insert(keyVal, dbElem); if (res) { dbType2.numberOfWrties++; if (dbType2.numberOfWrties >= dbType2.max_writes) { dbType2.numberOfWrties = 0; trigger_primitive_storage <Key, Data>(dbType2); } } return(res); }
//----< add key value pair info for collection type db >------------------------------------- public bool addKeyValyePair <Key, Data, T>(DBEngine <Key, DBElement <Key, Data> > dbType2, DBElement <Key, Data> dbElem, Key keyVal) where Data : IEnumerable <T> { bool res = dbType2.insert(keyVal, dbElem); if (res) { dbType2.numberOfWrties++; if (dbType2.numberOfWrties >= dbType2.max_writes) { dbType2.numberOfWrties = 0; trigger_collection_storage <Key, Data, T>(dbType2); } } return(res); }
static void Main(string[] args) { "Demonstrating Persist Engine".title('='); DBEngine <int, DBElement <int, string> > dbType1 = new DBEngine <int, DBElement <int, string> >(); DBEngine <string, DBElement <string, List <string> > > dbType2 = new DBEngine <string, DBElement <string, List <string> > >(); //Demonstrating primitive type DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.name = "Jurassic World"; elem1.descr = "Story on escape from giant creatures"; elem1.timeStamp = DateTime.Now; elem1.payload = "A giant creature attacks the park and becomes a killing machine"; dbType1.insert(DBElementExtensions.generate_int_key(), elem1); DBElement <int, string> elem2 = new DBElement <int, string>(); elem2.name = "Cast Away"; elem2.descr = "Story of surviving a crash landing on a deserted island."; elem2.timeStamp = DateTime.Now; elem2.children.AddRange(new List <int> { 4, 5 }); elem2.payload = "Directed by Robert Zemeckis and written by Willian Broyles Jr."; dbType1.insert(DBElementExtensions.generate_int_key(), elem2); dbType1.showDB(); //Demostrating IEnumberable Type DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >(); newerelem1.name = "Movie Name - The Good the Bad and the Ugly"; newerelem1.descr = "A bounty hunting scam joins two men in an uneasy alliance "; newerelem1.payload = new List <string> { "Clint Eastwood", " Eli Wallach", "Lee Van Cleef" }; String key = "The Good, the Bad and the Ugly"; dbType2.insert(key, newerelem1); DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >(); newerelem2.name = "Movie Name - Django Unchained"; newerelem2.descr = "With the help of a German hunter, a freed slave sets to rescue"; newerelem2.children.AddRange(new[] { key, "Life Is Beautiful" }); newerelem2.payload = new List <string> { "Jamie Foxx", "Christoph Waltz", "Leonardo DiCaprio" }; newerelem2.payload.Add("Quentin Tarantino"); String key1 = "Django Unchained"; dbType2.insert(key1, newerelem2); dbType2.showEnumerableDB(); string xmlFile1 = "nosqdldb_primitive.xml"; dbType1.persist_db <int, DBElement <int, string>, string>(xmlFile1); WriteLine("\nSuccesfully persisted dbengine contents to xml file :" + xmlFile1); string xmlFile2 = "nosqdldb.xml"; dbType2.persist_db <string, DBElement <string, List <string> >, List <string>, string>(xmlFile2); WriteLine("\nSuccesfully persisted dbengine contents to xml file : " + xmlFile2); string xmlFile3 = "read_nosqldb_primitive.xml"; WriteLine("\n\n Before Augumenting DB from xml file : " + xmlFile3); dbType1.showDB(); dbType1.augument_db <int, DBElement <int, string>, string>(xmlFile3); WriteLine("\n\n After Augumenting DB from xml file : " + xmlFile3); dbType1.showDB(); string xmlFile4 = "read_nosqdldb.xml"; WriteLine("\n\n Before Augumenting DB from xml file : " + xmlFile4); dbType2.showEnumerableDB(); dbType2.augument_db <string, DBElement <string, List <string> >, List <string>, string>(xmlFile4); WriteLine("\n\n After Augumenting DB from xml file : " + xmlFile4); dbType2.showEnumerableDB(); DBEngine <int, DBElement <int, string> > dbType1New = new DBEngine <int, DBElement <int, string> >(); DBEngine <string, DBElement <string, List <string> > > dbType2New = new DBEngine <string, DBElement <string, List <string> > >(); string xmlFile5 = "read_nosqldb_primitive.xml"; WriteLine("\n\n Before Restoring DB from xml file : " + xmlFile5); dbType1New.showDB(); dbType1New.augument_db <int, DBElement <int, string>, string>(xmlFile5); WriteLine("\n\n After Restoring DB from xml file : " + xmlFile5); dbType1New.showDB(); string xmlFile6 = "read_nosqdldb.xml"; WriteLine("\n\n Before Restoring DB from xml file : " + xmlFile6); dbType2New.showEnumerableDB(); dbType2New.augument_db <string, DBElement <string, List <string> >, List <string>, string>(xmlFile6); WriteLine("\n\n After Restoring DB from xml file : " + xmlFile6); dbType2New.showEnumerableDB(); }
static void Main(string[] args) { DBEngine <int, DBElement <int, string> > dbType1 = new DBEngine <int, DBElement <int, string> >(); DBEngine <string, DBElement <string, List <string> > > dbType2 = new DBEngine <string, DBElement <string, List <string> > >(); DBItemEditor editor = new DBItemEditor(); //Demonstrating primitive type "\nDemonstrating Requirement #2 - Primitive Type DB".title(); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.name = "Jurassic World"; elem1.descr = "Story on escape from giant creatures"; elem1.timeStamp = DateTime.Now; elem1.payload = "A giant creature attacks the park and becomes a killing machine"; editor.addKeyValyePair <int, String>(dbType1, elem1, DBElementExtensions.generate_int_key()); DBElement <int, string> elem2 = new DBElement <int, string>(); elem2.name = "Cast Away"; elem2.descr = "Story of surviving a crash landing on a deserted island."; elem2.timeStamp = DateTime.Now; elem2.children.AddRange(new List <int> { 4, 5 }); elem2.payload = "Directed by Robert Zemeckis and written by Willian Broyles Jr."; editor.addKeyValyePair <int, String>(dbType1, elem2, DBElementExtensions.generate_int_key()); dbType1.showDB(); Console.WriteLine("\n\n Before updating metadata"); IEnumerable <int> keys1 = dbType1.Keys(); int first = keys1.First(); dbType1.showDB(); Console.WriteLine("\n\n After updating metadata"); editor.updateMetadataInfo <int, String>(dbType1, first, "Reborn -Cast Away", "The guy who survived in deserted insland"); dbType1.showDB(); IEnumerable <int> keys = dbType1.Keys(); int firstDB1Key = keys.ElementAt(0); int secondDB1Key = keys.ElementAt(1); DBElement <int, string> elem22 = new DBElement <int, string>(); elem22.name = "Titanic Reborn"; elem22.descr = "A new movie directed in 2015 with the same plot line"; elem22.timeStamp = DateTime.Now; elem22.children.AddRange(new List <int> { 1 }); elem22.payload = "The movie will feature same actors but director changes"; editor.updatePayloadInfo <int, String>(dbType1, elem22, secondDB1Key); Console.WriteLine("\n\n Before adding child Instance " + secondDB1Key + " from key " + firstDB1Key); dbType1.showDB(); editor.addChildren <int, string>(dbType1, firstDB1Key, secondDB1Key); Console.WriteLine("\n\n After adding child Instance : " + secondDB1Key + " from key " + firstDB1Key); dbType1.showDB(); Console.WriteLine("\n\n Before removing child Instance key " + 114 + " from key " + firstDB1Key); dbType1.showDB(); editor.removeChildren <int, string>(dbType1, firstDB1Key, 114); Console.WriteLine("\n\n After removing child Instance key " + 114 + " from key " + firstDB1Key); dbType1.showDB(); }