void TestR7() { "Demonstrating Requirement #7".title(); Write("\n\n --- Query the value of specified key Start---\n"); Query<int, int, string> query = new Query<int, int, string>(db); DBElement<int, string> elemR7 = new DBElement<int, string>(); query.checkValueByKey(2, out elemR7); elemR7.showElement(); Write("\n\n --- Query the value of specified key End---\n"); WriteLine(); /*****************************************************************/ Write("\n\n --- Query the children of specified key Start---\n"); Write("Children of value with key #2"); List<int> children = new List<int>(); children = query.childrenByKey(2); foreach(var child in children) Write("\n"+child); Write("\n\n --- Query the children of specified key End---\n"); /*****************************************************************/ Write("\n\n --- Query the keys share a same pattern Start---\n"); DBElement<int, string> elemR701 = new DBElement<int, string>(); elemR701.name = "r701"; elemR701.descr = "su cs"; elemR701.timeStamp = DateTime.Now; elemR701.children.AddRange(new List<int>{ 1, 2, 3 }); elemR701.payload = "cs"; dbString.insert("SU cs dep", elemR701); DBElement<int, string> elemR702 = new DBElement<int, string>(); elemR702.name = "r702"; elemR702.descr = "su math"; elemR702.timeStamp = DateTime.Now; elemR702.children.AddRange(new List<int>{ 4, 5, 2 }); elemR702.payload = "math"; dbString.insert("SU math dep", elemR702); DBElement<int, string> elemR703 = new DBElement<int, string>(); elemR703.name = "r703"; elemR703.descr = "cornell math"; elemR703.timeStamp = DateTime.Now; elemR703.children.AddRange(new List<int>{ 4, 5, 2 }); elemR703.payload = "music"; dbString.insert("CORNELL music dep", elemR703); IEnumerable<string> keys = dbString.Keys(); Write("All keys in this database:"); foreach(var key in keys) Write("\n"+key); Query<string, int, string> queryString = new Query<string, int, string>(dbString); List<string> keyString = new List<string>(); keyString = queryString.keysWithPattern(dbString,"SU"); Write("\n\nKeys with pattern SU:"); foreach(var key in keyString) Write("\n"+key); Write("\n\n --- Query the keys share a same pattern End---\n"); /*****************************************************************/ Write("\n\n --- Query the keys share a same pattern Start---\n"); db.showDB(); Write("\n\nKeys with pattern ‘description’ in there metadata(name & descr):"); List<int> keyslist = new List<int>(); keyslist = query.keysSameMdataPattern("description"); foreach(var key in keyslist) Write("\n"+key); Write("\n\n --- Query the keys share a same pattern End---\n"); /*****************************************************************/ Write("\n\n --- Query the keys of value created in the same time interval Start---\n"); DBElement<int, string> temp = new DBElement<int, string>(); db.getValue(7, out temp); temp.timeStamp = new DateTime(1991, 4, 15, 8, 9, 05); db.getValue(82, out temp); temp.timeStamp = new DateTime(1994, 9, 7, 10, 8, 55); db.showDB(); DateTime dt1 = new DateTime(1990, 4, 15, 5, 9, 05); DateTime dt2 = new DateTime(1995, 9, 7, 5, 8, 05); Write("\n\nkeys of value created after 1990/4/15 05:09:05 but before 1995/9/7 05:08:05:"); keyslist = query.keysSameTinterval(dt1, dt2); foreach(var key in keyslist) Write("\n"+key); Write("\n\n --- Query the keys of value created in the same time interval Start---\n"); }
void TestR8() { "Demonstrating Requirement #8".title(); Query<int, int, string> query = new Query<int, int, string>(db); List<int> keyslist = new List<int>(); DBElement<int, string> temp = new DBElement<int, string>(); db.getValue(7, out temp); temp.timeStamp = new DateTime(1991, 4, 15, 8, 9, 05); db.getValue(82, out temp); temp.timeStamp = new DateTime(1994, 9, 7, 10, 8, 55); db.showDB(); DateTime dt1 = new DateTime(1990, 4, 15, 5, 9, 05); DateTime dt2 = new DateTime(1995, 9, 7, 5, 8, 05); Write("\n\nQuery: keys of value created after 1990/4/15 05:09:05 but before 1995/9/7 05:08:05:"); keyslist = query.keysSameTinterval(dt1, dt2); QueryKeysDB<int> queryStore = new QueryKeysDB<int>(keysFromQuery, keyslist); queryStore.storeKeys(1, "key set #1", "keys of value created after 1990/4/15 05:09:05 but before 1995/9/7 05:08:05"); DBElement<int, List<int>> temp2 = new DBElement<int, List<int>>(); keysFromQuery.getValue(1, out temp2); List<int> keyslist2 = new List<int>(); keyslist2 = temp2.payload; Write("\n\nRead keys returned from the query in the new database\n"); foreach(var key in keyslist2) Write("\n"+key); WriteLine(); }
//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; }