コード例 #1
0
        public void MergeUpByKey <Tkey>(long start, long number1, long number2, Func <object, Tkey> keyfunction,
                                        IComparer <Tkey> comparer)
        {
            if (number1 == 0 || number2 == 0)
            {
                return;
            }
            PTypeSequence pts = (PTypeSequence)tp;
            PType         tel = pts.ElementType;


            PaEntry entry  = this.Element(start);           // Вход в начальный элемент
            long    off1   = entry.offset;
            PaEntry entry2 = this.Element(start + number1); // Вход в начальный элемент второй группы
            long    off2   = entry2.offset;

            IComparer <Tkey> compar = comparer;

            if (compar == null)
            {
                compar = Comparer <Tkey> .Default;
            }
            Func <object, object, int> comparePObj = (object o1, object o2) =>
            {
                return(compar.Compare(keyfunction(o1), keyfunction(o2)));
            };

            this.cell.CombineParts(tel, off1, number1, off2, number2, comparePObj);
        }
コード例 #2
0
ファイル: PxEntry.cs プロジェクト: agmarchuk/PolarDB
        public void Sort(KeyString keyfunction)
        {
            if (typ.Vid != PTypeEnumeration.sequence)
            {
                throw new Exception("Sort can't be implemented to this vid");
            }
            PTypeSequence pts  = (PTypeSequence)typ;
            long          llen = this.Count();

            if (llen < 2)
            {
                return;           // сортировать не нужно
            }
            // Указатель на нулевой элемент и размер головы записи
            long p0   = this.Element(0).offset;
            int  size = pts.ElementType.HeadSize;

            // организуем массивы ключей и индексов - номеров записей
            string[] keys    = new string[llen];
            int[]    indexes = Enumerable.Range(0, (int)llen).ToArray();
            // Вычислим и запишем ключи
            int i = 0;

            foreach (PxEntry e in this.Elements())
            {
                keys[i] = keyfunction(e);
                i++;
            }
            // Сортируем два массива
            Array.Sort(keys, indexes);
            ReorderSequenceArrayHeads(llen, p0, size, indexes);
        }
コード例 #3
0
        public void InsertAtomTest()
        {
            var seqtriplets = new PTypeSequence(new PType(PTypeEnumeration.integer));

            var testdb = new object[] { 1, 2, 3, 5, 15, 19, 21 };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");

            hCell.Fill(testdb);
            try
            {
                Assert.AreEqual(7, hCell.Root.Count());
                Assert.AreEqual(3, (int)hCell.Root.Element(2).Get().Value);
                Assert.AreEqual(5, (int)hCell.Root.Element(3).Get().Value);
                Assert.AreEqual(21, (int)hCell.Root.Element(6).Get().Value);

                hCell.InsertElement(0, 12);
                Assert.AreEqual(8, hCell.Root.Count());
                Assert.AreEqual(12, (int)hCell.Root.Element(0).Get().Value);
                Assert.AreEqual(3, (int)hCell.Root.Element(3).Get().Value);
                Assert.AreEqual(21, (int)hCell.Root.Element(7).Get().Value);

            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #4
0
        // Использовать надо следующим образом:

        public static void Test()
        {
            PType objectTriplets = new PTypeSequence(new PTypeRecord(
                                                         new NamedType("deleted", new PType(PTypeEnumeration.boolean)),
                                                         new NamedType("s", new PType(PTypeEnumeration.sstring)),
                                                         new NamedType("p", new PType(PTypeEnumeration.sstring)),
                                                         new NamedType("o", new PType(PTypeEnumeration.sstring))
                                                         ));
            // инициализация таблицы
            var directCell = new PaCell(objectTriplets, "path", false);

            // Более компактный способ заполнения ячейки
            directCell.Clear();
            directCell.Fill(new object[0]);
            //foreach (var element in elements) // Закомментарил из-за отсутствия перечислителя elements
            {
                directCell.Root.AppendElement(new object[] { false, "subject", "predicate", "object" });
            }
            directCell.Flush();
            // Создание индекса
            FixedIndex <SubjPred> sp_index = new FixedIndex <SubjPred>("..sp", directCell.Root, entry =>
            {
                return(new SubjPred()
                {
                    subj = (string)entry.Field(1).Get(), pred = (string)entry.Field(1).Get()
                });
            });
        }
コード例 #5
0
        internal KVSequencePortion(DataNode dn,
                                   long offset_in_mainstream_for_seq, long offset_for_keys, long offset_for_offsets, long offset_for_dic1, long offset_for_dic2, PType tp_element)
        {
            this.dn                 = dn;
            this.offset_for_seq     = offset_in_mainstream_for_seq;
            this.offset_for_keys    = offset_for_keys;
            this.offset_for_offsets = offset_for_offsets;
            this.offset_for_dic1    = offset_for_dic1;
            this.offset_for_dic2    = offset_for_dic2;
            this.tp_element         = tp_element;
            PType tp_sequ = new PTypeSequence(new PTypeRecord(
                                                  new NamedType("key", new PType(PTypeEnumeration.integer)),
                                                  new NamedType("value", tp_element)));
            // Создадим базу данных, состоящую из последовательности и двух индексных массивов: массива ключей и массива офсетов
            PagedStream stream1 = new PagedStream(dn.fob, dn.main_stream, offset_in_mainstream_for_seq);

            keyvalue_seq = new PaCell(tp_sequ, stream1, false);
            PagedStream stream2 = new PagedStream(dn.fob, dn.main_stream, offset_for_keys);

            keys = new PaCell(new PTypeSequence(new PType(PTypeEnumeration.integer)), stream2, false);
            //keys_arr = keys.IsEmpty ? new int[0] : keys.Root.ElementValues().Cast<int>().ToArray();
            PagedStream stream3 = new PagedStream(dn.fob, dn.main_stream, offset_for_offsets);

            offsets = new PaCell(new PTypeSequence(new PType(PTypeEnumeration.longinteger)), stream3, false);
        }
コード例 #6
0
ファイル: PhSortTest.cs プロジェクト: jurik/PolarDBTest
        public void SortAtomTest()
        {
            var seqtriplets = new PTypeSequence(new PType(PTypeEnumeration.integer));

            var testdb = new object[] { 15, 2, 31, 35, 11, 190, 21 };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");

            hCell.Fill(testdb);
            try
            {
                Assert.AreEqual(7, hCell.Root.Count());
                Assert.AreEqual(31, (int)hCell.Root.Element(2).Get().Value);
                Assert.AreEqual(35, (int)hCell.Root.Element(3).Get().Value);
                Assert.AreEqual(21, (int)hCell.Root.Element(6).Get().Value);

                hCell.Sort(e =>
                {
                    return (int)e.Get().Value;
                });
                Assert.AreEqual(7, hCell.Root.Count());
                Assert.AreEqual(2, (int)hCell.Root.Element(0).Get().Value);
                Assert.AreEqual(21, (int)hCell.Root.Element(3).Get().Value);
                Assert.AreEqual(190, (int)hCell.Root.Element(6).Get().Value);

            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #7
0
ファイル: Main301.cs プロジェクト: agmarchuk/PolarDB
        public static void Main301()
        {
            Console.WriteLine("Start Main301");

            // ============ Типы структур и значения с труктур в объектном представлении ===========
            // Создадим типы записи и последовательности записей
            PType tp1 = new PTypeRecord(
                new NamedType("name", new PType(PTypeEnumeration.sstring)),
                new NamedType("age", new PType(PTypeEnumeration.integer)));
            PType tp2 = new PTypeSequence(tp1);

            // Создадим структурные значения этих типов в объектном представлении
            object val1 = new object[] { "Иванов", 22 };
            object val2 = new object[]
            {
                new object[] { "Иванов", 22 },
                new object[] { "Петров", 33 },
                new object[] { "Сидоров", 44 }
            };

            // Визуализация структур в объектном представлении
            Console.WriteLine(tp1.Interpret(val1));
            Console.WriteLine(tp2.Interpret(val2));

            // ============== Сериализация/Десериализация =============
            // Сериализация выполняет отображение структуры на поток символов (текстовая сериализация) или
            // поток байтов (бинарная сериализация). Десериализация выполняет обратное преобразование.
            Stream stream = new MemoryStream();
            // сериализация делается через текстовый райтер
            TextWriter tw = new StreamWriter(stream);

            TextFlow.Serialize(tw, val2, tp2);
            tw.Flush();
            // посмотрим что записалось
            stream.Position = 0L;
            TextReader tr  = new StreamReader(stream);
            string     sss = tr.ReadToEnd();

            Console.WriteLine("Накопилось в стриме: " + sss);

            // десериализаця делатеся через текстовый ридер
            stream.Position = 0L;
            object val = TextFlow.Deserialize(tr, tp2);

            // Теперь надо посмотреть что в объекте
            Console.WriteLine("После цикла сериализация/десериализация: " + tp2.Interpret(val));

            // Бинарная сериализация
            // bool - 1 байт
            // byte - 1 байт
            // int - 4 байта
            // long, double - 8 байтов
            // строка - набор байтов определяемый BinaryWriter.Write((string)s)
            // запись - подряд стоящие сериализации полей записи
            // последовательность - long длина последовательности, подряд стоящие развертки элементов
            //
            // Бинарная сериализация совместима с BinaryWriter и BinaryReader

            // выполняется точно также, как текстовая сериализация (пример сделаю позже)
        }
コード例 #8
0
        public void Fill2VolumedSeqRecordWithRestTest()
        {
            var seqtriplets = new PTypeSequence(new PType(PTypeEnumeration.integer));
            var testdb = new object[] { 1, 2, 3, 5, 15, 19, 21, 90, 100, 13 };

            string testpacfilename = "test.pxc";
            PxCell xCell = new PxCell(seqtriplets, testpacfilename, false);
            xCell.Fill2(testdb);

            try
            {
                Assert.AreEqual(10, xCell.Root.Count());
                Assert.AreEqual(1, (int)xCell.Root.Element(0).Get().Value);
                Assert.AreEqual(3, (int)xCell.Root.Element(2).Get().Value);
                Assert.AreEqual(5, (int)xCell.Root.Element(3).Get().Value);
                Assert.AreEqual(15, (int)xCell.Root.Element(4).Get().Value);
                Assert.AreEqual(19, (int)xCell.Root.Element(5).Get().Value);
                Assert.AreEqual(90, (int)xCell.Root.Element(7).Get().Value);
                Assert.AreEqual(13, (int)xCell.Root.Element(9).Get().Value);

                var objects = xCell.Root.Elements().Select(e => e.Get().Value).ToArray();
                Assert.AreEqual(1, (int)xCell.Root.Elements().ToArray()[0].Get().Value);
                Assert.AreEqual(19, (int)xCell.Root.Elements().ToArray()[5].Get().Value);
                Assert.AreEqual(90, (int)xCell.Root.Elements().ToArray()[7].Get().Value);
                Assert.AreEqual(100, (int)xCell.Root.Elements().ToArray()[8].Get().Value);

            }
            finally
            {
                xCell.Close();
            }
        }
コード例 #9
0
        // Основной сканер: быстро пробегаем по элементам, обрабатываем пары (offset, pobject), возвращаем true
        public void Scan(Func <long, object, bool> handler)
        {
            if (tp.Vid != PTypeEnumeration.sequence)
            {
                throw new Exception("Err in TPath formula: ElementValues() can't be applyed to structure of vid " + tp.Vid);
            }
            PTypeSequence mts = (PTypeSequence)tp;
            PType         t   = mts.ElementType;
            long          ll  = this.Count();

            if (ll == 0)
            {
                return;
            }
            PaEntry first = this.Element(0);
            long    off   = first.offset;

            for (long ii = 0; ii < ll; ii++)
            {
                long   offout;
                object pobject = cell.GetPObject(t, off, out offout);
                bool   ok      = handler(off, pobject);
                off = offout;
                if (!ok)
                {
                    throw new Exception("Scan handler catched 'false' at element " + ii);
                }
            }
        }
コード例 #10
0
ファイル: PxEntry.cs プロジェクト: agmarchuk/PolarDB
        public PxEntry BinarySearchFirst(Func <PxEntry, int> elementDepth)
        {
            PxEntry sequ = this;
            var     typ  = sequ.typ;

            if (typ.Vid != PTypeEnumeration.sequence)
            {
                throw new Exception("Function FindZero can't be applied to the type with vid=" + typ.Vid);
            }
            PTypeSequence mts  = (PTypeSequence)sequ.typ;
            PType         tel  = mts.ElementType;
            long          llen = sequ.Count();

            if (llen == 0)
            {
                throw new Exception("No elements to FindZero");
            }
            var first_el    = sequ.Element(0);
            var first_depth = elementDepth(first_el);

            if (first_depth == 0)
            {
                return(first_el);
            }
            PxEntry found = BSF(first_el, llen, elementDepth);

            //if (found.offset == long.MinValue) throw new Exception("Zero element did't foound by FindZero()");
            return(found);
        }
コード例 #11
0
        public void UpdateAtomsTest()
        {
            var seqtriplets = new PTypeSequence(new PType(PTypeEnumeration.integer));

            var testdb = new object[] { 1, 4, 6, 8, 13, 88};

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");
            try
            {
                hCell.Fill(testdb);
                Assert.AreEqual(6, hCell.Root.Count());
                var e = (int)hCell.Root.Element(2).Get().Value;
                Assert.AreEqual(e, 6);
                Assert.AreEqual((int)hCell.Root.Element(0).Get().Value, 1);
                Assert.AreEqual((int)hCell.Root.Element(2).Get().Value, 6);
                Assert.AreEqual((int)hCell.Root.Element(5).Get().Value, 88);

                var entry = (PhEntry)hCell.Root.Element(2);
                entry.Set(987);
                Assert.AreEqual((int)hCell.Root.Element(2).Get().Value, 987);
            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #12
0
ファイル: PxEntry.cs プロジェクト: agmarchuk/PolarDB
        private void Sort(long start, long number, KeyString keyfunction)
        {
            PTypeSequence pts = (PTypeSequence)typ;

            if (number < 2)
            {
                return;             // сортировать не нужно
            }
            if (number < 10000000)
            {
                // Указатель на начальный элемент и размер головы записи
                PxEntry e    = this.Element(start);
                long    p0   = e.offset;
                int     size = pts.ElementType.HeadSize;
                // организуем массивы ключей и индексов - номеров записей
                string[] keys    = new string[number];
                int[]    indexes = Enumerable.Range(0, (int)number).ToArray();
                // Вычислим и запишем ключи

                for (long ii = 0; ii < number; ii++)
                {
                    keys[ii]  = keyfunction(e);
                    e.offset += size;
                }
                // Сортируем два массива
                Array.Sort(keys, indexes);
                ReorderSequenceArrayHeads(number, p0, size, indexes);
            }
            else
            {
            }
        }
コード例 #13
0
        public void FillRecordsTest()
        {
            var seqtriplets = new PTypeSequence(
                                        new PTypeRecord(
                                            new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                            new NamedType("predicate", new PType(PTypeEnumeration.sstring)),
                                            new NamedType("obj", new PType(PTypeEnumeration.sstring)))
                                );

            var testdb = new object[] {
                new object[] {"a", "b", "c"},
                new object[] {"a1", "b1", "c1"},
                new object[] {"da", "db", "dc"}
            };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");
            try
            {
                hCell.Fill(testdb);
                Assert.AreEqual(3, hCell.Root.Count());
                var e = (object[])hCell.Root.Element(2).Get().Value;
                Assert.AreEqual(e.Length, 3);
                Assert.AreEqual(e[0], "da");
                Assert.AreEqual(e[1], "db");
                Assert.AreEqual(e[2], "dc");

                Assert.AreEqual(hCell.Root.Element(2).Field(2).Get().Value, "dc");
            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #14
0
        // Голова построена, для хвоста место выдалено место уже выделено и оно "под головкой"
        internal void FillTail(object valu, PType typ, Queue <VTO> vtoList)
        {
            switch (typ.Vid)
            {
            case PTypeEnumeration.sstring:
            {
                string str = (string)valu;
                int    len = str.Length;
                bw.Write(Encoding.Unicode.GetBytes(str));
            }
            break;

            case PTypeEnumeration.sequence:
            {
                PTypeSequence mts = (PTypeSequence)typ;
                PType         tel = mts.ElementType;
                // Пишем массив последовательности
                long llen = ((object[])valu).Length;
                // Если строка, то специальная обработка
                object[] arr = (object[])valu;
                foreach (object va in arr)
                {
                    FillHead(va, tel, vtoList);
                }
            }
            break;

            default: throw new Exception("unexpected type in FillTail");
            }
        }
コード例 #15
0
        public PaEntry BinarySearchFirst(Func <PaEntry, int> elementDepth)
        {
            PaEntry sequ = this;
            var     typ  = sequ.Type;

            if (typ.Vid != PTypeEnumeration.sequence)
            {
                throw new Exception("Function BinarySearchFirst can't be applied to the type with vid=" + typ.Vid);
            }
            PTypeSequence mts = (PTypeSequence)sequ.Type;
            PType         tel = mts.ElementType;

            if (!tel.HasNoTail)
            {
                throw new Exception("Function BinarySearchFirst can't be applied to elements with vid=" + tel.Vid);
            }
            long llen = sequ.Count();

            if (llen == 0)
            {
                sequ.offset = Int64.MinValue; return(sequ);
            }
            var first_el    = sequ.Element(0);
            var first_depth = elementDepth(first_el);

            if (first_depth == 0)
            {
                return(first_el);
            }
            PaEntry found = BinarySearchFirst(first_el, llen, elementDepth);

            //if (found.offset == long.MinValue) throw new Exception("Zero element did't foound by FindZero()");
            return(found);
        }
コード例 #16
0
        public PaEntry Element(long index)
        {
            if (tp.Vid != PTypeEnumeration.sequence)
            {
                throw new Exception("Err in TPath formula: Element() can't be applyed to structure of vid " + tp.Vid);
            }
            PTypeSequence mts  = (PTypeSequence)tp;
            PType         t    = mts.ElementType;
            long          llen = this.Count();

            if (index < 0 || index >= llen)
            {
                throw new Exception("Err in TPath formula: wrong index of Element " + index);
            }
            // для внешних равноэлементных последовательностей - специальная формула
            if (t.HasNoTail || index == 0)
            {
                return(new PaEntry(t, this.offset + 8 + index * t.HeadSize, cell));
            }
            //cell.SetOffset(this.offset); //Убрал
            long pos = this.offset + 8;

            for (long ii = 0; ii < index; ii++)
            {
                pos = Skip(t, pos);
            }
            return(new PaEntry(t, pos, cell));
        }
コード例 #17
0
        internal object ScanObject(PType typ)
        {
            switch (typ.Vid)
            {
            case PTypeEnumeration.none: return(null);

            case PTypeEnumeration.boolean: return(br.ReadByte());

            case PTypeEnumeration.integer: return(br.ReadInt32());

            case PTypeEnumeration.longinteger: return(br.ReadInt64());

            case PTypeEnumeration.real: return(br.ReadDouble());

            case PTypeEnumeration.@byte: return(br.ReadByte());

            case PTypeEnumeration.sstring:
            {
                //int len = br.ReadInt32();
                //char[] chrs = br.ReadChars(len);
                //return new string(chrs);
                return(br.ReadString());
            }

            case PTypeEnumeration.record:
            {
                PTypeRecord r_tp   = (PTypeRecord)typ;
                object[]    fields = new object[r_tp.Fields.Length];
                for (int i = 0; i < r_tp.Fields.Length; i++)
                {
                    fields[i] = ScanObject(r_tp.Fields[i].Type);
                }
                return(fields);
            }

            case PTypeEnumeration.sequence:
            {
                PTypeSequence mts  = (PTypeSequence)typ;
                PType         tel  = mts.ElementType;
                long          llen = br.ReadInt64();
                object[]      els  = new object[llen];
                for (long ii = 0; ii < llen; ii++)
                {
                    els[ii] = ScanObject(tel);
                }
                return(els);
            }

            case PTypeEnumeration.union:
            {
                PTypeUnion mtu = (PTypeUnion)typ;
                int        v   = br.ReadByte();
                PType      mt  = mtu.Variants[v].Type;
                return(new object[] { v, ScanObject(mt) });
            }

            default: throw new Exception("Err in TPath ScanObject(): type is not implemented " + typ.Vid);
            }
        }
コード例 #18
0
ファイル: Task03.cs プロジェクト: agmarchuk/PolarDB
        //START_SOURCE_CODE
        public void Run()
        {
            Console.WriteLine("Start Task03_PolarDB");
            dbpath = System.IO.Path.GetTempPath();
            PType  tp   = new PType(PTypeEnumeration.sstring);
            PaCell cell = new PaCell(tp, dbpath + "test.pac", false);

            cell.Clear();
            cell.Fill("Привет из ячейки базы данных!");
            Console.WriteLine("Содержимое ячейки: {0}", cell.Root.Get());

            PType tp_rec = new PTypeRecord(
                new NamedType("имя", new PType(PTypeEnumeration.sstring)),
                new NamedType("возраст", new PType(PTypeEnumeration.integer)),
                new NamedType("мужчина", new PType(PTypeEnumeration.boolean)));
            object rec_value = new object[] { "Пупкин", 22, true };
            PaCell cell_rec  = new PaCell(tp_rec, dbpath + "test_rec.pac", false);

            cell_rec.Clear();
            cell_rec.Fill(rec_value);
            object from_rec = cell_rec.Root.Get();

            Console.WriteLine(tp_rec.Interpret(from_rec));

            PType  tp_seq    = new PTypeSequence(tp_rec);
            object seq_value = new object[]
            {
                new object[] { "Иванов", 24, true },
                new object[] { "Петрова", 18, false },
                new object[] { "Пупкин", 22, true }
            };
            PaCell cell_seq = new PaCell(tp_seq, dbpath + "test_seq.pac", false);

            cell_seq.Clear();
            cell_seq.Fill(seq_value);
            object from_seq = cell_seq.Root.Get();

            Console.WriteLine(tp_seq.Interpret(from_seq));

            cell_seq.Root.AppendElement(new object[] { "Сидоров", 23, true });
            Console.WriteLine(tp_seq.Interpret(cell_seq.Root.Get()));

            long v0 = cell_seq.Root.Count();
            var  v1 = cell_seq.Root.Element(2).Field(0).Get();
            var  v2 = cell_seq.Root.Element(3).Field(1).Get();

            Console.WriteLine($"{v0} {v1} {v2}");

            cell_seq.Root.Element(1).Field(1).Set(19);
            cell_seq.Root.Element(1).Field(2).Set(true);
            Console.WriteLine(tp_seq.Interpret(cell_seq.Root.Get()));
            cell_seq.Flush();
            cell_seq.Close();
            cell_rec.Flush();
            cell_rec.Close();
            cell.Flush();
            cell.Close();
        }
コード例 #19
0
        public IndexHalfkeyImmutable(string path_name)
        {
            PType tp_hkey  = new PType(PTypeEnumeration.integer);
            PType tp_index = new PTypeSequence(new PTypeRecord(
                                                   new NamedType("halfkey", tp_hkey),
                                                   new NamedType("offset", new PType(PTypeEnumeration.longinteger))));

            index_cell = new PaCell(tp_index, path_name + ".pac", false);
        }
コード例 #20
0
        public IndexHalfkeyImmutable(System.IO.Stream stream)
        {
            PType tp_hkey  = new PType(PTypeEnumeration.integer);
            PType tp_index = new PTypeSequence(new PTypeRecord(
                                                   new NamedType("halfkey", tp_hkey),
                                                   new NamedType("offset", new PType(PTypeEnumeration.longinteger))));

            index_cell = new PaCell(tp_index, stream, false);
        }
コード例 #21
0
        public SemiIndex(string indexName, PType field_type)
        {
            PType tp_index = new PTypeSequence(new PTypeRecord(
                                                   new NamedType("key", field_type),
                                                   new NamedType("value", new PType(PTypeEnumeration.longinteger)),
                                                   new NamedType("deleted", new PType(PTypeEnumeration.boolean))));

            acell = new PaCell(tp_index, indexName + ".pac", false);
            xcell = new PxCell(tp_index, indexName + ".pxc", false);
        }
コード例 #22
0
        public PIndex(string ind_path, PaCell table, int field_numb)
        {
            this.table      = table;
            this.field_numb = field_numb;
            PTypeSequence table_type = (PTypeSequence)table.Type;
            PType         field_type = ((PTypeRecord)table_type.ElementType).Fields[field_numb].Type;
            PType         tp_index   = new PTypeSequence(new PTypeRecord(
                                                             new NamedType("key", field_type),
                                                             new NamedType("value", new PType(PTypeEnumeration.longinteger))));

            icell = new PxCell(tp_index, ind_path + "index.pxc", false);
        }
コード例 #23
0
ファイル: UnitTestCells.cs プロジェクト: agmarchuk/PolarDB
        public void TestSeqCell()
        {
            PType  tp_seq   = new PTypeSequence(new PType(PTypeEnumeration.integer));
            PaCell cell_seq = new PaCell(tp_seq, new MemoryStream(), false);

            cell_seq.Clear();
            cell_seq.Fill(new object[0]);
            cell_seq.Root.AppendElement(99);
            cell_seq.Root.AppendElement(98);
            cell_seq.Root.AppendElement(97);
            Assert.IsTrue(tp_seq.Interpret(cell_seq.Root.Get()) == "[99,98,97]");
        }
コード例 #24
0
        public SITest(string path)
        {
            this.path = path;
            PType tp = new PTypeSequence(new PTypeRecord(
                                             new NamedType("id", new PType(PTypeEnumeration.sstring)),
                                             new NamedType("name", new PType(PTypeEnumeration.sstring)),
                                             new NamedType("fd", new PType(PTypeEnumeration.sstring)),
                                             new NamedType("deleted", new PType(PTypeEnumeration.boolean))));

            cell = new PaCell(tp, path + "twi.pac", false);
            si   = new SemiIndex(path + "ind_id", new PType(PTypeEnumeration.sstring));
            ni   = new SemiIndex(path + "ind_name", new PType(PTypeEnumeration.sstring));
        }
コード例 #25
0
 // Техническая процедура Пропускает поле, выдает адрес, следующий за ним. Указатель никуда не установлен
 private long Skip(PType tp, long off)
 {
     if (tp.HasNoTail)
     {
         return(off + tp.HeadSize);
     }
     if (tp.Vid == PTypeEnumeration.sstring)
     {
         long offout;
         cell.ReadString(off, out offout);
         return(offout);
     }
     if (tp.Vid == PTypeEnumeration.record)
     {
         long        field_offset = off;
         PTypeRecord mtr          = (PTypeRecord)tp;
         foreach (var pa in mtr.Fields)
         {
             field_offset = Skip(pa.Type, field_offset);
         }
         return(field_offset);
     }
     if (tp.Vid == PTypeEnumeration.sequence)
     {
         PTypeSequence mts  = (PTypeSequence)tp;
         PType         tel  = mts.ElementType;
         long          llen = cell.ReadLong(off);
         if (tel.HasNoTail)
         {
             return(off + 8 + llen * tel.HeadSize);
         }
         long element_offset = off + 8;
         for (long ii = 0; ii < llen; ii++)
         {
             element_offset = Skip(tel, element_offset);
         }
         return(element_offset);
     }
     if (tp.Vid == PTypeEnumeration.union)
     {
         PTypeUnion mtu = (PTypeUnion)tp;
         int        v   = cell.ReadByte(off);
         if (v < 0 || v >= mtu.Variants.Length)
         {
             throw new Exception("Err in Skip (TPath-formula): wrong variant for union " + v);
         }
         PType mt = mtu.Variants[v].Type;
         return(Skip(mt, off + 1));
     }
     throw new Exception("Assert err: 2874");
 }
コード例 #26
0
        public Database(string path)
        {
            this.path = path;
            PType tp_catalogue = new PTypeSequence(new PTypeRecord(
                                                       new NamedType("collectionname", new PType(PTypeEnumeration.sstring)),
                                                       new NamedType("collectionelementtype", PType.TType)));

            cell_catalogue = new PaCell(tp_catalogue, path + "catalogue.pac", false);
            if (cell_catalogue.IsEmpty)
            {
                cell_catalogue.Fill(new object[0]);
            }
            BuildCollectionCache();
        }
コード例 #27
0
        public EntitiesWideTable(string path, int count)
        {
            //  this.path = path;

            PType DiaRec = new PTypeRecord(
                new NamedType("start", new PType(PTypeEnumeration.longinteger)),
                new NamedType("number", new PType(PTypeEnumeration.longinteger)));
            PType tp = new PTypeSequence(new PTypeRecord(
                                             new NamedType("entity", new PType(PTypeEnumeration.integer)),
                                             new NamedType("spo", DiaRec),   //TODO use count
                                             new NamedType("spo_op", DiaRec),
                                             new NamedType("spd", DiaRec)));

            ewtable = new PaCell(tp, path + "ewtable.pac", false);
        }
コード例 #28
0
        public IndexKeyImmutable(string path_name)
        {
            Type typ = typeof(Tkey);

            if (typ != typeof(int) && typ != typeof(long))
            {
                throw new Exception("Err: wrong type of key");
            }
            PType tp_key   = typ == typeof(int) ? new PType(PTypeEnumeration.integer) :  new PType(PTypeEnumeration.longinteger);
            PType tp_index = new PTypeSequence(new PTypeRecord(
                                                   new NamedType("key", tp_key),
                                                   new NamedType("offset", new PType(PTypeEnumeration.longinteger))));

            index_cell = new PaCell(tp_index, path_name + ".pac", false);
        }
コード例 #29
0
        public EntitiesWideTable(string path, DiapasonScanner <string>[] scanners)
        {
            this.path     = path;
            this.scanners = scanners;
            PType DiaRec = new PTypeRecord(
                new NamedType("start", new PType(PTypeEnumeration.longinteger)),
                new NamedType("number", new PType(PTypeEnumeration.longinteger)));
            PType tp = new PTypeSequence(new PTypeRecord(
                                             new NamedType("entity", new PType(PTypeEnumeration.sstring)),
                                             new NamedType("spo", DiaRec),
                                             new NamedType("spo_op", DiaRec),
                                             new NamedType("spd", DiaRec)));

            ewtable = new PaCell(tp, path + "ewtable.pac", false);
        }
コード例 #30
0
 public override void InitTypes()
 {
     tp_entity      = new PType(PTypeEnumeration.integer);
     tp_otriple_seq = new PTypeSequence(new PTypeRecord(
                                            new NamedType("subject", tp_entity),
                                            new NamedType("predicate", tp_entity),
                                            new NamedType("object", tp_entity)));
     // Тип для экономного выстраивания индекса s-p для dtriples
     tp_dtriple_spf = new PTypeSequence(new PTypeRecord(
                                            new NamedType("subject", tp_entity),
                                            new NamedType("predicate", tp_entity),
                                            new NamedType("offset", new PType(PTypeEnumeration.longinteger))));
     tp_entities_column = new PTypeSequence(tp_entity);
     tp_Data_column     = new PTypeSequence(new PType(PTypeEnumeration.longinteger));
 }
コード例 #31
0
        public void Open(bool readOnlyMode)
        {
            doublesCell = new PaCell(new PTypeSequence(new PType(PTypeEnumeration.real)), dataCellPath + "/doublesLiterals.pac", readOnlyMode);
            var pTypeString      = new PType(PTypeEnumeration.sstring);
            var pTypeStringsPair = new PTypeRecord(new NamedType("value", pTypeString),
                                                   new NamedType("add info", pTypeString));

            stringsCell      = new PaCell(new PTypeSequence(pTypeStringsPair), dataCellPath + "/stringsLiterals.pac", readOnlyMode);
            boolsCell        = new PaCell(new PTypeSequence(new PType(PTypeEnumeration.boolean)), dataCellPath + "/booleansLiterals.pac", readOnlyMode);
            typedObjectsCell = new PaCell(new PTypeSequence(pTypeStringsPair), dataCellPath + "/typedObjectsLiterals.pac", readOnlyMode);
            stringsArhive    = new Archive(dataCellPath + "/strings archive");
            var ptypeCode = new PTypeSequence(new PType(PTypeEnumeration.@byte));

            StringsArchedCell = new PaCell(new PTypeSequence(new PTypeRecord(new NamedType("string code", ptypeCode), new NamedType("lang code", ptypeCode))), dataCellPath + "/strings archive/binary data", false);
        }
コード例 #32
0
        public void SortByKey <Tkey>(Func <object, Tkey> keyfunction, IComparer <Tkey> comparer = null)
        {
            if (tp.Vid != PTypeEnumeration.sequence)
            {
                throw new Exception("SortByKey can't be implemented to this vid");
            }
            PTypeSequence pts = (PTypeSequence)tp;

            if (!pts.ElementType.HasNoTail)
            {
                throw new Exception("SortByKey can't be implemented to this type");
            }
            long llen = this.Count();

            SortByKey(0, llen, keyfunction, comparer);
        }
コード例 #33
0
        public void SortByKey <Tkey>(long start, long number, Func <object, Tkey> keyfunction,
                                     IComparer <Tkey> comparer)
        {
            PTypeSequence pts = (PTypeSequence)this.Type;

            if (number < 2)
            {
                return;             // сортировать не нужно
            }
            int  size       = pts.ElementType.HeadSize;
            long bufferSize = bufferBytes / size; // Это неправильно, правильнее - "зацепиться" за размер Tkey

            if (number <= bufferSize)
            {
                // Указатель на начальный элемент и размер головы записи
                PaEntry e = this.Element(start);
                // организуем массивы значений элементов и ключей
                object[] elements = new object[number];
                Tkey[]   keys     = new Tkey[number];
                // Вычислим и запишем значения и ключи
                for (long ii = 0; ii < number; ii++)
                {
                    var v = e.Get();
                    elements[ii] = v;
                    keys[ii]     = keyfunction(v);
                    e.offset    += size;
                }
                // Сортируем два массива
                Array.Sort(keys, elements, comparer);
                // Возвращаем значения
                e = this.Element(start);
                for (long ii = 0; ii < number; ii++)
                {
                    e.Set(elements[ii]);
                    e.offset += size;
                }
            }
            else
            {
                long half = number / 2;
                SortByKey <Tkey>(start, half, keyfunction, comparer);
                SortByKey <Tkey>(start + half, number - half, keyfunction, comparer);

                MergeUpByKey <Tkey>(start, half, number - half, keyfunction, comparer);
                Console.WriteLine("MergeUpByKey {0} values", number);
            }
        }
コード例 #34
0
ファイル: TableSignatures.cs プロジェクト: agmarchuk/PolarDB
        public TableSignatures(PType tp_rec)
        {
            // table_ind, part_ind, node_ind, streams: [integer] (? набор номеров стримов для данной части)
            PType tp_sections = new PTypeSequence(new PTypeRecord(
                                                      new NamedType("table_ind", new PType(PTypeEnumeration.integer)),
                                                      new NamedType("section_ind", new PType(PTypeEnumeration.integer)),
                                                      new NamedType("node_ind", new PType(PTypeEnumeration.integer))));

            tp_record  = tp_rec;
            signatures = new Tuple <PType, PType>[] {
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)), // пустая команда для тестирования
                new Tuple <PType, PType>(new PType(PTypeEnumeration.integer),
                                         new PTypeRecord(
                                             new NamedType("id", new PType(PTypeEnumeration.integer)),
                                             new NamedType("nm", new PType(PTypeEnumeration.sstring)),
                                             new NamedType("ag", new PType(PTypeEnumeration.integer)))),         // sendinttest - имитация Get(k)
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)),    // Clear()
                new Tuple <PType, PType>(new PType(PTypeEnumeration.integer), new PType(PTypeEnumeration.none)), // Init(nodenum)
                new Tuple <PType, PType>(new PTypeRecord(
                                             new NamedType("pair", tp_record),
                                             new NamedType("dynindex", new PType(PTypeEnumeration.boolean))),
                                         new PType(PTypeEnumeration.none)), // AppendOnlyRecord(tab, record, bool dynindex)
                new Tuple <PType, PType>(new PTypeRecord(
                                             new NamedType("indx_nom", new PType(PTypeEnumeration.integer)),
                                             new NamedType("ext_key", new PType(PTypeEnumeration.integer)),
                                             new NamedType("pri_key", new PType(PTypeEnumeration.integer)),
                                             new NamedType("dynindex", new PType(PTypeEnumeration.boolean))),
                                         new PType(PTypeEnumeration.none)),                                   // AppendOnlyExtKey(int tab, int indx_nom, int ext_key, int pri_key, bool dynindex)
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)), // Flush()
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)), // CalculateStaticIndex()
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)), // Activate()
                new Tuple <PType, PType>(new PTypeRecord(
                                             //new NamedType("tab", new PType(PTypeEnumeration.integer)),
                                             new NamedType("key", new PType(PTypeEnumeration.integer))),
                                         tp_record), // GetByKey(tab, key) -> tp_record
                new Tuple <PType, PType>(new PTypeRecord(
                                             //new NamedType("tab", new PType(PTypeEnumeration.integer)),
                                             new NamedType("exindnom", new PType(PTypeEnumeration.integer)),
                                             new NamedType("exkey", new PType(PTypeEnumeration.integer))),
                                         new PTypeSequence(new PType(PTypeEnumeration.integer))),             // GetAllPrimaryByExternal(tab, exindnom, exkey) -> object[] { prikey,... }
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)), // CreateDatabase()
                new Tuple <PType, PType>(ConfigObject.tp, new PType(PTypeEnumeration.none)),                  // SaveConfiguration(configuration)
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)), // LoadConfiguration()
                new Tuple <PType, PType>(new PType(PTypeEnumeration.none), new PType(PTypeEnumeration.none)), // ActivateDatabase()
                new Tuple <PType, PType>(ConfigObject.tp, new PType(PTypeEnumeration.none)),                  // SetConfiguration(conf)
            }.ToArray();
        }
コード例 #35
0
ファイル: Graph.cs プロジェクト: Sergey303/GraphTesting
 private void InitTypes()
 {
     this.tp_triplets =
         new PTypeSequence(
             new PTypeUnion(
                 new NamedType("empty", new PType(PTypeEnumeration.none)), // не используется, нужен для выполнения правила атомарного варианта
                 new NamedType("op",
                               new PTypeRecord(
                                   new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                   new NamedType("predicate", new PType(PTypeEnumeration.sstring)),
                                   new NamedType("obj", new PType(PTypeEnumeration.sstring)))),
                 new NamedType("dp",
                               new PTypeRecord(
                                   new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                   new NamedType("predicate", new PType(PTypeEnumeration.sstring)),
                                   new NamedType("data", new PType(PTypeEnumeration.sstring)),
                                   new NamedType("lang", new PType(PTypeEnumeration.sstring))))));
     this.tp_quads =
         new PTypeSequence(
             new PTypeRecord(
                 new NamedType("hs_e", new PType(PTypeEnumeration.integer)),
                 new NamedType("vid", new PType(PTypeEnumeration.integer)),
                 new NamedType("hs_p", new PType(PTypeEnumeration.integer)),
                 new NamedType("off", new PType(PTypeEnumeration.longinteger))));
     this.tp_graph = new PTypeSequence(new PTypeRecord(
                                           new NamedType("hs_e", new PType(PTypeEnumeration.integer)),
                                           new NamedType("direct",
                                                         new PTypeSequence(
                                                             new PTypeRecord(
                                                                 new NamedType("hs_p", new PType(PTypeEnumeration.integer)),
                                                                 new NamedType("off", new PTypeSequence(new PType(PTypeEnumeration.longinteger)))))),
                                           new NamedType("inverse",
                                                         new PTypeSequence(
                                                             new PTypeRecord(
                                                                 new NamedType("hs_p", new PType(PTypeEnumeration.integer)),
                                                                 new NamedType("off", new PTypeSequence(new PType(PTypeEnumeration.longinteger)))))),
                                           new NamedType("data",
                                                         new PTypeSequence(
                                                             new PTypeRecord(
                                                                 new NamedType("hs_p", new PType(PTypeEnumeration.integer)),
                                                                 new NamedType("off", new PTypeSequence(new PType(PTypeEnumeration.longinteger))))))));
     tp_n4 = new PTypeSequence(new PTypeRecord(
                                   new NamedType("hs_e", new PType(PTypeEnumeration.integer)),
                                   new NamedType("s4", new PTypeFString(4))));
 }
コード例 #36
0
ファイル: PxSortTest.cs プロジェクト: jurik/PolarDBTest
        public void SimpleSortRecordTest()
        {
            PType tp_seq = new PTypeSequence(new PTypeRecord(
                new NamedType("name", new PType(PTypeEnumeration.sstring)),
                new NamedType("id", new PType(PTypeEnumeration.sstring))));

            object[] testdb = new object[] {
                new object[] { "Petr", "0120"},
                new object[] { "Dan", "12"},
                new object[] { "Ivan", "54"},
                new object[] { "Jim", "13"},
                new object[] { "Wai", "1"},
                new object[] { "Ken", "15"},
                new object[] { "Aby", "916"},
            };

            string testpacfilename = "test.pxc";
            PxCell xCell = new PxCell(tp_seq, testpacfilename, false);
            try
            {
                xCell.Fill2(testdb);

                Assert.AreEqual(7, xCell.Root.Count());
                Assert.AreEqual("Ivan", xCell.Root.Element(2).Field(0).Get().Value.ToString());
                Assert.AreEqual("Jim", xCell.Root.Element(3).Field(0).Get().Value.ToString());
                Assert.AreEqual("Ken", xCell.Root.Element(5).Field(0).Get().Value.ToString());
                Assert.AreEqual("Aby", xCell.Root.Element(6).Field(0).Get().Value.ToString());

                xCell.Root.Sort(e =>
                {
                    return (string)e.Field(0).Get().Value;
                });

                Assert.AreEqual(7, xCell.Root.Count());
                Assert.AreEqual("Ivan", xCell.Root.Element(2).Field(0).Get().Value.ToString());
                Assert.AreEqual("Jim", xCell.Root.Element(3).Field(0).Get().Value.ToString());
                Assert.AreEqual("Petr", xCell.Root.Element(5).Field(0).Get().Value.ToString());
                Assert.AreEqual("Wai", xCell.Root.Element(6).Field(0).Get().Value.ToString());
            }
            finally
            {
                xCell.Close();
            }
        }
コード例 #37
0
ファイル: UnitTest1.cs プロジェクト: agmarchuk/Polar
        public void TestMethod1()
        {
            // Проверка формирования комплексного типа
            PType seqtriplets = new PTypeSequence(
                new PTypeUnion(
                    new NamedType("empty", new PType(PTypeEnumeration.none)),
                    new NamedType("op",
                        new PTypeRecord(
                            new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                            new NamedType("predicate", new PType(PTypeEnumeration.sstring)),
                            new NamedType("obj", new PType(PTypeEnumeration.sstring)))),
                    new NamedType("dp",
                        new PTypeRecord(
                            new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                            new NamedType("predicate", new PType(PTypeEnumeration.sstring)),
                            new NamedType("data", new PType(PTypeEnumeration.sstring)),
                            new NamedType("lang", new PType(PTypeEnumeration.sstring))))));
            // Проверка конструирования объекта
            object[] testdb = new object[] {
                new object[] { 1, new object[] {"a", "b", "c"}},
                new object[] { 1, new object[] {"a1", "b1", "c1"}},
                new object[] { 2, new object[] {"da", "db", "dc", "lang"}}
            };
            // Проверка интерпретации объекта
            string result0 = "[op^{\"a\",\"b\",\"c\"},op^{\"a1\",\"b1\",\"c1\"},dp^{\"da\",\"db\",\"dc\",\"lang\"}]";
            string result = seqtriplets.Interpret(testdb);
            Assert.AreEqual(result, result0);

            string path = @"..\..\..\Databases\";
            // Создание ячейки плавающего формата
            string testpacfilename = path + "test.pac";
            if (System.IO.File.Exists(testpacfilename)) { System.IO.File.Delete(testpacfilename); }
            PaCell cell = new PaCell(seqtriplets, testpacfilename, false); // false - чтобы заполнять
            // Заполнение ячейки данными из объекта
            cell.Fill(testdb);
            // Проверка того, что имеется в ячейке
            var cell_pvalue = cell.Root.GetValue();
            Console.WriteLine(cell_pvalue.Type.Interpret(cell_pvalue.Value));
            string result2 = cell_pvalue.Type.Interpret(cell_pvalue.Value);
            Assert.AreEqual(result2, result0);
            cell.Close();
            System.IO.File.Delete(testpacfilename);
        }
コード例 #38
0
        public void FillAtomsTest()
        {
            var seqtriplets = new PTypeSequence(new PType(PTypeEnumeration.sstring));

            var testdb = new object[] { "test1", "obj24", "ggg", "ntest45", "nq1"
            };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");
            try
            {
                hCell.Fill(testdb);
                Assert.AreEqual(5, hCell.Root.Count());
                var e = (string)hCell.Root.Element(2).Get().Value;
                Assert.AreEqual(e.Length, 3);
                Assert.AreEqual(e, "ggg");
                Assert.AreEqual((string)hCell.Root.Element(0).Get().Value, "test1");
                Assert.AreEqual((string)hCell.Root.Element(4).Get().Value, "nq1");
            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #39
0
        public void Fill2SeqWithVolumeTest()
        {
            var seqtriplets = new PTypeSequence(
                    new PTypeUnion(
                        new NamedType("op",
                            new PTypeRecord(
                                new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                new NamedType("char", new PType(PTypeEnumeration.character))
                            )),
                        new NamedType("dp",
                            new PTypeRecord(
                                new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                new NamedType("integer", new PType(PTypeEnumeration.integer)),
                                new NamedType("bool", new PType(PTypeEnumeration.boolean))
                            )
                        )
                    ));

            var testdb = new object[] {
                new object[] { 0, new object[] {"a", 'c'}},
                new object[] { 0, new object[] {"a1", 'c'}},
                new object[] { 1, new object[] {"da", 3, true}},
                new object[] { 0, new object[] {"a1", '1'}},
                new object[] { 1, new object[] {"da1", 7, true}},
                new object[] { 0, new object[] {"a2", '2'}},
                new object[] { 1, new object[] {"da2", 9, false}},
                new object[] { 1, new object[] {"da2", 11, true}},
                new object[] { 1, new object[] {"da3", 13, false}}
            };

            string testpacfilename = "test.pxc";
            PxCell xCell = new PxCell(seqtriplets, testpacfilename, false);

            xCell.Fill2(testdb);

            CheckSequence(xCell);
        }
コード例 #40
0
        public void UpdateUnionTest()
        {
            var seqtriplets = new PTypeSequence(
                                new PTypeUnion(
                                    new NamedType("empty", new PType(PTypeEnumeration.none)),
                                    new NamedType("op",
                                        new PTypeRecord(
                                            new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                            new NamedType("predicate", new PType(PTypeEnumeration.sstring)),
                                            new NamedType("obj", new PType(PTypeEnumeration.sstring)))),
                                    new NamedType("dp",
                                        new PTypeRecord(
                                            new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                            new NamedType("predicate", new PType(PTypeEnumeration.sstring)),
                                            new NamedType("data", new PType(PTypeEnumeration.sstring)),
                                            new NamedType("lang", new PType(PTypeEnumeration.sstring)))))
                                );

            var testdb = new object[] {
                new object[] { 1, new object[] {"a", "b", "c"}},
                new object[] { 1, new object[] {"a1", "b1", "c1"}},
                new object[] { 2, new object[] {"da", "db", "dc", "lang"}}
            };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");
            try
            {
                hCell.Fill(testdb);
                Assert.AreEqual(3, hCell.Root.Count());
                var e = (object[])hCell.Root.Element(1).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 1);
                Assert.IsInstanceOfType(e[1], typeof(object[]));
                var o = (object[])e[1];
                Assert.AreEqual(o.Length, 3);
                Assert.AreEqual(o[0], "a1");
                Assert.AreEqual(o[1], "b1");
                Assert.AreEqual(o[2], "c1");

                PhEntry entry = hCell.Root.Element(1);
                entry.Set(new object[] { 1, new object[] { "a2", "b2", "c134" } });

                e = (object[])hCell.Root.Element(1).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 1);
                Assert.IsInstanceOfType(e[1], typeof(object[]));
                o = (object[])e[1];
                Assert.AreEqual(o.Length, 3);
                Assert.AreEqual(o[0], "a2");
                Assert.AreEqual(o[1], "b2");
                Assert.AreEqual(o[2], "c134");

            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #41
0
        public void InsertElementInnerTest()
        {
            var seqtriplets = new PTypeSequence(
                new PTypeUnion(
                    new NamedType("test1",
                        new PTypeRecord(
                            new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                            new NamedType("obj", new PType(PTypeEnumeration.sstring)))),
                    new NamedType("test2",
                        new PTypeRecord(
                            new NamedType("name", new PType(PTypeEnumeration.sstring)),
                            new NamedType("value", new PType(PTypeEnumeration.integer))))
                 ));

            var testdb = new object[] {
                new object[] { 0, new object[] {"a11", "b11"}},
                new object[] { 0, new object[] {"a12", "b12"}},
                new object[] { 1, new object[] {"a21", 22}},
                new object[] { 0, new object[] {"a13", "b13"}},
                new object[] { 0, new object[] {"a14", "b14"}},
                new object[] { 0, new object[] {"a15", "b15"}},
                new object[] { 0, new object[] {"a16", "b16"}},
            };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");
            hCell.Fill(testdb);
            try
            {
                Assert.AreEqual(hCell.Root.Count(), 7);
                var e = (object[])hCell.Root.Element(2).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 1);
                var o = (object[])e[1];
                Assert.AreEqual(o[0], "a21");
                Assert.AreEqual(o[1], 22);

                e = (object[])hCell.Root.Element(3).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a13");
                Assert.AreEqual(o[1], "b13");

                e = (object[])hCell.Root.Element(4).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a14");
                Assert.AreEqual(o[1], "b14");

                hCell.InsertElement(4, new object[] { 0, new object[] { "a44", "b44" } });

                Assert.AreEqual(hCell.Root.Count(), 8);

                e = (object[])hCell.Root.Element(4).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a44");
                Assert.AreEqual(o[1], "b44");

                e = (object[])hCell.Root.Element(5).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a14");
                Assert.AreEqual(o[1], "b14");

                e = (object[])hCell.Root.Element(7).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a16");
                Assert.AreEqual(o[1], "b16");

                hCell.InsertElement(0, new object[] { 0, new object[] { "a55", "b55" } });

                Assert.AreEqual(hCell.Root.Count(), 9);

                e = (object[])hCell.Root.Element(0).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a55");
                Assert.AreEqual(o[1], "b55");

                e = (object[])hCell.Root.Element(5).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a44");
                Assert.AreEqual(o[1], "b44");

                e = (object[])hCell.Root.Element(8).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a16");
                Assert.AreEqual(o[1], "b16");
            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #42
0
ファイル: NameTableUniversal.cs プロジェクト: agmarchuk/Polar
        // =========== Ключевой фрагмент ============
        public IGetDictionaryLong<string, int> InsertPortion(IEnumerable<string> s_flow)
        {
            HashSet<string> hs = new HashSet<string>();
            foreach (string s in s_flow) hs.Add(s);

            //string[] ssa = hs.OrderBy(s => new HashedString() { Str = s }).ToArray(); // Надо сделать более экономно
            string[] ssa = hs.Select(s => new { s = s, hs = new HashedString() { Str = s } })
                .OrderBy(pa => pa.hs)
                .Select(pa => pa.s).ToArray();

            if (ssa.Length == 0) throw new Exception("name table empty");

            //s_index_array.IndexCell.Close(); // cssequence.Close();
            // Подготовим основную ячейку для работы
            if (System.IO.File.Exists(path + "tmp.pac")) System.IO.File.Delete(path + "tmp.pac");
            //System.IO.File.Copy(s_index_array_path + ".pac", path + "tmp.pac");

            // Это по общей логике, но если снаружи изменится, надо изменить и тут
            PType tp_s_index_seq = new PTypeSequence(new PTypeRecord(
                new NamedType("halfkey", new PType(PTypeEnumeration.integer)),
                new NamedType("offset", new PType(PTypeEnumeration.longinteger))));
            PaCell source = new PaCell(tp_s_index_seq, path + "tmp.pac", false);
            source.Fill(new object[0]);
            foreach (var v in s_index_array.IndexCell.Root.ElementValues()) source.Root.AppendElement(v);
            source.Flush();
            PaCell target = s_index_array.IndexCell;
            target.Clear();
            target.Fill(new object[0]);

            int ssa_ind = 0;
            bool ssa_notempty = true;
            string ssa_current = ssa_notempty ? ssa[ssa_ind] : null;
            ssa_ind++;

            // Для накопления пар
            List<KeyValuePair<string, int>> accumulator = new List<KeyValuePair<string, int>>(ssa.Length);

            // Очередной (новый) код (индекс)
            int code_new = 0;
            if (!source.IsEmpty && source.Root.Count() > 0)
            {
                code_new = (int)source.Root.Count();
                PaEntry tab_entry = table.Element(0); // не было проверки на наличие хотя бы одного элемента
                // Сканируем индексный массив, элементы являются парами {halfkey, offset}
                foreach (object[] val in source.Root.ElementValues())
                {
                    // Пропускаю элементы из нового потока, которые меньше текущего сканированного элемента
                    int halfkey = (int)val[0];
                    string s = null; // Будет запрос если понадобится
                    int cmp = 0;
                    while (ssa_notempty) //  && (cmp = ssa_current.CompareTo(s)) <= 0
                    {
                        int hash_current = ssa_current.GetHashModifiedBernstein();//1;//ssa_current.GetHashCode();
                        cmp = hash_current.CompareTo(halfkey);
                        if (cmp == 0)
                        { // Дополнительное упрядочивание по строке
                            if (s == null)
                            {
                                tab_entry.offset = (long)val[1];
                                s = (string)tab_entry.Field(1).Field(1).Get();
                            }
                            cmp = ssa_current.CompareTo(s);
                        }
                        if (cmp < 0)
                        { // добавляется новый код
                            // добавляем код в таблицу
                            long offset = table.TableCell.Root.AppendElement(new object[] { false, new object[] { code_new, ssa_current } });
                            // Автоматом добавляем начало строки в offsets
                            offset_array.IndexCell.Root.AppendElement(offset);
                            // добавляем строчку в строковый индекс
                            target.Root.AppendElement(new object[] { hash_current, offset });
                            accumulator.Add(new KeyValuePair<string, int>(ssa_current, code_new));
                            code_new++;
                        }
                        else if (cmp == 0)
                        { // используется существующий код
                            tab_entry.offset = (long)val[1];
                            object[] ob = (object[])tab_entry.Get();
                            object[] rec = (object[])ob[1];
                            int code = (int)rec[0];
                            string key = (string)rec[1];
                            accumulator.Add(new KeyValuePair<string, int>(key, code));
                        }
                        else // if (cmp > 0)
                            break; // Нужно дойти до него на следующем элементе в следующем цикле
                        if (ssa_ind < ssa.Length)
                            ssa_current = ssa[ssa_ind++]; //ssa.ElementAt<string>(ssa_ind);
                        else
                            ssa_notempty = false;
                    }
                    target.Root.AppendElement(val); // переписывается тот же объект
                }
            }
            // В массиве ssa могут остаться элементы, их надо просто добавить
            if (ssa_notempty)
            {
                do
                {
                    // добавляем код в таблицу
                    long offset = table.TableCell.Root.AppendElement(new object[] { false, new object[] { code_new, ssa_current } });
                    // Автоматом добавляем начало строки в offsets
                    offset_array.IndexCell.Root.AppendElement(offset);
                    // добавляем строчку в строковый индекс
                    target.Root.AppendElement(new object[] { ssa_current.GetHashModifiedBernstein(), offset });//ssa_current.GetHashCode()
                    accumulator.Add(new KeyValuePair<string, int>(ssa_current, code_new));
                    code_new++;
                    if (ssa_ind < ssa.Length) ssa_current = ssa[ssa_ind];
                    ssa_ind++;
                }
                while (ssa_ind <= ssa.Length);
            }

            table.TableCell.Flush();
            offset_array.IndexCell.Flush();
            target.Flush();

            source.Close();
            System.IO.File.Delete(path + "tmp.pac");

            // Финальный аккорд: формирование и выдача словаря
            DictionaryLong<string, int> dic = new DictionaryLong<string, int>(s=>(ulong) s.GetHashCode(), (ulong) accumulator.Count);
            foreach (var keyValuePair in accumulator.Where(keyValuePair => !dic.ContainsKey(keyValuePair.Key)))
            {
                dic.Add(keyValuePair.Key, keyValuePair.Value);
            }

            return dic;
        }
コード例 #43
0
        public void InsertElementOutterTest()
        {
            var seqtriplets = new PTypeSequence(
                            new PTypeUnion(
                                new NamedType("test1",
                                    new PTypeRecord(
                                        new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                                        new NamedType("obj", new PType(PTypeEnumeration.sstring)))),
                                new NamedType("test2",
                                    new PTypeRecord(
                                        new NamedType("name", new PType(PTypeEnumeration.sstring)),
                                        new NamedType("value", new PType(PTypeEnumeration.integer))))
                             ));

            var testdb = new object[] {
                new object[] { 0, new object[] {"a11", "b11"}},
            };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");
            hCell.Fill(testdb);
            try
            {
                hCell.InsertElement(1000, new object[] { 0, new object[] { "a12", "b12" } });
                Assert.AreEqual(hCell.Root.Count(), 2);

                object[] e = (object[])hCell.Root.Element(1).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                object[] o = (object[])e[1];
                Assert.AreEqual(o[0], "a12");
                Assert.AreEqual(o[1], "b12");

                int step = 23725;

                for (int i = 1; i < step - 1; i++)
                {
                    hCell.InsertElement(i, new object[] { 0, new object[] { "a" + i, "b" + i } });
                    Assert.AreEqual(hCell.Root.Count(), i + 2);

                    e = (object[])hCell.Root.Element(i).Get().Value;
                    Assert.AreEqual(e.Length, 2);
                    Assert.AreEqual(e[0], 0);
                    o = (object[])e[1];
                    Assert.AreEqual(o[0], "a" + i);
                    Assert.AreEqual(o[1], "b" + i);
                }

                var str = string.Empty;
                hCell.InsertElement(step - 2, new object[] { 0, new object[] { "a003", "b003" } });
                Assert.AreEqual(hCell.Root.Count(), step + 1);

                e = (object[])hCell.Root.Element(step - 3).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a" + (step - 3));
                Assert.AreEqual(o[1], "b" + (step - 3));

                e = (object[])hCell.Root.Element(step - 2).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a003");
                Assert.AreEqual(o[1], "b003");

                var s = (object[])hCell.Root.Elements().Last().Get().Value;
                Assert.AreEqual(s.Length, 2);
                Assert.AreEqual(s[0], 0);
                o = (object[])s[1];
                Assert.AreEqual(o[0], "a12");
                Assert.AreEqual(o[1], "b12");

                hCell.InsertElement(step, new object[] { 0, new object[] { "a005", "b005" } });
                Assert.AreEqual(hCell.Root.Count(), step + 2);

                e = (object[])hCell.Root.Element(step - 3).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a" + (step - 3));
                Assert.AreEqual(o[1], "b" + (step - 3));

                e = (object[])hCell.Root.Element(step - 2).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a003");
                Assert.AreEqual(o[1], "b003");

                s = (object[])hCell.Root.Elements().Last().Get().Value;
                Assert.AreEqual(s.Length, 2);
                Assert.AreEqual(s[0], 0);
                o = (object[])s[1];
                Assert.AreEqual(o[0], "a12");
                Assert.AreEqual(o[1], "b12");

                hCell.InsertElement(step + 1, new object[] { 0, new object[] { "a006", "b006" } });
                Assert.AreEqual(hCell.Root.Count(), step + 3);

                e = (object[])hCell.Root.Element(step - 3).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a" + (step - 3));
                Assert.AreEqual(o[1], "b" + (step - 3));

                e = (object[])hCell.Root.Element(step - 2).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a003");
                Assert.AreEqual(o[1], "b003");

                s = (object[])hCell.Root.Elements().Last().Get().Value;
                Assert.AreEqual(s.Length, 2);
                Assert.AreEqual(s[0], 0);
                o = (object[])s[1];
                Assert.AreEqual(o[0], "a12");
                Assert.AreEqual(o[1], "b12");
            }
            finally
            {
                hCell.Close();
            }
        }
コード例 #44
0
        public void DeleteOutterElementTest()
        {
            var seqtriplets = new PTypeSequence(
                new PTypeUnion(
                    new NamedType("test1",
                        new PTypeRecord(
                            new NamedType("subject", new PType(PTypeEnumeration.sstring)),
                            new NamedType("obj", new PType(PTypeEnumeration.sstring)))),
                    new NamedType("test2",
                        new PTypeRecord(
                            new NamedType("name", new PType(PTypeEnumeration.sstring)),
                            new NamedType("value", new PType(PTypeEnumeration.integer))))
                 ));

            var testdb = new object[] {
                new object[] { 0, new object[] {"a11", "b11"}},
                new object[] { 1, new object[] {"a21", 22}},
               };

            PhCell hCell = new PhCell(seqtriplets, string.Empty, "test");
            hCell.Fill(testdb);
            try
            {
                Assert.AreEqual(hCell.Root.Count(), 2);
                var e = (object[])hCell.Root.Element(1).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 1);
                var o = (object[])e[1];
                Assert.AreEqual(o[0], "a21");
                Assert.AreEqual(o[1], 22);

                int step = 17359;
                for (int i = 2; i < step; i++)
                {
                    hCell.AppendElement(new object[] { 0, new object[] { "a" + i, "b" + i } });
                    Assert.AreEqual(hCell.Root.Count(), i + 1);
                    e = (object[])hCell.Root.Element(i).Get().Value;
                    Assert.AreEqual(e.Length, 2);
                    Assert.AreEqual(e[0], 0);
                    o = (object[])e[1];
                    Assert.AreEqual(o[0], "a" + i);
                    Assert.AreEqual(o[1], "b" + i);
                }

                hCell.AppendElement(new object[] { 0, new object[] { "a" + step, "b" + step } });
                Assert.AreEqual(hCell.Root.Count(), step + 1);

                hCell.AppendElement(new object[] { 0, new object[] { "a" + (step + 1), "b" + (step + 1) } });
                Assert.AreEqual(hCell.Root.Count(), step + 2);

                hCell.AppendElement(new object[] { 0, new object[] { "a" + (step + 2), "b" + (step + 2) } });
                Assert.AreEqual(hCell.Root.Count(), step + 3);

                hCell.DeleteElement(step + 1);
                Assert.AreEqual(hCell.Root.Count(), step + 2);
                e = (object[])hCell.Root.Element(step - 1).Get().Value;
                Assert.AreEqual(e.Length, 2);
                Assert.AreEqual(e[0], 0);
                o = (object[])e[1];
                Assert.AreEqual(o[0], "a" + (step - 1));
                Assert.AreEqual(o[1], "b" + (step - 1));

                var e1 = (object[])hCell.Root.Element(step).Get().Value;
                Assert.AreEqual(e1.Length, 2);
                Assert.AreEqual(e1[0], 0);
                o = (object[])e1[1];
                Assert.AreEqual(o[0], "a" + step);
                Assert.AreEqual(o[1], "b" + step);

                hCell.DeleteElement(step);
                Assert.AreEqual(hCell.Root.Count(), step + 1);
                e = (object[])hCell.Root.Element(step - 2).Get().Value;
                e1 = (object[])hCell.Root.Element(step).Get().Value;

                //проверяем работу списка после добавления элемента
                var s = (object[])hCell.Root.Elements().Last().Get().Value;
                Assert.AreEqual(s.Length, 2);
                Assert.AreEqual(s[0], 0);
                o = (object[])s[1];
                Assert.AreEqual(o[0], "a" + (step + 2));
                Assert.AreEqual(o[1], "b" + (step + 2));

            }
            finally
            {
                hCell.Close();
            }
        }