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); }
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(); }
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(); }
public bool XMLRestoreLOS(string pathnameLOS, DBEngine <string, DBElement <string, List <string> > > dbLOS) { DBElement <string, List <string> > elemStringLOS; XDocument xdocLOS = new XDocument(); try { xdocLOS = XDocument.Load(pathnameLOS); } catch (Exception e) { Console.Write("\n Problem found: {0}\n", e.ToString()); return(false); } 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); } } return(true); }
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); }
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"); }
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 static void showDB(this DBEngine <int, DBElement <int, string> > db) { db.show <int, DBElement <int, string>, string>(); }
public IntAndString(DBEngine <int, DBElement <int, string> > db) { persistDB = db; }
public StringAndStringList(DBEngine <string, DBElement <string, List <string> > > db) { persistDB = db; }
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>(); }
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) { QueryEngine<int, DBElement<int, string>, string> qe = new QueryEngine<int, DBElement<int, string>, string>(); QueryEngine<string, DBElement<string, List<string>>, List<string>> qeLOS = new QueryEngine<string, DBElement<string, List<string>>, List<string>>(); DBEngine<int, DBElement<int, string>> db = new DBEngine<int, DBElement<int, string>>(); DBElement<int, string> elem = new DBElement<int, string>(); DBElement<int, string> Result = new DBElement<int, string>(); elem.name = "Test"; elem.descr = "Test descr"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List<int> { 1, 2, 3 }); elem.payload = "Test Payload"; db.insert(0, elem); DBElement<int, string> elem1 = new DBElement<int, string>(); elem1.name = "Test 1"; elem1.descr = "Test descr 1"; elem1.timeStamp = DateTime.Now; elem1.children.AddRange(new List<int> { 4, 5, 6 }); elem1.payload = "Test Payload 1"; db.insert(1, elem1); DBEngine<string, DBElement<string, List<string>>> dbLOS = new DBEngine<string, DBElement<string, List<string>>>(); DBElement<string, List<string>> elemLOS = new DBElement<string, List<string>>(); DBElement<string, List<string>> ResultLOS = new DBElement<string, List<string>>(); elemLOS.name = "Test"; elemLOS.descr = "Test descr"; elemLOS.timeStamp = DateTime.Now; elemLOS.children = new List<string> { "One", "Two", "Three" }; elemLOS.payload = new List<string> { "Test Payload", "Payload" }; dbLOS.insert("One", elemLOS); DBElement<string, List<string>> elemLOS1 = new DBElement<string, List<string>>(); elemLOS1.name = "Test 2"; elemLOS1.descr = "Test descr 2"; elemLOS1.timeStamp = DateTime.Now; elemLOS1.children = new List<string> { "Four", "Five", "Six" }; elemLOS1.payload = new List<string> { "Test Payload 2", "Payload 2" }; dbLOS.insert("Two", elemLOS1); if (qe.getValue(0, db, out Result)) WriteLine("Value of key 0:\n{0}\n Payload: {1}", Result.showMetaData(), Result.payload.ToString()); else WriteLine("Key not found"); WriteLine(); if (qeLOS.getValue("One", dbLOS, out ResultLOS)) WriteLine("Value of key:\n{0}\n Payload: {1}", ResultLOS.showMetaData(), qeLOS.ToString(ResultLOS.payload)); else WriteLine("Key not found"); WriteLine(); if (qe.getChildren(0, db, out children)) WriteLine("Children of specified key: {0}", qe.ToString(children)); else WriteLine("Key not found"); if (qeLOS.getChildren("One", dbLOS, out childrenLOS)) WriteLine("Children of specified key: {0}", qeLOS.ToString(childrenLOS)); else WriteLine("Key not found"); WriteLine(); if (qe.searchPattern(0, db, out Keys)) Console.WriteLine("Set of Keys: {0}", qe.ToString(Keys)); else Console.WriteLine("No Keys found!"); if (qeLOS.searchPattern("O", dbLOS, out KeysLOS)) Console.WriteLine("Set of Keys: {0}", qeLOS.ToString(KeysLOS)); else Console.WriteLine("No Keys found!"); WriteLine(); if (qe.searchString("descr", db, out Keys)) Console.WriteLine("Set of Keys: {0}", qe.ToString(Keys)); else Console.WriteLine("\nNo Keys found!"); if (qeLOS.searchString("Test", dbLOS, out KeysLOS)) Console.WriteLine("Set of Keys: {0}", qeLOS.ToString(KeysLOS)); else Console.WriteLine("No Keys found!"); WriteLine(); if (qe.searchInterval("10/7/1999 12:00:00 AM", "", db, out Keys)) Console.WriteLine("Set of Keys: {0}", qe.ToString(Keys)); else Console.WriteLine("\nNo Keys found!"); if (qeLOS.searchInterval("10/7/1999 12:00:00 AM", "", dbLOS, out KeysLOS)) Console.WriteLine("Set of Keys: {0}", qeLOS.ToString(KeysLOS)); else Console.WriteLine("No Keys found!"); WriteLine(); }
public QueryEngine(DBEngine <Key, DBElement <Key, Data> > dbEngine) { db = dbEngine; }
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 = "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>(); QueryEngine <int, string> QETest = new QueryEngine <int, string>(db); WriteLine(); Write("\n --- Query element which key=2"); QueryEngine <int, string> query = new QueryEngine <int, string>(db); DBElement <int, string> elem; bool valQuery = query.KeyQuery(2, out elem); if (valQuery) { Write("\n This element exist"); } Write(elem.showElement <int, string>()); WriteLine(); Write("\n --- Test queries for children of a specified key ---"); Write("\n --- Query for children of element which key=3 ---"); List <int> children; bool childQuery = query.queryChildren(3, out children); if (childQuery) { Write("\n This element has child"); } foreach (var child in children) { Write("\n Key of child: {0}", child.ToString()); } WriteLine(); Write("\n --- Test all keys that contain a specified string in their metadata section---"); Write("\n --- query for \"star war\" in metadata, return keys ---"); Func <int, bool> stringTest = QETest.defineStringQuery("star war"); List <int> keyCollection; bool stringQuery = QETest.processQuery(stringTest, out keyCollection); foreach (var k in keyCollection) { Write("\n Key: {0}", k.ToString()); } WriteLine(); WriteLine("\n --- Test query according to a specified time-date interval ---"); DateTime time1 = new DateTime(2015, 10, 1, 0, 0, 0); DateTime time2 = new DateTime(2015, 10, 2, 11, 0, 0); List <int> timeCollection; Func <int, bool> timeTest = QETest.defineTimeQuery(time1, time2); bool timeResult = QETest.processQuery(timeTest, out timeCollection); foreach (var k in timeCollection) { WriteLine("key in specific time interval: {0}", k.ToString()); } WriteLine(); WriteLine("--- When time is not provided ---"); DateTime time3 = new DateTime(); timeTest = QETest.defineTimeQuery(time1, time3); bool timeQuery2 = QETest.processQuery(timeTest, out timeCollection); foreach (var k in timeCollection) { WriteLine("key in specific time interval: {0}", k.ToString()); } 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"); }
public static void showEnumerableDB(this DBEngine <string, DBElement <string, List <string> > > db) { db.show <string, DBElement <string, List <string> >, List <string>, string>(); }