Esempio n. 1
0
        internal LiteFileStream(LiteCollection <LiteFileInfo <TFileId> > files, LiteCollection <BsonDocument> chunks, LiteFileInfo <TFileId> file, BsonValue fileId, FileAccess mode)
        {
            _files  = files;
            _chunks = chunks;
            _file   = file;
            _fileId = fileId;
            _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);

                if (_file.Length > 0)
                {
                    // delete all chunks before re-write
                    var count = _chunks.DeleteMany("_id BETWEEN { f: @0, n: 0 } AND { f: @0, n: 99999999 }", _fileId);

                    ENSURE(count == _file.Chunks);

                    // clear file content length+chunks
                    _file.Length = 0;
                    _file.Chunks = 0;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Delete a file inside datafile and all metadata related
        /// </summary>
        public bool Delete(TFileId id)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            // get Id as BsonValue
            var fileId = _db.Mapper.Serialize(typeof(TFileId), id);

            // remove file reference
            var deleted = _files.Delete(fileId);

            if (deleted)
            {
                // delete all chunks
                _chunks.DeleteMany("_id BETWEEN { f: @0, n: 0} AND {f: @0, n: @1 }", fileId, int.MaxValue);
            }

            return(deleted);
        }