当前位置: 首页>>代码示例>>C#>>正文


C# IDocumentStore.Query方法代码示例

本文整理汇总了C#中IDocumentStore.Query方法的典型用法代码示例。如果您正苦于以下问题:C# IDocumentStore.Query方法的具体用法?C# IDocumentStore.Query怎么用?C# IDocumentStore.Query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IDocumentStore的用法示例。


在下文中一共展示了IDocumentStore.Query方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

        public Task Run(IDocumentStore store)
        {
            var logger = store.Configuration.Logger;
            var configuration = store.Configuration;

            if (!configuration.RunDocumentMigrationsOnStartup)
                return Task.FromResult(0);

            return Task.Factory.StartNew(() =>
            {
                var migrator = new DocumentMigrator(configuration);

                foreach (var table in configuration.Tables.Values.OfType<DocumentTable>())
                {
                    var baseDesign = configuration.TryGetDesignByTablename(table.Name);
                    if (baseDesign == null)
                    {
                        throw new InvalidOperationException($"Design not found for table '{table.Name}'");
                    }

                    while (true)
                    {
                        QueryStats stats;

                        var rows = store
                            .Query(table, out stats,
                                @where: "AwaitsReprojection = @AwaitsReprojection or Version < @version",
                                @select: "Id, AwaitsReprojection, Version, Discriminator, Etag",
                                take: 100,
                                @orderby: "newid()",
                                parameters: new { AwaitsReprojection = true, version = configuration.ConfiguredVersion })
                            .ToList();

                        if (stats.TotalResults == 0) break;

                        logger.Information("Found {0} document that must be migrated.", stats.TotalResults);

                        foreach (var row in rows)
                        {
                            var key = (string)row[table.IdColumn];
                            var currentDocumentVersion = (int)row[table.VersionColumn];
                            var discriminator = ((string)row[table.DiscriminatorColumn]).Trim();
                            var concreteDesign = store.Configuration.GetOrCreateDesignByDiscriminator(baseDesign, discriminator);

                            var shouldUpdate = false;

                            if ((bool)row[table.AwaitsReprojectionColumn])
                            {
                                shouldUpdate = true;
                                logger.Information("Reprojection document {0}/{1}.",
                                    concreteDesign.DocumentType.FullName, key, currentDocumentVersion, configuration.ConfiguredVersion);
                            }

                            if (migrator.ApplicableCommands(concreteDesign, currentDocumentVersion).Any())
                            {
                                shouldUpdate = true;
                            }

                            if (shouldUpdate)
                            {
                                try
                                {
                                    using (var session = new DocumentSession(store))
                                    {
                                        session.Load(concreteDesign.DocumentType, key);
                                        session.SaveChanges(lastWriteWins: false, forceWriteUnchangedDocument: true);
                                    }
                                }
                                catch (ConcurrencyException) { }
                                catch (Exception exception)
                                {
                                    logger.Error(exception, "Error while migrating document of type {0} with id {1}.", concreteDesign.DocumentType.FullName, key);
                                    Thread.Sleep(100);
                                }
                            }
                            else
                            {
                                logger.Information("Document did not change.");

                                var projection = new Dictionary<string, object>
                                {
                                    {table.VersionColumn, configuration.ConfiguredVersion}
                                };

                                store.Update(table, key, (Guid)row[table.EtagColumn], projection);
                            }
                        }
                    }
                }

            }, TaskCreationOptions.LongRunning);
        }
开发者ID:asgerhallas,项目名称:HybridDb,代码行数:92,代码来源:DocumentMigrationRunner.cs


注:本文中的IDocumentStore.Query方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。