public bool searchString(string pattern, DBEngine <Key, DBElement <Key, Data> > db, out List <Key> resultKeys) { //------------< Returns set of keys whose metadata contain the given string >------------- resultKeys = db.Keys().ToList <Key>(); resultKeys.Clear(); List <Key> Keys = db.Keys().ToList <Key>(); DBElement <Key, Data> elem = new DBElement <Key, Data>(); bool flag = false; foreach (Key key in Keys) { db.getValue(key, out elem); if ((elem.name.ToString().IndexOf(pattern) >= 0) || (elem.descr.ToString().IndexOf(pattern) >= 0) || (elem.timeStamp.ToString().IndexOf(pattern) >= 0)) { resultKeys.Add(key); flag = true; } else { foreach (var child in elem.children) //search each child for the string { if (child.ToString().IndexOf(pattern) >= 0) { resultKeys.Add(key); flag = true; } } } } if (flag) { return(true); } return(false); }
public bool searchInterval(string startDate, string EndDate, DBEngine<Key, DBElement<Key, Data>> db, out List<Key> resultKeys) { //----------< Returns the set of keys whose DateTimes are more //----------< than the given start date and less than the given //----------< end date. Default for end date is DateTime.Now >-------------- resultKeys = db.Keys().ToList<Key>(); resultKeys.Clear(); DateTime start = Convert.ToDateTime(startDate); //Source: https://msdn.microsoft.com/en-us/library/cc165448.aspx DateTime end; if (EndDate == "") end = DateTime.Now; else end = Convert.ToDateTime(EndDate); bool flag = false; List<Key> Keys = db.Keys().ToList<Key>(); DBElement<Key, Data> elem = new DBElement<Key, Data>(); foreach (Key key in Keys) { db.getValue(key, out elem); if ((elem.timeStamp >= start) && (elem.timeStamp <= end)) { resultKeys.Add(key); flag = true; } } if (flag) return true; return false; }
public bool searchPattern(Key key, DBEngine<Key, Value> db, out List<Key> resultKeys) { //-----------< Searched for keys starting with "Key". If default Keys are specified //-----------< which are -999 for int and "*" for string, the set of all keys for the //-----------< given db will be returned >----------------------------------------- resultKeys = db.Keys().ToList<Key>(); List<Key> Keys = db.Keys().ToList<Key>(); bool flag = false; if ((key.ToString().CompareTo("*") == 0) || (key.ToString().CompareTo("-999") == 0)) flag = true; else { resultKeys.Clear(); foreach (var item in Keys) { string itemString = item.ToString(); char firstChar = itemString.ToCharArray()[0]; if (key.ToString().CompareTo(firstChar.ToString()) == 0) { resultKeys.Add((Key)item); flag = true; } } } if (flag) return true; else return false; }
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); }
public void XMLRestoreLOS(string pathnameLOS, DBEngine <string, DBElement <string, List <string> > > dbLOS) { DBElement <string, List <string> > elemStringLOS; XDocument xdocLOS = XDocument.Load(pathnameLOS); var elemLOS = xdocLOS.Root.Elements("element"); //extracting all the elements from the xml file foreach (XElement i in elemLOS) { XElement keyNodeLOS = (XElement)i.PreviousNode; string keyLOS = keyNodeLOS.Value.ToString(); //we know that the key is of type string if (!dbLOS.Keys().Contains(keyLOS)) { elemStringLOS = new DBElement <string, List <string> >(); elemStringLOS.name = i.Element("name").Value.ToString(); elemStringLOS.timeStamp = Convert.ToDateTime(i.Element("timeStamp").Value.ToString()); //the format stored in the file may or may not be a string elemStringLOS.descr = i.Element("descr").Value.ToString(); elemStringLOS.payload = new List <string>() { }; //we know that the payload is List<string> elemStringLOS.children.Clear(); var childrenNode = i.Element("children").Elements("key"); foreach (var c in childrenNode) { elemStringLOS.children.Add(c.Value.ToString()); //since each child is a string } var payloadNode = i.Element("payload").Elements("item"); foreach (var p in payloadNode) { elemStringLOS.payload.Add(p.Value.ToString()); } dbLOS.insert(keyLOS, elemStringLOS); } } }
/* The restore functions capture elements from the XML files provided. * Key will always be the node preceeding the element node in our file * structure, so the previous node of element is our key for that element. * Then the keys and the corresponding values are extracted and entered into * the corresponding database. If a key is already present in the database, * I have chosen to discard it. */ public void XMLRestore(string pathname, DBEngine <int, DBElement <int, string> > db) { DBElement <int, string> elemString; XDocument xdoc = XDocument.Load(pathname); var elem = xdoc.Root.Elements("element"); //extracting all elements from the file foreach (XElement i in elem) { XElement keyNode = (XElement)i.PreviousNode; int key = Int32.Parse(keyNode.Value.ToString()); //key is of type int for this database, so we need to cast it if (!db.Keys().Contains(key)) { elemString = new DBElement <int, string>(); elemString.name = i.Element("name").Value.ToString(); elemString.timeStamp = Convert.ToDateTime(i.Element("timeStamp").Value.ToString()); //the format extracted from the file may or may not be a string elemString.descr = i.Element("descr").Value.ToString(); elemString.payload = i.Element("payload").Value.ToString(); //we know that the payload is string elemString.children.Clear(); var childrenNode = i.Element("children").Elements("key"); //since each child of the element is stored as a <key> in the file foreach (var c in childrenNode) { elemString.children.Add(Int32.Parse(c.Value)); } db.insert(key, elemString); } } }
void TestR9() { "Demonstrating Requirement #9".title(); DBElement <string, List <string> > pack1 = new DBElement <string, List <string> >("DBElement.cs", "package structure"); packages.insert("DBElement", pack1); DBElement <string, List <string> > pack2 = new DBElement <string, List <string> >("DBEngine.cs", "package structure"); packages.insert("DBEngine", pack2); DBElement <string, List <string> > pack3 = new DBElement <string, List <string> >("DBFactory.cs", "package structure"); pack3.children.AddRange(new List <string> { "DBEngine" }); packages.insert("DBFactory", pack3); DBElement <string, List <string> > pack4 = new DBElement <string, List <string> >("Dispaly.cs", "package structure"); pack4.children.AddRange(new List <string> { "DBElement", "DBEngine", "DBExtensions", "UtilityExtentions" }); packages.insert("Display", pack4); DBElement <string, List <string> > pack5 = new DBElement <string, List <string> >("PersistEngine.cs", "package structure"); pack5.children.AddRange(new List <string> { "DBElement", "DBEngine" }); packages.insert("PersistEngine", pack5); DBElement <string, List <string> > pack6 = new DBElement <string, List <string> >("QueryEngine.cs", "package structure"); pack6.children.AddRange(new List <string> { "DBElement", "DBEngine" }); packages.insert("QueryEngine", pack6); DBElement <string, List <string> > pack7 = new DBElement <string, List <string> >("Scheduler.cs", "package structure"); pack7.children.AddRange(new List <string> { "DBElement", "DBEngine", "PersistEngine" }); packages.insert("Scheduler", pack7); DBElement <string, List <string> > pack8 = new DBElement <string, List <string> >("TestExec.cs", "package structure"); pack8.children.AddRange(new List <string> { "DBElement", "DBEngine", "PersistEngine", "DBExtensions", "DBFactory", "Display", "QueryEngine", "Scheduler", "UtiltiyExtension" }); packages.insert("TestExec", pack8); foreach (var key in packages.Keys()) { DBElement <string, List <string> > elem; packages.getValue(key, out elem); elem.payload = new List <string>(); elem.payload.AddRange(new List <string> { "content" }); } PersistWapper <string, DBElement <string, List <string> > > persist = new StringAndStringList(packages); persist.writeToXML("project2.xml"); Write("\n write the package structure into XML file ./project2.xml"); WriteLine(); }
//----< 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()); } }
public bool getValue(Key key, DBEngine <Key, Value> db, out Value elemResult) { //----------< Returns the value of a specified key >------------------- if (db.Keys().Contains(key)) { db.getValue(key, out elemResult); return(true); } elemResult = default(Value); return(false); }
/* This constructor takes the given inputs, creates a new instance * of the Dictionary, and adds the values to it. * There is nothing more you can do to it except for calling the * constructor. There is an additional showDBF() function, which * can be used to show the immutable database if needed. */ public DBFactory(List<Key> keys, DBEngine<Key, Value> db) { dbStore = new Dictionary<Key, Value>(); Value value; foreach (Key key in keys) { if (!db.Keys().Contains(key)) { } db.getValue(key, out value); dbStore[key] = value; } }
// process the query according to the Func public bool processQuery(Func <Key, bool> queryPredicate, out List <Key> keyCollection) { keyCollection = new List <Key>(); foreach (var key in db.Keys()) { if (queryPredicate(key)) { keyCollection.Add(key); } } if (keyCollection.Count() > 0) { return(true); } return(false); }
public bool getChildren(Key key, DBEngine<Key, DBElement<Key, Data>> db, out List<Key> children) { //--------------< Returns the list of children for a specified key in a specified db >------------ children = new List<Key>() { }; DBElement<Key, Data> Result = new DBElement<Key, Data>(); if (db.Keys().Contains(key)) { children.Clear(); db.getValue(key, out Result); foreach (var child in Result.children) children.Add(child); return true; } Result = default(DBElement<Key, Data>); children = default(List<Key>); return false; }
public void XMLWriteLOS(DBEngine <string, DBElement <string, List <string> > > dbLOS, out string pathnameLOS) { pathnameLOS = "xmlDocLOS.xml"; XDocument docLOS = new XDocument(new XElement("nosqldbLOS")); docLOS.Declaration = new XDeclaration("1.0", "utf - 8", "yes"); XElement keyTypeLOS = new XElement("keytype", "string"); XElement payloadTypeLOS = new XElement("payloadtype", "ListOfString"); docLOS.Root.Add(keyTypeLOS); docLOS.Root.Add(payloadTypeLOS); DBElement <string, List <string> > ValLOS = new DBElement <string, List <string> >(); foreach (var keyLOS in dbLOS.Keys()) { XElement keyNodeLOS = new XElement("key", keyLOS); dbLOS.getValue(keyLOS, out ValLOS); XElement elementNodeLOS = new XElement("element"); elementNodeLOS.Add(new XElement("name", ValLOS.name)); elementNodeLOS.Add(new XElement("descr", ValLOS.descr)); elementNodeLOS.Add(new XElement("timeStamp", ValLOS.timeStamp.ToString())); XElement childrenNodeLOS = new XElement("children"); foreach (var item in ValLOS.children) { childrenNodeLOS.Add(new XElement("key", item)); } elementNodeLOS.Add(childrenNodeLOS); XElement payloadNodeLOS = new XElement("payload"); //since payload is List<string> for this type of database foreach (var item in ValLOS.payload) { payloadNodeLOS.Add(new XElement("item", item)); } elementNodeLOS.Add(payloadNodeLOS); docLOS.Root.Add(keyNodeLOS); docLOS.Root.Add(elementNodeLOS); } docLOS.Save(pathnameLOS); }