//----< process children query using queryPredicate >----------------------------------- public bool processChildrenQuery <Key, Value, Data>(Func <Key, bool> queryPredicate, out List <Key> keyCollection, Key searchKey, DBEngine <Key, Value> db) { keyCollection = new List <Key>(); Value value; if (db.Keys().Contains(searchKey)) { db.getValue(searchKey, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; foreach (Key child in elem.children) { if (queryPredicate(child)) { keyCollection.Add(child); } } } if (keyCollection.Count() > 0) { return(true); } return(false); }
//----< build queryPredicate for timestamp query >--------------------------------------- public Func <Key, bool> queryTimestamp <Key, Value, Data>(DBEngine <Key, Value> db, DateTime initial, DateTime final = default(DateTime)) { Func <Key, bool> queryPredicate = (Key key) => { Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; int init = elem.timeStamp.CompareTo(initial); if (final == default(DateTime)) { final = DateTime.Now; } int fin = elem.timeStamp.CompareTo(final); if (init >= 0 && fin <= 0) { return(true); } return(false); }; return(queryPredicate); }
//----< build queryPredicate for metadata query >--------------------------------------- /* * Query returns true if query(key) condition is satisfied. * In this example the query is checking to see if the query * parameter, the captured string, test, is a substring of * the database element referenced by key. */ public Func <Key, bool> queryMetadata <Key, Value, Data>(string queryParam, DBEngine <Key, Value> db) { // Func<int, bool> is delegate that binds to // functions of the form bool query(int key). Func <Key, bool> queryPredicate = (Key key) => { Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; if (elem.name.Contains(queryParam)) { return(true); } if (elem.descr.Contains(queryParam)) { return(true); } if (elem.timeStamp.ToString().Contains(queryParam)) { return(true); } foreach (Key child in elem.children) { if (child.ToString().Contains(queryParam)) { return(true); } } return(false); }; return(queryPredicate); }
//----< Public Constructor >----------------------------------------------------- public DBFactory(DBEngine <Key, Value> db) { dbStore = new Dictionary <Key, Value>(); foreach (Key key in db.Keys()) { Value value; db.getValue(key, out value); dbStore[key] = value; } }
//----< 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()); } }
//----< Convert Enumerable DB to XML file format >--------------------------------- public static XDocument PersistToXML <Key, Value, Data, T>(this DBEngine <Key, Value> dbStore) where Data : IEnumerable <T> { XDocument xDocument = new XDocument(); xDocument.Declaration = new XDeclaration("1.0", "utf-8", "yes"); xDocument.Add(new XComment("Persisting DB to XML at " + DateTime.Now.ToString())); XElement root = new XElement("NoSqlDb"); root.Add(new XElement("KeyType", typeof(Key).ToString())); root.Add(new XElement("PayloadType", typeof(Data).ToString())); foreach (Key key in dbStore.Keys()) { XElement dbEntry = new XElement("DBEntry"); root.Add(dbEntry); dbEntry.Add(new XElement("Key", key.ToString())); Value element; dbStore.getValue(key, out element); DBElement <Key, Data> dbElement = element as DBElement <Key, Data>; XElement xmlElement = new XElement("element"); xmlElement.Add(new XElement("name", dbElement.name)); xmlElement.Add(new XElement("descr", dbElement.descr)); xmlElement.Add(new XElement("timestamp", dbElement.timeStamp)); XElement children = new XElement("children"); if (dbElement.children.Count > 0) { foreach (Key child in dbElement.children) { children.Add(new XElement("key", child.ToString())); } } xmlElement.Add(children); XElement payload = new XElement("payload"); Data data = dbElement.payload; foreach (T item in data) { payload.Add(new XElement("item", item.ToString())); } xmlElement.Add(payload); dbEntry.Add(xmlElement); } xDocument.Add(root); return(xDocument); }
//----< Query for value of a given key >------------------------------------------------ public static Value queryValue <Key, Value, Data>(Key key, DBEngine <Key, Value> db) { Value value = default(Value); if (db.Keys().Contains(key)) { db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; return(value); } else { "Key doesn't exist!".error(); return(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); } }
//----< Convert primitive DB to XML format >--------------------------------------- public static XDocument ToXML <Key, Value, Data>(this DBEngine <Key, Value> dbStore) { XDocument xDocument = new XDocument(); XElement root = new XElement("NoSqlDb"); foreach (Key key in dbStore.Keys()) { XElement dbEntry = new XElement("DBEntry"); root.Add(dbEntry); dbEntry.Add(new XElement("Key", key.ToString())); Value element; dbStore.getValue(key, out element); DBElement <Key, Data> dbElement = element as DBElement <Key, Data>; XElement xmlElement = new XElement("element"); xmlElement.Add(new XElement("name", dbElement.name)); xmlElement.Add(new XElement("descr", dbElement.descr)); xmlElement.Add(new XElement("timestamp", dbElement.timeStamp)); XElement children = new XElement("children"); if (dbElement.children.Count > 0) { foreach (Key child in dbElement.children) { children.Add(new XElement("key", child.ToString())); } } xmlElement.Add(children); XElement payload = new XElement("payload"); payload.Add(new XElement("item", dbElement.payload.ToString())); xmlElement.Add(payload); dbEntry.Add(xmlElement); } xDocument.Add(root); return(xDocument); }
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"); }