예제 #1
0
        public void InsertMatchData(params object[] data)
        {
            Stopwatch watch = Stopwatch.StartNew();

            List<WriteModel<BsonDocument>> bulkModelList = new List<WriteModel<BsonDocument>>();
            
            string json = "";
            foreach(var d in data)
            {
                json += JSonSerializer.Serialize(d) + ",";
            }

            foreach (var item in data)
            {
                var model = BsonDocument.Parse(JSonSerializer.Serialize(item));

                var filter = Builders<BsonDocument>.Filter.Eq("HomeTeam", model["HomeTeam"].AsString) & Builders<BsonDocument>.Filter.Eq("AwayTeam", model["AwayTeam"].AsString) & Builders<BsonDocument>.Filter.Eq("Organization", model["Organization"].AsString);
                var bulkModel = new UpdateOneModel<BsonDocument>(filter, new BsonDocument { { "$set", model } });
                bulkModel.IsUpsert = true;

                bulkModelList.Add(bulkModel);
            }

            Console.WriteLine("Model generation : " + watch.ElapsedMilliseconds);
            if(bulkModelList.Count > 0)
                _dbDriver.GetCollection("MatchData").BulkWrite(bulkModelList);

            Console.WriteLine("Database operation : " + watch.ElapsedMilliseconds);

            watch.Stop();
        }
예제 #2
0
        public void BatchUpdate <T>(string collectionName, List <MongoUpdateOrAddModel> updateOrAddDataList) where T : class
        {
            try
            {
                var database = MongoUtils.CreateMongoDB(_mongoDBConnectionStr, _dataBaseName);
                UpdateOneModel <T> update;

                var collection = database.GetCollection <T>(collectionName);

                List <WriteModel <T> > list = new List <WriteModel <T> >();

                foreach (var v in updateOrAddDataList)
                {
                    update          = new UpdateOneModel <T>(v.Filter, v.Update);
                    update.IsUpsert = false;
                    list.Add(update);
                }

                collection.BulkWrite(list);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #3
0
        public void SyncStudents(District district, School school, IList<SISStudent> sisStudents)
        {
            var filter = Builders<Student>.Filter;
            var update = Builders<Student>.Update;

            // First mark all students in the school as volatile
            _collection.UpdateMany(
                filter.Eq(x => x.School.Id, school.Id),
                update.Set(x => x.Volatile, true));

            // Now update all students accordingly
            var models = new List<UpdateOneModel<Student>>();
            foreach (var sisStudent in sisStudents)
            {
                var model = new UpdateOneModel<Student>(
                    filter.Eq(student => student.ExternalId, sisStudent.Data.Id),
                    update
                        .Set(student => student.Active, true)
                        .Set(student => student.District, district)
                        .Set(student => student.ExternalId, sisStudent.Data.Id)
                        .Set(student => student.School, school)
                        .Set(student => student.SISData, sisStudent)
                        .Set(student => student.Volatile, false));
                model.IsUpsert = true;
                models.Add(model);
            }
            _collection.BulkWrite(models);

            // Lastly inactivate any old schools
            _collection.UpdateMany(
                filter.Eq(x => x.School.Id, school.Id) & filter.Eq(x => x.Volatile, true),
                update
                    .Set(x => x.Active, false)
                    .Set(x => x.Volatile, false));
        }
예제 #4
0
        public bool UpdateBulk(int BatchSize)
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();
            bool Updated = false;

            try
            {
                var builder   = Builders <LatLong> .Filter;
                var filterDef = builder.And(builder.Ne(l => l.Latitude, "0"), builder.Ne(l => l.Longitude, "0"));
                var updateDef = Builders <LatLong> .Update.Set(l => l.Latitude, "0").Set(l => l.Longitude, "0");

                var records = _coords.Find(filterDef).Limit(BatchSize).ToList();

                var bulkOps = new List <WriteModel <LatLong> >();
                foreach (var record in records)
                {
                    var upsertOne = new UpdateOneModel <LatLong>(filterDef, updateDef)
                    {
                        IsUpsert = true
                    };
                    bulkOps.Add(upsertOne);
                }
                Console.WriteLine("Time taken: " + timer.Elapsed.TotalSeconds + " seconds");
                _coords.BulkWrite(bulkOps);
                Console.WriteLine("Time taken: " + timer.Elapsed.TotalSeconds + " seconds");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            timer.Stop();
            return(Updated);
        }
예제 #5
0
        private WriteModel <ConstellationState> GetConstellationUpdate(DiffObject.ConstellationState constellationState)
        {
            if (constellationState == null)
            {
                return(null);
            }
            var cursor = constellationState.TxCursor;

            WriteModel <ConstellationState> updateModel = null;

            if (constellationState.IsInserted)
            {
                updateModel = new InsertOneModel <ConstellationState>(new ConstellationState
                {
                    TxCursor = cursor
                });
            }
            else if (constellationState.IsDeleted)
            {
                throw new InvalidOperationException("Stellar data entry cannot be deleted");
            }
            else
            {
                updateModel = new UpdateOneModel <ConstellationState>(Builders <ConstellationState> .Filter.Empty, Builders <ConstellationState> .Update.Set(s => s.TxCursor, cursor));
            }

            return(updateModel);
        }
        public async Task BulkUpdate(List <Product> productsToUpdate)
        {
            try
            {
                var models = new List <WriteModel <ProductDTO> >();

                foreach (var product in productsToUpdate)
                {
                    var filter = new ExpressionFilterDefinition <ProductDTO>(p => p.ProductId == product.ProductId);
                    var updateDefinitionList = new List <UpdateDefinition <ProductDTO> >
                    {
                        new UpdateDefinitionBuilder <ProductDTO>().Set(p => p.AvailableQuantity, product.AvailableQuantity),
                        new UpdateDefinitionBuilder <ProductDTO>().Set(p => p.ReservedQuantity, product.ReservedQuantity)
                    };

                    var action = new UpdateOneModel <ProductDTO>(filter, Builders <ProductDTO> .Update.Combine(updateDefinitionList));
                    models.Add(action);
                }

                await _collection.BulkWriteAsync(models);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Error trying to access mongo, method: {nameof(IMongoCollection<Order>.BulkWriteAsync)}");
                throw;
            }
        }
예제 #7
0
        private void RunUpdateCommand(GridFSBucket bucket, BsonDocument command)
        {
            var collectionName = command["update"].AsString;
            var collection     = bucket.Database.GetCollection <BsonDocument>(collectionName);
            var requests       = new List <WriteModel <BsonDocument> >();

            foreach (BsonDocument updateStatement in command["updates"].AsBsonArray)
            {
                var filter = updateStatement["q"].AsBsonDocument;
                var update = updateStatement["u"].AsBsonDocument;
                var upsert = updateStatement.GetValue("upsert", false).ToBoolean();
                var multi  = updateStatement.GetValue("multi", false).ToBoolean();
                WriteModel <BsonDocument> request;
                if (multi)
                {
                    request = new UpdateManyModel <BsonDocument>(filter, update)
                    {
                        IsUpsert = upsert
                    };
                }
                else
                {
                    request = new UpdateOneModel <BsonDocument>(filter, update)
                    {
                        IsUpsert = upsert
                    };
                }
                requests.Add(request);
            }
            collection.BulkWrite(requests);
        }
예제 #8
0
        public async Task <bool> UpdateWatchInfo(IEnumerable <VillageModel> models)
        {
            var listOfUpdateModels = new List <UpdateOneModel <VillageModel> >();

            foreach (var model in models)
            {
                var filter = Builders <VillageModel> .Filter.And(new[] {
                    Builders <VillageModel> .Filter.Eq("coordinateX", model.CoordinateX),
                    Builders <VillageModel> .Filter.Eq("coordinateY", model.CoordinateY)
                });

                var update = Builders <VillageModel> .Update
                             .Set(s => s.Warehourse, model.Warehourse)
                             .Set(s => s.Granary, model.Granary)
                             .Set(s => s.ResourcesProduction, model.ResourcesProduction)
                             .Set(s => s.Resources, model.Resources)
                             .Set(s => s.NextBuildingPlanExecutionTime, model.NextBuildingPlanExecutionTime)
                             .Set(s => s.IsWaitingForResources, model.IsWaitingForResources)
                             .Set(s => s.Attacks, model.Attacks);

                var updateOneModel = new UpdateOneModel <VillageModel>(filter, update)
                {
                    IsUpsert = true
                };

                listOfUpdateModels.Add(updateOneModel);
            }

            var result = await _collection.BulkWriteAsync(listOfUpdateModels);

            return(result.ModifiedCount > 0);
        }
예제 #9
0
        public async Task <bool> UpdateBaseInfos(IEnumerable <VillageModel> models)
        {
            var listOfUpdateModels = new List <UpdateOneModel <VillageModel> >();

            foreach (var model in models)
            {
                var filter = Builders <VillageModel> .Filter.And(new[] {
                    Builders <VillageModel> .Filter.Eq("coordinateX", model.CoordinateX),
                    Builders <VillageModel> .Filter.Eq("coordinateY", model.CoordinateY)
                });

                var update = Builders <VillageModel> .Update
                             .Set(s => s.VillageId, model.VillageId)
                             .Set(s => s.PlayerName, model.PlayerName)
                             .Set(s => s.VillageName, model.VillageName)
                             .Set(s => s.Alliance, model.Alliance)
                             .Set(s => s.Tribe, model.Tribe)
                             .Set(s => s.IsCapital, model.IsCapital);

                var updateOneModel = new UpdateOneModel <VillageModel>(filter, update)
                {
                    IsUpsert = true
                };

                listOfUpdateModels.Add(updateOneModel);
            }

            var result = await _collection.BulkWriteAsync(listOfUpdateModels);

            return(result.ModifiedCount > 0);
        }
예제 #10
0
        public override void AddToSet(string key, string value, double score)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }


            var filter = new BsonDocument("$and", new BsonArray
            {
                new BsonDocument(nameof(SetDto.Key), key),
                new BsonDocument(nameof(SetDto.Value), value),
                new BsonDocument("_t", nameof(SetDto)),
            });
            var update = new BsonDocument
            {
                ["$set"]         = new BsonDocument(nameof(SetDto.Score), score),
                ["$setOnInsert"] = new BsonDocument
                {
                    ["_t"] = new BsonArray {
                        nameof(BaseJobDto), nameof(ExpiringJobDto), nameof(KeyJobDto), nameof(SetDto)
                    },
                    [nameof(KeyJobDto.ExpireAt)] = BsonNull.Value
                }
            };

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update)
            {
                IsUpsert = true
            };

            _writeModels.Add(writeModel);
        }
        public virtual void SetJobParameter(string id, string name, string value)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            var filter = new BsonDocument("_id", ObjectId.Parse(id));

            BsonValue bsonValue;

            if (value == null)
            {
                bsonValue = BsonNull.Value;
            }
            else
            {
                bsonValue = value;
            }

            var update = new BsonDocument("$set", new BsonDocument($"{nameof(JobDto.Parameters)}.{name}", bsonValue));

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update);

            _writeModels.Add(writeModel);
        }
        protected virtual void SetCounter(string key, long amount, TimeSpan?expireIn)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var filter = new BsonDocument(nameof(CounterDto.Key), key);

            BsonValue bsonDate = BsonNull.Value;

            if (expireIn != null)
            {
                bsonDate = BsonValue.Create(DateTime.UtcNow.Add(expireIn.Value));
            }

            var update = new BsonDocument
            {
                ["$inc"]         = new BsonDocument(nameof(CounterDto.Value), amount),
                ["$set"]         = new BsonDocument(nameof(KeyJobDto.ExpireAt), bsonDate),
                ["$setOnInsert"] = new BsonDocument
                {
                    ["_t"] = new BsonArray {
                        nameof(BaseJobDto), nameof(ExpiringJobDto), nameof(KeyJobDto), nameof(CounterDto)
                    },
                }
            };

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update)
            {
                IsUpsert = true
            };

            _writeModels.Add(writeModel);
        }
예제 #13
0
        private async Task PopulateCategory(CancellationToken cancellationToken)
        {
            _logger?.LogDebug("Populating Category in existing events...");

            var fb = Builders <RecordedEventDocument> .Filter;

            var cursor = await _events
                         .Find(fb.Not(fb.Exists(e => e.Category)))
                         .ToCursorAsync(cancellationToken);

            var batch = new List <WriteModel <RecordedEventDocument> >();

            while (await cursor.MoveNextAsync(cancellationToken))
            {
                batch.Clear();

                foreach (var document in cursor.Current)
                {
                    var category = _streamNameResolver.AggregateName(document.Stream);

                    var definition = new UpdateOneModel <RecordedEventDocument>(
                        Builders <RecordedEventDocument> .Filter.Where(e => e.Id == document.Id),
                        Builders <RecordedEventDocument> .Update.Set(e => e.Category, category)
                        );

                    batch.Add(definition);
                }

                if (batch.Count > 0)
                {
                    await _events.BulkWriteAsync(batch, null, cancellationToken);
                }
            }
        }
예제 #14
0
        public void BulkWrite_with_UpdateOne(
            [Values(false, true)] bool async)
        {
            var subject = CreateSubject();
            var model   = new UpdateOneModel <B>(_providedFilter, "{$set: {x: 1}}")
            {
                IsUpsert = true
            };
            var options = new BulkWriteOptions();

            if (async)
            {
                subject.BulkWriteAsync(new[] { model }, options, CancellationToken.None);

                _derivedCollection.Received().BulkWriteAsync(
                    Arg.Is <IEnumerable <WriteModel <B> > >(v => v.OfType <UpdateOneModel <B> >()
                                                            .Where(m => RenderFilter(m.Filter).Equals(_expectedFilter) &&
                                                                   RenderUpdate(m.Update).Equals(BsonDocument.Parse("{$set: {x: 1}}")) &&
                                                                   m.IsUpsert == model.IsUpsert).Count() == 1),
                    options,
                    CancellationToken.None);
            }
            else
            {
                subject.BulkWrite(new[] { model }, options, CancellationToken.None);

                _derivedCollection.Received().BulkWrite(
                    Arg.Is <IEnumerable <WriteModel <B> > >(v => v.OfType <UpdateOneModel <B> >()
                                                            .Where(m => RenderFilter(m.Filter).Equals(_expectedFilter) &&
                                                                   RenderUpdate(m.Update).Equals(BsonDocument.Parse("{$set: {x: 1}}")) &&
                                                                   m.IsUpsert == model.IsUpsert).Count() == 1),
                    options,
                    CancellationToken.None);
            }
        }
        public override void PersistJob(string jobId)
        {
            var filter = CreateJobIdFilter(jobId);
            var update = new BsonDocument("$set", new BsonDocument(nameof(KeyJobDto.ExpireAt), BsonNull.Value));

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update);

            _writeModels.Add(writeModel);
        }
예제 #16
0
        private UpdateOneModel <BsonDocument> ParseUpdateOne(BsonDocument request)
        {
            var filter = new BsonDocumentFilterDefinition <BsonDocument>((BsonDocument)request["filter"]);
            var update = new BsonDocumentUpdateDefinition <BsonDocument>((BsonDocument)request["update"]);
            var model  = new UpdateOneModel <BsonDocument>(filter, update);

            model.IsUpsert = request.GetValue("upsert", false).ToBoolean();
            return(model);
        }
        /// <inheritdoc />
        public async Task <bool> Share(string requestingUserId, IShareData shareData)
        {
            var emailAddresses = shareData.EncryptedKeys.Keys;
            var userIds        = await userCollection
                                 .AsQueryable()
                                 .Where(x => emailAddresses.Contains(x.Email))
                                 .Select(x => new
            {
                Id    = x.Id,
                Email = x.Email
            })
                                 .ToListAsync();

            var mappedValues = userIds.ToDictionary(x => x.Id, x => shareData.EncryptedKeys[x.Email]);

            var models = new List <WriteModel <FileRevisionDto> >();

            foreach (var file in shareData.Files)
            {
                foreach (var revision in file.Revisions)
                {
                    var accessKeys = new List <AccessKeyDto>();
                    foreach (var kv in revision.AccessKeys)
                    {
                        var userId    = kv.Key;
                        var accessKey = kv.Value;

                        if (userId != requestingUserId)
                        {
                            throw new Exception("Invalid user");
                        }

                        accessKeys.Add(new AccessKeyDto
                        {
                            SymmetricEncryptedFileKey = accessKey.SymmetricEncryptedFileKey,
                            IssuerId = accessKey.IssuerId,
                            UserId   = userId
                        });
                    }

                    var filter = Builders <FileRevisionDto> .Filter.Eq(x => x.Id, revision.RevisionId);

                    var update = Builders <FileRevisionDto> .Update.PushEach(x => x.AccessKeys, accessKeys);

                    var upsertOne = new UpdateOneModel <FileRevisionDto>(filter, update)
                    {
                        IsUpsert = true
                    };
                    models.Add(upsertOne);
                }
            }

            var e = await fileRevisionCollection.BulkWriteAsync(models);

            return(true);
        }
예제 #18
0
        private List <WriteModel <AccountModel> > GetAccountUpdates(List <DiffObject.Account> accounts)
        {
            if (accounts == null || accounts.Count < 1)
            {
                return(null);
            }
            var filter = Builders <AccountModel> .Filter;
            var update = Builders <AccountModel> .Update;

            var accLength = accounts.Count;
            var updates   = new List <WriteModel <AccountModel> >(accLength);

            updates.AddRange(Enumerable.Repeat(default(WriteModel <AccountModel>), accLength));

            Parallel.For(0, accLength, (i) =>
            {
                var acc = accounts[i];
                var currentAccFilter = filter.Eq(a => a.Id, acc.Id);
                if (acc.IsInserted)
                {
                    updates[i] = new InsertOneModel <AccountModel>(new AccountModel
                    {
                        Id                = acc.Id,
                        Nonce             = acc.Nonce,
                        PubKey            = acc.PubKey,
                        RequestRateLimits = acc.RequestRateLimits,
                        Withdrawal        = (acc.Withdrawal ?? 0)
                    });
                }
                else if (acc.IsDeleted)
                {
                    updates[i] = new DeleteOneModel <AccountModel>(currentAccFilter);
                }
                else
                {
                    var updateDefs = new List <UpdateDefinition <AccountModel> >();
                    if (acc.Nonce != 0)
                    {
                        updateDefs.Add(update.Set(a => a.Nonce, acc.Nonce));
                    }

                    if (acc.RequestRateLimits != null)
                    {
                        updateDefs.Add(update.Set(a => a.RequestRateLimits, acc.RequestRateLimits));
                    }

                    if (acc.Withdrawal.HasValue)
                    {
                        updateDefs.Add(update.Set(a => a.Withdrawal, acc.Withdrawal.Value));
                    }

                    updates[i] = new UpdateOneModel <AccountModel>(currentAccFilter, update.Combine(updateDefs));
                }
            });
            return(updates);
        }
예제 #19
0
        private void DeleteEventsFromStorageId(string id, BsonDocument[] events)
        {
            var filter = Builders <BsonDocument> .Filter.Eq("_id", id);

            var delete = Builders <BsonDocument> .Update.PullAll("Events", events);

            var model = new UpdateOneModel <BsonDocument>(filter, delete);

            writeModels.Add(model);
        }
예제 #20
0
        private void InsertEventToPositionByStorageId(string id, BsonDocument e, int pos)
        {
            var filter = Builders <BsonDocument> .Filter.Eq("_id", id);

            var insert = Builders <BsonDocument> .Update.PushEach("Events", new[] { e }, null, pos);

            var model = new UpdateOneModel <BsonDocument>(filter, insert);

            writeModels.Add(model);
        }
        public override void ExpireJob(string jobId, TimeSpan expireIn)
        {
            var filter = CreateJobIdFilter(jobId);
            var update = new BsonDocument("$set",
                                          new BsonDocument(nameof(KeyJobDto.ExpireAt), DateTime.UtcNow.Add(expireIn)));

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update);

            _writeModels.Add(writeModel);
        }
예제 #22
0
        private WriteModel <AccountModel>[] GetAccountUpdates(List <DiffObject.Account> accounts)
        {
            if (accounts == null || accounts.Count < 1)
            {
                return(null);
            }
            var filter = Builders <AccountModel> .Filter;
            var update = Builders <AccountModel> .Update;

            var accLength = accounts.Count;
            var updates   = new WriteModel <AccountModel> [accLength];

            for (int i = 0; i < accLength; i++)
            {
                var acc = accounts[i];
                var currentAccFilter = filter.Eq(a => a.Id, acc.Id);
                if (acc.IsInserted)
                {
                    updates[i] = new InsertOneModel <AccountModel>(new AccountModel
                    {
                        Id                = acc.Id,
                        Nonce             = acc.Nonce,
                        PubKey            = acc.PubKey,
                        RequestRateLimits = acc.RequestRateLimits,
                        Withdrawal        = (acc.Withdrawal.HasValue ? acc.Withdrawal.Value : 0)
                    });
                }
                else if (acc.IsDeleted)
                {
                    updates[i] = new DeleteOneModel <AccountModel>(currentAccFilter);
                }
                else
                {
                    var updateDefs = new List <UpdateDefinition <AccountModel> >();
                    if (acc.Nonce != 0)
                    {
                        updateDefs.Add(update.Set(a => a.Nonce, acc.Nonce));
                    }

                    if (acc.RequestRateLimits != null)
                    {
                        updateDefs.Add(update.Set(a => a.RequestRateLimits, acc.RequestRateLimits));
                    }

                    if (acc.Withdrawal.HasValue)
                    {
                        updateDefs.Add(update.Set(a => a.Withdrawal, acc.Withdrawal.Value));
                    }

                    updates[i] = new UpdateOneModel <AccountModel>(currentAccFilter, update.Combine(updateDefs));
                }
            }
            return(updates);
        }
예제 #23
0
        public async Task Import(IEnumerable <EmployeeCourse> entities)
        {
            var models = new List <WriteModel <EmployeeCourse> >();

            if (entities != null && entities.Any())
            {
                foreach (var entity in entities)
                {
                    await InitializeInsertModel(entity);

                    Expression <Func <EmployeeCourse, bool> > filter = x => x.DefCode == entity.DefCode;
                    var updater = Builders <EmployeeCourse> .Update
                                  .Set(x => x.EmployeeCode, entity.EmployeeCode)
                                  .Set(x => x.FacutlyCode, entity.FacutlyCode)
                                  .Set(x => x.CourseCode, entity.CourseCode)
                                  .Set(x => x.MajorCode, entity.MajorCode)
                                  .Set(x => x.TimeStart, entity.TimeStart)
                                  .Set(x => x.TimeEnd, entity.TimeEnd)
                                  .Set(x => x.Start, entity.Start)
                                  .Set(x => x.End, entity.End)
                                  .Set(x => x.CourseTime, entity.CourseTime)
                                  .Set(x => x.CourseStart, entity.CourseStart)
                                  .Set(x => x.CourseEnd, entity.CourseEnd)
                                  .Set(x => x.MajorCode, entity.MajorCode)
                                  .Set(x => x.ValueToSearch, entity.ValueToSearch)
                                  .Set(x => x.CreatedDate, entity.CreatedDate)
                                  .Set(x => x.IsPublished, entity.IsPublished)
                                  .Set(x => x.LastModified, entity.LastModified);

                    var model = new UpdateOneModel <EmployeeCourse>(filter, updater)
                    {
                        IsUpsert = true
                    };
                    models.Add(model);
                    if (models.Count >= 1000)
                    {
                        await _context.EmployeeCourseRepository.BulkWrite(models);

                        models.Clear();
                    }
                }
                if (models.Count > 0)
                {
                    await _context.EmployeeCourseRepository.BulkWrite(models);

                    models.Clear();
                }
            }
        }
        public override void ExpireHash(string key, TimeSpan expireIn)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var filter = new BsonDocument(nameof(KeyJobDto.Key), key);

            var update = new BsonDocument("$set",
                                          new BsonDocument(nameof(HashDto.ExpireAt), DateTime.UtcNow.Add(expireIn)));
            var writeModel = new UpdateOneModel <BsonDocument>(filter, update);

            _writeModels.Add(writeModel);
        }
예제 #25
0
        public async Task UpdateManyAsync(ICollection <TDocument> documents, CancellationToken cancellationToken = default)
        {
            var writeModels = new List <WriteModel <TDocument> >();

            foreach (var doc in documents)
            {
                var filter = Builders <TDocument> .Filter.Eq(doc => doc.Id, doc.Id);

                var writeModel = new UpdateOneModel <TDocument>(filter, new ObjectUpdateDefinition <TDocument>(doc));

                writeModels.Add(writeModel);
            }

            await Collection.BulkWriteAsync(writeModels, new BulkWriteOptions(), cancellationToken);
        }
        public override void PersistHash(string key)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var filter = new BsonDocument(nameof(KeyJobDto.Key), key);

            var update = new BsonDocument("$set",
                                          new BsonDocument(nameof(HashDto.ExpireAt), BsonNull.Value));

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update);

            _writeModels.Add(writeModel);
        }
예제 #27
0
        public static async Task <bool> UpdateAsync <TEntity>(this IMongoDatabase database,
                                                              UpdateOneModel <TEntity> update, string collectionName)
        {
            try
            {
                await database.GetCollection <TEntity>(collectionName).UpdateOneAsync(update.Filter, update.Update);

                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(e);
            }

            return(false);
        }
예제 #28
0
        public IEnumerable <WriteModel <DbCachedEntry> > GetModel()
        {
            var filter = Builders <DbCachedEntry> .Filter.Eq(e => e.CacheKey, Entry.CacheKey);

            var updateDefinition = Builders <DbCachedEntry> .Update
                                   .Set(e => e.CacheKey, Entry.CacheKey)
                                   .Set(e => e.Expiry, Entry.Expiry)
                                   .Set(e => e.Value, Entry.Value);

            var model = new UpdateOneModel <DbCachedEntry>(filter, updateDefinition)
            {
                IsUpsert = true
            };

            yield return(model);
        }
예제 #29
0
        private List <WriteModel <OrderModel> > GetOrderUpdates(List <DiffObject.Order> orders)
        {
            if (orders == null || orders.Count < 1)
            {
                return(null);
            }
            unchecked
            {
                var filter = Builders <OrderModel> .Filter;
                var update = Builders <OrderModel> .Update;

                var ordersLength = orders.Count;
                var updates      = new List <WriteModel <OrderModel> >(ordersLength);
                updates.AddRange(Enumerable.Repeat(default(WriteModel <OrderModel>), ordersLength));

                Parallel.For(0, ordersLength, (i) =>
                {
                    var order = orders[i];

                    var currentOrderFilter = filter.And(filter.Eq(s => s.Id, (long)order.OrderId));

                    if (order.IsInserted)
                    {
                        updates[i] = new InsertOneModel <OrderModel>(new OrderModel
                        {
                            Id          = (long)order.OrderId,
                            Amount      = order.AmountDiff,
                            QuoteAmount = order.QuoteAmountDiff,
                            Price       = order.Price,
                            Account     = order.Account
                        });
                    }
                    else if (order.IsDeleted)
                    {
                        updates[i] = new DeleteOneModel <OrderModel>(currentOrderFilter);
                    }
                    else
                    {
                        updates[i] = new UpdateOneModel <OrderModel>(
                            currentOrderFilter,
                            update.Inc(b => b.Amount, order.AmountDiff).Inc(b => b.QuoteAmount, order.QuoteAmountDiff)
                            );
                    }
                });
                return(updates);
            }
        }
        public override void AddJobState(string jobId, IState state)
        {
            var filter   = CreateJobIdFilter(jobId);
            var stateDto = new StateDto
            {
                Name      = state.Name,
                Reason    = state.Reason,
                CreatedAt = DateTime.UtcNow,
                Data      = state.SerializeData()
            }.ToBsonDocument();

            var update = new BsonDocument("$push", new BsonDocument(nameof(JobDto.StateHistory), stateDto));

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update);

            _writeModels.Add(writeModel);
        }
        public override void SetRangeInHash(string key, IEnumerable <KeyValuePair <string, string> > keyValuePairs)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (keyValuePairs == null)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            var fields = new BsonDocument();

            foreach (var pair in keyValuePairs)
            {
                var field = pair.Key;
                var value = pair.Value;
                fields[$"{nameof(HashDto.Fields)}.{field}"] = value;
            }

            var update = new BsonDocument
            {
                ["$set"]         = fields,
                ["$setOnInsert"] = new BsonDocument
                {
                    ["_t"] = new BsonArray {
                        nameof(BaseJobDto), nameof(ExpiringJobDto), nameof(KeyJobDto), nameof(HashDto)
                    },
                    [nameof(HashDto.ExpireAt)] = BsonNull.Value
                }
            };

            var filter = new BsonDocument("$and", new BsonArray
            {
                new BsonDocument(nameof(HashDto.Key), key),
                new BsonDocument("_t", nameof(HashDto))
            });

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update)
            {
                IsUpsert = true
            };

            _writeModels.Add(writeModel);
        }
        public override void AddToSet(string key, string value, double score)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var filter = CreateSetFilter(key, value);
            var update = CreateSetUpdate(value, score);

            var writeModel = new UpdateOneModel <BsonDocument>(filter, update)
            {
                IsUpsert = true
            };

            _writeModels.Add(writeModel);
        }
예제 #33
0
        public void AddDataSource(object stardardData, params object[] data)
        {
            Stopwatch watch = Stopwatch.StartNew();

            List<WriteModel<BsonDocument>> bulkModelList = new List<WriteModel<BsonDocument>>();

            BsonDocument sData = null;
            if (stardardData != null)
            {
                sData = stardardData as BsonDocument;
                if (sData == null)
                {
                    sData = BsonDocument.Parse(JSonSerializer.Serialize(stardardData));
                }
            }

            foreach (var item in data)
            {
                var model = BsonDocument.Parse(JSonSerializer.Serialize(item));
                if (stardardData != null)
                {
                    model.AddRange(sData);
                }

                var filter = Builders<BsonDocument>.Filter.Eq("source", model["source"].AsString);
                var bulkModel = new UpdateOneModel<BsonDocument>(filter, new BsonDocument { { "$set", model } });
                bulkModel.IsUpsert = true;

                bulkModelList.Add(bulkModel);
            }
            
            Console.WriteLine("Model generation : " + watch.ElapsedMilliseconds);

            _dbDriver.GetCollection("DataSource").BulkWrite(bulkModelList);

            Console.WriteLine("Database operation : " + watch.ElapsedMilliseconds);

            watch.Stop();
        }
예제 #34
0
        public void UpdateStatus(List<Tuple<string, string>> data)
        {
            Stopwatch watch = Stopwatch.StartNew();

            List<WriteModel<BsonDocument>> bulkModelList = new List<WriteModel<BsonDocument>>();
            
            foreach (var item in data)
            {
                var filter = Builders<BsonDocument>.Filter.Eq("source", item.Item1);
                var bulkModel = new UpdateOneModel<BsonDocument>(filter, new BsonDocument { { "$set", new BsonDocument { { "status" , item.Item2 } } } });
                bulkModel.IsUpsert = true;

                bulkModelList.Add(bulkModel);
            }

            Console.WriteLine("Model generation : " + watch.ElapsedMilliseconds);

            if(bulkModelList.Count > 0)
                _dbDriver.GetCollection("DataSource").BulkWrite(bulkModelList);

            Console.WriteLine("Database operation : " + watch.ElapsedMilliseconds);

            watch.Stop();
        }
예제 #35
0
 private UpdateOneModel<BsonDocument> ParseUpdateOne(BsonDocument request)
 {
     var filter = new BsonDocumentFilterDefinition<BsonDocument>((BsonDocument)request["filter"]);
     var update = new BsonDocumentUpdateDefinition<BsonDocument>((BsonDocument)request["update"]);
     var model = new UpdateOneModel<BsonDocument>(filter, update);
     model.IsUpsert = request.GetValue("upsert", false).ToBoolean();
     return model;
 }
예제 #36
0
        public static void ConvertDateTime()
        {
            List<WriteModel<BsonDocument>> bulkModelList = new List<WriteModel<BsonDocument>>();

            var _client = new MongoClient("mongodb://*****:*****@ds055935.mlab.com:55935/websuctioner");
            var _dataSources = _client.GetDatabase("websuctioner").GetCollection<BsonDocument>("DataSource").Find<BsonDocument>(new BsonDocument()).ToList();

            foreach (var dataSource in _dataSources)
            {
                var filter = Builders<BsonDocument>.Filter.Eq("source", dataSource["source"].AsString);

                string[] dateSplit = dataSource["custom"].AsBsonDocument["Date"].AsString.Split(new char[] { '.', '-', ':', '/', '\\', ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] timeSplit = dataSource["custom"].AsBsonDocument["Time"].AsString.Split(new char[] { '.', '-', ':', '/', '\\', ',' }, StringSplitOptions.RemoveEmptyEntries);

                DateTime date = DateTime.MinValue;
                try
                {
                    if (dateSplit.Length == 3)
                    {
                        date = new DateTime(int.Parse(dateSplit[2]), int.Parse(dateSplit[1]), int.Parse(dateSplit[0]));

                        if (timeSplit.Length == 2)
                        {
                            int minutes = int.Parse(timeSplit[0]) * 60 + int.Parse(timeSplit[1]);
                            date = date.AddMinutes(minutes);
                        }
                    }
                }
                catch { }
                var update = Builders<BsonDocument>.Update.Set("custom.DateTime", date);
                var bulkModel = new UpdateOneModel<BsonDocument>(filter, update);
                bulkModel.IsUpsert = true;

                bulkModelList.Add(bulkModel);
            }

            _client.GetDatabase("websuctioner").GetCollection<BsonDocument>("DataSource").BulkWrite(bulkModelList);
        }
 private Task RunUpdateCommandAsync(GridFSBucket bucket, BsonDocument command)
 {
     var collectionName = command["update"].AsString;
     var collection = bucket.Database.GetCollection<BsonDocument>(collectionName);
     var requests = new List<WriteModel<BsonDocument>>();
     foreach (BsonDocument updateStatement in command["updates"].AsBsonArray)
     {
         var filter = updateStatement["q"].AsBsonDocument;
         var update = updateStatement["u"].AsBsonDocument;
         var upsert = updateStatement.GetValue("upsert", false).ToBoolean();
         var multi = updateStatement.GetValue("multi", false).ToBoolean();
         WriteModel<BsonDocument> request;
         if (multi)
         {
             request = new UpdateManyModel<BsonDocument>(filter, update) { IsUpsert = upsert };
         }
         else
         {
             request = new UpdateOneModel<BsonDocument>(filter, update) { IsUpsert = upsert };
         }
         requests.Add(request);
     }
     return collection.BulkWriteAsync(requests);
 }