Esempio n. 1
0
        public Stream CreateStream(out int number)
        {
            number = Count();
            PagedStream.InitPagedStreamHead(collection_stream, number * sz, 0L, -1L);
            collection_stream.Flush(); fob.Flush();
            Stream created = new PagedStream(fob, collection_stream, number * sz);

            streams.Add(created);
            return(created);
        }
Esempio n. 2
0
        public StreamStorage(string dbpath)
        {
            bool fob_exists = File.Exists(dbpath);
            // Открываем или создаем файл-носитель хранилища
            FileStream fs = new FileStream(dbpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            // Создаем собственно блочное (страничное) хранилище
            fob = new FileOfBlocks(fs);

            fob.DeactivateCache();

            sz = PagedStream.HEAD_SIZE;
            // Далее идет корявый способ создания трех потоков (Stream), нужных для базы данных
            Stream first_stream = fob.GetFirstAsStream();

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

            // Если main_stream нулевой длины, надо инициировать конфигурацию стримов
            bool toinit = main_stream.Length == 0;

            if (toinit)
            {
                // инициируем 2 головы для потоков
                PagedStream.InitPagedStreamHead(main_stream, 0L, 0L, -1L); // Это будет для коллекций
                PagedStream.InitPagedStreamHead(main_stream, PagedStream.HEAD_SIZE, 0L, -1L);
                main_stream.Flush(); fob.Flush();
            }
            collection_stream = new PagedStream(fob, main_stream, 0L);
            PagedStream special_stream = new PagedStream(fob, main_stream, sz); //TODO: резерв

            // Если main_stream ненулевой длины, надо инициировать имеющуюся конфигурацию стримов
            if (collection_stream.Length > 0)
            {
                int nstreams = (int)(collection_stream.Length / sz);
                // инициируем nstreams голов для потоков
                for (int i = 0; i < nstreams; i++)
                {
                    //PagedStream.InitPagedStreamHead(main_stream, i * sz, 0L, -1L);
                    streams.Add(new PagedStream(fob, collection_stream, i * sz));
                }
                collection_stream.Flush(); fob.Flush();
            }
        }
Esempio n. 3
0
        public PagedStreamStore(string dbpath, int nstreams)
        {
            bool fob_exists = File.Exists(dbpath);
            // Открываем или создаем файл-носитель хранилища
            FileStream fs = new FileStream(dbpath, FileMode.OpenOrCreate, FileAccess.ReadWrite);

            // Создаем собственно блочное (страничное) хранилище
            fob = new FileOfBlocks(fs);
            // Далее идет корявый способ создания трех потоков (Stream), нужных для базы данных
            Stream first_stream = fob.GetFirstAsStream();

            if (!fob_exists)
            {
                PagedStream.InitPagedStreamHead(first_stream, 8L, 0, PagedStream.HEAD_SIZE);
                fob.Flush();
            }
            PagedStream main_stream = new PagedStream(fob, fob.GetFirstAsStream(), 8L);
            long        sz          = PagedStream.HEAD_SIZE;

            // Если main_stream нулевой длины, надо инициировать конфигурацию стримов
            if (main_stream.Length == 0)
            {
                // инициируем nstreams голов для потоков
                for (int i = 0; i < nstreams; i++)
                {
                    PagedStream.InitPagedStreamHead(main_stream, i * sz, 0L, -1L);
                }
                main_stream.Flush(); fob.Flush();
            }
            // создадим nstreams потоков
            pstreams = new PagedStream[nstreams];
            for (int i = 0; i < nstreams; i++)
            {
                pstreams[i] = new PagedStream(fob, main_stream, i * sz);
            }
        }
Esempio n. 4
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));
            }
        }