//----< Demonstrating req 7 - Queries - Value, children of a key>------------------- public void TestR7(DBEngine <int, DBElement <int, string> > dbType1, DBEngine <string, DBElement <string, List <string> > > dbType2) { "\n Demonstrating Requirement #7 - Queries - Primitive DB Contents".title(); dbType1.showDB(); "\n Demonstrating Requirement #7 - Queries - Collection DB Contents".title(); dbType2.showEnumerableDB(); QueryEngine <int, string> queryEnginePrimitive = new QueryEngine <int, string>(dbType1); "\n Demonstrating Requirement #7A - The value of a specified key - Primitive DB".title(); DBElement <int, string> queryElementPrimitive; IEnumerable <int> dbType1keys = dbType1.Keys(); int lastKeyDB1 = dbType1keys.Last(); Write("\n Input Key :" + lastKeyDB1); Write("\n Value of Key \n"); queryEnginePrimitive.getValueForKey(lastKeyDB1, out queryElementPrimitive); queryElementPrimitive.showElement(); QueryEngine <string, List <string> > queryEngine = new QueryEngine <string, List <string> >(dbType2); "\n Demonstrating Requirement #7A - The value of a specified key - Collection DB".title(); DBElement <string, List <string> > queryElement; IEnumerable <string> keys = dbType2.Keys(); String lastKey = keys.Last(); Write("\n Input Key :" + lastKey); Write("\n Value of Key \n"); queryEngine.getValueForKey(lastKey, out queryElement); queryElement.showEnumerableElement(); "\n Demonstrating Requirement #7B - The children of a specified key - Primitive DB".title(); List <int> childrenDB1 = new List <int>(); IEnumerable <int> keys1 = dbType1.Keys(); int firstKey1 = keys1.First(); Write("\n Input Key :" + firstKey1); StringBuilder accum = new StringBuilder(); accum.Append(String.Format(" children of key: {0}", firstKey1.ToString())); queryEnginePrimitive.getChildren(firstKey1, out childrenDB1); childrenDB1.showkeys(); "\n Demonstrating Requirement #7B- The children of a specified key - Collection DB".title(); List <String> children; IEnumerable <string> keys2 = dbType2.Keys(); String firstKey2 = keys2.First(); Write("\n Input Key :" + firstKey2); StringBuilder accum2 = new StringBuilder(); accum2.Append(String.Format(" children of key: {0}", firstKey2)); queryEngine.getChildren(firstKey2, out children); children.showkeys(); String lastKey2 = keys2.Last(); Write("\n\n Input Key :" + lastKey2); StringBuilder accum3 = new StringBuilder(); accum3.Append(String.Format(" children of key: {0}", lastKey2)); queryEngine.getChildren(lastKey2, out children); children.showkeys(); }
//----< 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(); }
//----< 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(); }
//----< 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()); } }
//----< 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"); } }
//----< 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(); }
//----< 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(); }
//----< process query using queryPredicate >----------------------- bool processQuery(Func <Key, bool> queryPredicate, DBFactory <Key, Data> dbFactory) { bool res = false; foreach (var key in db.Keys()) { if (queryPredicate(key)) { res = true; dbFactory.addKey(key); } } if (res) { return(true); } return(false); }
//----<Demonstrating categories >------------------- public void testR12() { DBEngine <string, DBElement <string, List <string> > > dbType2New = new DBEngine <string, DBElement <string, List <string> > >(); string dir = "..\\..\\..\\..\\input_xml\\"; string xmlFile6 = "categories.xml"; "\n\nDemonstrating Requirement #12 - Categories DB".title(); WriteLine("\n\n Creating Categories DB from xml file : " + xmlFile6); dbType2New.augument_db <string, DBElement <string, List <string> >, List <string>, string>(dir + xmlFile6); dbType2New.showEnumerableDB(); "\n\nQueries on Categories DB".title(); String inputKey = "Food Products"; QueryEngine <string, List <string> > queryEngine = new QueryEngine <string, List <string> >(dbType2New); DBElement <string, List <string> > queryElement; IEnumerable <string> keys = dbType2New.Keys(); String first = keys.First(); queryEngine.getValueForKey(first, out queryElement); List <String> values = queryElement.payload; Write("\nList of keys for the db items in category \"" + inputKey + "\" are "); foreach (String key in values) { Write(" {0}, ", key); } List <String> children; String child = values[values.Count - 2]; queryEngine.getChildren(child, out children); Write("\nList of categories to which \"" + values[values.Count - 2] + "\" belong are "); foreach (String key1 in children) { Write(" {0}, ", key1); } WriteLine("\n"); }
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); DBElement <int, string> queryElementPrimitive; IEnumerable <int> dbType1keys = dbType1.Keys(); int lastKeyDB1 = dbType1keys.Last(); Write("\n\n\n Input Key :" + lastKeyDB1); queryEnginePrimitive.getValueForKey(lastKeyDB1, out queryElementPrimitive); queryElementPrimitive.showElement(); List <int> childrenDB1 = new List <int>(); IEnumerable <int> keys1 = dbType1.Keys(); int lastKey1 = keys1.Last(); Write("\n\n\n Input Key :" + lastKey1); StringBuilder accum = new StringBuilder(); accum.Append(String.Format(" children of key: {0}", lastKey1.ToString())); queryEnginePrimitive.getChildren(lastKey1, out childrenDB1); childrenDB1.showkeys(); Write("\n\n\n "); 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); } }
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(); }