public override bool Sync(IEnumerable <Guid> remove, IEnumerable <Entry> add) { var ancestorDb = new AncestorContext(); var entryDb = new EntryContext(); try { entryDb.Database.BeginTransaction(); var now = DateTime.UtcNow; if (add != null) { foreach (var entry in add) { entryDb.Write(entry.Key, entry.Id, entry.Map, now); ancestorDb.Write(entry.Id, entry.Key, now); foreach (var ancestor in entry.Ancestors) { ancestorDb.Write(ancestor, entry.Key, now); } } } if (remove != null) { foreach (var guid in remove) { ancestorDb.Delete(guid, now); entryDb.Delete(guid, now); } } entryDb.Database.CompleteTransaction(); return(true); } catch (Exception ex) { LogHelper.Error <SqlIndexer>($"Error trying to sync content with indexer", ex); } try { entryDb.Database.AbortTransaction(); } catch (Exception) { // Just swallow the error, as the previous catch above would have logged it } return(false); }
public override void Up() { try { // Create tables _schemaHelper.CreateTable <Dto.Entry.Entry100>(); _schemaHelper.CreateTable <Dto.Ancestor.Ancestor100>(); // Scrape all existing content and place into the index var ancestorDb = new AncestorContext(); var entryDb = new EntryContext(); var now = DateTime.UtcNow; var contents = new Stack <Umbraco.Core.Models.IContent>(); var contentService = ApplicationContext.Current.Services.ContentService; foreach (var content in contentService.GetChildren(Umbraco.Core.Constants.System.Root)) { if (content.Published) { contents.Push(content); } } while (contents.Count != 0) { var content = contents.Pop(); foreach (var child in contentService.GetChildren(content.Id)) { if (child.Published) { contents.Push(child); } } foreach (var entry in new ContentService().Entries(new Umbraco.Core.Models.IContent[] { content })) { entryDb.Write(entry.Key, entry.Id, entry.Map, now); ancestorDb.Write(entry.Id, entry.Key, now); foreach (var ancestor in entry.Ancestors) { ancestorDb.Write(ancestor, entry.Key, now); } } System.Threading.Thread.Sleep(50); } } catch (Exception ex) { LogHelper.Error <SqlIndexer>($"Error trying to create content with indexer", ex); } }