public void XMLWrite(DBEngine<int, DBElement<int, string>> db, out string pathname)
 {
     pathname = "xmlDoc.xml";
     // The root element is nosqldb or nosqldbLOS, which we can say, is the name
     // of the database of the corresponding type. The declaration is important
     // from an XML parser point of view.
     DBElement<int, string> Val = new DBElement<int, string>();
     XDocument doc = new XDocument(new XElement("nosqldb"));
     doc.Declaration = new XDeclaration("1.0", "utf - 8", "yes");
     XElement keyType = new XElement("keytype", "int");
     XElement payloadType = new XElement("payloadtype", "string");
     doc.Root.Add(keyType);
     doc.Root.Add(payloadType);
     foreach (var key in db.Keys())
     {
         XElement keyNode = new XElement("key", key);
         db.getValue(key, out Val);
         XElement elementNode = new XElement("element");
         elementNode.Add(new XElement("name", Val.name));
         elementNode.Add(new XElement("descr", Val.descr));
         elementNode.Add(new XElement("timeStamp", Val.timeStamp.ToString()));
         XElement childrenNode = new XElement("children"); //since children is List<Key>
         foreach (var item in Val.children)
         {
             childrenNode.Add(new XElement("key", item));
         }
         elementNode.Add(childrenNode);
         elementNode.Add(new XElement("payload", Val.payload)); //since payload is string for this type of database
         doc.Root.Add(keyNode);
         doc.Root.Add(elementNode);
     }
     doc.Save(pathname);
     
 }
 /* Defining a query using lambda function to search specific key 
 */
 public void key_value_search(DBEngine<int, DBElement<int, string>> db, IQuery<int, DBElement<int, string>> i_query, QueryEngine<int, DBElement<int, string>> qe )
 {
     "Query for value with specified key (key = 2):".title();
     WriteLine();
     int key_to_search = 2;
     Func<int, string, bool> keyValueSearch = (int key, string search) => //lambda function
     {
         if (!db.Keys().Contains(key))
             return false;
         else
         {
             if (key == int.Parse(search))
             {
                 DBElement<int, string> ele = new DBElement<int, string>();
                 db.getValue(key, out ele);
                 return true;
             }
             else { return false; }
         }
     };
     // pass query to query engine and call simpleQuery to make query on DBEngine
     qe.simpleQuery(keyValueSearch, key_to_search.ToString(), out i_query);
     WriteLine();
     foreach (var key in i_query.Keys())
     {
         DBElement<int, string> temp = new DBElement<int, string>();
         i_query.getValue(key, out temp);
         WriteLine("key : {0}", key);
         temp.showElement();
         WriteLine();
     }
 }
Exemple #3
0
        //---------< Call QueryEngine based on the query type >-----------
        public bool call(DBEngine <string, DBElement <string, List <string> > > db, string request)
        {
            if (db.Keys() == null)
            {
                return(false);
            }
            switch (request)
            {
            case "value":
                if (qe.getValue(query, db, out elem))
                {
                    return(true);
                }
                break;

            case "children":
                if (qe.getChildren(query, db, out replyList))
                {
                    return(true);
                }
                break;

            case "pattern":
                if (qe.searchPattern(query, db, out replyList))
                {
                    return(true);
                }
                break;

            case "string":
                if (qe.searchString(query, db, out replyList))
                {
                    return(true);
                }
                break;

            case "interval":
                if (qe.searchInterval(query, "", db, out replyList))
                {
                    return(true);
                }
                break;

            default: return(false);
            }
            return(false);
        }
        static void Main(string[] args)
        {
            "Testing PersistEngine Package".title('=');
            WriteLine();
            DBEngine<int, DBElement<int, string>> db = new DBEngine<int, DBElement<int, string>>();
            "\nSave to an XML file".title();
            PersistEngineTest p1 = new PersistEngineTest();
            p1.insertData(db);
            dynamic allKeys = db.Keys();
            PersistEngine<int,DBElement<int,string>> pEngine = new PersistEngine<int, DBElement<int, string>>(db);
            pEngine.persistToXML(allKeys);
            WriteLine("\n\nAbove database is stored as XML file in local machine");
            WriteLine();

            WriteLine("\nThe persisted XML file along with new key/value pairs are augmented to the database.\n");
            WriteLine("Below shown key/value pairs are augmented to the database.\n");
            pEngine.augmentDatabaseFromXML(db);                                         //Augment the persisted database along with new values to the main database
            pEngine.persistToXML(allKeys);
            db.showDB();
            WriteLine();
            WriteLine();

            "\nPersist database every 5 seconds until its cancelled".title();
            WriteLine();

            pEngine.scheduledSaveDatabase();
            WriteLine();
            WriteLine();

            "\nProject dependancy and realtionships".title();
            WriteLine();
            DBEngine<string, DBElement<string, List<string>>> dependancyDb = new DBEngine<string, DBElement<string, List<string>>>();
            PersistEngine<string, DBElement<string, List<string>>> pEngineString = new PersistEngine<string, DBElement<string, List<string>>>(dependancyDb);
            try
            {
                Console.WriteLine("\nBelow details provide information on dependancy of every package in the project\n");
                pEngine.displayDependancy();
                dependancyDb.showEnumerableDB();
                WriteLine();
            }
            catch (Exception e)
            {
                WriteLine("\n" + e.Message + "\n");
            }
        }
    /* Defining a query using lambda function to search specific key 
    */
    public bool key_value_search(DBEngine<string, DBElement<string, List<string>>> db, out IQuery<string, DBElement<string, List<string>>> i_query, QueryEngine<string, DBElement<string, List<string>>> qe, string key_to_search = "12345")
    {
      "Query for value with specified key (key = element2):".title();
      WriteLine();

      Func<string, string, bool> keyValueSearch = (string key, string search) => //lambda function
      {
        if (!db.Keys().Contains(key))
          return false;
        else
        {
          if (key == (search))
          {
            DBElement<string, List<string>> ele = new DBElement<string, List<string>>();
            db.getValue(key, out ele);
            return true;
          }
          else { return false; }
        }
      };
      // pass query to query engine and call simpleQuery to make query on DBEngine

      if (qe.simpleQuery(keyValueSearch, key_to_search.ToString(), out i_query))
      {
        WriteLine();
        foreach (var key in i_query.Keys())
        {
          DBElement<string, List<string>> temp = new DBElement<string, List<string>>();
          i_query.getValue(key, out temp);
          WriteLine("key : {0}", key);
          temp.showEnumerableElement();
          WriteLine();
        }
        return true;
      }
      else
      {
        return false;
      }
    }
 /* Defining a query using lambda function to search specific elements belonging in specific  
 *  time-interval with end of time interval equal to present
 */
 public void date_time_specific(DBEngine<string, DBElement<string, List<string>>> db, out IQuery<string, DBElement<string, List<string>>> i_query, QueryEngine<string, DBElement<string, List<string>>> qe, DateTime start, DateTime end = new DateTime())
 {
   "Query for keys with specified date time interval: start=10/4/2015 end=10/5/2015".title();
   WriteLine();
   Func<string, DateTime, DateTime, bool> TimeDateQuery = (string key, DateTime query_start, DateTime query_end) => //lambda function
   {
     if (!db.Keys().Contains(key))
       return false;
     else
     {
       DBElement<string, List<string>> ele = new DBElement<string, List<string>>();
       db.getValue(key, out ele);
       int start_result = DateTime.Compare(query_start, ele.timeStamp);
       int end_result = DateTime.Compare(query_end, ele.timeStamp);
       if ((start_result <= 0) && (end_result >= 0))
       {
         return true;
       }
       else return false;
     }
   };
   // pass query to query engine and call simpleQuery to make query on DBEngine
   qe.simpleQueryDate(TimeDateQuery, out i_query, start, end);
   WriteLine();
   foreach (var key in i_query.Keys())
   {
     DBElement<string, List<string>> temp = new DBElement<string, List<string>>();
     i_query.getValue(key, out temp);
     WriteLine("key : {0}", key);
     temp.showEnumerableElement();
     WriteLine();
   }
 }
 /* Defining a query using lambda function to search specific string in metadata 
 */
 public bool metadata_string(DBEngine<string, DBElement<string, List<string>>> db, out IQuery<string, DBElement<string, List<string>>> i_query, QueryEngine<string, DBElement<string, List<string>>> qe, string metadata_str = "ele")
 {
   "Query for specified string in metadata: String = 'ele' ".title();
   WriteLine();
   Func<string, string, bool> stringMetadata = (string key, string search) => //lambda function
   {
     if (!db.Keys().Contains(key))
       return false;
     else
     {
       DBElement<string, List<string>> ele = new DBElement<string, List<string>>();
       db.getValue(key, out ele);
       if (ele.name.Contains(search) || ele.descr.Contains(search))
         return true;
       else return false;
     }
   };
   // pass query to query engine and call simpleQuery to make query on DBEngine
   if (qe.simpleQuery(stringMetadata, metadata_str, out i_query))
   {
     WriteLine();
     foreach (var key in i_query.Keys())
     {
       DBElement<string, List<string>> temp = new DBElement<string, List<string>>();
       i_query.getValue(key, out temp);
       WriteLine("key : {0}", key);
       temp.showEnumerableElement();
       WriteLine();
     }
     return true;
   } else
   {
     return false;
   }
 }
    /* Defining a query using lambda function to search specific key matching default pattern
    */
    public void default_pattern_matching(DBEngine<string, DBElement<string, List<string>>> db, out IQuery<string, DBElement<string, List<string>>> i_query, QueryEngine<string, DBElement<string, List<string>>> qe)
    {
      string pattern = "";
      "Query for keys matching with specified pattern (pattern = none  -> default case):".title();
      WriteLine();
      Func<string, string, bool> keysMatchingPattern = (string key, string search) => //lambda function
      {
        if (!db.Keys().Contains(key))
          return false;
        else
        {
          if (key.ToString().Contains(search))
          {
            DBElement<string, List<string>> ele = new DBElement<string, List<string>>();
            db.getValue(key, out ele);
            return true;
          }
          else return false;
        }
      };
      // pass query to query engine and call simpleQuery to make query on DBEngine
      qe.simpleQuery(keysMatchingPattern, pattern, out i_query);
      WriteLine();
      foreach (var key in i_query.Keys())
      {
        DBElement<string, List<string>> temp = new DBElement<string, List<string>>();
        i_query.getValue(key, out temp);
        WriteLine("key : {0}", key);
        temp.showEnumerableElement();
        WriteLine();
      }

    }
 private void display_children(IQuery<string, DBElement<string, List<string>>> i_query, DBEngine<string, DBElement<string, List<string>>> db)
 {
   foreach (var key in i_query.Keys())
   {
     DBElement<string, List<string>> temp = new DBElement<string, List<string>>();
     i_query.getValue(key, out temp);
     WriteLine("children of element with key {0} :", key);
     WriteLine();
     if (temp.children != null)
     {
       int i = 0;
       foreach (string child in temp.children)
       {
         WriteLine("Children {0}", ++i);
         DBElement<string, List<string>> temp_child = new DBElement<string, List<string>>();
         if (db.Keys().Contains(child))
         {
           db.getValue(child, out temp_child);
           WriteLine("key : {0}", child);
           temp_child.showEnumerableElement();
           WriteLine();
         }
         else
         {
           WriteLine("no value with key {0} is present in database", child);
           WriteLine();
         }
       }
     }
   }
 }
Exemple #10
0
 /// <summary>
 /// Creation of xml and the dbelement contents into xml
 /// </summary>
 /// <param name="db"></param>
 /// <param name="displayItems"></param>
 private static void XMLCreation(DBEngine<int, DBElement<int, string>> db, XElement displayItems)
 {
     foreach (int key in db.Keys())
     {
         XElement displayitem = new XElement("Item");
         DBElement<int, string> elem = new DBElement<int, string>();
         db.getValue(key, out elem);
         XElement displayKey = new XElement("Key", key); //read the key  
         XElement displayValue = new XElement("Value");  //read the value
         XElement displayName = new XElement("Name", elem.name); //read the name
         XElement displayDescr = new XElement("Description", elem.descr);    //read the desc
         XElement displayTime = new XElement("TimeStamp", elem.timeStamp.ToString("yyyy-MM-dd HH:mm:ss"));
         XElement displayChildren = new XElement("Children");
         displayChildren = new XElement("Children"); //read the children into xml
         foreach (var i in elem.children)
         {
             XElement displayChild = new XElement("Child", i);
             displayChildren.Add(displayChild);
         }
         XElement displayPayLoad = new XElement("Payload", elem.payload);    //read the payload
         displayValue.Add(displayName);
         displayValue.Add(displayDescr);
         displayValue.Add(displayTime);
         if (elem.children.Count > 0)
             displayValue.Add(displayChildren);
         displayValue.Add(displayPayLoad);
         displayitem.Add(displayKey);
         displayitem.Add(displayValue);
         displayItems.Add(displayitem);
     }
 }
 /* Defining a query using lambda function to search children of specific element 
 */
 public void key_children_search(DBEngine<int, DBElement<int, string>> db, IQuery<int, DBElement<int, string>> i_query, QueryEngine<int, DBElement<int, string>> qe)
 {
     int specific_key = 2;
     "Query for children of specified key (key = 2):".title();
     WriteLine();
     Func<int, string, bool> childrenQuery = (int key, string search) => //lambda function
     {
         if (!db.Keys().Contains(key))
             return false;
         if (key == int.Parse(search))
         {
             DBElement<int, string> ele = new DBElement<int, string>();
             db.getValue(key, out ele);
             return true;
         }
         else return false;
     };
     // pass query to query engine and call simpleQuery to make query on DBEngine
     qe.simpleQuery(childrenQuery, specific_key.ToString(), out i_query);
     WriteLine();
     foreach (var key in i_query.Keys())
     {
         DBElement<int, string> temp = new DBElement<int, string>();
         i_query.getValue(key, out temp);
         WriteLine("children of element with key {0} :", key);
         WriteLine();
         if (temp.children != null)
         {
             int i = 0;
             foreach (int child in temp.children)
             {
                 WriteLine("Children {0}", i++);
                 DBElement<int, string> temp_child = new DBElement<int, string>();
                 if (db.Keys().Contains(child))
                 {
                     db.getValue(child, out temp_child);
                     WriteLine("key : {0}", child);
                     temp_child.showElement();
                     WriteLine();
                 }
                 else
                 {
                     WriteLine("no value with key {0} is present in database", child);
                     WriteLine();
                 }
             }
         }
     }
 }
        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);
        }
 /* Defining a query using lambda function to search children of specific element 
 */
 public bool key_children_search(DBEngine<string, DBElement<string, List<string>>> db, out IQuery<string, DBElement<string, List<string>>> i_query, QueryEngine<string, DBElement<string, List<string>>> qe, string specific_key = "element2")
 {
   
   "Query for children of specified key (key = element2):".title();
   WriteLine();
   Func<string, string, bool> childrenQuery = (string key, string search) => //lambda function
   {
     if (!db.Keys().Contains(key))
       return false;
     if (key == (search))
     {
       DBElement<string, List<string>> ele = new DBElement<string, List<string>>();
       db.getValue(key, out ele);
       return true;
     }
     else return false;
   };
   // pass query to query engine and call simpleQuery to make query on DBEngine
   if (qe.simpleQuery(childrenQuery, specific_key.ToString(), out i_query))
   {
     WriteLine();
     display_children(i_query, db);
     return true;
   }
   else
   {
     return false;
   }
 }
 private void restore_database(out string str, ref IEnumerator<string> msg_enumerator, DBEngine<int, DBElement<int, string>> db)
 {
     str = "";
       "Database before restoring".title();
       db.showDB();
       Console.WriteLine();
       while (msg_enumerator.MoveNext())
       {
     if (msg_enumerator.Current == "Source")
     {
       msg_enumerator.MoveNext();
       Console.WriteLine(msg_enumerator.Current);
       int count = db.Keys().ToArray().Length;
       db.create_db_from_xml(msg_enumerator.Current);
       if(db.Keys().ToArray().Length > count)
       {
     str += "Database Restored from " + msg_enumerator.Current + " file.";
       } else
       {
     str += "Database not able to restore from " + msg_enumerator.Current + "file";
       }
       return;
     }
       }
       str += "Database persistence failed.";
 }
 public void XMLRestoreLOS(string pathnameLOS, DBEngine<string, DBElement<string, List<string>>> dbLOS)
 {
     DBElement<string, List<string>> elemStringLOS;
     XDocument xdocLOS = XDocument.Load(pathnameLOS);
     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);
         }
     }
 }
 /* The restore functions capture elements from the XML files provided.
  * Key will always be the node preceeding the element node in our file
  * structure, so the previous node of element is our key for that element.
  * Then the keys and the corresponding values are extracted and entered into
  * the corresponding database. If a key is already present in the database,
  * I have chosen to discard it.
  */
 public void XMLRestore(string pathname, DBEngine<int, DBElement<int, string>> db)
 {
     DBElement<int, string> elemString;
     XDocument xdoc = XDocument.Load(pathname);
     var elem = xdoc.Root.Elements("element"); //extracting all elements from the file
     foreach (XElement i in elem)
     {
         XElement keyNode = (XElement)i.PreviousNode;
         int key = Int32.Parse(keyNode.Value.ToString()); //key is of type int for this database, so we need to cast it
         if (!db.Keys().Contains(key))
         {
             elemString = new DBElement<int, string>();
             elemString.name = i.Element("name").Value.ToString();
             elemString.timeStamp = Convert.ToDateTime(i.Element("timeStamp").Value.ToString()); //the format extracted from the file may or may not be a string
             elemString.descr = i.Element("descr").Value.ToString();
             elemString.payload = i.Element("payload").Value.ToString(); //we know that the payload is string
             elemString.children.Clear();
             var childrenNode = i.Element("children").Elements("key"); //since each child of the element is stored as a <key> in the file
             foreach (var c in childrenNode)
                 elemString.children.Add(Int32.Parse(c.Value));
             db.insert(key, elemString);
         }
     }
 }
 private void time_date_inerval(out string str, ref IEnumerator<string> msg_enumerator, DBEngine<string, DBElement<string, List<string>>> db)
 {
     IQuery<string, DBElement<string, List<string>>> i_query = new DBEngine<string, DBElement<string, List<string>>>();
       QueryEngine<string, DBElement<string, List<string>>> qe = new QueryEngine<string, DBElement<string, List<string>>>(db);
       QueryPredicate qp = new QueryPredicate();
       DateTime start = new DateTime();
       DateTime end = new DateTime();
       str = "";
       bool flag = false;
       while (msg_enumerator.MoveNext()) {
     switch(msg_enumerator.Current.ToString())
     {
       case "sdate-time":
     msg_enumerator.MoveNext();
     DateTime.TryParse(msg_enumerator.Current.ToString(), out start);
     break;
       case "edate-time":
     msg_enumerator.MoveNext();
     if (msg_enumerator.Current.ToString() != "" && DateTime.TryParse(msg_enumerator.Current.ToString(), out end)) ;
     else
       flag = true;
     break;
     }
       }
       DBElement<string, List<string>> ele = new DBElement<string, List<string>>();
       if (flag && qp.default_date_time_specific(db, out i_query, qe, start))
       {
     foreach (var key in i_query.Keys())
       if (i_query.getValue(key, out ele))
     str += " Element within timespan starting:" + start + " and ending: " + DateTime.Now + " found in Database.\n Key: " + key + "\nValue at given key: " + ele.showElement<string, List<string>, string>();
       }
       else if (qp.default_date_time_specific(db, out i_query, qe, start, end))
       {
     foreach (var key in i_query.Keys())
     {
       if (i_query.getValue(key, out ele))
       {
     str += " Element within timespan starting:" + start + " and ending: " + end + " found in Database.\n Key: " + key + "\nValue at given key: " + ele.showElement<string, List<string>, string>();
       }
     }
       } else
       {
     str = "Element within timespan: " + start + " and ending: " + end + " Not found in Database.\n";
       }
 }
 private void string_metadata(out string str, ref IEnumerator<string> msg_enumerator, DBEngine<string, DBElement<string, List<string>>> db)
 {
     IQuery<string, DBElement<string, List<string>>> i_query = new DBEngine<string, DBElement<string, List<string>>>();
       QueryEngine<string, DBElement<string, List<string>>> qe = new QueryEngine<string, DBElement<string, List<string>>>(db);
       QueryPredicate qp = new QueryPredicate();
       string search = "";
       str = "";
       msg_enumerator.MoveNext();
       msg_enumerator.MoveNext();
       search = msg_enumerator.Current.ToString();
       DBElement<string, List<string>> ele = new DBElement<string, List<string>>();
       if (qp.metadata_string(db, out i_query, qe, search))
       {
     foreach (var key in i_query.Keys())
     {
       if (i_query.getValue(key, out ele))
       {
     str += " Element with string:" + search + " found in Database.\n Key: "+ key +"\nValue at given key: " + ele.showElement<string, List<string>, string>();
       }
     }
       }
       else
       {
     str = "Element with string: " + search + " in metadata Not found in Database.\n";
       }
 }
 static void Main(string[] args)
 {
     
     //<------Create test DB----->
     "Testing for PersistEngine package".title();
     DBEngine<int, DBElement<int, string>> db = new DBEngine<int, DBElement<int, string>>();
     DBEngine<string, DBElement<string, List<string>>> enum_db = new DBEngine<string, DBElement<string, List<string>>>();
     
     for (int i = 0; i < 3; i++)
     {
         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> { 1, 2, 3 });
         elem.payload = "elem's payload";
         elem.showElement();
         db.insert(i, elem);
     }
     db.showDB();
     WriteLine();
     //<-------Save DB data to XML file--------->
     "Demonstrating XDocument class".title('=');
     WriteLine();
     "Creating XML string using XDocument".title();
     WriteLine();
     db.create_xml_from_db(false);
     //<---Empty DBEngine<int, DBElement<int, string>>--->
     "Removing entries from Db if any".title();
     if (db != null)
     {
         db.removeAll();
     }
     "Current DB state".title();
     db.showDB();
     WriteLine();
     db.create_db_from_xml();
     WriteLine();
     //<---------Create test enum DB-------->
     for (int i = 0; i < 3; i++)
     {
         DBElement<string, List<string>> elem = new DBElement<string, List<string>>();
         elem.name = "element";
         elem.descr = "test element";
         elem.timeStamp = DateTime.Now;
         elem.children.AddRange(new List<string> { "child1", "child2" });
         elem.payload = new List<string> { "payload 1", "payload 2" };
         elem.showEnumerableElement();
         enum_db.insert(i.ToString(), elem);
     }
     enum_db.showEnumerableDB();
     WriteLine();
     //<-------Save enum_DB data to XML file--------->
     "Creating XML string using XDocument".title();
     enum_db.create_xml_from_enum_db(false);
     WriteLine();
     
     if (enum_db != null)
     {
         enum_db.removeAll();
     }
     //<----Empty DBEngine<string, DBElement<string, List<string>>----->
     "Removing entries from enum Db if any".title();
     if (enum_db != null)
     {
         string[] key_array = enum_db.Keys().ToArray();
         foreach (string key in key_array)
         {
             enum_db.remove(key);
         }
     }            
     "Cureent enum DB state".title();
     enum_db.showEnumerableDB();
     WriteLine();
     enum_db.create_enum_db_from_xml();
     WriteLine();
     WriteLine();
     
 }
        static void Main(string[] args)
        {
            "Testing PersistEngine Package".title('=');
            WriteLine();
            DBEngine<int, DBElement<int, string>> db = new DBEngine<int, DBElement<int, string>>();
            "\nSave to an XML file".title();
            DBElement<int, string> elem1 = new DBElement<int, string>();
            elem1.name = "Usain Bolt";
            elem1.descr = "Athelte";
            elem1.timeStamp = DateTime.Now;
            elem1.children.AddRange(new List<int> { 2 });
            elem1.payload = "Fastest in the world";
            db.insert(1, elem1);

            DBElement<int, string> elem2 = new DBElement<int, string>();
            elem2.name = "Saina Nehwal";
            elem2.descr = "Badminton Player";
            elem2.timeStamp = DateTime.Now;
            elem2.children.AddRange(new List<int> { 1 });
            elem2.payload = "Famous badminton player";
            db.insert(2, elem2);
            db.showDB();
            WriteLine();

            dynamic allKeys = db.Keys();
            PersistEngine<int,DBElement<int,string>> pEngine = new PersistEngine<int, DBElement<int, string>>(db);
            pEngine.persistToXML(allKeys);
            WriteLine("\n\nAbove database is stored as XML file in local machine");
            WriteLine();

            WriteLine("\nThe persisted XML file along with new key/value pairs are augmented to the database.\n");
            WriteLine("Below shown key/value pairs are augmented to the database.\n");
            pEngine.augmentDatabaseFromXML(db);                                         //Augment the persisted database along with new values to the main database
            pEngine.persistToXML(allKeys);
            db.showDB();
            WriteLine();
            WriteLine();

            "\nPersist database every 5 seconds until its cancelled".title();
            WriteLine();

            pEngine.scheduledSaveDatabase();
            WriteLine();
            WriteLine();

            "\nProject dependancy and realtionships".title();
            WriteLine();
            DBEngine<string, DBElement<string, List<string>>> dependancyDb = new DBEngine<string, DBElement<string, List<string>>>();
            PersistEngine<string, DBElement<string, List<string>>> pEngineString = new PersistEngine<string, DBElement<string, List<string>>>(dependancyDb);
            try
            {
                Console.WriteLine("\nBelow details provide information on dependancy of every package in the project\n");
                pEngine.displayDependancy();
                dependancyDb.showEnumerableDB();
                WriteLine();
            }
            catch (Exception e)
            {
                WriteLine("\n" + e.Message + "\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();
      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");

      "Test for interface IQuery".title();
      IQuery<int, DBElement<int, string>> iq = db;
      foreach (int db_key in iq.Keys())
      {
           DBElement<int, string> ele = new DBElement<int, string>();
           iq.getValue(db_key, out ele);
           WriteLine("element at key {0}: {1}", db_key, ele);
      }

      int[] key_array = db.Keys().ToArray();
      WriteLine("removing element with key:{0} from database <int, DBElement<int, string>>",key_array[2]);
      WriteLine();
      db.remove(key_array[2]);
      db.show<int, DBElement<int, string>, string>();
      WriteLine();

      WriteLine("Removing all elements from database:");
      db.removeAll();
      WriteLine("DB after removal:");
      db.show<int, DBElement<int, string>, string>();


      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();
      WriteLine();
      
      string[] key_enum_array = newdb.Keys().ToArray();
      WriteLine("removing element with key:{0} from database <string, DBElement<int, List<string>>>",key_enum_array[0]);
      WriteLine();
      newdb.remove(key_enum_array[0]);
      newdb.show<string, DBElement<string, List<string>>, List<string>, string>();
      WriteLine();

      WriteLine("removing all elements from database:");
      newdb.removeAll();
      WriteLine("DB after removal:");
      newdb.show<string, DBElement<string, List<string>>, List<string>, string>(); 
    }
Exemple #22
0
 public void performOperations(DBEngine<string, DBElement<string, List<string>>> testDict, DBElement<string, List<string>> value)
 {       /*----------Perform operations as per the input given in the XML document--------------*/
     if (value.operation == "addition")
     {
         testDict.insert(value.key, value);          //insert the key/value pairs to the main database
         string s = "Database after inserting key " + value.key + " is";
         printDatabase(testDict, s);
     }
     if (value.operation == "edit")
     {
         testDict.saveValue(value.key, value);       //edit the value for the given key
         string s = "Database after editing key " + value.key + " is";
         printDatabase(testDict, s);
     }
     if (value.operation == "delete")
     {
         testDict.delete(value.key);                 //delete the key/value pair
         string s = "Database after deleting key " + value.key + " is";
         printDatabase(testDict, s);
     }
     if (value.operation == "persist database")
     {
         PersistEngine<string, DBElement<string, List<string>>> persist = new PersistEngine<string, DBElement<string, List<string>>>(testDict);
         var keys = testDict.Keys();
         persist.persistToXMLListPayload(keys);
         printDatabase(testDict, "Persisted database is:");
     }
     if (value.operation == "Query value")
     {
         DBElement<string, List<string>> valueOfKey = testDict.getValueOfKey(value.key);
         printQuery("Querying the database for value of key " + value.key + " is");
         Console.WriteLine("\n\nThe value of the Key {0} is:\n", value.key);
         valueOfKey.showEnumerableElement();
     }
     if (value.operation == "Query children")
     {
         QueryEngine<string, DBElement<string, List<string>>> qEngine = new QueryEngine<string, DBElement<string, List<string>>>(testDict);
         printQuery("Querying the database for value of key " + value.key + " is");
         List<string> children = qEngine.getChildrenOfKey(value.key);
         Console.WriteLine("\nThe children of the Key {0} are:\n", value.key);
         displayChildren(children);
     }
     if (value.operation == "Augment database")
     {
         PersistEngine<string, DBElement<string, List<string>>> persist = new PersistEngine<string, DBElement<string, List<string>>>(testDict);
         string fileName = "C:\\Users\\rakeshh91\\Documents\\Rakesh Documents\\Class Materials\\SMA\\Assignments\\Assignment 4 - Implementation\\CommPrototype\\augmentDatabase.xml";
         persist.augmentDatabaseFromXMLStringList(testDict, fileName);
         printDatabase(testDict, "Database after augmenting is:");
     }
 }