static void Main(string[] args)
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            ItemFactory <int, string> itemFactory       = new ItemFactory <int, string>();

            "Demonstrating Requirement #2".title();
            DBElement <int, string> elem = itemFactory.Create();

            elem.name      = "element";
            elem.descr     = "test element";
            elem.timeStamp = DateTime.Now.AddDays(-4);
            elem.children.AddRange(new List <int> {
                1, 2, 3
            });
            elem.payload = "elem's payload";
            WriteLine("\n Item to be inserted.. \n");
            elem.showElement();
            db.insert(1, elem);
            db.showDB();
            WriteLine("\n Inserting second element into DB :");
            DBElement <int, string> elem2 = new DBElement <int, string>();

            elem2.name      = "element2";
            elem2.descr     = "test element2";
            elem2.timeStamp = DateTime.Now;
            elem2.children.AddRange(new List <int> {
                1, 2, 3, 4
            });
            elem2.payload = "elem2's payload";
            WriteLine("\nItem to be inserted.. \n");
            elem2.showElement();
            db.insert(2, elem2);
            WriteLine("\n\n DB after insertion:");
            db.showDB();
            WriteLine("\n\n DB after insertion:");
            db.showDB();
            var dict = new Dictionary <int, DBElement <int, string> >();
            var keys = db.searchForTimeStamp(DateTime.Now.AddDays(-6), DateTime.Now.AddDays(-3));

            Console.WriteLine("\nRunning a query on database..\n");
            Console.WriteLine("\n\nThe result are the following keys:\n\n");
            foreach (var item in keys)
            {
                dynamic result = db.searchValue(item);
                Console.Write("{0}, ", item);
                dict.Add(item, result);
            }
            DBFactory <int, DBElement <int, string> > dbFactory = new DBFactory <int, DBElement <int, string> >(dict);

            "Immutable database:".title('.');
            Console.WriteLine("\n\nImmutable database is of type DBFactory, which is immutable (Found in DBFactory.cs).");
        }
示例#2
0
        private void test1()
        {
            DBEngine <int, DBElement <int, string> > db = new DBEngine <int, DBElement <int, string> >();
            DBEngine <string, DBElement <string, List <string> > > dbString = new DBEngine <string, DBElement <string, List <string> > >();
            ItemFactory <int, string> itemFactory = new ItemFactory <int, string>();

            //------------Search for value---------------------
            "Search for a value".title('.');
            WriteLine("\n\n Elements in DB :");
            db.showDB();
            int key = 2;

            WriteLine("\n\nSearching for value of Key {0}\n\n", key);
            var value = db.searchValue <int, string>(key);

            if (value != null)
            {
                value.showElement();
            }
            else
            {
                WriteLine("\nKey not found..!\n\n");
            }
            //------------Search for value---------------------

            //------------Search for children---------------------
            "Search for children:".title('.');
            WriteLine("\n\n Elements in DB :");
            db.showDB();
            key = 3;
            WriteLine("\n\nSearching for Children of Key {0}\n", key);
            dynamic children = db.searchChildren(key);

            foreach (var child in children)
            {
                Write("{0}, ", child);
            }
            //------------Search for children---------------------
        }
        /// <summary>
        /// Queries the database.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public String QueryDatabase(String input)
        {
            String result    = String.Empty;
            var    dbElement = ParseXml(input);

            switch (dbElement.dbOperation)
            {
            case "Add":
                db.insert(dbElement.key, dbElement);
                result = "Key " + dbElement.key + " inserted successfully!";
                break;

            case "Edit":
                db.replaceKeyInstance(dbElement.key, dbElement);
                result = "Key " + dbElement.key + "'s value modified successfully!";
                break;

            case "Delete":
                if (db.delete(dbElement.key))
                {
                    result = "Key " + dbElement.key + " deleted successfully!";
                }
                else
                {
                    result = "Key " + dbElement.key + " not found!!";
                }
                break;

            case "SearchChild":
                var children = db.searchChildren(dbElement.key);
                result = "Children are " + string.Join(", ", children.ToArray());
                break;

            case "Search":
                var value = db.searchValue(dbElement.key);
                if (value != null)
                {
                    result  = "Key " + dbElement.key + " found!!!!!\n\n";
                    result += value.showEnumerableElement();
                }
                else
                {
                    result = "Key " + dbElement.key + " not found!!";
                }
                break;

            case "Persist":
                db.toXml();
                result = "Database persisted successfully";
                break;

            case "Restore":
                db.restoreDatabase();
                result = "Database successfully restored!!";
                break;

            default: break;
            }
            string dbView = db.showEnumerableDB();

            "\nDB Contents:\n".title();
            result += dbView;
            return(result);
        }