//----< Create & Populate DB >-------------------------------------- void TestCreate() { "Demonstrating Requirement #2 (DB <int, string>)".title(); DBElement <int, string> elem = new DBElement <int, string>(); elem.name = "element"; elem.descr = "test element"; elem.timeStamp = DateTime.Now; elem.children.AddRange(new List <int> { 2, 3 }); elem.payload = "elem's payload"; "Adding below element with Key = 1".title2(); db.insert(1, elem); WriteLine(); }
static void Main(string[] args) { /* * Create and edit a DBEngine first. * Then create a DBFactory using the DBEngine. */ "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(); 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); Write("\n --- Create DBFactory<int, DBElement<int, string>> from DBEngine<int,DBElement<int, string>> ---"); DBFactory <int, DBElement <int, string> > dbFactory = new DBFactory <int, DBElement <int, string> >(db); foreach (int dbKey in dbFactory.Keys()) { DBElement <int, string> value; dbFactory.getValue(dbKey, out value); value.showElement(); } dbFactory.showDB(); Write("\n\n"); }
//----< Parse Enumerable type DB from XML format >---------------------------------- public static void FromXML <Key, Value, Data, T>(this DBEngine <Key, Value> db, XDocument xDocument) where Data : IEnumerable <T> { XElement noSqlDB = xDocument.Root; string keytype = noSqlDB.Element("KeyType").Value; string payloadtype = noSqlDB.Element("PayloadType").Value; if (!(typeof(Key).ToString() == keytype && typeof(Data).ToString() == payloadtype)) { WriteLine(); "The persisted DB does not match the current DB!!".error(); WriteLine(); WriteLine(" Current DB: \n Key Type: {0} \n Payload Type: {1}", typeof(Key), typeof(Data)); WriteLine(" DB to be read: \n Key Type: {0} \n Payload Type: {1}", keytype, payloadtype); } else { IEnumerable <XElement> dbEntries = xDocument.Root.Elements("DBEntry"); foreach (XElement dbEntry in dbEntries) { string temp = dbEntry.Element("Key").Value; Key key = (Key)Convert.ChangeType(temp, typeof(Key)); DBElement <Key, Data> element = new DBElement <Key, Data>(); XElement elem = dbEntry.Element("element"); element.name = elem.Element("name").Value; element.descr = elem.Element("descr").Value; string time = elem.Element("timestamp").Value; element.timeStamp = (DateTime)Convert.ChangeType(time, typeof(DateTime)); if (elem.Element("children") != null) { IEnumerable <XElement> childKeys = elem.Element("children").Elements("key"); if (childKeys.Count() > 0) { foreach (XElement child in childKeys) { temp = child.Element("key").Value; Key childKey = (Key)Convert.ChangeType(temp, typeof(Key)); element.children.Add(childKey); } } } IEnumerable <XElement> items = elem.Element("payload").Elements("item"); List <T> payload = new List <T>(); foreach (XElement item in items) { temp = item.Value; T payloadItem = (T)Convert.ChangeType(temp, typeof(T)); payload.Add(payloadItem); } Data payloadData = (Data)Convert.ChangeType(payload, typeof(Data)); element.payload = payloadData; Value value = (Value)Convert.ChangeType(element, typeof(Value)); db.insert(key, value); } } }
//----< show query results >------------------------------------------------------------- public DBFactory <Key, Value> queryResults <Key, Value, Data>(bool result, List <Key> keyCollection, DBEngine <Key, Value> db) { DBEngine <Key, Value> newDb = new DBEngine <Key, Value>(); if (result) // query succeeded for at least one key { Value value; foreach (Key key in keyCollection) { db.getValue(key, out value); newDb.insert(key, value); } DBFactory <Key, Value> dbFactory = new DBFactory <Key, Value>(newDb); return(dbFactory); } else { DBFactory <Key, Value> dbFactory = new DBFactory <Key, Value>(newDb); return(dbFactory); } }
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"); }
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>(); Write("\n\n"); }