public void 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("NoSQL Database"); //title for XML xml.Add(comment); XElement NoSQLelem = new XElement("NoSQLDB_ELEMENTS"); XElement type = new XElement("numbers", typeof(Key)); XElement payload = new XElement("name", typeof(Data)); xml.Add(NoSQLelem); NoSQLelem.Add(type); NoSQLelem.Add(payload); foreach (Key k1 in db.Keys()) { XElement tags = new XElement("open_close"); XElement key = new XElement("new_Key", k1); //tag for new keys generated tags.Add(key); Value value1; db.getValue(k1, out value1); DBElement <Key, Data> element = value1 as DBElement <Key, Data>; WriteLine(element.showElement()); XElement dbelement = persistdbelement <Key, Data>(element); tags.Add(dbelement); NoSQLelem.Add(tags); xml.Save("XML_FILE.xml"); //XML file name } }
{ // Function to perform addition of relationships public static bool addRelations <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Key key2) { Value val1, val2; bool key1_present = dbedit.getValue(key1, out val1); // check if key1 is present if (key1_present) { bool key2_present = dbedit.getValue(key2, out val2); // check if key2 is present if (key2_present) { DBElement <Key, Data> element = val1 as DBElement <Key, Data>; element.children.Add(key1); } } return(true); }
public Value queryvalue<Key, Value, Data>(DBEngine<Key, Value> db, Key key) { Value getqueryvalue; bool key_present = db.getValue(key, out getqueryvalue); if (key_present) // check if key is present return getqueryvalue; //if present return value else Console.WriteLine("key not present"); // else error message return default(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()); } }
public List<key> querychildren<key, value, Data>(DBEngine<key, value> db, key Key) { value getqueryvalue; bool key_present = db.getValue(Key, out getqueryvalue); // check if key present DBElement<key, Data> temp = getqueryvalue as DBElement<key, Data>; // create new element to store value if (key_present) return temp.children; else Console.WriteLine("invalid key"); // if key not present error message return null; }
// function to perform removal of relationships public static bool removeRelation <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Key key2) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; if (elem.children.Contains(key2)) { elem.children.Remove(key2); } } return(true); }
// Function to perform editing of name in metedata public static bool editName <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, string new_name) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.name = new_name; return(true); } else { return(false); } }
// function to edit instances public static bool editInstance <Key, Value, Data>(this DBEngine <Key, Value> dbedit, Key key1, Data new_instance) { Value value; if (dbedit.getValue(key1, out value)) { DBElement <Key, Data> elem = value as DBElement <Key, Data>; elem.payload = new_instance; return(true); } else { return(false); } }
static void Main(string[] args) { "Testing DBEngine Package".title('='); WriteLine(); // New element being created Write("\n --- Test DBElement<int,string> ---"); DBElement <int, string> elem1 = new DBElement <int, string>(); elem1.payload = "a payload"; Write(elem1.showElement <int, string>()); WriteLine(); // New element being created DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord"); elem2.payload = "The Empire strikes back!"; Write(elem2.showElement <int, string>()); WriteLine(); // New element being created 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(); WriteLine("\n\n removing element of key 2"); bool p4 = db.remove(2); db.show <int, DBElement <int, string>, string>(); Write("\n --- Test DBElement<string,List<string>> ---"); // New element being created of type string and list of strings 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(); //new elements being created 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); }; // create a new database of string and list of string type 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(); // removing element WriteLine("removing element "); newdb.remove("newerelem2"); newdb.show <string, DBElement <string, List <string> >, List <string>, string>(); // testing editing of metadata text "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"); }