예제 #1
0
        void TestR7_2()
        {
            Write("\n ---Query for keys matching a specific pattern ---");
            Write("\n return all keys which contain string \"key\"");
            QueryEngine <string, List <string> > query2 = new QueryEngine <string, List <string> >(DBListOfString);
            List <string>       result;
            Func <string, bool> defineQuery = query2.defineSpecPatternQuery();

            query2.processQuery(defineQuery, out result);
            foreach (var r in result)
            {
                Write("\n key: {0}", r.ToString());
            }
            WriteLine();
            Write("\n ---Query keys contains a specific string in metadata ---");
            Write("\n return all keys that contains \"elemLos2\" in its metadata");
            defineQuery = query2.defineStringQuery("elemLos2");
            query2.processQuery(defineQuery, out result);
            foreach (var r in result)
            {
                Write("\n key: {0}", r.ToString());
            }
            WriteLine();
            Write("\n ---Query for values written within a specific time interval---");
            Write("\n Query for value written between 10/1/2015 00:00:00 and 10/2/2015 11:00:00");
            DateTime time1 = new DateTime(2015, 10, 1, 0, 0, 0);
            DateTime time2 = new DateTime(2015, 10, 2, 11, 0, 0);

            defineQuery = query2.defineTimeQuery(time1, time2);
            query2.processQuery(defineQuery, out result);
            foreach (var r in result)
            {
                Write("\n key: {0}", r.ToString());
            }
        }
        static void Main(string[] args)
        {
            Write("\n --- Test specific key query ---");
            Write("\n ---Database contents");

            DBElement <int, string> elem1 = new DBElement <int, string>();

            elem1.descr     = "payload desxription";
            elem1.name      = "element 1";
            elem1.timeStamp = DateTime.Now;
            elem1.payload   = "a payload";

            WriteLine();

            DBElement <int, string> elem2 = new DBElement <int, string>("Darth Vader", "Evil Overlord");

            elem2.descr     = "star war 2";
            elem2.name      = "element 2";
            elem2.timeStamp = new DateTime(2015, 9, 10, 12, 30, 1);
            elem2.payload   = "The Empire strikes back!";

            WriteLine();

            var elem3 = new DBElement <int, string>("Luke Skywalker", "Young HotShot");

            elem3.name      = "element 3";
            elem3.descr     = "star war 3";
            elem3.timeStamp = new DateTime(2015, 10, 2, 8, 0, 0);
            elem3.children  = new List <int> {
                1, 2, 3
            };
            elem3.payload = "X-Wing fighter in swamp - Oh oh!";

            WriteLine();
            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);

            db.show <int, DBElement <int, string>, string>();
            QueryEngine <int, string> QETest = new QueryEngine <int, string>(db);

            WriteLine();

            Write("\n --- Query element which key=2");
            QueryEngine <int, string> query = new QueryEngine <int, string>(db);
            DBElement <int, string>   elem;
            bool valQuery = query.KeyQuery(2, out elem);

            if (valQuery)
            {
                Write("\n  This element exist");
            }
            Write(elem.showElement <int, string>());
            WriteLine();

            Write("\n --- Test queries for children of a specified key ---");
            Write("\n --- Query for children of element which key=3 ---");
            List <int> children;
            bool       childQuery = query.queryChildren(3, out children);

            if (childQuery)
            {
                Write("\n  This element has child");
            }
            foreach (var child in children)
            {
                Write("\n Key of child: {0}", child.ToString());
            }
            WriteLine();

            Write("\n --- Test all keys that contain a specified string in their metadata section---");
            Write("\n --- query for \"star war\" in metadata, return keys ---");

            Func <int, bool> stringTest = QETest.defineStringQuery("star war");
            List <int>       keyCollection;
            bool             stringQuery = QETest.processQuery(stringTest, out keyCollection);

            foreach (var k in keyCollection)
            {
                Write("\n Key: {0}", k.ToString());
            }
            WriteLine();

            WriteLine("\n --- Test query according to a specified time-date interval ---");
            DateTime         time1 = new DateTime(2015, 10, 1, 0, 0, 0);
            DateTime         time2 = new DateTime(2015, 10, 2, 11, 0, 0);
            List <int>       timeCollection;
            Func <int, bool> timeTest   = QETest.defineTimeQuery(time1, time2);
            bool             timeResult = QETest.processQuery(timeTest, out timeCollection);

            foreach (var k in timeCollection)
            {
                WriteLine("key in specific time interval: {0}", k.ToString());
            }
            WriteLine();
            WriteLine("--- When time is not provided ---");
            DateTime time3 = new DateTime();

            timeTest = QETest.defineTimeQuery(time1, time3);
            bool timeQuery2 = QETest.processQuery(timeTest, out timeCollection);

            foreach (var k in timeCollection)
            {
                WriteLine("key in specific time interval: {0}", k.ToString());
            }
            WriteLine();
        }