Example #1
0
        private static void LastSite()
        {
            var documentStore = DocumentStoreHolder.Store;
            var batchSize     = 10000;
            var position      = 0;


            var optionsBuilder = new DbContextOptionsBuilder <AoContext>();

            optionsBuilder.UseSqlServer(
                "Server=.;Database=.;User=*;Password=*;");

            while (true)
            {
                using var context = new AoContext(optionsBuilder.Options);
                var users = context.Site.Skip(position).Take(batchSize).Select(x => new TestModels.Site()
                {
                    Id               = "Site/" + x.Id,
                    UserId           = x.UserId ?? 0,
                    Name             = x.Name,
                    ParentSiteId     = "Site/" + x.ParentId,
                    PresentationName = x.PresentationName,
                    X        = x.Xcoord,
                    Y        = x.Ycoord,
                    Accuracy = x.Accuracy,
                    Date     = x.EditDate
                }).ToArray();

                if (users.Length == 0)
                {
                    break;
                }

                position += batchSize;
                using var ravenSession = documentStore.OpenSession();
                foreach (var site in users)
                {
                    ravenSession.Store(site);
                }

                ravenSession.SaveChanges();
            }
        }
Example #2
0
        private static void LastFraAO()
        {
            var siteAreas = new Dictionary <int, List <int> >();

            var theStore  = DocumentStoreHolder.Store;
            var batchSize = 100;

            theStore.ExecuteIndexes(new AbstractIndexCreationTask[]
            {
                //new RapporteringsvolumIndex(),
                //new ObservationvolumIndex(),
                //new AreaIndex(),
                //new ObservatorFirstIndex2(),
                //new ObservatorAreaIndex2(),
                new AreaYearTaxonIndex(),
                new AreaYearIndex(),
            });


            for (int i = 10297801; i < 30000000; i += batchSize)
            {
                using (var context = new AoContext())
                {
                    context.ChangeTracker.AutoDetectChangesEnabled = false;
                    context.ChangeTracker.QueryTrackingBehavior    = QueryTrackingBehavior.NoTracking;
                    context.Database.SetCommandTimeout(180);


                    // Read from db
                    var sightings = context.Sighting.AsNoTracking()
                                    //.Include(x => x.SightingState)
                                    .Include(x => x.SightingRelation)
                                    .Where(x =>
                                           x.SightingTypeId == 0 &&
                                           x.SightingState.Any(y => y.IsActive && y.SightingStateTypeId == 30) &&
                                           x.SiteId != null && x.Id > i)
                                    .OrderBy(x => x.Id)
                                    //.Skip(i)
                                    .Take(batchSize)
                                    .ToList();

                    var siteNewIds = sightings
                                     .Select(x => x.SiteId).Distinct()
                                     .Where(x => !siteAreas.ContainsKey(x.Value));

                    var newSiteAreas = context.SiteAreas
                                       .Where(x => siteNewIds.Contains(x.SiteId))
                                       .ToList()
                                       .GroupBy(x => x.SiteId)
                                       .ToDictionary(x => x.Key, y => y.Select(z => z.AreasId).ToList());

                    foreach (var newSiteArea in newSiteAreas)
                    {
                        siteAreas.Add(newSiteArea.Key, newSiteArea.Value);
                    }

                    //Map
                    var sightingIndexes = sightings.Select(x => new SightingIndex
                    {
                        Id         = x.Id.ToString(),
                        TaxonId    = x.TaxonId ?? 0,
                        StartDate  = x.StartDate ?? new DateTime(0, 0, 0),
                        ReporterId = x.SightingRelation.Where(y => y.SightingRelationTypeId == 1)
                                     .Select(y => y.UserId).ToList(),
                        ObserverIds = x.SightingRelation.Where(y => y.SightingRelationTypeId == 2)
                                      .Select(y => y.UserId).ToList(),
                        AreaIds = siteAreas[x.SiteId.Value]
                    }).ToList();

                    // Write to Elasticsearch
                    //var response = client.IndexMany(sightingIndexes);

                    // Write to RavenDB
                    using (var session = theStore.OpenSession())
                    {
                        foreach (var index in sightingIndexes)
                        {
                            session.Store(index);
                        }

                        session.SaveChanges();
                    }

                    Console.WriteLine($"{DateTime.Now:hh:mm:ss} {i}: sightingCount: {sightings.Count}");
                }
            }
        }