Exemple #1
0
        public async Task <AddAddPacketPartResult> AddIfNotExistsPacketPartAsync(AgentIdentifierData agentId, AddPacketPartRequest request, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentException("request");
            }

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

            try
            {
                await this.UpdateLeaseIfNeeded();

                await this.chunks.InsertOneAsync(this.Get(request));

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

                return(AddAddPacketPartResult.SuccessResult(request));
            }
        }
Exemple #2
0
        public async Task <AddAddPacketsPartsResult> AddIfNotExistsPacketsPartsAsync(AgentIdentifierData 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);
            }

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

                return(AddAddPacketsPartsResult.CreateResult(this.storageToken, requests.Select(request => AddAddPacketPartResult.SuccessResult(request))));
            }
            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);
                    results.Add(rqNonDupKeyErrors.Any() ? AddAddPacketPartResult.FailResult(request) : AddAddPacketPartResult.SuccessResult(request));
                }

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