mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Optimizing import task
--HG-- branch : 1.x
This commit is contained in:
@@ -35,14 +35,9 @@ namespace Orchard.Recipes.RecipeHandlers {
|
||||
var identity = elementId.Value;
|
||||
var status = element.Attribute("Status");
|
||||
|
||||
importContentSession.Set(identity, element.Name.LocalName);
|
||||
|
||||
var item = importContentSession.Get(identity);
|
||||
if (item == null) {
|
||||
item = _orchardServices.ContentManager.New(element.Name.LocalName);
|
||||
}
|
||||
else {
|
||||
item = _orchardServices.ContentManager.Get(item.Id, VersionOptions.DraftRequired);
|
||||
}
|
||||
importContentSession.Store(identity, item);
|
||||
}
|
||||
|
||||
// Second pass to import the content items.
|
||||
|
@@ -589,8 +589,6 @@ namespace Orchard.ContentManagement {
|
||||
};
|
||||
}
|
||||
|
||||
importContentSession.Store(identity, item);
|
||||
|
||||
var context = new ImportContentContext(item, element, importContentSession);
|
||||
foreach (var contentHandler in Handlers) {
|
||||
contentHandler.Importing(context);
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Orchard.ContentManagement {
|
||||
@@ -8,13 +9,19 @@ namespace Orchard.ContentManagement {
|
||||
private const int BulkPage = 128;
|
||||
private int _lastIndex = 0;
|
||||
|
||||
private readonly Dictionary<ContentIdentity, ContentItem> _identities;
|
||||
private readonly Dictionary<ContentIdentity, int> _identities;
|
||||
private readonly Dictionary<int, ContentIdentity> _contentItemIds;
|
||||
private readonly Dictionary<ContentIdentity, string> _contentTypes;
|
||||
|
||||
public ImportContentSession(IContentManager contentManager) {
|
||||
_contentManager = contentManager;
|
||||
_identities = new Dictionary<ContentIdentity, ContentItem>(new ContentIdentity.ContentIdentityEqualityComparer());
|
||||
_identities = new Dictionary<ContentIdentity, int>(new ContentIdentity.ContentIdentityEqualityComparer());
|
||||
_contentItemIds = new Dictionary<int, ContentIdentity>();
|
||||
_contentTypes = new Dictionary<ContentIdentity, string>(new ContentIdentity.ContentIdentityEqualityComparer());
|
||||
}
|
||||
public void Set(string id, string contentType) {
|
||||
var contentIdentity = new ContentIdentity(id);
|
||||
_contentTypes[contentIdentity] = contentType;
|
||||
}
|
||||
|
||||
public ContentItem Get(string id) {
|
||||
@@ -22,64 +29,56 @@ namespace Orchard.ContentManagement {
|
||||
|
||||
// lookup in local cache
|
||||
if (_identities.ContainsKey(contentIdentity))
|
||||
return _identities[contentIdentity];
|
||||
return _contentManager.Get(_identities[contentIdentity], VersionOptions.DraftRequired);
|
||||
|
||||
// no result ? then check if there are some more content items to load from the db
|
||||
if(_lastIndex != int.MaxValue) {
|
||||
|
||||
if(_lastIndex == int.MaxValue) {
|
||||
// everything has already been loaded from db
|
||||
return null;
|
||||
}
|
||||
var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
|
||||
IEnumerable<ContentItem> block;
|
||||
|
||||
var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
|
||||
IEnumerable<ContentItem> block;
|
||||
// load identities in blocks
|
||||
while ((block = _contentManager.HqlQuery()
|
||||
.ForVersion(VersionOptions.Latest)
|
||||
.OrderBy(x => x.ContentItemVersion(), x => x.Asc("Id"))
|
||||
.Slice(_lastIndex, BulkPage)).Any()) {
|
||||
|
||||
// load identities in blocks
|
||||
while ((block = _contentManager.HqlQuery()
|
||||
.ForVersion(VersionOptions.Latest)
|
||||
.OrderBy(x => x.ContentItemVersion(), x => x.Asc("Id"))
|
||||
.Slice(_lastIndex, BulkPage)).Any()) {
|
||||
foreach (var item in block) {
|
||||
_lastIndex++;
|
||||
|
||||
foreach (var item in block) {
|
||||
_lastIndex++;
|
||||
// ignore content item if it has already been imported
|
||||
if (_contentItemIds.ContainsKey(item.Id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// ignore content item if it has already been imported
|
||||
if (_contentItemIds.ContainsKey(item.Id)) {
|
||||
continue;
|
||||
var identity = _contentManager.GetItemMetadata(item).Identity;
|
||||
|
||||
// ignore content item if the same identity is already present
|
||||
if (_identities.ContainsKey(identity)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
_identities.Add(identity, item.Id);
|
||||
_contentItemIds.Add(item.Id, identity);
|
||||
|
||||
if (equalityComparer.Equals(identity, contentIdentity)) {
|
||||
return _contentManager.Get(item.Id, VersionOptions.DraftRequired);
|
||||
}
|
||||
}
|
||||
|
||||
var identity = _contentManager.GetItemMetadata(item).Identity;
|
||||
|
||||
// ignore content item if the same identity is already present
|
||||
if (_identities.ContainsKey(identity)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
_identities.Add(identity, item);
|
||||
_contentItemIds.Add(item.Id, identity);
|
||||
|
||||
if (equalityComparer.Equals(identity, contentIdentity)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
_contentManager.Flush();
|
||||
_contentManager.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
_lastIndex = int.MaxValue;
|
||||
return null;
|
||||
}
|
||||
|
||||
public void Store(string id, ContentItem item) {
|
||||
var contentIdentity = new ContentIdentity(id);
|
||||
if (_identities.ContainsKey(contentIdentity)) {
|
||||
_identities.Remove(contentIdentity);
|
||||
_contentItemIds.Remove(item.Id);
|
||||
if(!_contentTypes.ContainsKey(contentIdentity)) {
|
||||
throw new ArgumentException("Unknown content type for " + id);
|
||||
|
||||
}
|
||||
_identities.Add(contentIdentity, item);
|
||||
|
||||
if (!_contentItemIds.ContainsKey(item.Id)) {
|
||||
_contentItemIds.Add(item.Id, contentIdentity);
|
||||
}
|
||||
return _contentManager.New(_contentTypes[contentIdentity]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user