/*-------------------Get the chidren of a particular key-----------------*/ public List <Key> getChildrenOfKey(Key key) { try { if (db.Contains(key)) { dynamic elem1 = db.getValueOfKey(key); return(elem1.children); } else { WriteLine("\nKey is not present in the dictionary\n"); return(default(List <Key>)); } } catch (Exception e) { WriteLine("\n" + e.Message + "\n"); return(default(List <Key>)); } }
/*-----------------Function to persist the database contents to XML doc------------------*/ public void persistToXML(dynamic keys) { try { XDocument xmlDoc = new XDocument(); XElement database = new XElement("Database"); xmlDoc.Add(database); XElement record = new XElement("Record"); database.Add(record); foreach (var item in keys) { XElement key = new XElement("Key", item); database.Add(key); dynamic elem2 = db.getValueOfKey(item); XElement value = new XElement("Value"); database.Add(value); XElement name = new XElement("name", elem2.name); value.Add(name); XElement descr = new XElement("descr", elem2.descr); value.Add(descr); XElement timeStamp = new XElement("timeStamp", elem2.timeStamp); value.Add(timeStamp); XElement children = new XElement("children"); value.Add(children); foreach (var c in elem2.children) { XElement child = new XElement("child", c); children.Add(child); } XElement payload = new XElement("payload", elem2.payload); value.Add(payload); } saveXML(xmlDoc); } catch (Exception e) { Console.WriteLine("\n" + e.Message + "\n"); } }
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:"); } }
/*-------------------Function to edit the name and description metadata---------------*/ public bool editMetaData(Key key, string name, string description) { try { if (db.Contains(key)) { dynamic elem1 = db.getValueOfKey(key); elem1.name = name; elem1.descr = description; return(true); } else { WriteLine("\nKey is not present in the dictionary\n"); return(false); } } catch (Exception e) { WriteLine("\n" + e.Message + "\n"); return(false); } }
static void Main(string[] args) { "Testing DBFactory Package".title('='); WriteLine(); DBFactoryTest dbft = new DBFactoryTest(); "\nCreation of immutable database".title(); WriteLine(); "\nOriginal database".title(); DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); dbft.insertData(db); "\n Fetch all the keys which are even from the above database".title(); try { QueryEngine <int, DBElement <int, string> > qEngine = new QueryEngine <int, DBElement <int, string> >(db); Dictionary <int, DBElement <int, string> > dictFactory = new Dictionary <int, DBElement <int, string> >(); DBFactory <int, DBElement <int, string> > dbFactory; var keys = qEngine.getListKeyPattern(); if (keys != null) { foreach (var key in keys) { var val = db.getValueOfKey(key); dictFactory.Add(key, val); //add keys and values to the dictionary } dbFactory = new DBFactory <int, DBElement <int, string> >(dictFactory); //store the dictionary in the WriteLine("\nThe below key/value pairs with even keys pattern are saved as an immutable database\n"); dbFactory.showDB(); //display the immutable database WriteLine(); WriteLine(); } else { WriteLine("\nNo keys are obtained from a query for creation of immutable database\n"); } WriteLine(); WriteLine(); } catch (Exception e) { Console.WriteLine("\n" + e.Message + "\n"); } }