Example #1
0
        /// <summary>
        /// Examines the context changes from the ChangeTracker
        /// and returns a collection of LuceneIndexChanges
        /// </summary>
        /// <returns>LuceneIndexChangeset - collection of entity changes converted to LuceneIndexChanges</returns>
        private LuceneIndexChangeset GetChangeset()
        {
            LuceneIndexChangeset changes = new LuceneIndexChangeset();

            foreach (var entity in contextInterface.ChangeTracker.Entries().Where(x => x.State != EntityState.Unchanged))
            {
                Type entityType = entity.Entity.GetType();
                bool implementsILuceneIndexable = typeof(ILuceneIndexable).IsAssignableFrom(entityType);
                if (implementsILuceneIndexable == true)
                {
                    MethodInfo method = entityType.GetMethod("ToDocument");
                    if (method != null)
                    {
                        LuceneIndexChange change = new LuceneIndexChange(entity.Entity as ILuceneIndexable);

                        // If the entity doesn't have a guid IndexId, then add one in
                        if ((change.Entity.IndexId == Guid.Empty || change.Entity.IndexId == null) &&
                            (change.State == LuceneIndexState.Added || change.State == LuceneIndexState.Updated))
                        {
                            change.Entity.IndexId = Guid.NewGuid();
                        }

                        switch (entity.State)
                        {
                        case EntityState.Added:
                            change.State = LuceneIndexState.Added;
                            break;

                        case EntityState.Deleted:
                            change.State = LuceneIndexState.Removed;
                            break;

                        case EntityState.Modified:
                            change.State = LuceneIndexState.Updated;
                            break;

                        default:
                            change.State = LuceneIndexState.Unchanged;
                            break;
                        }
                        changes.Entries.Add(change);
                    }
                }
            }

            return(changes);
        }
Example #2
0
        public async Task <int> SaveChangesAsync(bool index = true)
        {
            int result = 0;

            if (contextInterface.ChangeTracker.HasChanges())
            {
                // get the chages
                LuceneIndexChangeset changes = GetChangeset();

                // call the base async SaveChangesAsync method
                result = await contextInterface.SaveChangesAsync();

                if (changes.HasChanges == true && index == true)
                {
                    indexer.Update(changes);
                }
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// Call the SaveChanges method on the context
        /// and index any changes
        /// </summary>
        /// <returns>Value passed back from the SaveChanges method on the underlying context</returns>
        public int SaveChanges(bool index = true)
        {
            int result = 0;

            if (contextInterface.ChangeTracker.HasChanges())
            {
                // get the Lucene Changeset from the current ChangeTracker
                LuceneIndexChangeset changes = GetChangeset();

                // Call the base AFTER we have collected out changeset
                result = contextInterface.SaveChanges();

                // finally, update our lucene with the changes if there are any
                if (changes.HasChanges && index == true)
                {
                    indexer.Update(changes);
                }
            }

            return(result);
        }
Example #4
0
        public void Update(LuceneIndexChangeset changeset)
        {
            using (var writer = new IndexWriter(directory, analyzer, maxFieldLength))
            {
                foreach (var change in changeset.Entries)
                {
                    switch (change.State)
                    {
                    case LuceneIndexState.Added:
                        writer.AddDocument(change.Entity.ToDocument());
                        break;

                    case LuceneIndexState.Removed:
                        writer.DeleteDocuments(new Term("IndexId", change.Entity.IndexId.ToString()));
                        break;

                    case LuceneIndexState.Updated:
                        writer.UpdateDocument(new Term("IndexId", change.Entity.IndexId.ToString()), change.Entity.ToDocument());
                        break;

                    default:
                        break;     // do nothing if the state doesn't involve changing the data
                    }
                }

                // only call the optimize if specified in the changeset
                // as this can be potentially quite an expensive task
                if (changeset.Optimize == true)
                {
                    writer.Optimize();
                }

                // flush the changes
                writer.Flush(true, true, changeset.HasDeletes);
            }
        }
Example #5
0
        /// <summary>
        /// Update a single LuceneIndexChange
        /// </summary>
        /// <param name="change">The change to make</param>
        /// <param name="optimize">Whether to optimize afterwards</param>
        public void Update(LuceneIndexChange change, bool optimize = false)
        {
            LuceneIndexChangeset changeset = new LuceneIndexChangeset(change, optimize);

            Update(changeset);
        }