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 identity = elementId.Value;
|
||||||
var status = element.Attribute("Status");
|
var status = element.Attribute("Status");
|
||||||
|
|
||||||
|
importContentSession.Set(identity, element.Name.LocalName);
|
||||||
|
|
||||||
var item = importContentSession.Get(identity);
|
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.
|
// 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);
|
var context = new ImportContentContext(item, element, importContentSession);
|
||||||
foreach (var contentHandler in Handlers) {
|
foreach (var contentHandler in Handlers) {
|
||||||
contentHandler.Importing(context);
|
contentHandler.Importing(context);
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Orchard.ContentManagement {
|
namespace Orchard.ContentManagement {
|
||||||
@@ -8,13 +9,19 @@ namespace Orchard.ContentManagement {
|
|||||||
private const int BulkPage = 128;
|
private const int BulkPage = 128;
|
||||||
private int _lastIndex = 0;
|
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<int, ContentIdentity> _contentItemIds;
|
||||||
|
private readonly Dictionary<ContentIdentity, string> _contentTypes;
|
||||||
|
|
||||||
public ImportContentSession(IContentManager contentManager) {
|
public ImportContentSession(IContentManager contentManager) {
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
_identities = new Dictionary<ContentIdentity, ContentItem>(new ContentIdentity.ContentIdentityEqualityComparer());
|
_identities = new Dictionary<ContentIdentity, int>(new ContentIdentity.ContentIdentityEqualityComparer());
|
||||||
_contentItemIds = new Dictionary<int, ContentIdentity>();
|
_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) {
|
public ContentItem Get(string id) {
|
||||||
@@ -22,64 +29,56 @@ namespace Orchard.ContentManagement {
|
|||||||
|
|
||||||
// lookup in local cache
|
// lookup in local cache
|
||||||
if (_identities.ContainsKey(contentIdentity))
|
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
|
// no result ? then check if there are some more content items to load from the db
|
||||||
|
if(_lastIndex != int.MaxValue) {
|
||||||
|
|
||||||
if(_lastIndex == int.MaxValue) {
|
var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
|
||||||
// everything has already been loaded from db
|
IEnumerable<ContentItem> block;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var equalityComparer = new ContentIdentity.ContentIdentityEqualityComparer();
|
// load identities in blocks
|
||||||
IEnumerable<ContentItem> block;
|
while ((block = _contentManager.HqlQuery()
|
||||||
|
.ForVersion(VersionOptions.Latest)
|
||||||
|
.OrderBy(x => x.ContentItemVersion(), x => x.Asc("Id"))
|
||||||
|
.Slice(_lastIndex, BulkPage)).Any()) {
|
||||||
|
|
||||||
// load identities in blocks
|
foreach (var item in block) {
|
||||||
while ((block = _contentManager.HqlQuery()
|
_lastIndex++;
|
||||||
.ForVersion(VersionOptions.Latest)
|
|
||||||
.OrderBy(x => x.ContentItemVersion(), x => x.Asc("Id"))
|
|
||||||
.Slice(_lastIndex, BulkPage)).Any()) {
|
|
||||||
|
|
||||||
foreach (var item in block) {
|
// ignore content item if it has already been imported
|
||||||
_lastIndex++;
|
if (_contentItemIds.ContainsKey(item.Id)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// ignore content item if it has already been imported
|
var identity = _contentManager.GetItemMetadata(item).Identity;
|
||||||
if (_contentItemIds.ContainsKey(item.Id)) {
|
|
||||||
continue;
|
// 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;
|
_contentManager.Flush();
|
||||||
|
_contentManager.Clear();
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_lastIndex = int.MaxValue;
|
_lastIndex = int.MaxValue;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Store(string id, ContentItem item) {
|
if(!_contentTypes.ContainsKey(contentIdentity)) {
|
||||||
var contentIdentity = new ContentIdentity(id);
|
throw new ArgumentException("Unknown content type for " + id);
|
||||||
if (_identities.ContainsKey(contentIdentity)) {
|
|
||||||
_identities.Remove(contentIdentity);
|
|
||||||
_contentItemIds.Remove(item.Id);
|
|
||||||
}
|
}
|
||||||
_identities.Add(contentIdentity, item);
|
|
||||||
|
|
||||||
if (!_contentItemIds.ContainsKey(item.Id)) {
|
return _contentManager.New(_contentTypes[contentIdentity]);
|
||||||
_contentItemIds.Add(item.Id, contentIdentity);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user