コード例 #1
0
ファイル: MongoDbPacketsStore.cs プロジェクト: gonsovsky/job
        public async Task <AddAddPacketPartResult> AddIfNotExistsPacketPartAsync(AgentIdentity agentId, AddPacketPartRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentException("request");
            }

            if (!this.initialized)
            {
                await this.InitAsync(cancellationToken);
            }

            var doc = this.Get(agentId, request);

            try
            {
                await this.chunks.InsertOneAsync(doc, null, cancellationToken);

                return(AddAddPacketPartResult.SuccessResult(request, this.storageToken, doc.Id));
            }
            catch (MongoWriteException ex)
            {
                // Instrumentation ???
                var nonDupKeyExceptions = ex.WriteError.Category != ServerErrorCategory.DuplicateKey;
                if (nonDupKeyExceptions)
                {
                    return(AddAddPacketPartResult.FailResult(request));
                }

                // пока не удастся получить id для документа который зафейлился из exception-а
                // https://jira.mongodb.org/browse/SERVER-4637
                var id = await this.chunks.Find(x => x.PacketId == doc.PacketId && x.StartPosition == doc.StartPosition).Project(x => x.Id).FirstOrDefaultAsync(cancellationToken);

                if (id == null)
                {
                    return(AddAddPacketPartResult.FailResult(request));
                }
                return(AddAddPacketPartResult.SuccessResult(request, this.storageToken, id));
            }
        }
コード例 #2
0
ファイル: MongoDbPacketsStore.cs プロジェクト: gonsovsky/job
        public async Task <AddAddPacketsPartsResult> AddIfNotExistsPacketsPartsAsync(AgentIdentity agentId, IList <AddPacketPartRequest> requests, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (requests == null)
            {
                throw new ArgumentException("requests");
            }

            if (!requests.Any())
            {
                return(AddAddPacketsPartsResult.EmptyResult());
            }

            if (!this.initialized)
            {
                await this.InitAsync(cancellationToken);
            }

            var stopWatch = Stopwatch.StartNew();

            // IsOrdered = false, неупорядоченные операции - при выполнении отдельных операций они выполняются не упорядочено и они не останавливают исполнение остальных операций
            try
            {
                var docs = requests.Select(x => this.Get(agentId, x)).ToList();
                await this.chunks.InsertManyAsync(docs, new InsertManyOptions()
                {
                    IsOrdered = false
                }, cancellationToken);

                stopWatch.Stop();
                return(AddAddPacketsPartsResult.CreateResult(this.storageToken, requests.Select((request, i) => {
                    return AddAddPacketPartResult.SuccessResult(request, this.storageToken, docs[i].Id);
                })));
            }
            catch (MongoBulkWriteOperationException ex)
            {
                // Instrumentation ???
                var results = new List <AddAddPacketPartResult>();
                for (int i = 0; i < requests.Count; i++)
                {
                    var request           = requests[i];
                    var rqNonDupKeyErrors = ex.WriteErrors.Where(x => x.Index == i && x.Category != ServerErrorCategory.DuplicateKey);
                    if (rqNonDupKeyErrors.Any())
                    {
                        results.Add(AddAddPacketPartResult.FailResult(request));
                    }
                    else
                    {
                        // TODO оптимизировать, не делать запрос на каждый fail вариант
                        var id = await this.chunks.Find(x => x.PacketId == request.PacketId && x.StartPosition == request.StartPosition).Project(x => x.Id).FirstOrDefaultAsync(cancellationToken);

                        if (id == null)
                        {
                            results.Add(AddAddPacketPartResult.FailResult(request));
                        }
                        else
                        {
                            results.Add(AddAddPacketPartResult.SuccessResult(request, this.storageToken, id));
                        }
                    }
                }

                //var failed = new List<AddAddPacketPartResult>();
                //var reqToSelect = new List<AddPacketPartRequest>();
                //for (int i = 0; i < requests.Count; i++)
                //{
                //    var request = requests[i];
                //    var rqNonDupKeyErrors = ex.WriteErrors.Where(x => x.Index == i && x.Category != ServerErrorCategory.DuplicateKey);
                //    if (rqNonDupKeyErrors.Any())
                //    {
                //        failed.Add(AddAddPacketPartResult.FailResult(request));
                //    }
                //    else
                //    {
                //        reqToSelect.Add(request);
                //    }
                //}

                //// select
                //Expression<Func<PacketPart, bool>> filter = null;

                //var f = Builders<PacketPart>.Filter
                //    .Or(reqToSelect.Select(r =>))

                //FilterDefinition<PacketPart> filterDef =
                //var ids = await this.chunks.Find(x => x.PacketId == request.PacketId && x.StartPosition == request.StartPosition).Project(x => x.Id).FirstOrDefaultAsync(cancellationToken);

                stopWatch.Stop();
                return(AddAddPacketsPartsResult.CreateResult(this.storageToken, results));
            }
        }