Improving indexing process

This commit is contained in:
Sebastien Ros
2014-08-27 12:22:23 -07:00
parent 42189e9297
commit 5f92beb0fb
@@ -33,6 +33,7 @@ namespace Orchard.Indexing.Services {
private readonly ShellSettings _shellSettings;
private readonly ILockFileManager _lockFileManager;
private readonly IClock _clock;
private readonly ITransactionManager _transactionManager;
private const int ContentItemsPerLoop = 50;
private IndexingStatus _indexingStatus = IndexingStatus.Idle;
@@ -44,7 +45,8 @@ namespace Orchard.Indexing.Services {
IAppDataFolder appDataFolder,
ShellSettings shellSettings,
ILockFileManager lockFileManager,
IClock clock) {
IClock clock,
ITransactionManager transactionManager) {
_taskRepository = taskRepository;
_contentRepository = contentRepository;
_indexManager = indexManager;
@@ -52,6 +54,7 @@ namespace Orchard.Indexing.Services {
_appDataFolder = appDataFolder;
_shellSettings = shellSettings;
_lockFileManager = lockFileManager;
_transactionManager = transactionManager;
_clock = clock;
Logger = NullLogger.Instance;
}
@@ -143,12 +146,16 @@ namespace Orchard.Indexing.Services {
private bool BatchIndex(string indexName, string settingsFilename, IndexSettings indexSettings) {
var addToIndex = new List<IDocumentIndex>();
var deleteFromIndex = new List<int>();
bool loop = false;
// Rebuilding the index ?
if (indexSettings.Mode == IndexingMode.Rebuild) {
Logger.Information("Rebuilding index");
_indexingStatus = IndexingStatus.Rebuilding;
do {
loop = true;
// load all content items
var contentItems = _contentRepository
.Table.Where(versionRecord => versionRecord.Published && versionRecord.Id > indexSettings.LastContentId)
@@ -183,19 +190,31 @@ namespace Orchard.Indexing.Services {
Logger.Warning(ex, "Unable to index content item #{0} during rebuild", item.Id);
}
}
if (contentItems.Count < ContentItemsPerLoop) {
loop = false;
}
else {
_contentManager.Clear();
_transactionManager.RequireNew();
}
} while (loop);
}
if (indexSettings.Mode == IndexingMode.Update) {
Logger.Information("Updating index");
_indexingStatus = IndexingStatus.Updating;
do {
var indexingTasks = _taskRepository
.Table.Where(x => x.Id > indexSettings.LastIndexedId)
.OrderBy(x => x.Id)
.Take(ContentItemsPerLoop)
.ToList()
.GroupBy(x => x.ContentItemRecord.Id)
.Select(group => new {TaskId = group.Max(task => task.Id), Delete = group.Last().Action == IndexingTaskRecord.Delete, Id = group.Key, ContentItem = _contentManager.Get(group.Key, VersionOptions.Published)})
.Select(group => new { TaskId = group.Max(task => task.Id), Delete = group.Last().Action == IndexingTaskRecord.Delete, Id = group.Key, ContentItem = _contentManager.Get(group.Key, VersionOptions.Published) })
.OrderBy(x => x.TaskId)
.ToArray();
@@ -226,6 +245,16 @@ namespace Orchard.Indexing.Services {
Logger.Warning(ex, "Unable to index content item #{0} during update", item.Id);
}
}
if (indexingTasks.Length < ContentItemsPerLoop) {
loop = false;
}
else {
_contentManager.Clear();
_transactionManager.RequireNew();
}
}
while (loop);
}
// save current state of the index