Exemplo n.º 1
0
        public static void Main16()
        {
            string path = "../../../";

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            Random rnd = new Random();

            Console.WriteLine("Start GetStarted/Main16");
            int           cnt       = 0;
            Func <Stream> GenStream = () => new FileStream(path + "Databases/f" + (cnt++) + ".bin",
                                                           FileMode.OpenOrCreate, FileAccess.ReadWrite);

            TripleStoreInt32 store = new TripleStoreInt32(GenStream);

            int  npersons = 4_000_000;
            bool toload   = false;

            if (toload)
            {
                //nt.Clear();
                //nt.GetSetStr("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
                //int name_cod = nt.GetSetStr("http://fogid.net/o/name");

                IEnumerable <object> qu_persons = Enumerable.Range(0, npersons)
                                                  .SelectMany(i =>
                {
                    return(new object[] {
                        new object[] { "p" + i, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                                       new object[] { 1, "http://fogid.net/o/person" } },
                        new object[] { "p" + i, "http://fogid.net/o/name",
                                       new object[] { 2, "p" + i } },
                        new object[] { "p" + i, "http://fogid.net/o/age",
                                       new object[] { 2, "33 years" } }
                    });
                });
                IEnumerable <object> qu_fotos = Enumerable.Range(0, npersons * 2)
                                                .SelectMany(i =>
                {
                    return(new object[] {
                        new object[] { "f" + i, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                                       new object[] { 1, "http://fogid.net/o/photo" } },
                        new object[] { "f" + i, "http://fogid.net/o/name",
                                       new object[] { 2, "DSP" + i } }
                    });
                });
                IEnumerable <object> qu_reflections = Enumerable.Range(0, npersons * 6)
                                                      .SelectMany(i =>
                {
                    return(new object[] {
                        new object[] { "r" + i, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                                       new object[] { 1, "http://fogid.net/o/reflection" } },
                        new object[] { "r" + i, "http://fogid.net/o/reflected",
                                       new object[] { 1, "p" + rnd.Next(npersons) } },
                        new object[] { "r" + i, "http://fogid.net/o/indoc",
                                       new object[] { 1, "f" + rnd.Next(npersons * 2) } }
                    });
                });

                sw.Restart();
                var ntriples = store.GenerateTripleFlow(qu_persons.Concat(qu_fotos).Concat(qu_reflections));
                foreach (var t in ntriples.Skip(98).Take(10))
                {
                    Console.WriteLine(store.ToStr(t));
                }
                store.Build(ntriples);
                sw.Stop();
                Console.WriteLine($"store Build ok. Duration={sw.ElapsedMilliseconds}");
            }
            else
            {
                sw.Restart();
                store.Refresh();
                sw.Stop();
                Console.WriteLine($"Refresh for Phototeka {npersons} persons. Duration={sw.ElapsedMilliseconds}");
            }


            // Испытываю
            // Беру персону
            string id = "p" + (npersons * 2 / 3);
            int    nid, reflected, indoc, name;

            if (!store.TryGetCode(id, out nid))
            {
                throw new Exception("338433");
            }
            if (!store.TryGetCode("http://fogid.net/o/reflected", out reflected))
            {
                throw new Exception("338434");
            }
            if (!store.TryGetCode("http://fogid.net/o/indoc", out indoc))
            {
                throw new Exception("338435");
            }
            if (!store.TryGetCode("http://fogid.net/o/name", out name))
            {
                throw new Exception("338436");
            }
            var query1 = store.GetInverse(nid)
                         .Cast <object[]>()
                         .Where(t => (int)t[1] == reflected)
                         .SelectMany(t => store.GetBySubj((int)t[0])
                                     .Where(tt => (int)((object[])tt)[1] == indoc)
                                     .Select(tt => store.GetBySubj((int)((object[])((object[])tt)[2])[1])
                                             .Where(ttt => (int)((object[])ttt)[1] == name)
                                             .FirstOrDefault())
                                     )

            ;

            foreach (object[] t in query1)
            {
                Console.WriteLine(store.DecodeTriple(t));
            }
            Console.WriteLine();


            int nprobe = 1000;
            int total  = 0;

            sw.Restart();
            for (int i = 0; i < nprobe; i++)
            {
                // Беру случайную персону
                if (!store.TryGetCode("p" + rnd.Next(npersons), out nid))
                {
                    throw new Exception("338437");
                }
                var fots = store.GetInverse(nid)
                           .Cast <object[]>()
                           .Where(t => (int)t[1] == reflected)
                           .SelectMany(t => store.GetBySubj((int)t[0])
                                       .Where(tt => (int)((object[])tt)[1] == indoc)
                                       .Select(tt => store.GetBySubj((int)((object[])((object[])tt)[2])[1])
                                               .Where(ttt => (int)((object[])ttt)[1] == name)
                                               .FirstOrDefault())
                                       ).ToArray();
                total += fots.Length;
            }
            sw.Stop();
            Console.WriteLine($"{nprobe} tests ok. Duration={sw.ElapsedMilliseconds}");
            Console.WriteLine("========");

            var que = store.GetByObjName("p2").ToArray();

            foreach (var tri in que)
            {
                Console.WriteLine(store.ToStr(tri));
            }

            sw.Restart();
            total = 0;
            for (int i = 0; i < nprobe; i++)
            {
                string scod = "p" + rnd.Next(npersons);
                total += store.GetByObjName(scod).Where(obj => (string)((object[])((object[])obj)[2])[1] == scod).Count();
            }
            sw.Stop();
            Console.WriteLine($"{nprobe} GetByObjString tests ok. Duration={sw.ElapsedMilliseconds} total={total}");
            Console.WriteLine();


            return;

            // конец проверки

            int nelements = 5_000_000;
            // Начало таблицы имен 0 - type, 1 - name, 2 - person, 3 - parent
            int b = 4; // Начальный индекс назначаемых идентификаторов сущностей

            if (toload)
            {
                sw.Restart();
                var query = Enumerable.Range(0, nelements)
                            .SelectMany(i => new object[]
                {
                    new object[] { nelements + b - i - 1, 0, new object[] { 1, 2 } },
                    new object[] { nelements + b - i - 1, 1, new object[] { 2, "" + (nelements + b - i - 1) } },
                    new object[] { nelements + b - i - 1, 3, new object[] { 1, rnd.Next(nelements) + b } } // родитель
                });     // по 3 триплета на запись
                store.Build(query);
                sw.Stop();
                Console.WriteLine($"load of {nelements * 2} triples ok. Duration={sw.ElapsedMilliseconds}");
            }
            else
            {
                sw.Restart();
                store.Refresh();
                sw.Stop();
                Console.WriteLine($"Refresh for {nelements * 2} triples ok. Duration={sw.ElapsedMilliseconds}");
            }

            // Для проверки работы запрошу запись с ключом nelements * 2 / 3
            int ke = nelements * 2 / 3 + 2;
            var qu = store.GetBySubj(ke);

            foreach (object[] t in qu)
            {
                Console.WriteLine($"{t[0]} {t[1]}");
            }
            var qui = store.GetInverse(ke);

            foreach (object[] t in qui)
            {
                Console.WriteLine($"{t[0]} {t[1]} {((object[])t[2])[1]}");
            }

            nprobe = 1000;

            sw.Restart();
            for (int i = 0; i < nprobe; i++)
            {
                int subj = rnd.Next(nelements);
                var quer = store.GetBySubj(subj);
                if (quer.Count() != 3)
                {
                    foreach (object[] t in quer)
                    {
                        Console.WriteLine($"{t[0]} {t[1]}");
                    }
                }
            }
            sw.Stop();
            Console.WriteLine($"{nelements} elements {nprobe} GetAll search ok. duration={sw.ElapsedMilliseconds}");

            sw.Restart();
            total = 0;
            for (int i = 0; i < nprobe; i++)
            {
                int obj  = rnd.Next(nelements);
                var quer = store.GetInverse(obj);
                total += quer.Count();
            }
            sw.Stop();
            Console.WriteLine($"inverse elements {nprobe} GetInverse search ok. duration={sw.ElapsedMilliseconds} total = {total}");

            return;

            var qu2 = store.GetByObjName("p12345");

            foreach (object[] t in qu2)
            {
                Console.WriteLine($"{t[0]} {t[1]}");
            }


            nprobe = 1000;
            sw.Restart();
            total = 0;
            for (int i = 0; i < nprobe; i++)
            {
                var quer = store.GetByObjName("" + rnd.Next(nelements));
                total += quer.Count();
            }
            sw.Stop();
            Console.WriteLine($"Test === OBJECT===== {nprobe} queries for {total} elements. duration={sw.ElapsedMilliseconds}");
        }
Exemplo n.º 2
0
        static void Main19()
        {
            string path = "/Home/data/Databases/";

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            Random rnd = new Random();

            //Random rnd = new Random(11111);
            Console.WriteLine("Start TripleStore experiments");
            int           cnt       = 0;
            Func <Stream> GenStream = () => new FileStream(path + "f" + (cnt++) + ".bin",
                                                           FileMode.OpenOrCreate, FileAccess.ReadWrite);

            TripleStoreInt32 store = new TripleStoreInt32(GenStream, path);

            int  npersons = 40_000;
            bool toload   = true;

            if (toload)
            {
                IEnumerable <object> qu_persons = Enumerable.Range(0, npersons)
                                                  .SelectMany(i =>
                {
                    return(new object[] {
                        new object[] { "p" + i, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                                       new object[] { 1, "http://fogid.net/o/person" } },
                        new object[] { "p" + i, "http://fogid.net/o/name",
                                       new object[] { 2, "p" + i } },
                        new object[] { "p" + i, "http://fogid.net/o/age",
                                       new object[] { 2, "33 years" } }
                    });
                });
                IEnumerable <object> qu_fotos = Enumerable.Range(0, npersons * 2)
                                                .SelectMany(i =>
                {
                    return(new object[] {
                        new object[] { "f" + i, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                                       new object[] { 1, "http://fogid.net/o/photo" } },
                        new object[] { "f" + i, "http://fogid.net/o/name",
                                       new object[] { 2, "DSP" + i } }
                    });
                });
                IEnumerable <object> qu_reflections = Enumerable.Range(0, npersons * 6)
                                                      .SelectMany(i =>
                {
                    return(new object[] {
                        new object[] { "r" + i, "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
                                       new object[] { 1, "http://fogid.net/o/reflection" } },
                        new object[] { "r" + i, "http://fogid.net/o/reflected",
                                       new object[] { 1, "p" + rnd.Next(npersons) } },
                        new object[] { "r" + i, "http://fogid.net/o/indoc",
                                       new object[] { 1, "f" + rnd.Next(npersons * 2) } }
                    });
                });

                var triples_set = qu_persons.Concat(qu_fotos).Concat(qu_reflections);
                sw.Restart();
                store.Clear();
                store.Load(triples_set);
                store.Build();
                sw.Stop();
                Console.WriteLine($"store Build ok. Duration={sw.ElapsedMilliseconds}");
            }
            else
            {
                sw.Restart();
                store.Refresh();
                sw.Stop();
                Console.WriteLine($"Refresh for Phototeka {npersons} persons. Duration={sw.ElapsedMilliseconds}");
            }


            // Испытываю
            Console.WriteLine("===== single GetBySubject =====");
            // Беру персону
            string id = "p" + (npersons * 2 / 3);
            // Проверяю работу выборки по субъекту
            var trs = store.Get_s(id);

            // Печатаю
            foreach (object[] t in trs)
            {
                Console.WriteLine(store.TripleToString(t));
            }

            Console.WriteLine("===== single GetInverse =====");

            var trs2 = store.Get_t(id);

            // Печатаю
            foreach (object[] t in trs2)
            {
                Console.WriteLine(store.TripleToString(t));
            }

            Console.WriteLine("===== 1000 GetBySubject =====");

            sw.Restart();
            int ntriples = 0;

            for (int i = 0; i < 1000; i++)
            {
                string c = "p" + rnd.Next(npersons);
                ntriples += store.Get_s(c).Count();
            }
            sw.Stop();
            Console.WriteLine($"ok. duration={sw.ElapsedMilliseconds}");

            int pers = (npersons * 2 / 30);

            //pers = 243; //24;
            Console.WriteLine($"===== single Like {"p" + pers} =====");
            var trs3 = store.Like("p" + pers).ToArray();

            // Печатаю
            foreach (object[] t in trs3)
            {
                Console.WriteLine(store.TripleToString(t));
            }

            if (!store.TryGetCode(id, out int nid))
            {
                throw new Exception("222233");
            }
            //var trs2 = store.GetInverse(nid);
            //// Печатаю
            //foreach (object[] t in trs2) Console.WriteLine(store.DecodeTriple(t));

            //    return;

            int reflected, indoc, name;

            Console.WriteLine("===== Complex query =====");

            if (!store.TryGetCode("http://fogid.net/o/reflected", out reflected))
            {
                throw new Exception("338434");
            }
            if (!store.TryGetCode("http://fogid.net/o/indoc", out indoc))
            {
                throw new Exception("338435");
            }
            if (!store.TryGetCode("http://fogid.net/o/name", out name))
            {
                throw new Exception("338436");
            }
            var query1 = store.Get_t(nid)
                         .Cast <object[]>()
                         .Where(t => (int)t[1] == reflected)
                         .SelectMany(t => store.Get_s((int)t[0])
                                     .Where(tt => (int)((object[])tt)[1] == indoc)
                                     .Select(tt => store.Get_s((int)((object[])((object[])tt)[2])[1])
                                             .Where(ttt => (int)((object[])ttt)[1] == name)
                                             .FirstOrDefault())
                                     )

            ;

            foreach (object[] t in query1)
            {
                Console.WriteLine(store.TripleToString(t));
            }
            Console.WriteLine();

            Console.WriteLine("===== 1000 GetBySubject =====");

            int nprobe = 1000;
            int total  = 0;

            sw.Restart();
            for (int i = 0; i < nprobe; i++)
            {
                // Беру случайную персону
                if (!store.TryGetCode("p" + rnd.Next(npersons), out nid))
                {
                    throw new Exception("338437");
                }
                var fots = store.Get_t(nid)
                           //.Cast<object[]>()
                           //.Where(t => (int)t[1] == reflected)
                           //.SelectMany(t => store.Get_s((int)t[0])
                           //    .Where(tt => (int)((object[])tt)[1] == indoc)
                           //    .Select(tt => store.Get_s((int)((object[])((object[])tt)[2])[1])
                           //        .Where(ttt => (int)((object[])ttt)[1] == name)
                           //        .FirstOrDefault()))
                ;
                total += fots.Count();
            }
            sw.Stop();
            Console.WriteLine($"{nprobe} tests ok. Duration={sw.ElapsedMilliseconds} total={total}");

            /*
             *          var que = store.GetByObjName("p2").ToArray();
             *          foreach (var tri in que)
             *          {
             *              Console.WriteLine(store.ToStr(tri));
             *          }
             *
             *          sw.Restart();
             *          total = 0;
             *          for (int i = 0; i < nprobe; i++)
             *          {
             *              string scod = "p" + rnd.Next(npersons);
             *              total += store.GetByObjName(scod).Where(obj => (string)((object[])((object[])obj)[2])[1] == scod).Count();
             *          }
             *          sw.Stop();
             *          Console.WriteLine($"{nprobe} GetByObjString tests ok. Duration={sw.ElapsedMilliseconds} total={total}");
             *          Console.WriteLine();
             */
        }