Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored to add in Purge/PurgeAll to IFullTextSession

SVN: branches/1.2.x@3132
  • Loading branch information...
commit 738e3078a41f80f13e2730a3fc46bbc77a39584a 1 parent 8d5984b
Paul Hatcher phatcher authored
10 src/NHibernate.Search/Backend/AddLuceneWork.cs
View
@@ -4,16 +4,8 @@ namespace NHibernate.Search.Backend
{
public class AddLuceneWork : LuceneWork
{
- private readonly Document document;
-
- public AddLuceneWork(object id, System.Type entityClass, Document document) : base(id, entityClass)
- {
- this.document = document;
- }
-
- public Document Document
+ public AddLuceneWork(object id, string idInString, System.Type entityClass, Document document) : base(id, idInString, entityClass, document)
{
- get { return document; }
}
}
}
2  src/NHibernate.Search/Backend/DeleteLuceneWork.cs
View
@@ -2,7 +2,7 @@ namespace NHibernate.Search.Backend
{
public class DeleteLuceneWork : LuceneWork
{
- public DeleteLuceneWork(object id, System.Type entityClass) : base(id, entityClass)
+ public DeleteLuceneWork(object id, string idInString, System.Type entityClass) : base(id, idInString, entityClass)
{
}
}
45 src/NHibernate.Search/Backend/LuceneWork.cs
View
@@ -1,16 +1,35 @@
+using Lucene.Net.Documents;
+
namespace NHibernate.Search.Backend
{
- public class LuceneWork
+ public abstract class LuceneWork
{
- private readonly System.Type entityClass;
private readonly object id;
+ private readonly string idInString;
+ private readonly System.Type entityClass;
+ private readonly Document document;
- protected LuceneWork(object id, System.Type entityClass)
+ // Flag indicating that the lucene work has to be indexed in batch mode
+ private bool isBatch;
+
+ #region Constructors
+
+ protected LuceneWork(object id, string idInString, System.Type entityClass) : this(id, idInString, entityClass, null)
+ {
+ }
+
+ protected LuceneWork(object id, string idInString, System.Type entityClass, Document document)
{
- this.entityClass = entityClass;
this.id = id;
+ this.idInString = idInString;
+ this.entityClass = entityClass;
+ this.document = document;
}
+ #endregion
+
+ #region Property methods
+
public System.Type EntityClass
{
get { return entityClass; }
@@ -20,5 +39,23 @@ public object Id
{
get { return id; }
}
+
+ public string IdInString
+ {
+ get { return idInString; }
+ }
+
+ public Document Document
+ {
+ get { return document; }
+ }
+
+ public bool IsBatch
+ {
+ get { return isBatch; }
+ set { isBatch = value; }
+ }
+
+ #endregion
}
}
2  src/NHibernate.Search/Impl/LuceneWorkspace.cs → src/NHibernate.Search/Backend/LuceneWorkspace.cs
View
@@ -9,7 +9,7 @@
namespace NHibernate.Search.Impl
{
-//TODO introduce the notion of read only IndexReader? We cannot enforce it because Lucene use abstract classes, not interfaces
+ //TODO introduce the notion of read only IndexReader? We cannot enforce it because Lucene use abstract classes, not interfaces
/// <summary>
/// Lucene workspace
/// This is not intended to be used in a multithreaded environment
14 src/NHibernate.Search/Backend/PurgeAllLuceneWork.cs
View
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace NHibernate.Search.Backend
+{
+ public class PurgeAllLuceneWork : LuceneWork
+ {
+ public PurgeAllLuceneWork(System.Type entity)
+ : base(null, null, entity, null)
+ {
+ }
+ }
+}
14 src/NHibernate.Search/Backend/WorkType.cs
View
@@ -4,6 +4,18 @@ public enum WorkType
{
Add,
Delete,
- Update
+ Update,
+ /// <summary>
+ /// Used to remove a specific instance of a class from an index.
+ /// </summary>
+ Purge,
+ /// <summary>
+ /// Used to remove all instances of a class from an index.
+ /// </summary>
+ PurgeAll,
+ /// <summary>
+ /// Used for batch indexing.
+ /// </summary>
+ Index
}
}
85 src/NHibernate.Search/Engine/DocumentBuilder.cs
View
@@ -178,45 +178,50 @@ public void AddToWorkQueue(object entity, object id, WorkType workType, List<Luc
return;
}
/*bool searchForContainers = false;*/
- if (workType == WorkType.Add)
- {
- Document doc = GetDocument(entity, id);
- queue.Add(new AddLuceneWork(id, entityClass, doc));
- /*searchForContainers = true;*/
- }
- else if (workType == WorkType.Delete)
- {
- queue.Add(new DeleteLuceneWork(id, entityClass));
- }
- else if (workType == WorkType.Update)
- {
- Document doc = GetDocument(entity, id);
- /**
- * even with Lucene 2.1, use of indexWriter to update is not an option
- * We can only delete by term, and the index doesn't have a term that
- * uniquely identify the entry.
- * But essentially the optimization we are doing is the same Lucene is doing, the only extra cost is the
- * double file opening.
- */
- queue.Add(new DeleteLuceneWork(id, entityClass));
- queue.Add(new AddLuceneWork(id, entityClass, doc));
- /*searchForContainers = true;*/
- }
- else
+ string idString = idBridge.ObjectToString(id);
+
+ switch (workType)
{
- throw new AssertionFailure("Unknown WorkType: " + workType);
+ case WorkType.Add:
+ queue.Add(new AddLuceneWork(id, idString, entityClass, GetDocument(entity, id)));
+ /*searchForContainers = true;*/
+ break;
+
+ case WorkType.Delete:
+ case WorkType.Purge:
+ queue.Add(new DeleteLuceneWork(id, idString, entityClass));
+ break;
+
+ case WorkType.PurgeAll:
+ queue.Add(new PurgeAllLuceneWork(entityClass));
+ break;
+
+ case WorkType.Update:
+ /**
+ * even with Lucene 2.1, use of indexWriter to update is not an option
+ * We can only delete by term, and the index doesn't have a term that
+ * uniquely identify the entry.
+ * But essentially the optimization we are doing is the same Lucene is doing, the only extra cost is the
+ * double file opening.
+ */
+ queue.Add(new DeleteLuceneWork(id, idString, entityClass));
+ queue.Add(new AddLuceneWork(id, idString, entityClass, GetDocument(entity, id)));
+ /*searchForContainers = true;*/
+ break;
+
+ default:
+ throw new AssertionFailure("Unknown WorkType: " + workType);
}
/**
- * When references are changed, either null or another one, we expect dirty checking to be triggered (both sides
- * have to be updated)
- * When the internal object is changed, we apply the {Add|Update}Work on containedIns
-
- if (searchForContainers)
- {
- processContainedIn(entity, queue, rootPropertiesMetadata, searchFactory);
- }
- **/
+ * When references are changed, either null or another one, we expect dirty checking to be triggered (both sides
+ * have to be updated)
+ * When the internal object is changed, we apply the {Add|Update}Work on containedIns
+ */
+ /*
+ if (searchForContainers)
+ processContainedIn(entity, queue, rootPropertiesMetadata, searchFactory);
+ */
}
/*
@@ -224,15 +229,13 @@ private void processContainedIn(Object instance, List<LuceneWork> queue, Propert
{
not supported
}
+ */
- private void processContainedInValue(
- Object value, List<LuceneWork> queue, Class valueClass,
- DocumentBuilder builder, SearchFactory searchFactory)
+ private void ProcessContainedInValue(object value, List<LuceneWork> queue, System.Type valueClass, DocumentBuilder builder, SearchFactory searchFactory)
{
- Serializable id = (Serializable) builder.GetMemberValue(value, builder.idGetter);
- builder.AddToWorkQueue(value, id, WorkType.UPDATE, queue, searchFactory);
+ object id = DocumentBuilder.GetMemberValue(value, builder.idGetter);
+ builder.AddToWorkQueue(value, id, WorkType.Update, queue, searchFactory);
}
- */
public Document GetDocument(object instance, object id)
{
13 src/NHibernate.Search/IFullTextSession.cs
View
@@ -16,5 +16,18 @@ public interface IFullTextSession : ISession
/// Indexation is batched per transaction</summary>
/// <param name="entity"></param>
IFullTextSession Index(Object entity);
+
+ /// <summary>
+ /// Purge the instance with the specified identity from the index, but not the database.
+ /// </summary>
+ /// <param name="clazz"></param>
+ /// <param name="id"></param>
+ void Purge(System.Type clazz, object id);
+
+ /// <summary>
+ /// Purge all instances from the index, but not the database.
+ /// </summary>
+ /// <param name="clazz"></param>
+ void PurgeAll(System.Type clazz);
}
}
91 src/NHibernate.Search/Query/FullTextSessionImpl.cs → src/NHibernate.Search/Impl/FullTextSessionImpl.cs
View
@@ -21,41 +21,8 @@ public FullTextSessionImpl(ISession session)
this.session = session;
}
-
- public IQuery CreateFullTextQuery<TEntity>(string defaultField, string queryString)
- {
- QueryParser queryParser = new QueryParser(defaultField, new StandardAnalyzer());
- Query query = queryParser.Parse(queryString);
- return CreateFullTextQuery(query, typeof (TEntity));
- }
-
- public IQuery CreateFullTextQuery<TEntity>(string queryString)
- {
- QueryParser queryParser = new QueryParser("",new StandardAnalyzer());
- Query query = queryParser.Parse(queryString);
- return CreateFullTextQuery(query, typeof (TEntity));
- }
-
- public IQuery CreateFullTextQuery(Query luceneQuery, params System.Type[] entities)
- {
- return new FullTextQueryImpl(luceneQuery, entities, (ISessionImplementor)session);
- }
-
- public IFullTextSession Index(object entity)
- {
- SearchFactory searchFactory = SearchFactory.GetSearchFactory(session);
- object id = session.GetIdentifier(entity);
- searchFactory.PerformWork(entity, id, session, WorkType.Update);
- return this;
- }
-
- public void Dispose()
- {
- session.Dispose();
- }
-
-
#region Delegating to Inner Session
+
public void Flush()
{
session.Flush();
@@ -400,5 +367,61 @@ public ISessionImplementor GetSessionImplementation()
#endregion
+
+ public IQuery CreateFullTextQuery<TEntity>(string defaultField, string queryString)
+ {
+ QueryParser queryParser = new QueryParser(defaultField, new StandardAnalyzer());
+ Query query = queryParser.Parse(queryString);
+ return CreateFullTextQuery(query, typeof(TEntity));
+ }
+
+ public IQuery CreateFullTextQuery<TEntity>(string queryString)
+ {
+ QueryParser queryParser = new QueryParser("", new StandardAnalyzer());
+ Query query = queryParser.Parse(queryString);
+ return CreateFullTextQuery(query, typeof(TEntity));
+ }
+
+ public IQuery CreateFullTextQuery(Query luceneQuery, params System.Type[] entities)
+ {
+ return new FullTextQueryImpl(luceneQuery, entities, (ISessionImplementor)session);
+ }
+
+ public IFullTextSession Index(object entity)
+ {
+ SearchFactory searchFactory = SearchFactory.GetSearchFactory(session);
+
+ // TODO: Check to see this entity type is indexed
+ object id = session.GetIdentifier(entity);
+ searchFactory.PerformWork(entity, id, session, WorkType.Update);
+
+ // TODO: Why do we return this?
+ return this;
+ }
+
+ public void PurgeAll(System.Type entity)
+ {
+ Purge(entity, null);
+ }
+
+ public void Purge(System.Type entity, object id)
+ {
+ if (entity == null) return;
+ WorkType workType;
+
+ // TODO: Check to see this entity type is indexed
+ SearchFactory searchFactory = SearchFactory.GetSearchFactory(session);
+ if (id == null)
+ workType = WorkType.PurgeAll;
+ else
+ workType = WorkType.Purge;
+
+ searchFactory.PerformWork(entity, id, session, workType);
+ }
+
+ public void Dispose()
+ {
+ session.Dispose();
+ }
}
}
5 src/NHibernate.Search/NHibernate.Search.csproj
View
@@ -53,6 +53,7 @@
<Compile Include="AttributeUtil.cs" />
<Compile Include="Backend\Impl\Lucene\LuceneBackendQueueProcessor.cs" />
<Compile Include="Backend\Impl\Lucene\LuceneBackendQueueProcessorFactory.cs" />
+ <Compile Include="Backend\PurgeAllLuceneWork.cs" />
<Compile Include="Query\FullTextSearchHelper.cs" />
<Compile Include="LuceneQueryExpression.cs" />
<Compile Include="Backend\Impl\Lucene\LuceneWorker.cs" />
@@ -78,7 +79,7 @@
<Compile Include="Backend\IQueueingProcessor.cs" />
<Compile Include="Bridge\Builtin\StringBridge.cs" />
<Compile Include="Environment.cs" />
- <Compile Include="Query\FullTextSessionImpl.cs" />
+ <Compile Include="Impl\FullTextSessionImpl.cs" />
<Compile Include="Index.cs" />
<Compile Include="Backend\LuceneWork.cs" />
<Compile Include="SearchException.cs" />
@@ -89,7 +90,7 @@
<Compile Include="Bridge\TwoWayString2FieldBridgeAdaptor.cs" />
<Compile Include="Bridge\Builtin\ValueTypeBridge.cs" />
<Compile Include="Backend\Work.cs" />
- <Compile Include="Impl\LuceneWorkspace.cs" />
+ <Compile Include="Backend\LuceneWorkspace.cs" />
<Compile Include="Backend\WorkType.cs" />
<Compile Include="Bridge\IStringBridge.cs" />
<Compile Include="Bridge\ITwoWayFieldBridge.cs" />
Please sign in to comment.
Something went wrong with that request. Please try again.