Example #1
0
        protected override void AddInIndex(PaEntry entry)
        {
            int node;

            if (entry.Type.Vid == PTypeEnumeration.integer)
            {
                node = (int)entry.Get();
                if (!searchIndex.ContainsKey(node))
                {
                    searchIndex.Add(node, entry.offset);
                }
            }
            else
            {
                node = (int)entry.Field(0).Get();

                if (!searchIndex.ContainsKey(node))
                {
                    searchIndex.Add(node, entry.offset);
                }
                foreach (var chiledEntry in entry.Field(1).Elements())
                {
                    AddInIndex(chiledEntry);
                }
            }
        }
Example #2
0
        // Сортировка по дополнительному индексу. Предполагается, что по первичному индексу сортировка уже произведена
        public void AdditionalIndex(int second_field)
        {
            long   start = 0;
            object current_prime_value = null;

            if (table.Count() == 0)
            {
                return;
            }
            PaEntry tab_entry   = table.Element(0); // Нулевой элемент таблицы
            PType   second_type = tab_entry.Field(second_field).Type;
            long    llen        = index_cell.Root.Count();

            if (llen == 0)
            {
                return;
            }
            PaEntry ent    = index_cell.Root.Element(0);
            PType   tel    = ent.Type;
            int     size   = tel.HeadSize;
            long    offset = ent.offset;

            for (long ii = 0; ii < llen; ii++)
            {
                ent.offset = offset;
                offset    += size;
                long tab_offset = (long)ent.Get();
                tab_entry.offset = tab_offset;
                object prime_value = tab_entry.Field(i_field).Get();
                int    cmp;
                if (current_prime_value == null)
                {
                    current_prime_value = prime_value;
                    start = ii;
                }
                else if ((cmp = ((IComparable)prime_value).CompareTo(current_prime_value)) != 0 || ii == llen - 1) // Смена значения первичного ключа
                {
                    long number = ii - start + (cmp == 0 && ii == llen - 1? 1 : 0);
                    if (number > 1)
                    { // нужно сделать сортировку по вторичному ключу
                        index_cell.Root.SortByKey <string>(start, number, (object v) =>
                        {
                            tab_entry.offset = (long)v;
                            return((string)tab_entry.Field(second_field).Get());
                        }, null); // Дефолтный компаратор???
                    }
                    current_prime_value = prime_value;
                    start = ii;
                }
            }
        }
Example #3
0
        public IEnumerable <Literal> GetDataBySubjPred1(int subj, int pred)
        {
            if (dtriples.Root.Count() == 0)
            {
                return(Enumerable.Empty <Literal>());
            }
            PaEntry dtriple_entry = dtriples.Root.Element(0);

            return(dtriples_sp.Root.BinarySearchAll(ent =>
            {
                int su = (int)ent.Field(0).Get();
                int cmp = su.CompareTo(subj);
                if (cmp != 0)
                {
                    return cmp;
                }
                int pr = (int)ent.Field(1).Get();
                return pr.CompareTo(pred);
            })
                   .Select(en =>
            {
                dtriple_entry.offset = (long)en.Field(2).Get();
                return ToLiteral((object[])dtriple_entry.Field(2).Get());
            }));
        }
Example #4
0
        // =============== Частные случаи =================

        public PValue GetById(string id)
        {
            if (table.Count() == 0)
            {
                return(new PValue(null, Int64.MinValue, null));
            }
            PaEntry entry       = table.Element(0);
            PaEntry index_entry = index_cell.Root.BinarySearchFirst(ent =>
            {
                long off     = (long)ent.Get();
                entry.offset = off;
                return(0 - id.CompareTo((string)entry.Field(i_field).Get()));
            });

            if (index_entry.offset == Int64.MinValue)
            {
                return(new PValue(null, Int64.MinValue, null));
            }
            long cell_offset = (long)index_entry.Get();

            entry.offset = cell_offset;
            var rec = entry.GetValue();

            return(rec);
        }
Example #5
0
        public void MakeIndexed()
        {
            // Подготовим индексы для заполнения
            n_index.Close();
            c_index.Close();
            n_index = new PaCell(tp_ind, niCell, false);
            n_index.Clear();
            n_index.Fill(new object[0]);
            c_index = new PaCell(tp_ind, ciCell, false);
            c_index.Clear();
            c_index.Fill(new object[0]);
            foreach (PaEntry ent in nc_cell.Root.Elements())
            {
                long off = ent.offset;
                n_index.Root.AppendElement(off);
                c_index.Root.AppendElement(off);
            }
            n_index.Flush();
            c_index.Flush();

            // Индекс n_index отсортирован по построению. Надо сортировать c_index
            PaEntry nc_entry = nc_cell.Root.Element(0);

            c_index.Root.SortByKey(obj =>
            {
                nc_entry.offset = (long)obj;
                return(nc_entry.Field(0).Get());
            });
            Open(true);
        }
Example #6
0
        public IEnumerable <Literal> GetDataBySubjPred3(int subj, int pred)
        {
            if (dtriples_sp.IsEmpty)
            {
                return(Enumerable.Empty <Literal>());
            }
            var itemEntriry = ewtHash.GetEntity(subj);

            if (itemEntriry.IsEmpty)
            {
                return(Enumerable.Empty <Literal>());
            }
            var diapason = (object[])itemEntriry.Field(3).Get();

            if (dtriples_sp.Root.Count() == 0)
            {
                return(Enumerable.Empty <Literal>());
            }
            PaEntry dtriple_entry = dtriples.Root.Element(0);
            long    num           = (long)diapason[1];

            var query = dtriples_sp.Root.Elements((long)diapason[0], num)
                        .Select <PaEntry, object[]>(ent => (object[])ent.Get())
                        .Where(ve => (int)ve[1] == pred)
                        .Select(ve =>
            {
                dtriple_entry.offset = (long)ve[2];
                return(ToLiteral((object[])dtriple_entry.Field(2).Get()));
            });

            return(query);
        }
Example #7
0
        public IEnumerable <Literal> GetDataBySubjPred2(int subj, int pred)
        {
            if (dtriples_sp.IsEmpty)
            {
                return(Enumerable.Empty <Literal>());
            }
            var itemEntriry = ewt.EWTable.Root.BinarySearchFirst(ent =>
            {
                var rec = (object[])ent.Get();
                int ob  = (int)rec[0];
                int cmp = ob.CompareTo(subj);
                return(cmp);
            });

            if (itemEntriry.IsEmpty)
            {
                return(Enumerable.Empty <Literal>());
            }
            var diapason = (object[])itemEntriry.Field(3).Get();

            if (dtriples_sp.Root.Count() == 0)
            {
                return(Enumerable.Empty <Literal>());
            }
            PaEntry dtriple_entry = dtriples.Root.Element(0);

            return(dtriples_sp.Root.Elements((long)diapason[0], (long)diapason[1])
                   .Where(entry => pred == (int)((object[])entry.Get())[1])
                   .Select(en =>
            {
                dtriple_entry.offset = (long)en.Field(2).Get();
                return ToLiteral((object[])dtriple_entry.Field(2).Get());
            }));
        }
Example #8
0
        public IEnumerable <Literal> GetDataBySubjPred(int subj, int pred)
        {
            if (subj == Int32.MinValue)
            {
                return(new Literal[0]);
            }
            Literal[] res;
            var       key = new KeyValuePair <int, int>(subj, pred);

            if (spDCache.TryGetValue(key, out res))
            {
                return(res);
            }

            object[] diapason      = GetDiapasonFromHash(subj, pred, 2);
            PaEntry  dtriple_entry = dtriples.Root.Element(0);

            res = diapason == null
                ? new Literal[0]
                : dtriples_sp.Root.Elements((long)diapason[0], (long)diapason[1])
                  .Where(entry => pred == (int)((object[])entry.Get())[1])
                  .Select(en =>
            {
                dtriple_entry.offset = (long)en.Field(2).Get();
                return(ToLiteral((object[])dtriple_entry.Field(2).Get()));
            }).ToArray();
            spDCache.Add(key, res);
            return(res);
        }
Example #9
0
        internal IEnumerable <KeyValuePair <Literal, int> > GetDataBySubj(int subj)
        {
            if (subj == Int32.MinValue)
            {
                return(new KeyValuePair <Literal, int> [0]);
            }
            IEnumerable <KeyValuePair <Literal, int> > res;

            if (sPDCache.TryGetValue(subj, out res))
            {
                return(res);
            }
            object[] diapason      = GetDiapasonFromHash(subj, 0, 2);
            PaEntry  dtriple_entry = dtriples.Root.Element(0);

            res = diapason != null
                ? dtriples_sp.Root.Elements((long)diapason[0], (long)diapason[1])
                  //.Where(entry => pred == (int)((object[])entry.Get())[1])
                  .Select(en =>
            {
                var row              = ((object[])en.Get());
                var pred             = (int)row[1];
                dtriple_entry.offset = (long)row[2];
                Literal lit          = ToLiteral((object[])dtriple_entry.Field(2).Get());
                return(new KeyValuePair <Literal, int>(lit, pred));
            }).ToArray()
                : new KeyValuePair <Literal, int> [0];

            sPDCache.Add(subj, res);
            return(res);
        }
Example #10
0
 public NameTableInt(string path)
 {
     this.path    = path;
     this.nc_cell = new PaCell(new PTypeSequence(new PTypeRecord(
                                                     new NamedType("code", new PType(PTypeEnumeration.integer)),
                                                     new NamedType("name", new PType(PTypeEnumeration.sstring))
                                                     )),
                               path + "_nc.pac", false);
     if (nc_cell.IsEmpty)
     {
         nc_cell.Fill(new object[0]);
     }
     //PaEntry nc_entry =
     this.n_index = new DynaIndex <string>(path, "_n", off =>
     {
         PaEntry ent = nc_cell.Root.Element(0); // это возможно только если есть элемент, надо проверить
         ent.offset  = (long)off;
         return((string)ent.Field(1).Get());
     });
     this.c_index = new DynaIndex <int>(path, "_c", off =>
     {
         PaEntry ent = nc_cell.Root.Element(0); // это возможно только если есть элемент, надо проверить
         ent.offset  = (long)off;
         return((int)ent.Field(0).Get());
     });
     // Кодирование будет - номер записи, в которую попал идентификатор
     next_code = (int)n_index.Count();
     // Количество записей должно совпадать
     if (n_index.Count() != c_index.Count())
     {
         throw new Exception("Assert error: 9434");
     }
 }
Example #11
0
        public IEnumerable <KeyValuePair <Literal, string> > GetDataBySubj(string subj)
        {
            if (subj == "")
            {
                return(Enumerable.Empty <KeyValuePair <Literal, string> >());
            }
            IEnumerable <KeyValuePair <Literal, string> > res;

            if (sPDCache.TryGetValue(subj, out res))
            {
                return(res);
            }

            object[] diapason      = GetDiapasonFromHash(subj, "", 2);
            PaEntry  dtriple_entry = dtriples.Root.Element(0);

            res = diapason == null
                ? new KeyValuePair <Literal, string> [0]
                : dtriples_sp.Root.Elements((long)diapason[0], (long)diapason[1])
                  //.Where(entry => pred == (string)((object[])entry.Get())[1])
                  .Select(en =>
            {
                var row              = ((object[])en.Get());
                var pred             = (string)row[1];
                dtriple_entry.offset = (long)row[2];
                Literal lit          = ToLiteral((object[])dtriple_entry.Field(2).Get());
                return(new KeyValuePair <Literal, string>(lit, pred));
            }).ToArray();
            sPDCache.Add(subj, res);
            return(res);
        }
Example #12
0
        public void Load(Func <PaEntry, object[][]> genTriples)
        {
            intern_cell.Clear();
            intern_cell.Fill(new object[0]);
            foreach (PaEntry rec in table.Elements())
            {
                object[][] triples = genTriples(rec);
                foreach (object[] v3 in triples)
                {
                    intern_cell.Root.AppendElement(new object[] { false, v3[0], v3[1], v3[2], v3[3] });
                }
            }
            intern_cell.Flush();
            key_index.Load();
            key_index.AdditionalIndex(3); // дополнительный индекс - предикат

            //Проставим начала групп обратных ссылок и длину групп
            long    inv_beg = Int64.MinValue, inv_count = Int64.MinValue; // offset начала серии в intern_cell и длина серии
            PaEntry tab_el_ent = table.Element(0);
            long    key_offset = Int64.MaxValue;                          // будем фиксировать серию по совпадению оффсета с полем цели

            foreach (PaEntry ent in key_index.GetAll())
            {
                object[] v5      = (object[])ent.Get();
                long     tab_pnt = (long)v5[4]; // указатель на таблицу
                if (tab_pnt != key_offset)
                {                               // заканчивается серия, начинается новая серия
                    if (key_offset != Int64.MaxValue)
                    {                           // надо закончить предыдущую серию
                        tab_el_ent.offset = key_offset;
                        tab_el_ent.Field(5).Set(inv_beg);
                        tab_el_ent.Field(6).Set(inv_count);
                    }
                    // установим новую серию
                    key_offset = tab_pnt;
                    inv_beg    = ent.offset;
                    inv_count  = 0;
                }
                inv_count++;
            }
            // закончим последнюю серию
            tab_el_ent.offset = key_offset;
            tab_el_ent.Field(5).Set(inv_beg);
            tab_el_ent.Field(6).Set(inv_count);
        }
Example #13
0
        public int GetCode(string name)
        {
            if (string.IsNullOrEmpty(name) || n_index.Root.Count() == 0)
            {
                return(Int32.MinValue);
            }
            PaEntry nc_entry = nc_cell.Root.Element(0);
            var     qu       = n_index.Root.BinarySearchFirst(ent =>
            {
                nc_entry.offset = (long)ent.Get();
                return(((string)nc_entry.Field(1).Get()).CompareTo(name));
            });

            if (qu.IsEmpty)
            {
                return(Int32.MinValue);
            }
            nc_entry.offset = (long)qu.Get();
            return((int)nc_entry.Field(0).Get());
        }
Example #14
0
        public string GetName(int code)
        {
            if (code < 0 || code >= c_index.Root.Count())
            {
                return(null);
            }
            PaEntry nc_entry = nc_cell.Root.Element(0);
            var     qu       = c_index.Root.BinarySearchFirst(ent =>
            {
                nc_entry.offset = (long)ent.Get();
                return(((int)nc_entry.Field(0).Get()).CompareTo(code));
            });

            if (qu.IsEmpty)
            {
                return(null);
            }
            nc_entry.offset = (long)qu.Get();
            return((string)nc_entry.Field(1).Get());
        }
        protected override void AddInIndex(PaEntry entry)
        {
            int node;
            if (entry.Type.Vid == PTypeEnumeration.integer)
            {
                node = (int) entry.Get();
                if (!searchIndex.ContainsKey(node))
                    searchIndex.Add(node, entry.offset);
            }
            else
            {
                node = (int) entry.Field(0).Get();

                if (!searchIndex.ContainsKey(node))
                    searchIndex.Add(node, entry.offset);
                foreach (var chiledEntry in entry.Field(1).Elements())
                {
                    AddInIndex(chiledEntry);
                }
            }
        }
Example #16
0
        ////Здесь надо менять схему использования GroupedEntities
        //private object[] GetDiapasonFromHash0(int key, int pred, int direction)
        //{
        //    object[] row = null;
        //    if (!geHash.TryGetValue(key, out row)) return null;//new object[] { 0L, 0L };
        //    object[] predList = (object[])((object[])row[1 + direction])[1];
        //    object[] diap = null;
        //    foreach (object[] preddiap in predList)
        //    {
        //        int pre = (int)preddiap[0];
        //        if (pre == pred)
        //        {
        //            diap = (object[])preddiap[1];
        //            break;
        //        }
        //    }
        //    if (diap == null) return null;//new object[] { 0L, 0L };
        //    return diap;
        //}


        public virtual IEnumerable <Literal> GetDataBySubjPred(int subj, int pred)
        {
            Literal[] res;
            var       key = new KeyValuePair <int, int>(subj, pred);

            if (spDCache.TryGetValue(key, out res))
            {
                return(res);
            }

            object[] diapason = GetDiapasonFromHash(subj, pred, 2);
            if (diapason == null)
            {
                return(Enumerable.Empty <Literal>());
            }

            PaEntry dtriple_entry = dtriples.Root.Element(0);

            res = dtriples_sp.Root.Elements((long)diapason[0], (long)diapason[1])
                  .Where(entry => pred == (int)((object[])entry.Get())[1])
                  .Select(en =>
            {
                dtriple_entry.offset = (long)en.Field(2).Get();
                object[] uni         = (object[])dtriple_entry.Field(2).Get();
                Literal lit          = new Literal();
                int vid = (int)uni[0];
                if (vid == 1)
                {
                    lit.vid   = LiteralVidEnumeration.integer;
                    lit.value = (int)uni[1];
                }
                if (vid == 3)
                {
                    lit.vid   = LiteralVidEnumeration.date;
                    lit.value = (long)uni[1];
                }
                else if (vid == 2)
                {
                    lit.vid      = LiteralVidEnumeration.text;
                    object[] txt = (object[])uni[1];
                    lit.value    = new Text()
                    {
                        s = (string)txt[0], l = (string)txt[1]
                    };
                }
                return(lit);
            }).ToArray();
            spDCache.Add(key, res);
            return(res);
        }
Example #17
0
        public IEnumerable <Literal> GetDatInDiap(Diapason di, int pred)
        {
            Diapason internal_diap = dtriples_sp.Root.BinarySearchDiapason(di.start, di.numb, ent => ((int)ent.Field(1).Get()).CompareTo(pred));
            PaEntry  dtriple_entry = dtriples.Root.Element(0);

            return(dtriples_sp.Root.Elements(internal_diap.start, internal_diap.numb)
                   .Select(ent => (object[])ent.Get())
                   .Select(p_obj =>
            {
                long off = (long)((object[])p_obj)[2];
                dtriple_entry.offset = off;
                return PObjToLiteral(dtriple_entry.Field(2).Get());
            })
                   );
        }
Example #18
0
        public string GetName(int code)
        {
            long off = c_index.GetFirst(code);

            if (off != Int64.MinValue)
            { // Есть такой
                PaEntry ent = nc_cell.Root.Element(0);
                ent.offset = off;
                return((string)ent.Field(1).Get());
            }
            else
            {
                return(null);
            }
        }
Example #19
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append('{');
            sb.Append('[');
            if (IndexArray.IndexCell.Root.Count() > 0)
            {
                object  one   = IndexArray.IndexCell.Root.Element(0).Get();
                PaEntry entry = Table.Element(0);
                foreach (object v in IndexArray.IndexCell.Root.ElementValues())
                {
                    long offset;
                    if (one is long)
                    {
                        offset = (long)v;
                    }
                    else
                    {
                        offset = (long)((object[])v)[1];
                    }
                    entry.offset = offset;
                    sb.Append((string)entry.Field(1).Field(1).Get());
                    sb.Append(' ');
                }
            }
            sb.Append(']');
            // динамическая часть
            sb.Append('[');
            if (_unique)
            {
                foreach (PaEntry ent in keyent.Select(pair => pair.Value))
                {
                    sb.Append((string)ent.Field(1).Field(1).Get());
                    sb.Append(' ');
                }
            }
            else
            {
                sb.Append("NOT IMPLEMENTED");
            }
            sb.Append(']');
            sb.Append('}');
            return(sb.ToString());
        }
Example #20
0
        public IEnumerable <Literal> GetDataBySubjPred4(int subj, int pred)
        {
            object[] diapason = GetDiapasonFromHash(subj, pred, 2);
            if (diapason == null)
            {
                return(Enumerable.Empty <Literal>());
            }

            PaEntry dtriple_entry = dtriples.Root.Element(0);

            return(dtriples_sp.Root.Elements((long)diapason[0], (long)diapason[1])
                   //.Where(entry => pred == (int)((object[])entry.Get())[1])
                   .Select(en =>
            {
                dtriple_entry.offset = (long)en.Field(2).Get();
                return ToLiteral((object[])dtriple_entry.Field(2).Get());
            }));
        }
Example #21
0
 public IEnumerable <PaEntry> SearchAll(string ss)
 {
     if (table.Count() > 0)
     {
         ss = ss.ToLower();
         PaEntry  entry = table.Element(0);
         Diapason dia   = index_cell.Root.BinarySearchDiapason((PaEntry ent) =>
         {
             long off     = (long)ent.Get();
             entry.offset = off;
             string name  = ((string)entry.Field(i_field).Get()).ToLower();
             if (name.StartsWith(ss))
             {
                 return(0);
             }
             return(name.CompareTo(ss));
         });
         var query = index_cell.Root.Elements(dia.start, dia.numb)
                     .Select(ent =>
         {
             long off     = (long)ent.Get();
             entry.offset = off;
             return(entry);
         });
         //var query = index_cell.Root.BinarySearchAll((PaEntry ent) =>
         //{
         //    long off = (long)ent.Get();
         //    entry.offset = off;
         //    string name = ((string)entry.Field(i_field).Get()).ToLower();
         //    if (name.StartsWith(ss)) return 0;
         //    return name.CompareTo(ss);
         //}).Select(ent =>
         //{
         //    long off = (long)ent.Get();
         //    entry.offset = off;
         //    return entry;
         //});
         return(query);
     }
     else
     {
         return(Enumerable.Empty <PaEntry>());
     }
 }
Example #22
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append('[');
            if (Table.Count() > 0)
            {
                PaEntry entry = Table.Element(0);
                index_cell.Root.Scan((off, ob) =>
                {
                    entry.offset = (long)ob;
                    int c        = (int)((object[])entry.Field(1).Get())[0];
                    sb.Append(c); sb.Append(' ');
                    return(true);
                });
            }
            sb.Append(']');
            return(sb.ToString());
        }
Example #23
0
        public PaEntry GetFirst(Func <PaEntry, int> elementDepth)
        {
            if (table.Count() == 0)
            {
                return(new PaEntry(null, Int64.MinValue, null));
            }
            PaEntry entry          = table.Element(0);
            PaEntry entry_in_index = index_cell.Root.BinarySearchFirst(ent =>
            {
                long off     = (long)ent.Get();
                entry.offset = off;
                return(elementDepth(entry.Field(i_field)));
            });

            if (entry_in_index.offset == Int64.MinValue)
            {
                return(entry_in_index);                                         // не найден
            }
            entry.offset = (long)entry_in_index.Get();
            return(entry);
        }
Example #24
0
        public int SetNameGetCode(string name)
        {
            long off = n_index.GetFirst(name);

            if (off != Int64.MinValue)
            { // Есть такой
                PaEntry ent = nc_cell.Root.Element(0);
                ent.offset = off;
                return((int)ent.Field(0).Get());
            }
            else
            {
                int code = next_code;
                next_code++;
                var r_off = nc_cell.Root.AppendElement(new object[] { code, name });
                //nc_cell.Flush();
                n_index.Add(name, r_off);
                c_index.Add(code, r_off);
                return(code);
            }
        }
Example #25
0
 public IEnumerable <PaEntry> GetAll(Func <PaEntry, int> elementDepth)
 {
     if (table.Count() > 0)
     {
         PaEntry  entry = table.Element(0);
         Diapason dia   = index_cell.Root.BinarySearchDiapason((PaEntry ent) =>
         {
             long off     = (long)ent.Get();
             entry.offset = off;
             return(elementDepth(entry.Field(i_field)));
         });
         var query = index_cell.Root.Elements(dia.start, dia.numb)
                     .Select(ent =>
         {
             entry.offset = (long)ent.Get();
             return(entry);
         });
         return(query);
     }
     else
     {
         return(Enumerable.Empty <PaEntry>());
     }
 }
Example #26
0
 public void DeleteEntry(PaEntry record)
 {
     record.Field(0).Set(true);
 }