Exemple #1
0
        internal LiteFileStream(LiteEngine engine, LiteFileInfo file, FileAccess mode)
        {
            _engine = engine;
            _file   = file;
            _mode   = mode;

            if (mode == FileAccess.Read)
            {
                // initialize first data block
                _currentChunkData = this.GetChunkData(_currentChunkIndex);
            }
            else if (mode == FileAccess.Write)
            {
                _buffer = new MemoryStream(MAX_CHUNK_SIZE);

                // delete chunks content if needed
                if (file.Length > 0)
                {
                    var index   = 0;
                    var deleted = true;

                    // delete one-by-one to avoid all pages files dirty in memory
                    while (deleted)
                    {
                        deleted = _engine.Delete(LiteStorage.CHUNKS, LiteFileStream.GetChunckId(_file.Id, index++)); // index zero based
                    }
                }

                // clear size counters
                file.Length = 0;
                file.Chunks = 0;
            }
        }
        /// <summary>
        /// Delete a file inside datafile and all metadata related
        /// </summary>
        public bool Delete(string id)
        {
            if (id.IsNullOrWhiteSpace())
            {
                throw new ArgumentNullException(nameof(id));
            }

            // remove file reference in _files
            var deleted = _engine.Delete(FILES, id);

            // if not found, just return false
            if (!deleted)
            {
                return(false);
            }

            // delete all files chunks based on _id string
            var index = 0;

            // delete one-by-one to avoid all pages files dirty in memory
            while (deleted)
            {
                deleted = _engine.Delete(CHUNKS, LiteFileStream.GetChunckId(id, index++)); // index zero based
            }

            return(true);
        }