Ejemplo n.º 1
0
        public async Task AsyncBulkInserts()
        {
            using (var store = new DocumentStore())
            {
                #region bulk_inserts_5

                BulkInsertOperation bulkInsert = null;
                try
                {
                    bulkInsert = store.BulkInsert();
                    for (int i = 0; i < 1000 * 1000; i++)
                    {
                        await bulkInsert.StoreAsync(new Employee
                        {
                            FirstName = "FirstName #" + i,
                            LastName  = "LastName #" + i
                        });
                    }
                }
                finally
                {
                    if (bulkInsert != null)
                    {
                        await bulkInsert.DisposeAsync().ConfigureAwait(false);
                    }
                }
                #endregion
            }
        }
Ejemplo n.º 2
0
 public async Task AppendEventAsync(IEnumerable <DomainEventBase> events, CancellationToken cancellationToken)
 {
     using (BulkInsertOperation bulkInsert = _store.BulkInsert())
     {
         foreach (var item in events)
         {
             await bulkInsert.StoreAsync(item);
         }
     }
 }
Ejemplo n.º 3
0
        public async Task <string> Seed()
        {
            DetailedDatabaseStatistics stats = await _store.Maintenance.SendAsync(new GetDetailedStatisticsOperation()).ConfigureAwait(false);

            if (stats.CountOfDocuments > 0)
            {
                return("Database is already seeded");
            }

            Faker <Entry> generator = new Faker <Entry>()
                                      .StrictMode(true)
                                      .Ignore(e => e.Id)
                                      .RuleFor(e => e.Tags, f => Helper.GetRandomTags());

            List <Entry> entries = generator.Generate(20);

            await using BulkInsertOperation bulkInsert = _store.BulkInsert();
            foreach (Entry entry in entries)
            {
                await bulkInsert.StoreAsync(entry);
            }

            return("Database was empty, new data seeded");
        }
Ejemplo n.º 4
0
        private async Task ProcessQueue()
        {
            bool stopRequested = false;
            bool error         = false;

            using (var bulkInsert = new  BulkInsertOperation(this.database, this.store))
                using (var session = this.store.OpenAsyncSession(this.database))
                {
                    session.Advanced.MaxNumberOfRequestsPerSession = int.MaxValue;
                    while (true)
                    {
                        ProfilerRecord record = null;
                        try
                        {
                            record = blockingCollection.Take();
                        }
                        catch (ObjectDisposedException)
                        {
                            stopRequested = true;
                        }
                        catch (InvalidOperationException)
                        {
                            stopRequested = true;
                        }
                        catch (OperationCanceledException)
                        {
                            stopRequested = true;
                        }
                        catch (Exception ex)
                        {
                            logger.LogError(ex, "Failed to read record from blocking collection");
                            error = true;
                            try { this.StopProfilingImpl(); } catch { }
                        }
                        if (stopRequested || error)
                        {
                            try { this.blockingCollection.Dispose(); } catch { }
                            this.blockingCollection = null;
                            return;
                        }

                        var sqlStatementId           = "SqlStatements/" + GetHash(record.CommandText);
                        var normalizedCommandText    = listOfValuesRegex.Replace(record.CommandText, "( #ListOfValues# )");
                        var normalizedSqlStatementId = "NormalizedSqlStatements/" + GetHash(normalizedCommandText);

                        if (normalizedSqlStatementIds.Add(normalizedSqlStatementId))
                        {
                            if (!await session.Advanced.ExistsAsync(normalizedSqlStatementId))
                            {
                                var normalizedSqlStatement = new NormalizedSqlStatement
                                {
                                    Id  = normalizedSqlStatementId,
                                    Sql = normalizedCommandText
                                };
                                await bulkInsert.StoreAsync(normalizedSqlStatement, normalizedSqlStatementId);
                            }
                        }

                        if (sqlStatementIds.Add(sqlStatementId))
                        {
                            if (!await session.Advanced.ExistsAsync(sqlStatementId))
                            {
                                var sqlStatement = new SqlStatement
                                {
                                    Id = sqlStatementId,
                                    NormalizedSqlStatementId = normalizedSqlStatementId,
                                    Sql = record.CommandText
                                };
                                await bulkInsert.StoreAsync(sqlStatement, sqlStatementId);
                            }
                        }

                        var sqlStatementExecution = new SqlStatementExecution
                        {
                            CommandType = record.CommandType,
                            Context     = record.Context,
                        };
                        var metadata = new Dictionary <string, object>
                        {
                            [Constants.Documents.Metadata.Expires] = DateTime.UtcNow.Add(this.expiration)
                        };
                        await bulkInsert.StoreAsync(sqlStatementExecution, sqlStatementExecution.Id, new MetadataAsDictionary(metadata));
                    }
                }
        }