Exemple #1
0
        public void BulkWrite_with_ReplaceOne(
            [Values(false, true)] bool async)
        {
            var subject     = CreateSubject();
            var replacement = new B();
            var model       = new ReplaceOneModel <B>(_providedFilter, replacement)
            {
                IsUpsert = true
            };
            var options = new BulkWriteOptions();

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

                _mockDerivedCollection.Verify(
                    c => c.BulkWriteAsync(
                        It.Is <IEnumerable <WriteModel <B> > >(v => v.OfType <ReplaceOneModel <B> >()
                                                               .Where(m => RenderFilter(m.Filter).Equals(_expectedFilter) &&
                                                                      m.Replacement == model.Replacement &&
                                                                      m.IsUpsert == model.IsUpsert).Count() == 1),
                        options,
                        CancellationToken.None),
                    Times.Once);
            }
            else
            {
                subject.BulkWrite(new[] { model }, options, CancellationToken.None);

                _mockDerivedCollection.Verify(
                    c => c.BulkWrite(
                        It.Is <IEnumerable <WriteModel <B> > >(v => v.OfType <ReplaceOneModel <B> >()
                                                               .Where(m => RenderFilter(m.Filter).Equals(_expectedFilter) &&
                                                                      m.Replacement == model.Replacement &&
                                                                      m.IsUpsert == model.IsUpsert).Count() == 1),
                        options,
                        CancellationToken.None),
                    Times.Once);
            }
        }
 private void InitMyComponents()
 {
     btnregister.Clicked += async (sender, e) =>
      {
          if (txtRegSifre.Text != txtRegSifreTekrar.Text)
          {
              await DisplayAlert("Kayıt hatası", "Şifreler uyumsuz", "Ok");
              return;
          }
          var yeniKullanici = new Kullanici()
          {
              KullaniciAdi = txtRegKullaniciAdi.Text,
              Sifre = txtRegSifre.Text
          };
          if (!yeniKullanici.IsValid())
          {
              await DisplayAlert("Kayıt hatası", "Kullanıcı adı veya şifre kurallara uymamaktadır", "Ok");
          }
          else
          {
              var wm = new WriteModel<Kullanici>[1];
              wm[0] = new ReplaceOneModel<Kullanici>(new BsonDocument("_id", yeniKullanici.Id), yeniKullanici) { IsUpsert = true };
              collection.BulkWrite(wm);
              await DisplayAlert("Kayıt Başarılı", $"Hoşgeldin {yeniKullanici.KullaniciAdi} giriş yapabilirsin", "Ok");
              swRegister.IsToggled = false;
          }
      };
     btnLogin.Clicked += async (sender, e) =>
     {
         var kullanici = collection.AsQueryable().FirstOrDefault(x =>
             x.KullaniciAdi == txtKullaniciAdi.Text && x.Sifre == txtSifre.Text);
         
         if (kullanici == null)
         {
             await DisplayAlert("Giriş hatası", "Kullanıcı adı veya şifre hatalı", "Ok");
             return;
         }
         await DisplayAlert("Giriş Başarılı", $"Hoşgeldin {kullanici.KullaniciAdi}", "Ok");
     };
 }
Exemple #3
0
        public bool UpdateMany(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)
                {
                    record.Latitude  = "0";
                    record.Longitude = "0";
                    var upsertOne = new ReplaceOneModel <LatLong>(
                        Builders <LatLong> .Filter.Where(l => l.Id == record.Id),
                        record)
                    {
                        IsUpsert = true
                    };
                    bulkOps.Add(upsertOne);
                }
                Console.WriteLine("Time taken: " + timer.Elapsed.TotalSeconds + " seconds");
                //_coords.UpdateMany(recodrs, filterDef, updateDef);
                Console.WriteLine("Time taken: " + timer.Elapsed.TotalSeconds + " seconds");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            timer.Stop();
            return(Updated);
        }
Exemple #4
0
        public async Task PutChaptersAsync(List <ChapterDto> chapters)
        {
            if (!Context.Database.ListCollectionNames().ToList().Any(x => x.Contains("Chapters")))
            {
                Context.Database.CreateCollection("Chapters");
            }
            var collection = Context.Database.GetCollection <ChapterDto>("Chapters");
            var models     = new WriteModel <ChapterDto> [chapters.Count];

            for (var i = 0; i < chapters.Count; i++)
            {
                var item = chapters[i];
                models[i] = new ReplaceOneModel <ChapterDto>(
                    Builders <ChapterDto> .Filter.Where(x => x._id == item._id),
                    (ChapterDto)item
                    )
                {
                    IsUpsert = true
                };
            }
            ;
            await collection.BulkWriteAsync(models);
        }
Exemple #5
0
        public void BulkInsert(IEnumerable <T> items)
        {
            string keyname = "_id";

            #region Get Primary Key Name
            PropertyInfo[] props = typeof(T).GetProperties();

            foreach (PropertyInfo prop in props)
            {
                object[] attrs = prop.GetCustomAttributes(true);
                foreach (object attr in attrs)
                {
                    BsonIdAttribute authAttr = attr as BsonIdAttribute;
                    if (authAttr != null)
                    {
                        keyname = prop.Name;
                    }
                }
            }
            #endregion

            var bulkOps = new List <WriteModel <T> >();


            foreach (var entry in items)
            {
                var filter = Builders <T> .Filter.Eq(keyname, entry.GetType().GetProperty(keyname).GetValue(entry, null));

                var upsertOne = new ReplaceOneModel <T>(filter, entry)
                {
                    IsUpsert = true
                };

                bulkOps.Add(upsertOne);
            }
            _items.BulkWrite(bulkOps);
        }
Exemple #6
0
        /// <summary>
        /// Executes a query to replace many storage models (for Update or AddOrUpdate many).
        /// </summary>
        /// <typeparam name="TId">The type of the identifier.</typeparam>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <typeparam name="TMetadata">The type of the metadata.</typeparam>
        /// <param name="database">The database.</param>
        /// <param name="models">The set of storage models to write.</param>
        /// <param name="collectionName">Name of the collection.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="isUpsert">
        /// If set to <c>true</c> models will be defined as upserts (AddOrUpdate); otherwise they
        /// will be defined as updates.
        /// </param>
        /// <returns>The bulk write task results.</returns>
        /// <exception cref="System.ArgumentNullException">If models is null.</exception>
        private static async Task <BulkWriteResult <BsonDocument> > ReplaceManyAsync <TId, TModel, TMetadata>(
            MongoDatabase database,
            IDictionary <TId, StorageModel <TModel, TMetadata> > models,
            string collectionName,
            CancellationToken cancellationToken,
            bool isUpsert)
        {
            if (models == null)
            {
                throw new ArgumentNullException(nameof(models));
            }

            var modelTypeName = string.IsNullOrWhiteSpace(collectionName) ? typeof(TModel).Name : collectionName;

            var client     = database.CreateClient();
            var collection = client.GetCollection <BsonDocument>(modelTypeName);

            var updateModels = new List <WriteModel <BsonDocument> >();

            foreach (var keyValue in models)
            {
                var id = IdReader.ReadValue(keyValue.Value.Model);
                keyValue.Value.Id = id;
                var filter  = new BsonDocument("_id", BsonValue.Create(id));
                var bsonDoc = keyValue.Value.ToBsonDocument();

                var replaceOne = new ReplaceOneModel <BsonDocument>(filter, bsonDoc)
                {
                    IsUpsert = isUpsert
                };
                updateModels.Add(replaceOne);
            }

            var results = await collection.BulkWriteAsync(updateModels, null, cancellationToken);

            return(results);
        }
Exemple #7
0
        public Task AddOrUpdateManyAsync(IEnumerable <StudentOpticalForm> forms)
        {
            if (forms == null || !forms.Any())
            {
                return(Task.CompletedTask);
            }

            var writeModels = new List <WriteModel <StudentOpticalForm> >(forms.Count());

            foreach (var form in forms)
            {
                var model = new ReplaceOneModel <StudentOpticalForm>(
                    Builders <StudentOpticalForm> .Filter.Eq(x => x.ExamId, form.ExamId) &
                    Builders <StudentOpticalForm> .Filter.Eq(x => x.StudentId, form.StudentId),
                    form)
                {
                    IsUpsert = true,
                };

                writeModels.Add(model);
            }

            return(_context.StudentOpticalForms.BulkWriteAsync(writeModels));
        }
Exemple #8
0
        public async Task AddShowsAsync(IList <TvShow> shows)
        {
            var models = new WriteModel <TvShow> [shows.Count];

            //ReplaceOneModel with property IsUpsert set to true to upsert whole documents
            for (var i = 0; i < shows.Count; i++)
            {
                models[i] = new ReplaceOneModel <TvShow>(new BsonDocument(_idColumn, shows[i].Id), shows[i])
                {
                    IsUpsert = true
                };
            }
            ;

            try
            {
                await _context.Shows.BulkWriteAsync(models);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                throw;
            }
        }
Exemple #9
0
        public virtual void Down(IMongoDatabase db)
        {
            var bulk       = new List <WriteModel <BsonDocument> >();
            var collection = db.GetCollection <BsonDocument>(CollectionName);

            collection.FindSync(_ => true).ToList().ForEach(document =>
            {
                Down(document);

                var update = new ReplaceOneModel <BsonDocument>(
                    new BsonDocument {
                    { "_id", document["_id"] }
                },
                    document
                    );

                bulk.Add(update);
            });

            if (bulk.Count > 0)
            {
                collection.BulkWrite(bulk);
            }
        }
Exemple #10
0
        public Task UpdateManyAsync <T>(IEnumerable <T> items)
            where T : class, IDataModel, new()
        {
            var requests  = new List <ReplaceOneModel <T> >();
            var itemsList = items.ToList();

            foreach (var item in itemsList)
            {
                if (item.Id == default)
                {
                    item.Id = Guid.NewGuid();
                }

                var filter = Builders <T> .Filter.Eq(x => x.Id, item.Id);

                var request = new ReplaceOneModel <T>(filter, item)
                {
                    IsUpsert = true
                };
                requests.Add(request);
            }

            return(GetCollection <T>().BulkWriteAsync(requests));
        }
        public static DbDino QueueDino(List <WriteModel <DbDino> > dinoActions, DbServer server, string inventoryId, ulong revisionId, byte revisionIndex, int inventoryType, ulong inventoryItemId, string request)
        {
            //Parse the dino data
            DbDino dino = ParseDinoData(server, inventoryId, revisionId, revisionIndex, inventoryType, inventoryItemId, request);

            if (dino == null)
            {
                return(null);
            }

            //Enqueue adding this dino
            {
                //Create filter for updating this dino
                var filterBuilder = Builders <DbDino> .Filter;
                var filter        = filterBuilder.Eq("dino_id", dino.dino_id) & filterBuilder.Eq("server_id", server.id);

                //Now, add (or insert) this into the database
                var a = new ReplaceOneModel <DbDino>(filter, dino);
                a.IsUpsert = true;
                dinoActions.Add(a);
            }

            return(dino);
        }
Exemple #12
0
        /// <summary>
        /// To /v1/structures
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e)
        {
            //Authenticate
            DbServer server = await Program.ForceAuthServer(e);

            if (server == null)
            {
                return;
            }

            //Read structures data
            RevisionMappedDataPutRequest <StructureData> payload = Program.DecodeStreamAsJson <RevisionMappedDataPutRequest <StructureData> >(e.Request.Body);

            //Convert all structures
            List <WriteModel <DbStructure> > structureActions = new List <WriteModel <DbStructure> >();
            List <WriteModel <DbItem> >      itemActions      = new List <WriteModel <DbItem> >();

            foreach (var s in payload.data)
            {
                //Ignore if no tribe ID is set
                if (s.tribe == 0)
                {
                    continue;
                }

                //Convert
                DbStructure structure = new DbStructure
                {
                    classname          = Program.TrimArkClassname(s.classname),
                    current_health     = s.health,
                    current_item_count = 0,
                    has_inventory      = false,
                    location           = s.location,
                    max_health         = s.max_health,
                    max_item_count     = 0,
                    server_id          = server._id,
                    structure_id       = s.id,
                    tribe_id           = s.tribe,
                    revision_id        = payload.revision_id,
                    revision_type      = payload.revision_index,
                    custom_name        = null
                };

                //Set inventory flags
                structure.has_inventory      = s.max_items > 0;
                structure.custom_name        = s.name;
                structure.max_item_count     = s.max_items;
                structure.current_item_count = s.item_count;

                //Create filter for updating this structure
                var filterBuilder = Builders <DbStructure> .Filter;
                var filter        = filterBuilder.Eq("structure_id", s.id) & filterBuilder.Eq("server_id", server.id);

                //Now, update (or insert) this into the database
                var a = new ReplaceOneModel <DbStructure>(filter, structure);
                a.IsUpsert = true;
                structureActions.Add(a);
            }

            //Apply actions
            if (structureActions.Count > 0)
            {
                await Program.conn.content_structures.BulkWriteAsync(structureActions);

                structureActions.Clear();
            }
            await Tools.InventoryManager.UpdateInventoryItems(itemActions, Program.conn);

            //Write finished
            e.Response.StatusCode = 200;
            await Program.WriteStringToStream(e.Response.Body, "OK");
        }
        public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e)
        {
            //Authenticate
            DbServer server = await Program.ForceAuthServer(e);

            if (server == null)
            {
                return;
            }

            //Decode request data
            EggRequestData request = Program.DecodeStreamAsJson <EggRequestData>(e.Request.Body);

            //Generate salt
            int salt = Program.rand.Next();

            //Loop through and add eggs
            List <WriteModel <DbEgg> > actions = new List <WriteModel <DbEgg> >();

            foreach (var d in request.data)
            {
                //Get the egg ID
                ulong id = Program.GetMultipartID(d.id1, d.id2);

                //Attempt to find this already existing in the database
                DbEgg egg = await DbEgg.GetEggByItemID(Program.conn, server.id, id);

                if (egg == null)
                {
                    egg = new DbEgg
                    {
                        _id = MongoDB.Bson.ObjectId.GenerateNewId(),
                        sent_notifications = new List <string>(),
                        placed_time        = DateTime.UtcNow,
                        tribe_id           = d.tribe_id,
                        item_id            = id,
                        server_id          = server._id
                    };
                }

                //Write updated data
                egg.min_temperature     = d.min_temp;
                egg.max_temperature     = d.max_temp;
                egg.health              = d.health;
                egg.incubation          = d.incubation / 100;
                egg.tribe_id            = d.tribe_id;
                egg.hatch_time          = DateTime.UtcNow.AddSeconds(d.time_remaining);
                egg.updated_time        = DateTime.UtcNow;
                egg.current_temperature = d.temp;
                egg.location            = d.pos;
                egg.egg_type            = d.type;
                egg.parents             = d.parents;
                egg.updater_salt        = salt;

                //Update
                var a = new ReplaceOneModel <DbEgg>(DbEgg.GetFilterDefinition(server.id, id), egg);
                a.IsUpsert = true;
                actions.Add(a);
            }

            if (actions.Count > 0)
            {
                //Commit actions
                await Program.conn.content_eggs.BulkWriteAsync(actions);

                //Remove old eggs
                var filterBuilder = Builders <DbEgg> .Filter;
                var filter        = filterBuilder.Eq("server_id", server.id) & filterBuilder.Ne("updater_salt", salt);
                await Program.conn.content_eggs.DeleteManyAsync(filter);
            }
        }
        public override void Write(int flowID, IEnumerable<KeyValuePair<long, Tick>> flow)
        {
            List<ReplaceOneModel<Row>> buffer = new List<ReplaceOneModel<Row>>(InsertsPerQuery * 2);

            foreach (var kv in flow)
            {
                Row row = new Row(kv.Key, kv.Value);

                ReplaceOneModel<Row> model = new ReplaceOneModel<Row>(new ObjectFilterDefinition<Row>(row), row);
                model.IsUpsert = true;

                buffer.Add(model);

                if (buffer.Count == InsertsPerQuery)
                {
                    var result = collections[flowID].BulkWriteAsync(buffer).Result;
                    buffer.Clear();
                }
            }

            if (buffer.Count != 0)
                collections[flowID].BulkWriteAsync(buffer);
        }
Exemple #15
0
        static async Task Main(string[] args)
        {
            Setup();

            Console.WriteLine("Starting the data migration.");
            var datePipelineResults = TransformDatePipeline();

            Console.WriteLine($"I found {datePipelineResults.Count} docs where the lastupdated field is of type 'string'.");

            // Call  _moviesCollection.BulkWriteAsync, passing in the
            // datePipelineResults. You will need to use a ReplaceOneModel<Movie>
            // (https://api.mongodb.com/csharp/current/html/T_MongoDB_Driver_ReplaceOneModel_1.htm).
            //
            // // bulkWriteDatesResult = await _moviesCollection.BulkWriteAsync(...

            // Date Pipeline

            if (datePipelineResults.Count > 0)
            {
                BulkWriteResult <Movie> bulkWriteDatesResult = null;

                List <ReplaceOneModel <Movie> > listModel1 = new List <ReplaceOneModel <Movie> >();

                foreach (Movie movie in datePipelineResults)
                {
                    var model = new ReplaceOneModel <Movie>(Builders <Movie> .Filter.Where(x => x.Id == movie.Id), movie);
                    listModel1.Add(model);
                }

                bulkWriteDatesResult = await _moviesCollection.BulkWriteAsync(listModel1, new BulkWriteOptions()
                {
                    IsOrdered = false
                });

                Console.WriteLine($"{bulkWriteDatesResult.ProcessedRequests.Count} records updated.");
            }

            var ratingPipelineResults = TransformRatingPipeline();

            Console.WriteLine($"I found {ratingPipelineResults.Count} docs where the imdb.rating field is not a number type.");

            // Rating Pipeline

            if (ratingPipelineResults.Count > 0)
            {
                BulkWriteResult <Movie> bulkWriteRatingsResult = null;
                // Call  _moviesCollection.BulkWriteAsync, passing in the
                // ratingPipelineResults. You will need to use a ReplaceOneModel<Movie>
                // (https://api.mongodb.com/csharp/current/html/T_MongoDB_Driver_ReplaceOneModel_1.htm).
                //
                // // bulkWriteRatingsResult = await _moviesCollection.BulkWriteAsync(...

                List <ReplaceOneModel <Movie> > listModel2 = new List <ReplaceOneModel <Movie> >();
                foreach (Movie movie in ratingPipelineResults)
                {
                    ReplaceOneModel <Movie> model = new ReplaceOneModel <Movie>(Builders <Movie> .Filter.Where(x => x.Id == movie.Id), movie);
                    listModel2.Add(model);
                }

                bulkWriteRatingsResult = await _moviesCollection.BulkWriteAsync(listModel2, new BulkWriteOptions()
                {
                    IsOrdered = false
                });

                Console.WriteLine($"{bulkWriteRatingsResult.ProcessedRequests.Count} records updated.");
            }

            Console.WriteLine();
            Console.WriteLine("Checking the data conversions...");
            Verify();

            // Keep the console window open until user hits `enter` or closes.
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("Press <Enter> to close.");
            Console.ReadLine();
        }
Exemple #16
0
        /// <summary>
        /// To /v1/profiles
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e)
        {
            //Authenticate
            DbServer server = await Program.ForceAuthServer(e);

            if (server == null)
            {
                return;
            }

            //Read structures data
            ProfilesRequestData s = Program.DecodeStreamAsJson <ProfilesRequestData>(e.Request.Body);

            //Add all player profiles
            List <WriteModel <DbPlayerProfile> >            playerActions = new List <WriteModel <DbPlayerProfile> >();
            List <WriteModel <DbTribe> >                    tribeActions  = new List <WriteModel <DbTribe> >();
            List <RPCPayloadOnlinePlayers_Player>           rpcPlayers    = new List <RPCPayloadOnlinePlayers_Player>();
            Dictionary <int, RPCPayloadOnlinePlayers_Tribe> rpcTribes     = new Dictionary <int, RPCPayloadOnlinePlayers_Tribe>();

            foreach (var p in s.player_profiles)
            {
                //Fetch Steam info
                var steam = await Program.conn.GetSteamProfileById(p.steam_id);

                if (steam == null)
                {
                    continue;
                }

                //Convert
                DbPlayerProfile profile = new DbPlayerProfile
                {
                    ark_id     = ulong.Parse(p.ark_id),
                    ig_name    = p.ark_name,
                    last_login = p.last_login,
                    name       = steam.name,
                    server_id  = server._id,
                    steam_id   = p.steam_id,
                    tribe_id   = p.tribe_id,
                    icon       = steam.icon_url
                };

                //Create filter for updating this dino
                var filterBuilder = Builders <DbPlayerProfile> .Filter;
                var filter        = filterBuilder.Eq("steam_id", p.steam_id) & filterBuilder.Eq("server_id", server.id);

                //Now, add (or insert) this into the database
                var a = new ReplaceOneModel <DbPlayerProfile>(filter, profile);
                a.IsUpsert = true;
                playerActions.Add(a);

                //Add to RPC messages
                rpcPlayers.Add(new RPCPayloadOnlinePlayers_Player
                {
                    icon     = steam.icon_url,
                    name     = steam.name,
                    tribe_id = p.tribe_id
                });
            }

            //Add all tribe profiles
            foreach (var t in s.tribes)
            {
                //Convert
                DbTribe tribe = new DbTribe
                {
                    server_id   = server._id,
                    tribe_id    = t.tribe_id,
                    tribe_name  = t.name,
                    tribe_owner = 0
                };

                //Create filter for updating this dino
                var filterBuilder = Builders <DbTribe> .Filter;
                var filter        = filterBuilder.Eq("tribe_id", t.tribe_id) & filterBuilder.Eq("server_id", server.id);

                //Now, add (or insert) this into the database
                var a = new ReplaceOneModel <DbTribe>(filter, tribe);
                a.IsUpsert = true;
                tribeActions.Add(a);

                //Add to RPC messages
                rpcTribes.Add(t.tribe_id, new RPCPayloadOnlinePlayers_Tribe
                {
                    name = t.name
                });
            }

            //Apply actions
            if (tribeActions.Count > 0)
            {
                await Program.conn.content_tribes.BulkWriteAsync(tribeActions);

                tribeActions.Clear();
            }
            if (playerActions.Count > 0)
            {
                await Program.conn.content_player_profiles.BulkWriteAsync(playerActions);

                playerActions.Clear();
            }

            //Send RPC message

            /*Program.conn.GetRPC().SendRPCMessageToServer(LibDeltaSystem.RPC.RPCOpcode.PlayerListChanged, new RPCPayloadOnlinePlayers
             * {
             *  players = rpcPlayers,
             *  tribes = rpcTribes
             * }, server);*/

            //Write finished
            e.Response.StatusCode = 200;
            await Program.WriteStringToStream(e.Response.Body, "OK");
        }