Ejemplo n.º 1
0
        /// <summary>
        /// Shrink datafile
        /// </summary>
        public void Shrink()
        {
            var dbFileName = _fileStream.Name;
            var fileAccess = _fileStream.CanWrite ? FileAccess.ReadWrite : FileAccess.Read;
            var tempFile   = Path.GetDirectoryName(dbFileName) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(dbFileName) + ".temp" + Path.GetExtension(dbFileName);

            if (File.Exists(tempFile))
            {
                File.Delete(tempFile);
            }

            var entries = ListFiles();

            FileDB.CreateEmptyFile(tempFile, false);

            using (var tempDb = new FileDB(tempFile, FileAccess.ReadWrite))
            {
                foreach (var entry in entries)
                {
                    using (var stream = new MemoryStream())
                    {
                        Read(entry.ID, stream);
                        stream.Seek(0, SeekOrigin.Begin);
                        tempDb.Store(entry, stream);
                    }
                }
            }

            Dispose();

            File.Delete(dbFileName);
            File.Move(tempFile, dbFileName);

            Connect(dbFileName, fileAccess);
        }
Ejemplo n.º 2
0
        private void Connect(string fileName, FileAccess fileAccess)
        {
            if (!File.Exists(fileName))
            {
                FileDB.CreateEmptyFile(fileName);
            }

            // Não permite acesso somente gravação (transforma em leitura/gravação)
            var fa = fileAccess == FileAccess.Write || fileAccess == FileAccess.ReadWrite ? FileAccess.ReadWrite : FileAccess.Read;

            _fileStream = new FileStream(fileName, FileMode.Open, fa, FileShare.ReadWrite, (int)BasePage.PAGE_SIZE, FileOptions.None);

            _engine = new Engine(_fileStream);
        }