Exemplo n.º 1
0
        // Проверка скорости ввода данных
        public static void Main9()
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            Console.WriteLine("Start Program9");
            int mbytes    = 800;
            int nelements = mbytes / 8 * 1024 * 1024;

            // ======== Проверяем ввод данных через IO =======
            //FileStream fs = new FileStream(path + "filestream.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 10000000);
            FileStream fs = new FileStream(dbpath + "filestream.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);

            byte[] buffer = new byte[2048 * 32];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)(i & 255);
            }
            sw.Restart();
            for (int i = 0; i < 16 * mbytes; i++)
            {
                fs.Write(buffer, 0, buffer.Length);
            }
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms. for writing {mbytes} mbytes");

            // ======== Проверяем ввод данных через BinaryWriter =======
            fs.Position = 0L;
            BinaryWriter bw  = new BinaryWriter(fs);
            long         val = 29877777298392742L;

            sw.Restart();
            for (int i = 0; i < mbytes / 8 * 1024 * 1024; i++)
            {
                bw.Write(val);
            }
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms. for writing {mbytes / 8 * 1024 * 1024} long numbers");

            // ======== Проверяем ввод данных через ячейку =======
            PaCell cell = new PaCell(new PTypeSequence(new PType(PTypeEnumeration.longinteger)), dbpath + "sequ_long.pac", false);

            cell.Clear();
            cell.Fill(new object[0]);
            sw.Restart();
            for (int i = 0; i < nelements; i++)
            {
                cell.Root.AppendElement((long)(nelements - i));
            }
            cell.Flush();
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms. for writing {mbytes / 8 * 1024 * 1024} long elements");

            // ======== Проверяем ввод данных через ячейку =======
            PType tp_rec = new PTypeRecord(
                new NamedType("f1", new PType(PTypeEnumeration.integer)),
                new NamedType("f2", new PType(PTypeEnumeration.sstring)),
                new NamedType("f3", new PType(PTypeEnumeration.real)));
            PaCell cell2 = new PaCell(new PTypeSequence(tp_rec), dbpath + "sequ_rec.pac", false);

            cell2.Clear();
            cell2.Fill(new object[0]);
            sw.Restart();
            int ne = nelements / 3;

            for (int i = 0; i < ne; i++)
            {
                int id = nelements - i;
                cell2.Root.AppendElement(new object[] { id, "=" + id, 5.5 });
            }
            cell2.Flush();
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms. for writing {ne} record elements");

            // ======== Проверяем ввод данных через ячейку =======
            PType tp_rec3 = new PTypeRecord(
                new NamedType("f1", new PType(PTypeEnumeration.integer)),
                new NamedType("f2", new PType(PTypeEnumeration.sstring)),
                new NamedType("f3", new PType(PTypeEnumeration.real)));
            Stream       stream = new FileStream(dbpath + "fstream.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite); //new MemoryStream();
            BinaryWriter bw3    = new BinaryWriter(stream);

            sw.Restart();
            int ne3 = nelements / 3;

            for (int i = 0; i < ne3; i++)
            {
                int id = nelements - i;
                PaCell.SetPO(tp_rec3, bw3, new object[] { id, "=" + id, 5.5 });
            }

            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms. for writing {ne3} record elements to memory stream");

            // ======== Проверяем ввод данных через ячейку и страничное хранилище потоков =======
            PType tp_rec4 = new PTypeRecord(
                new NamedType("f1", new PType(PTypeEnumeration.integer)),
                new NamedType("f2", new PType(PTypeEnumeration.sstring)),
                new NamedType("f3", new PType(PTypeEnumeration.real)));

            //PagedStreamStore ps_store = new PagedStreamStore(path + "storage.bin", 4); // заказали 4 стрима, конкретные будут: ps_store[i]
            StreamStorage ps_store = new StreamStorage(dbpath + "storage9.bin", 4);

            ps_store.DeactivateCache();
            PaCell cell4 = new PaCell(new PTypeSequence(tp_rec), ps_store[0], false);

            cell4.Clear();
            cell4.Fill(new object[0]);
            sw.Restart();
            int ne4 = nelements / 3;

            for (int i = 0; i < ne4; i++)
            {
                int id = nelements - i;
                cell4.Root.AppendElement(new object[] { id, "=" + id, 5.5 });
            }
            cell4.Flush();
            sw.Stop();
            Console.WriteLine($"{sw.ElapsedMilliseconds} ms. for storing {ne} record elements");
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Start PagedFileStore");
            string       path         = "";
            string       fname        = path + "fob.bin";
            bool         fob_exists   = File.Exists(fname);
            FileStream   fs           = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileOfBlocks fob          = new FileOfBlocks(fs);
            Stream       first_stream = fob.GetFirstAsStream();

            if (!fob_exists)
            {
                PagedStream.InitPagedStreamHead(first_stream, 8L, 0, PagedStream.HEAD_SIZE);
                fob.Flush();
            }

            PagedStream ps = new PagedStream(fob, fob.GetFirstAsStream(), 8L);

            //ps.Clear();
            Console.WriteLine("stream length={0} position={1}", ps.Length, ps.Position);

            bool towrite = false;
            bool toread  = false;

            if (towrite)
            {
                DirectoryInfo dirin = new DirectoryInfo(@"D:\Home\FactographDatabases\testdir");
                BinaryWriter  bw    = new BinaryWriter(ps);
                ps.Position = 0L;
                foreach (FileInfo f in dirin.GetFiles())
                {
                    PaCell.SetPO(new PolarDB.PType(PolarDB.PTypeEnumeration.sstring), bw, f.Name);
                    Stream stream = f.OpenRead();
                    PaCell.SetPO(new PolarDB.PType(PolarDB.PTypeEnumeration.longinteger), bw, stream.Length);
                    stream.CopyTo(ps);
                    break;
                }
                ps.Flush();
            }
            else if (toread)
            {
                string       dirout = @"D:\Home\FactographDatabases\testout\";
                BinaryReader br     = new BinaryReader(ps);
                ps.Position = 0L;
                for (;;)
                {
                    if (ps.Position >= ps.Length)
                    {
                        break;
                    }
                    string     name       = (string)PolarDB.PaCell.GetPO(new PolarDB.PType(PolarDB.PTypeEnumeration.sstring), br);
                    FileStream stream_out = new FileStream(dirout + name, FileMode.CreateNew, FileAccess.Write);
                    byte[]     buff       = new byte[1000];
                    long       len        = (long)PolarDB.PaCell.GetPO(new PolarDB.PType(PolarDB.PTypeEnumeration.longinteger), br);
                    while (len > 0)
                    {
                        int count = (int)System.Math.Min((long)buff.Length, len);
                        int n     = ps.Read(buff, 0, count);
                        if (n != count)
                        {
                            throw new Exception("Err: 2898782349");
                        }
                        stream_out.Write(buff, 0, count);
                        len -= count;
                    }
                    stream_out.Flush();
                    stream_out.Dispose();
                }
            }
            bool tomake = false;

            if (tomake)
            {
                long cell_shift = ps.Length;
                Console.WriteLine("начало ячейки: {0}", cell_shift);
                PagedStream.InitPagedStreamHead(ps, cell_shift, 0L, -1L);
                PagedStream ps_cell = new PagedStream(fob, ps, cell_shift);
                PType       tp      = new PTypeSequence(new PType(PTypeEnumeration.integer));
                PaCell      cell    = new PaCell(tp, ps_cell, false);
                cell.Fill(new object[] { 111, 222, 333, 444, 555, 666, 777, 888, 999 });
                object ob = cell.Root.Get();
                Console.WriteLine("ob={0}", tp.Interpret(ob));
            }
            bool toadd = true;

            if (toadd)
            {
                long        cell_shift = 640;
                PagedStream ps_cell    = new PagedStream(fob, ps, cell_shift);
                PType       tp         = new PTypeSequence(new PType(PTypeEnumeration.integer));
                PaCell      cell       = new PaCell(tp, ps_cell, false);
                for (int i = 0; i < 1000000; i++)
                {
                    cell.Root.AppendElement(99999);
                }
                cell.Flush();

                Console.WriteLine("n elements={0}", cell.Root.Count());
            }
            bool tolook = false;

            if (tolook)
            {
                long        cell_shift = 640;
                PagedStream ps_cell    = new PagedStream(fob, ps, cell_shift);
                PType       tp         = new PTypeSequence(new PType(PTypeEnumeration.integer));
                PaCell      cell       = new PaCell(tp, ps_cell, false);
                object      ob         = cell.Root.Get();
                Console.WriteLine("ob={0}", tp.Interpret(ob));
            }
        }