Exemple #1
0
        // Это будет новый вариант класса Database, отличающийся тем, что он строго ориентирован на спецификацию через шаблонные деревья
        public static void Main(string[] args)
        {
            Console.WriteLine("ProgramDatabase starts.");
            Database2 db = new Database2("../../../Databases/", XElement.Parse(schema_str));

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

            bool toload = false;

            if (toload)
            {
                sw.Restart();
                string data_path = @"C:\home\FactographDatabases\PolarDemo\perpho.xml";
                db.LoadXML(XElement.Load(data_path));
                sw.Stop();
                Console.WriteLine("Load ok. Duration={0}", sw.ElapsedMilliseconds); // 4456 мс.
            }

            sw.Restart();
            var query = db.SearchByNameIn("Марчук", "person");

            Console.WriteLine(query.Count());
            sw.Stop();
            Console.WriteLine("SearchByNameIn ok. Duration={0}", sw.ElapsedMilliseconds); // 346 мс.

            sw.Restart();
            var portr = db.GetPortraitByIdIn(2870, "person");

            if (portr != null)
            {
                Console.WriteLine(portr.ToString());
            }
            sw.Stop();
            Console.WriteLine("GetPortraitByIdIn ok. Duration={0}", sw.ElapsedMilliseconds); // 4 мс.

            // Проверка портрета
            XElement format = new XElement("record", new XAttribute("type", "person"),
                                           new XElement("field", new XAttribute("prop", "name")),
                                           new XElement("field", new XAttribute("prop", "from-date")),
                                           new XElement("field", new XAttribute("prop", "description")),
                                           new XElement("inverse", new XAttribute("prop", "reflected"),
                                                        new XElement("record", new XAttribute("type", "reflection"),
                                                                     new XElement("field", new XAttribute("prop", "ground")),
                                                                     new XElement("direct", new XAttribute("prop", "in-doc"),
                                                                                  new XElement("record", new XAttribute("type", "photo-doc"),
                                                                                               new XElement("field", new XAttribute("prop", "name")))),
                                                                     null)),
                                           new XElement("field", new XAttribute("prop", "to-date")),
                                           new XElement("field", new XAttribute("prop", "sex")),
                                           null);

            sw.Restart();
            XElement portrait = null;

            portrait = db.GetPortraitById(2870, format);
            sw.Stop();
            System.Console.WriteLine("GetPortraitById OK. duration={0}", sw.ElapsedMilliseconds); // 35 ms.

            sw.Restart();
            portrait = db.GetPortraitById(2870, format);
            sw.Stop();
            //if (portrait != null) Console.WriteLine(portrait.ToString());
            System.Console.WriteLine("GetPortraitById OK. duration={0}", sw.ElapsedMilliseconds); // 4-5 ms.

            XElement tracer = XElement.Load(@"C:\home\FactographDatabases\PolarDemo\tracer.xml");

            sw.Restart(); int cnt = 0, sum = 0;
            foreach (XElement portra in tracer.Elements("portrait").Where(po => po.Attribute("type").Value == "person"))
            {
                int      id  = Int32.Parse(portra.Attribute("id").Value);
                XElement por = db.GetPortraitById(id, format);
                cnt++;
                sum += por.Elements("inverse").Count();
            }
            sw.Stop();
            Console.WriteLine("Tracer test ok. Duration={0} cnt={1} sum={2}", sw.ElapsedMilliseconds, cnt, sum); // 3611 мс., 134

            sw.Restart();
            foreach (XElement portra in tracer.Elements("portrait").Where(po => po.Attribute("type").Value == "person"))
            {
                int id = Int32.Parse(portra.Attribute("id").Value);
                db.GetPortraitById(id, format);
            }
            sw.Stop();
            Console.WriteLine("Tracer test ok. Duration={0}", sw.ElapsedMilliseconds); // 68 мс.
        }
Exemple #2
0
        //private int keyNew = 0;
        //private int counter = -1;
        internal Collection2(string cname, XElement schema, Database2 inDatabase)
        {
            collectionname  = cname;
            this.inDatabase = inDatabase;
            this.frecord    = schema.Elements("record").First(re => re.Attribute("type").Value == cname);
            string ftype = cname;

            // Это будет коллекция записей, сначала выявим тип записи для коллекции
            NamedType[] nt_arr = frecord.Elements()
                                 .Where(el => el.Name == "field" || el.Name == "direct")
                                 .Select(el =>
            {
                PType tpe = null;
                if (el.Name == "direct")
                {
                    tpe = new PType(PTypeEnumeration.integer);
                }
                else if (el.Name == "field")
                {
                    string el_type = el.Attribute("datatype").Value;
                    if (el_type == "string")
                    {
                        tpe = new PType(PTypeEnumeration.sstring);
                    }
                    else if (el_type == "int")
                    {
                        tpe = new PType(PTypeEnumeration.integer);
                    }
                }
                return(new NamedType(el.Attribute("prop").Value, tpe));
            })
                                 .ToArray();
            this.eType = new PTypeRecord(nt_arr);

            this.eeType = new PTypeRecord(
                new NamedType("deleted", new PType(PTypeEnumeration.boolean)),
                new NamedType("key", new PType(PTypeEnumeration.integer)),
                new NamedType("element", eType));
            string path = inDatabase.Path;

            cell = new PaCell(new PTypeSequence(eeType), path + cname + ".pac", false);
            if (cell.IsEmpty)
            {
                cell.Fill(new object[0]);
            }
            key_index = new FlexIndex2 <int>(path + cname + "_id_i", cell.Root, en => (int)en.Field(1).Get(), null);
            // Другие индексы
            foreach (var pair in frecord.Elements().Select((el, ind) => new { el = el, ind = ind }))
            {
                if (pair.el.Name != "direct")
                {
                    continue;
                }
                string           column           = pair.el.Attribute("prop").Value;
                string           totype           = pair.el.Element("record").Attribute("type").Value;
                string           name_combination = ftype + "(" + column + ")" + totype;
                FlexIndex2 <int> index            = new FlexIndex2 <int>(path + name_combination + ".pac", cell.Root,
                                                                         (PaEntry ent) => (int)ent.Field(2).Field(pair.ind).Get(), null);
                // Запишем в местный список
                indexes.Add(index);
                // Запишем в общий список
                inDatabase.external_indexes.Add(new IndexContext()
                {
                    type = ftype, prop = column, totype = totype, index = index
                });
            }
        }