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 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 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); }
void TestR3() { "Demonstrating Requirement #3".title(); Write("\n --- show orginal database ---"); DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "element"); elem2.payload = "The Empire strikes back!"; dbInt.insert(2, elem2); dbInt.showDB(); WriteLine(); Write("\n --- add a new key/value pair ---"); DBElement <int, string> elem3 = new DBElement <int, string>("Luke Skywalker", "element"); elem3.children.AddRange(new List <int> { 1, 5, 23 }); elem3.payload = "X-Wing fighter in swamp - Oh oh!"; dbInt.insert(3, elem3); dbInt.showDB(); WriteLine(); Write("\n --- delete key/value pair where key=1"); dbInt.delete(1); dbInt.showDB(); WriteLine(); }
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 metadata to string >------------------------------------- public static string showMetaData <Key, Data>(this DBElement <Key, Data> elem) { StringBuilder accum = new StringBuilder(); accum.Append(String.Format("\n name: {0}", elem.name)); accum.Append(String.Format("\n desc: {0}", elem.descr)); accum.Append(String.Format("\n time: {0}", elem.timeStamp)); if (elem.children.Count() > 0) { accum.Append(String.Format("\n Children: ")); bool first = true; foreach (Key key in elem.children) { if (first) { accum.Append(String.Format("{0}", key.ToString())); first = false; } else { accum.Append(String.Format(", {0}", key.ToString())); } } } return(accum.ToString()); }
//----< write details of element with enumerable Data to string >------ public static string showElement <Key, Data, T>(this DBElement <Key, Data> elem) where Data : IEnumerable <T> // constraint clause { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { IEnumerable <object> d = elem.payload as IEnumerable <object>; if (d == null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } else { bool first = true; accum.Append(String.Format("\n payload:\n ")); foreach (var item in elem.payload) // won't compile without constraint clause { if (first) { accum.Append(String.Format("{0}", item)); first = false; } else { accum.Append(String.Format(", {0}", item)); } } } } return(accum.ToString()); }
/* 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); } } }
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); } } }
// 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); }
static void Main(string[] args) { "Testing DBElement Package".title('='); WriteLine(); Write("\n --- Test DBElement<int,string> ---"); WriteLine(); DBElement <int, string> elem1 = new DBElement <int, string>(); 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 = new List <int> { 1, 2, 7 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; Write(elem3.showElement <int, string>()); WriteLine(); Write("\n --- Test DBElement<string,List<string>> ---"); WriteLine(); 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>, string>()); WriteLine(); DBElement <string, List <string> > newerelem1 = new DBElement <string, List <string> >(); newerelem1.name = "newerelem1"; newerelem1.descr = "same stuff"; newerelem1.children.Add("first_key"); newerelem1.children.Add("second_key"); newerelem1.payload = new List <string> { "alpha", "beta", "gamma" }; newerelem1.payload.AddRange(new[] { "delta", "epsilon" }); Write(newerelem1.showElement <string, List <string>, string>()); WriteLine(); Write("\n\n"); }
static void Main(string[] args) { Write("\n --- Test specific key query ---"); Write("\n ---Database contents"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.descr = "payload desxription"; elem1.name = "element 1"; elem1.timeStamp = DateTime.Now; elem1.payload = "a payload"; WriteLine(); DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.descr = "star war 2"; elem2.name = "element 2"; elem2.timeStamp = new DateTime(2015, 9, 10, 12, 30, 1); elem2.payload = "The Empire strikes back!"; WriteLine(); var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.name = "fight"; elem3.descr = "star war 3"; elem3.timeStamp = new DateTime(2015, 10, 2, 8, 0, 0); elem3.children = new List <int> { 1, 2, 3 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; WriteLine(); 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); db.show <int, DBElement <int, string>, string>(); WriteLine(); Write("\n --- Test creation of immutable database ---"); Write("\n --- create database for elements which contain \"element\" in metadata ---"); QueryEngine <int, string> query = new QueryEngine <int, string>(db); Func <int, bool> defineQuery = query.defineStringQuery("element"); List <int> keyCollection; bool result = query.processQuery(defineQuery, out keyCollection); immutableDB <int, DBElement <int, string> > imdb = new immutableDB <int, DBElement <int, string> >(db, keyCollection); imdb.ImmutableDB.showDB(); 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()); } }
//----< write details of element with simple Data to string >---------- public static string showElement <Key, Data>(this DBElement <Key, Data> elem) { StringBuilder accum = new StringBuilder(); accum.Append(elem.showMetaData()); if (elem.payload != null) { accum.Append(String.Format("\n payload: {0}", elem.payload.ToString())); } return(accum.ToString()); }
public override bool restore(string XmlFile) { XDocument doc = XDocument.Load(XmlFile); persistDB = new DBEngine <string, DBElement <string, List <string> > >(); if (doc == null) { return(false); } XDocument xml = XDocument.Parse(doc.ToString()); IEnumerable <XElement> allElem = xml.Descendants("element"); foreach (var elem in allElem) { IEnumerable <XElement> keys = elem.Descendants("key"); IEnumerable <XElement> names = elem.Descendants("name"); IEnumerable <XElement> descrs = elem.Descendants("descr"); IEnumerable <XElement> timestamps = elem.Descendants("timestamp"); IEnumerable <XElement> children = elem.Descendants("children").Descendants("key"); IEnumerable <XElement> payloads = elem.Descendants("payload"); DBElement <string, List <string> > element = new DBElement <string, List <string> >(); string key = keys.ElementAt(0).Value; foreach (var name_ in names) { element.name = name_.Value; } foreach (var descr_ in descrs) { element.descr = descr_.Value; } foreach (var timestamp_ in timestamps) { element.timeStamp = (DateTime)timestamp_; } foreach (var child in children) { element.children.Add(child.Value); } element.payload = new List <string>(); foreach (var payload_ in payloads) { foreach (var item in payload_.Descendants("item")) { element.payload.Add(item.Value); } } persistDB.insert(key, element); } return(true); }
public void showDBF(DBFactory<Key, Value, Data> db) //Source: DBExtensions.cs { 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} --\n", key); Console.WriteLine("Name: {0}", elem.name); Console.WriteLine("Description: {0}", elem.descr); Console.WriteLine("Timestamp: {0}", elem.timeStamp.ToString()); Console.WriteLine("Children: {0}", qe.ToString(elem.children)); //Console.WriteLine("Payload: {0}", qe.ToString(payload)); //-- Can't cast Data as Generic List } }
static void Main(string[] args) { TestWriteToXML(); Write("\n --- Test persist <string,<List<string>> to XML ---"); DBElement <string, List <string> > elem1 = new DBElement <string, List <string> >(); elem1.name = "elemLos1"; elem1.descr = "element with ListOfString payload"; elem1.timeStamp = new DateTime(2015, 10, 2, 11, 0, 0); elem1.payload = new List <string> { "one", "two", "three", "four", "five" }; DBElement <string, List <string> > elem2 = new DBElement <string, List <string> >(); elem2.name = "elemLos2"; elem2.descr = "element with ListOfString payload"; elem2.timeStamp = new DateTime(2015, 10, 2, 10, 0, 0); elem2.children = new List <string> { "key1", "key2", "key3" }; elem2.payload = new List <string> { "alpha", "beta", "gamma", "delta", "epsilon" }; DBEngine <string, DBElement <string, List <string> > > db = new DBEngine <string, DBElement <string, List <string> > >(); bool p1 = db.insert("key15", elem1); bool p2 = db.insert("key16", elem2); if (p1 && p2) { Write("\n all inserts succeeded"); } else { Write("\n at least one insert failed"); } PersistWapper <string, DBElement <string, List <string> > > persist2 = new StringAndStringList(db); persist2.writeToXML("DatabaseContent.xml"); persist2.persistDB = new DBEngine <string, DBElement <string, List <string> > >(); if (persist2.restore("DatabaseContent.xml")) { persist2.persistDB.show <string, DBElement <string, List <string> >, List <string>, string>(); } WriteLine(); }
//-------------< Following is the demonstration of each requirement, for each database type >---------- void TestR2() { //----< Demonstrating the structure of each element of the database. Taking only <int, string> for now >------- "Demonstrating Requirement #2".title('-'); WriteLine(); "Element Structure (Key=int, Value=DBElement<int, string>)".title('-'); DBElement<int, string> elem = new DBElement<int, string>(); elem.name = "Test Element"; elem.descr = "Description"; elem.timeStamp = DateTime.Now; elem.children.Add(5); elem.payload = "Hello!"; Write(elem.showElement<int, string>()); WriteLine(); db.insert(0, elem); }
static void Main(string[] args) { "Testing DBFactory Package".title('='); WriteLine(); DBEngine<int, DBElement<int, string>> db = new DBEngine<int, DBElement<int, string>>(); DBElement<int, string> elem = new DBElement<int, string>(); elem.name = "Name"; elem.descr = "Description"; elem.timeStamp = DateTime.Now; elem.children.Add(1); elem.payload = "Payload"; db.insert(1, elem); DBFactory<int, DBElement<int, string>, string> dbFact = new DBFactory<int, DBElement<int, string>, string>(new List<int> { 1 }, db); dbFact.showDBF(dbFact); }
// test write DBEngine<int,DBElement<int,string>> to xml static void TestWriteToXML() { Write("\n ---convert database content to XML ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.descr = "payload desxription"; elem1.name = "element 1"; elem1.timeStamp = DateTime.Now; elem1.payload = "a payload"; WriteLine(); DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.descr = "star war 2"; elem2.name = "element 2"; elem2.timeStamp = new DateTime(2015, 9, 10, 12, 30, 1); elem2.payload = "The Empire strikes back!"; WriteLine(); var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.name = "element 3"; elem3.descr = "star war 3"; elem3.timeStamp = new DateTime(2015, 10, 2, 8, 0, 0); elem3.children = new List <int> { 1, 2, 3 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; WriteLine(); 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); db.show <int, DBElement <int, string>, string>(); WriteLine(); IntAndString test = new IntAndString(db); test.writeToXML("DatabaseContent.xml"); Write("\n XML file has been created in ./bin/Debug"); }
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); }
static void Main(string[] args) { DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.descr = "payload desxription"; elem1.name = "element 1"; elem1.timeStamp = DateTime.Now; elem1.payload = "a payload"; DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.descr = "star war 2"; elem2.name = "element 2"; elem2.timeStamp = new DateTime(2015, 9, 10, 12, 30, 1); elem2.payload = "The Empire strikes back!"; var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.name = "element 3"; elem3.descr = "star war 3"; elem3.timeStamp = new DateTime(2015, 10, 2, 8, 0, 0); elem3.children = new List <int> { 1, 2, 3 }; elem3.payload = "X-Wing fighter in swamp - Oh oh!"; 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); WriteLine("--- Test Scheduler ---"); PersistWapper <int, DBElement <int, string> > persist = new IntAndString(db); Schedular <int, DBElement <int, string> > test = new Schedular <int, DBElement <int, string> >(); test.autoSave(persist, 1000, "DatabaseContent.xml"); ReadKey(); }
void TestR2() { "Demonstrating Requirement #2".title(); Write("\n --- When the instance is string --- \n"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.showElement(); dbInt.insert(1, elem1); dbInt.showDB(); WriteLine(); Write("\n --- When the instance is a list of string ---"); DBElement <string, List <string> > elem2 = new DBElement <string, List <string> >(); elem2.name = "elementLos1"; elem2.descr = "element with ListofStrings payload"; elem2.timeStamp = new DateTime(2015, 9, 29, 12, 0, 0); elem2.payload = new List <string> { "one", "two", "three", "four", "five" }; elem2.showElement(); DBListOfString.insert("key15", elem2); DBElement <string, List <string> > elem3 = new DBElement <string, List <string> >(); elem3.name = "elemLos2"; elem3.descr = "element with ListOfString payload"; elem3.timeStamp = new DateTime(2015, 10, 2, 10, 0, 0); elem3.children = new List <string> { "key1", "key2", "key3" }; elem3.payload = new List <string> { "alpha", "beta", "gamma", "delta", "epsilon" }; elem3.showElement(); DBListOfString.insert("key16", elem3); DBListOfString.showEnumerableDB(); WriteLine(); }
static void Main(string[] args) { "Testing DBExtensions 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>()); DBEngine <int, DBElement <int, string> > dbs = new DBEngine <int, DBElement <int, string> >(); dbs.insert(1, elem1); dbs.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.children = new List <string> { "Key1", "Key2" }; newelem1.payload = new List <string> { "one", "two", "three" }; Write(newelem1.showElement <string, List <string>, string>()); DBEngine <string, DBElement <string, List <string> > > dbe = new DBEngine <string, DBElement <string, List <string> > >(); dbe.insert("key1", newelem1); dbe.show <string, DBElement <string, List <string> >, List <string>, string>(); Write("\n\n"); }
void TestR7() { "Demonstrating Requirement #7".title(); Write("\n --- Test query ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.timeStamp = DateTime.Now; elem1.payload = "default payload"; dbInt.insert(1, elem1); Write("\n --show orginal database--"); dbInt.showDB(); WriteLine(); Write("\n --- Query for the value of a specified key=2 ---"); QueryEngine <int, string> query = new QueryEngine <int, string>(dbInt); DBElement <int, string> elem; query.KeyQuery(2, out elem); elem.showElement(); WriteLine(); Write("\n --- Query for all children of a specified key---"); List <int> children; bool child = query.queryChildren(3, out children); if (child) { foreach (var c in children) { Write("\n child is {0}", c.ToString()); } } else { Write("\n This element doesn't contain key"); } WriteLine(); }
static void Main(string[] args) { "Testing DBEngine Package".title('=');; WriteLine(); "Test db of scalar elements".title(); WriteLine(); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.payload = "a payload"; DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot"); elem3.payload = "X-Wing fighter in swamp - Oh oh!"; if (verbose) { Write("\n --- Test DBElement<int,string> ---"); WriteLine(); elem1.showElement(); WriteLine(); elem2.showElement(); WriteLine(); elem3.showElement(); WriteLine(); /* ElementFormatter is not ready for prime time yet */ //Write(ElementFormatter.formatElement(elem1.showElement<int, string>(), false)); } Write("\n --- Test DBEngine<int,DBElement<int,string>> ---"); WriteLine(); int key = 0; Func <int> keyGen = () => { ++key; return(key); }; 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.showDB(); WriteLine(); "Test db of enumerable elements".title(); WriteLine(); 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" }; 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"); DBElement <string, List <string> > newerelem2 = new DBElement <string, List <string> >(); newerelem2.name = "newerelem2"; newerelem2.descr = "better formatting"; newerelem2.children.AddRange(new List <string> { "first", "second" }); newerelem2.payload = new List <string> { "a", "b", "c" }; newerelem2.payload.Add("d"); newerelem2.payload.Add("e"); if (verbose) { Write("\n --- Test DBElement<string,List<string>> ---"); WriteLine(); newelem1.showEnumerableElement(); WriteLine(); newerelem1.showEnumerableElement(); WriteLine(); newerelem2.showEnumerableElement(); 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(), newelem1); newdb.insert(skeyGen(), newerelem1); newdb.insert(skeyGen(), newerelem2); newdb.showEnumerableDB(); Write("\n\n"); }
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"); }
private string pathnameLOS = ""; // they can be reused later. static void Main(string[] args) { PersistEngine pe = new PersistEngine(); TEST_PERSISTENGINE pet = new TEST_PERSISTENGINE(); DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >(); DBEngine <string, DBElement <string, List <string> > > dbLOS = new DBEngine <string, DBElement <string, List <string> > >(); DBElement <int, string> elem = new DBElement <int, string>(); DBElement <string, List <string> > elemLOS = new DBElement <string, List <string> >(); //Adding DBElement<int, string> elem.name = "name"; elem.descr = "descr"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List <int>() { 1, 2, 3 }); elem.payload = "hello"; db.insert(0, elem); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.name = "name1"; elem1.descr = "descr1"; elem1.children.AddRange(new List <int>() { 1, 2, 3 }); elem1.timeStamp = DateTime.Now; elem1.payload = "hello1"; db.insert(1, elem1); //Adding DBElement<string, List<string>> elemLOS.name = "name2"; elemLOS.descr = "descr2"; elem.timeStamp = DateTime.Now; elemLOS.payload = new List <string>() { "hello", "world" }; dbLOS.insert("Two", elemLOS); elemLOS = new DBElement <string, List <string> >(); elemLOS.name = "name3"; elemLOS.descr = "descr3"; elemLOS.timeStamp = DateTime.Now; elemLOS.children.AddRange(new List <string>() { "One", "Two", "Three" }); elemLOS.payload = new List <string>() { "fee", "foo", "bar" }; dbLOS.insert("Three", elemLOS); pe.XMLWrite(db, out pet.pathname); pe.XMLWriteLOS(dbLOS, out pet.pathnameLOS); WriteLine("\n{0}", XDocument.Load(pet.pathname).ToString()); WriteLine("\n{0}", XDocument.Load(pet.pathnameLOS).ToString()); pe.XMLRestore("../../xmlRestore.xml", db); db.show <int, DBElement <int, string>, string>(); pe.XMLRestoreLOS("../../xmlRestoreLOS.xml", dbLOS); dbLOS.show <string, DBElement <string, List <string> >, List <string>, string>(); }