예제 #1
0
        public async Task <Element> RemoveAsync(string id, bool permanently)
        {
            var lId = await _nrwl.AcquireWriterLockAsync($"{nameof(Files)}.{id}");

            Element f = null;

            try
            {
                if (permanently)
                {
                    await _elements.FindOneAndDeleteAsync(x => x.ID == id);

                    var lId2 = await _nrwl.AcquireWriterLockAsync($"{nameof(ChunkMap)}.{id}");

                    await _binaryStorage._maps.UpdateOneAsync(x => x.ID == id, Builders <ChunkMap> .Update.Set(x => x.Removed, true));

                    await _nrwl.ReleaseLockAsync($"{nameof(ChunkMap)}.{id}", lId2);
                }
                else
                {
                    var elemSearch = (await _elements.FindAsync(x => x.ID == id)).ToList();
                    if (elemSearch.Count != 0) //element found
                    {
                        var e = f = elemSearch.First();
                        var originalLocationNames = "";
                        var originalLocationIDs   = "";
                        var deleted = DateTime.Now;
                        do
                        {
                            var parentSearch = (await _elements.FindAsync(x => x.ID == e.ParentID)).ToList();
                            if (!parentSearch.Any())
                            {
                                await _nrwl.ReleaseLockAsync($"{nameof(Files)}.{id}", lId);

                                throw new MdbfsElementNotFoundException(nameof(id));
                            }
                            e = parentSearch.First();
                            originalLocationNames = e.Name + '/' + originalLocationNames;
                            originalLocationIDs   = e.ID + '/' + originalLocationIDs;
                        } while (e.ParentID != null);

                        f.Opened   = deleted;
                        f.Modified = deleted;
                        f.Removed  = true;
                        f.Metadata[nameof(EMetadataKeys.PathNames)] = originalLocationNames;
                        f.Metadata[nameof(EMetadataKeys.PathIDs)]   = originalLocationIDs;
                        f.Metadata[nameof(EMetadataKeys.Deleted)]   = deleted;
                        await _elements.FindOneAndReplaceAsync(x => x.ID == id, f);
                    }
                }
            }
            catch (Exception)
            {
                await _nrwl.ReleaseLockAsync($"{nameof(Files)}.{id}", lId);
            }

            await _nrwl.ReleaseLockAsync($"{nameof(Files)}.{id}", lId);

            return(f);
        }
예제 #2
0
        internal async Task <string> DuplicateAsync(string id)
        {
            var lId2 = await _nrwl.AcquireWriterLockAsync($"{nameof(ChunkMap)}.{id}");

            var mapSearch = (await _maps.FindAsync(x => x.ID == id)).ToList();

            if (!mapSearch.Any())
            {
                await _nrwl.ReleaseLockAsync($"{nameof(ChunkMap)}.{id}", lId2);

                throw new MdbfsElementNotFoundException();
            } //not found

            var map  = mapSearch.First();
            var nMap = new ChunkMap {
                ChunksIDs = new List <string>(), Length = map.Length, Removed = false
            };
            var chunksSearch = await _chunks.FindAsync(Builders <Chunk> .Filter.Where(x => map.ChunksIDs.Contains(x.ID)));

            var nChunks = new List <Chunk>();

            foreach (var ch in chunksSearch.ToEnumerable())
            {
                nChunks.Add(new Chunk {
                    Bytes = ch.Bytes
                });
            }

            await _chunks.InsertManyAsync(nChunks);

            nChunks.ForEach(x => nMap.ChunksIDs.Add(x.ID));
            await _maps.InsertOneAsync(nMap);

            await _nrwl.ReleaseLockAsync($"{nameof(ChunkMap)}.{id}", lId2);

            return(nMap.ID);
        }
예제 #3
0
        public static async Task <(bool success, BinaryUploadStream stream)> OpenAsync(IMongoCollection <ChunkMap> maps,
                                                                                       IMongoCollection <Chunk> chunks, int maxChunkLenght, string id, NamedReaderWriterLock namedReaderWriterLock)
        {
            if (id != null)
            {
            }
            var(success, map) = await CreateNewElementAsync(maps, id, namedReaderWriterLock);

            if (!success)
            {
                return(false, null);
            }

            var stream = new BinaryUploadStream(maps, chunks, maxChunkLenght, namedReaderWriterLock)
            {
                _map = map
            };

            stream._nrwlID = await namedReaderWriterLock.AcquireWriterLockAsync($"{nameof(Chunk)}.{map.ID}");

            return(true, stream);
        }