// Query for the value of specific key public bool KeyQuery(Key key, out DBElement <Key, Data> elem) { if (db.getValue(key, out elem)) { return(true); } return(false); }
/*void TestR3() * { * //----------< Demonstrating adding and removing of elements >----------- * "Demonstrating Requirement #3".title(); * WriteLine(); * "Adding <int, string> elements".title('-'); * DBElement<int, string> elem = new DBElement<int, string>(); * elem.name = "Element 1"; * elem.descr = "Test Element (int, string)"; * elem.timeStamp = DateTime.Now; * elem.children.AddRange(new List<int> { 1, 2, 3 }); * elem.payload = "Element 1's payload. (string)"; * WriteLine(elem.showElement()); * bool p1 = db.insert(1, elem); * WriteLine(); * DBElement<int, string> elem1 = new DBElement<int, string>(); * elem1.name = "Element 2"; * elem1.descr = "Again <int, string> but no children!"; * elem1.timeStamp = DateTime.Now; * elem1.children.Clear(); * elem1.payload = "Element 2's payload. (string)"; * WriteLine(elem1.showElement()); * bool p2 = db.insert(2, elem1); * WriteLine(); * DBElement<int, string> elem2 = new DBElement<int, string>(); * elem2.name = "Element 3"; * elem2.descr = "Test Element <int, string>. Different timeStamp."; * elem2.children.AddRange(new List<int> { 1, 2, 3 }); * elem2.timeStamp = DateTime.UtcNow; * elem2.payload = "Element 3's payload. (string)"; * WriteLine(elem2.showElement()); * WriteLine(); * bool p3 = db.insert(3, elem2); * "Adding <string, List<string>> elements".title('-'); * DBElement<string, List<string>> newelem = new DBElement<string, List<string>>(); * newelem.name = "New elem"; * newelem.descr = "Element 4. <string, List<string>>"; * newelem.timeStamp = DateTime.Now; * newelem.children.AddRange(new List<string> { "one", "two", "three" }); * newelem.payload = new List<string> { "one", "two", "three" }; * Write(newelem.showElement<string, List<string>, string>()); * bool p4 = dbLOS.insert("Four", newelem); * Console.WriteLine("\n\nInserting elements... "); * if (p1 && p2 && p3 && p4) * Console.WriteLine("All inserts succeeded"); * else * Console.WriteLine("\n\nAt Least one insert failed"); * "Database".title('-'); * db.show<int, DBElement<int, string>, string>(); * dbLOS.show<string, DBElement<string, List<string>>, List<string>, string>(); * WriteLine(); * "Removing elements".title(); * DBElement<int, string> pay = new DBElement<int, string>(); * db.remove(1, out pay); * Console.Write("\n\nRemoved element: {0}", pay.name); * WriteLine(); * db.remove(2, out pay); * Console.Write("\n\nRemoved element: {0}", pay.name); * WriteLine(); * db.remove(3, out pay); * Console.Write("\n\nRemoved element: {0}", pay.name); * WriteLine(); * "Database".title(); * db.show<int, DBElement<int, string>, string>(); * dbLOS.show<string, DBElement<string, List<string>>, List<string>, string>(); * WriteLine(); * }*/ void TestR4() { //----------< Demonstrating editing of items >------------- "Demonstrating Requirement #4".title(); WriteLine(); "Editing an item".title(); "Old Database".title(); db.show <int, DBElement <int, string>, string>(); dbLOS.show <string, DBElement <string, List <string> >, List <string>, string>(); WriteLine(); DBElement <string, List <string> > editElement = new DBElement <string, List <string> >(); dbLOS.getValue("Four", out editElement); editElement.name = "Element 4 (edited)"; editElement.descr = "New description"; editElement.timeStamp = DateTime.Now; editElement.children.Clear(); editElement.children.Add("five"); editElement.payload = new List <string> { "four", "five", "six" }; editElement.payload.Add("eight"); dbLOS.insert("Four", editElement); "New Database".title(); db.show <int, DBElement <int, string>, string>(); dbLOS.show <string, DBElement <string, List <string> >, List <string>, string>(); 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); }
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); }
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(); }
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; }
//----< 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); }
// the contructor of immutableDB, it takes a reference to database, // and takes a list of key indentify the key/value pair that should // be stored in immutable database. public immutableDB(DBEngine <Key, Value> db, List <Key> keys) { imDB = new DBEngine <Key, Value>(); foreach (var key in keys) { Value val; if (db.getValue(key, out val)) { imDB.insert(key, val); } } }
/* 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; } }
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); }
void TestR4() { "Demonstrating Requirement #4".title(); Write("\n --- show orginal database ---"); dbInt.showDB(); WriteLine(); Write("\n ---Add a child key 15 for key=3 ---"); DBElement <int, string> elem; dbInt.getValue(3, out elem); bool childadd = elem.addChild(15); if (childadd) { Write("\n the new child added successfully"); } else { Write("\n the child has already existed"); } dbInt.showDB(); WriteLine(); Write("\n --- Remove child key=15 from element key=3 ---"); bool childRemove = elem.removeChild(15); if (childRemove) { Write("\n the child has been removed"); } else { Write("\n the child does not exist"); } dbInt.showDB(); WriteLine(); Write("\n --- Replace an instance with new instance ---"); elem.payload = "The empire is failed"; dbInt.showDB(); WriteLine(); }
static void Main(string[] args) { "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(); 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(); 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.payload = new List <string> { "one", "two", "three" }; Write(newelem1.showElement <string, List <string> >()); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); 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"); Write(newerelem1.showElement <string, List <string>, string>()); WriteLine(); DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >(); newerelem2.name = "newerelem2"; newerelem2.descr = "better formatting"; newerelem1.children.AddRange(new[] { "first", "second" }); newerelem2.payload = new List <string> { "a", "b", "c" }; newerelem2.payload.Add("d"); newerelem2.payload.Add("e"); Write(newerelem2.showElement <string, List <string>, string>()); 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(), newerelem1); newdb.insert(skeyGen(), newerelem2); newdb.show <string, DBElement <string, List <string> >, List <string>, string>(); WriteLine(); "testing edits".title(); db.show <int, DBElement <int, string>, string>(); DBElement <int, string> editElement = new DBElement <int, string>(); db.getValue(1, out editElement); editElement.showElement <int, string>(); editElement.name = "editedName"; editElement.descr = "editedDescription"; db.show <int, DBElement <int, string>, string>(); WriteLine(); Write("\n\n"); "testing removal".title(); db.show <int, DBElement <int, string>, string>(); DBElement <int, string> pay = new DBElement <int, string>(); db.remove(1, out pay); Console.Write("\n\nRemoved element: {0}", pay.name); WriteLine(); newdb.show <string, DBElement <string, List <string> >, List <string>, string>(); db.show <int, DBElement <int, string>, string>(); Write("\n\n"); }