//-------< Add child relation to a particular DB-element >--------------------------- public static bool addRelation <Key, Value, Data>(this DBEngine <Key, Value> db_edit, Key key1, Key key2) { Value val1, val2; bool key2_present = db_edit.getValue(key2, out val2); if (key2_present) { bool key1_present = db_edit.getValue(key1, out val1); if (key1_present) { DBElement <Key, Data> elem = val1 as DBElement <Key, Data>; elem.children.Add(key2); elem.timeStamp = DateTime.Now; return(true); } else { return(false); } } else { return(false); } }
// delegate for time stamp query. public Func <Key, bool> defineTimeStampQuery(DateTime startTime, DateTime?endTime = null) { Func <Key, bool> queryPredicate = (Key key) => { if (!db.Keys().Contains(key)) { return(false); } else { if (endTime == null) { endTime = DateTime.Now; } DBElement <Key, Value> value; db.getValue(key, out value); DBElement <Key, Value> elem = value as DBElement <Key, Value>; int cond1 = DateTime.Compare(elem.timeStamp, startTime); int cond2 = DateTime.Compare(elem.timeStamp, (DateTime)endTime); if (cond1 >= 0 && cond2 <= 0) { return(true); } } return(false); }; return(queryPredicate); }
//-------< Get keys of DBElements containing a pattern in the metadata section >------------- public List <Key> get_keys_with_metadata <Key, Value, Data>(DBEngine <Key, Value> db, string pat) { List <Key> key_collection = new List <Key>(); foreach (Key key in db.Keys()) { Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; if (elem.name.Contains(pat) || elem.descr.Contains(pat) || elem.timeStamp.ToString().Contains(pat)) { key_collection.Add(key); } else if (elem.children.Count() > 0) { foreach (Key x in elem.children) { if (x.ToString().Contains(pat)) { key_collection.Add(key); } } } } if (key_collection.Count > 0) { return(key_collection); } else { return(default(List <Key>)); } }
public List <string> dateTimeSearch(DateTime fromDate, DateTime toDate, DBEngine <string, DBElement <string, List <string> > > db) { if (toDate == default(DateTime)) { toDate = DateTime.Now; } List <string> foundKeys = new List <string>(); IEnumerable <string> keys = db.Keys(); foreach (string key in keys) { DBElement <string, List <string> > elem = new DBElement <string, List <string> >(); db.getValue(key, out elem); if (elem.timeStamp <= toDate && elem.timeStamp > fromDate) { foundKeys.Add(key); } } if (foundKeys.Count == 0) { return(null); } else { return(foundKeys); } }
public Action<Message>edit(DBEngine<int, DBElement<int, string>> db) { Action<Message> Edit = (msg) => { XDocument xml = XDocument.Parse(msg.content.ToString()); XElement element = xml.Descendants("Msg").ElementAt(0).Descendants("Data").ElementAt(0); int key = int.Parse(element.Descendants("key").ElementAt(0).Value); DBElement<int, string> elem = new DBElement<int, string>(); Console.Write("\n\n --- edit a element which key ={0}--- ",key); Write("\n\n ---Database before edit---"); db.showDB(); if (!db.getValue(key, out elem)) { msg.content = "edit fail"; return; } elem.name = element.Descendants("value").Descendants("name").ElementAt(0).Value; elem.descr = element.Descendants("value").Descendants("descr").ElementAt(0).Value; elem.timeStamp = (DateTime)element.Descendants("value").Descendants("timestamp").ElementAt(0); IEnumerable<XElement> children = element.Descendants("value").Descendants("children").Descendants("key"); elem.children = new List<int>(); foreach (var child in children) elem.children.Add(int.Parse(child.Value)); IEnumerable<XElement> items = element.Descendants("value").Descendants("payload"); if (items.Count() == 1) elem.payload = items.ElementAt(0).Value; msg.content = "edit success"; Utilities.swapUrls(ref msg); Console.Write("\n\n --- Database after edit ---"); db.showDB(); }; return Edit; }
public List <string> metaDataPattern(string pattern, DBEngine <string, DBElement <string, List <string> > > db) { List <string> foundKeys = new List <string>(); IEnumerable <string> keys = db.Keys(); foreach (string key in keys) { DBElement <string, List <string> > elem = new DBElement <string, List <string> >(); db.getValue(key, out elem); if (elem.name.Contains(pattern)) { foundKeys.Add(key); } else if (elem.descr.Contains(pattern)) { foundKeys.Add(key); } else if (elem.timeStamp.ToString().Contains(pattern)) { foundKeys.Add(key); } } if (foundKeys.Count == 0) { return(null); } else { return(foundKeys); } }
public static DBElement <Key, Value> searchValue <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, Key key) { //ToDo: Check whether key exist DBElement <Key, Value> val; dbEngine.getValue(key, out val); return(val); }
//----< 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 List <Key> childrenByKey(Key key, DBEngine <Key, DBElement <Key, List <string> > > db) { if (db.getValue(key, out elemPay)) { if (elemPay.children.Count() > 0) { return(elemPay.children); } return(null); } return(null); }
//-------< Get the children list in DBelement of a particular key. >-------------- public List <Key> getChildren <Key, Value, Data>(DBEngine <Key, Value> db, Key key) { Value val; List <Key> ret = new List <Key>(); if (db.getValue(key, out val)) { DBElement <Key, Data> elem = val as DBElement <Key, Data>; return(elem.children); } return(default(List <Key>)); }
public static bool addRelationship <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, Key key, List <Key> children) { DBElement <Key, Value> elem; if (dbEngine.containsKey(key)) { dbEngine.getValue(key, out elem); elem.children.AddRange(children); return(true); } return(false); }
public List <string> valueByKey(Key key, DBEngine <Key, DBElement <Key, List <string> > > db) { if (db.getValue(key, out elemPay)) { if (elemPay.payload.Count() > 0) { return(elemPay.payload); } return(null); } return(null); }
//-------< Find the DBElement of a particular key. >--------------------------- public DBElement <Key, Data> findValue <Key, Value, Data>(DBEngine <Key, Value> db, Key key) { Value val; if (db.getValue(key, out val)) { return(val as DBElement <Key, Data>); } else { return(default(DBElement <Key, Data>)); } }
public static bool modifyMetadata <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, Key key, string name, string description) { DBElement <Key, Value> elem; if (dbEngine.containsKey(key)) { dbEngine.getValue(key, out elem); elem.name = name; elem.descr = description; return(true); } return(false); }
//----< write simple db elements out to Console >------------------ public static void show <Key, Value, Data>(this DBEngine <Key, Value> db) { "Displaying DB contents :".title('_'); 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("\n -- key = {0} --", key); } "".demarcation(); }
//----< write enumerable db elements out to Console >-------------- public static string show <Key, Value, Data, T>(this DBEngine <Key, Value> db) where Data : IEnumerable <T> { StringBuilder displayString = new StringBuilder(); foreach (Key key in db.Keys()) { Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; displayString.Append("\n\n -- key = " + key + " --"); displayString.Append(elem.showElement <Key, Data, T>()); } return(displayString.ToString()); }
//-------< Replace payload of a particular DB-element with a new payload.>-------------------- public static bool editInstance <Key, Value, Data>(this DBEngine <Key, Value> db_edit, Key key1, Data new_instance) { Value value; if (db_edit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.payload = new_instance; elem.timeStamp = DateTime.Now; return(true); } else { return(false); } }
//-------< Edit description of a particular DB-element >-------------------- public static bool editDescr <Key, Value, Data>(this DBEngine <Key, Value> db_edit, Key key1, string new_descr) { Value value; if (db_edit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.descr = new_descr; elem.timeStamp = DateTime.Now; return(true); } else { return(false); } }
public static List <Key> searchChildren <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, Key key) { List <Key> children = new List <Key>(); DBElement <Key, Value> val; if (dbEngine.containsKey(key)) { dbEngine.getValue(key, out val); foreach (var child in val.children) { children.Add(child); } return(children); } return(children); }
public static List <Key> searchMetadata <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, string searchString) { List <Key> matchedKeys = new List <Key>(); dynamic keys = dbEngine.Keys(); DBElement <Key, Value> val; foreach (var key in keys) { dbEngine.getValue(key, out val); if (Regex.IsMatch(val.name, searchString) || Regex.IsMatch(val.descr, searchString)) { matchedKeys.Add(key); } } return(matchedKeys); }
public static List <Key> searchForKeyPattern <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, string keyPattern) { List <Key> matchedKeys = new List <Key>(); dynamic keys = dbEngine.Keys(); DBElement <Key, Value> val; foreach (var key in keys) { dbEngine.getValue(key, out val); if ((Regex.IsMatch(key, keyPattern))) { matchedKeys.Add(key); } } return(matchedKeys); }
public static bool removeRelationship <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, Key key, List <Key> children) { DBElement <Key, Value> elem; if (dbEngine.containsKey(key)) { dbEngine.getValue(key, out elem); foreach (var child in children) { if (elem.children.Contains(child)) { elem.children.Remove(child); } } return(true); } return(false); }
public static void toXml(this DBEngine <string, DBElement <string, List <string> > > dbEngine) { dynamic dict = dbEngine.Keys(); DBElement <string, List <string> > result; XDocument xmlDoc = new XDocument(); XElement root = new XElement("noSqlDb"); xmlDoc.Add(root); foreach (var key in dict) { dbEngine.getValue(key, out result); XElement element = new XElement("element"); root.Add(element); XElement keyValue = new XElement("key", key); element.Add(keyValue); XElement value = new XElement("value"); element.Add(value); XElement children = new XElement("children"); value.Add(children); foreach (var item in result.children) { XElement child = new XElement("child", item); children.Add(child); } XElement description = new XElement("description", result.descr); XElement name = new XElement("name"); name.SetValue(result.name); XElement timestamp = new XElement("timestamp", result.timeStamp); XElement payLoad = new XElement("payload"); value.Add(description); value.Add(name); value.Add(timestamp); value.Add(payLoad); foreach (var item in result.payload) { XElement load = new XElement("load", item); payLoad.Add(load); } } saveXml(xmlDoc); }
//-------< Get keys of DBElements written within a specified time interval >------------- public List <Key> get_keys_within_timeInterval <Key, Value, Data>(DBEngine <Key, Value> db, DateTime t1, DateTime t2 = default(DateTime)) { List <Key> key_collection = new List <Key>(); if (t2 == default(DateTime)) { t2 = DateTime.Now; } foreach (Key key in db.Keys()) { Value value; db.getValue(key, out value); DBElement <Key, Data> elem = value as DBElement <Key, Data>; if (DateTime.Compare(elem.timeStamp, t1) >= 0 && DateTime.Compare(elem.timeStamp, t2) <= 0) { key_collection.Add(key); } } return(key_collection); }
public static List <Key> searchForTimeStamp <Key, Value>(this DBEngine <Key, DBElement <Key, Value> > dbEngine, DateTime from, DateTime?to) { List <Key> matchedKeys = new List <Key>(); dynamic keys = dbEngine.Keys(); DBElement <Key, Value> val; if (!to.HasValue) { to = DateTime.Now; } foreach (var key in keys) { dbEngine.getValue(key, out val); if (val.timeStamp >= from && val.timeStamp <= to) { matchedKeys.Add(key); } } return(matchedKeys); }
//-------< Remove child relation of a particular DB-element >-------------------- public static bool removeRelation <Key, Value, Data>(this DBEngine <Key, Value> db_edit, Key key1, Key key2) { Value value; if (db_edit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; if (elem.children.Contains(key2)) { elem.children.Remove(key2); elem.timeStamp = DateTime.Now; return(true); } else { return(false); } } else { return(false); } }
//-------< Save all the contents in the database to a single XML file. >--------------------------- public string persistDB <Key, Value, Data>(DBEngine <Key, Value> db) { XDocument xml = new XDocument(); xml.Declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("This is a No-SQL Database."); xml.Add(comment); XElement noSqlDb = new XElement("NoSqlDb"); XElement keyType = new XElement("KeyType", typeof(Key)); XElement payloadType = new XElement("PayloadType", typeof(Data)); xml.Add(noSqlDb); noSqlDb.Add(keyType); noSqlDb.Add(payloadType); //-------< Save the Key and DBElement in a single XML Element called Key-Value-Pair >--------- foreach (Key k in db.Keys()) { XElement pair = new XElement("Key-Value-Pair"); XElement key = new XElement("Key", k); pair.Add(key); Value val1; db.getValue(k, out val1); DBElement <Key, Data> elem = val1 as DBElement <Key, Data>; XElement dbelem = persistDBelement <Key, Data>(elem); pair.Add(dbelem); noSqlDb.Add(pair); } string file_name = generate_file_name(); xml.Save(file_name); return(file_name); }
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"); }