Пример #1
0
        public string Duplicate(string id)
        {
            var lId2      = _nrwl.AcquireWriterLock($"{nameof(ChunkMap)}.{id}");
            var lId       = _nrwl.AcquireReaderLock($"{nameof(Chunk)}.{id}");
            var mapSearch = _maps.Find(x => x.ID == id).ToList();

            if (!mapSearch.Any())
            {
                _nrwl.ReleaseLock($"{nameof(ChunkMap)}.{id}", lId2);
                _nrwl.ReleaseLock($"{nameof(Chunk)}.{id}", lId);
                throw new MdbfsElementNotFoundException();
            } //not found

            var map  = mapSearch.First();
            var nMap = new ChunkMap {
                ChunksIDs = new List <string>(), Length = map.Length, Removed = false
            };
            var chunksSearch = _chunks.Find(Builders <Chunk> .Filter.Where(x => map.ChunksIDs.Contains(x.ID)));
            var nChunks      = chunksSearch.ToEnumerable().Select(ch => new Chunk {
                Bytes = ch.Bytes
            }).ToList();

            _chunks.InsertMany(nChunks);
            nChunks.ForEach(x => nMap.ChunksIDs.Add(x.ID));
            _maps.InsertOne(nMap);
            _nrwl.ReleaseLock($"{nameof(ChunkMap)}.{id}", lId2);
            _nrwl.ReleaseLock($"{nameof(Chunk)}.{id}", lId);
            return(nMap.ID);
        }
Пример #2
0
        private static (bool success, ChunkMap map) CreateNewElement(IMongoCollection <ChunkMap> maps, string id, NamedReaderWriterLock nrwl)
        {
            var map = new ChunkMap {
                ID = id, ChunksIDs = new List <string>(), Length = 0
            };
            var lId = nrwl.AcquireReaderLock($"{nameof(ChunkMap)}.{id}");

            using var session = maps.Database.Client.StartSession();
            session.StartTransaction();
            try
            {
                if (id != null)
                {
                    if (!maps.Find(x => x.ID == id).Any())
                    {
                        map.ID = id;
                    }
                    else
                    {
                        session.AbortTransaction();
                        nrwl.ReleaseLock($"{nameof(ChunkMap)}.{id}", lId);
                        return(false, null);
                    }
                }

                maps.InsertOne(map);

                session.CommitTransaction();
                nrwl.ReleaseLock($"{nameof(ChunkMap)}.{id}", lId);
                return(true, map);
            }
            catch
            {
                nrwl.ReleaseLock($"{nameof(ChunkMap)}.{id}", lId);
            }

            session.AbortTransaction();
            nrwl.ReleaseLock($"{nameof(ChunkMap)}.{id}", lId);
            return(false, null);
        }
Пример #3
0
        public Element Get(string id)
        {
            var lId        = _nrwl.AcquireReaderLock($"{nameof(Files)}.{id}");
            var elemSearch = _elements.Find(x => x.ID == id).ToList();

            _nrwl.ReleaseLock($"{nameof(Files)}.{id}", lId);
            return(!elemSearch.Any() ? null : elemSearch.First());
        }