Permalink
Browse files

Implements Optimize and PurgeAll tasks

SVN: branches/1.2.x@3133
  • Loading branch information...
1 parent 738e307 commit f44afcd41666384671b96d653928fbe7e42f5c5d @phatcher phatcher committed Nov 22, 2007
@@ -9,12 +9,8 @@
-->
<configuration>
<configSections>
- <section name="nhibernate"
- type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"
- requirePermission="false" /><!-- Important under Medium Trust -->
- <section name="log4net"
- type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"
- requirePermission="false" /><!-- Important under Medium Trust -->
+ <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/><!-- Important under Medium Trust -->
+ <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false"/><!-- Important under Medium Trust -->
</configSections>
<appSettings/>
<connectionStrings/>
@@ -25,7 +21,9 @@
affects performance, set this value to true only
during development.
-->
- <compilation debug="true"/>
+ <compilation debug="true">
+ <assemblies>
+ <add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
@@ -44,42 +42,34 @@
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
- <pages enableViewState="false" enableViewStateMac="false" />
- <trust level="Medium" />
+ <pages enableViewState="false" enableViewStateMac="false"/>
+ <trust level="Medium"/>
<httpModules>
- <add name="CurrentSessionModule" type="NHibernate.Example.Web.CurrentSessionModule" />
+ <add name="CurrentSessionModule" type="NHibernate.Example.Web.CurrentSessionModule"/>
</httpModules>
</system.web>
-
<nhibernate>
- <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
- <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
- <add key="hibernate.connection.connection_string" value="Server=(local);initial catalog=nhibernate;Integrated Security=true" />
- <add key="hibernate.bytecode.provider" value="null" /><!-- Important under Medium Trust -->
- <add key="hibernate.current_session_context_class" value="managed_web" />
+ <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
+ <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
+ <add key="hibernate.connection.connection_string" value="Server=(local);initial catalog=nhibernate;Integrated Security=true"/>
+ <add key="hibernate.bytecode.provider" value="null"/><!-- Important under Medium Trust -->
+ <add key="hibernate.current_session_context_class" value="managed_web"/>
</nhibernate>
-
<log4net>
<!-- Define some output appenders -->
- <appender name="trace"
- type="log4net.Appender.TraceAppender, log4net">
+ <appender name="trace" type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
- <param name="ConversionPattern"
- value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
+ <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
-
- <appender name="console"
- type="log4net.Appender.ConsoleAppender, log4net">
+ <appender name="console" type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
- <param name="ConversionPattern"
- value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
+ <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
-
<root>
- <priority value="WARN" />
- <appender-ref ref="trace" />
+ <priority value="WARN"/>
+ <appender-ref ref="trace"/>
</root>
</log4net>
</configuration>
@@ -24,7 +24,7 @@ public LuceneBackendQueueProcessor(List<LuceneWork> queue, SearchFactory searchF
/// We rely on the both the DocumentBuilder.GetHashCode() and the GetWorkHashCode() to
/// sort them by predictive order at all times, and to put deletes before adds
/// </summary>
- private static void SortQueueToAvoidDeadLocks(List<LuceneWork> queue, LuceneWorkspace luceneWorkspace)
+ private static void SortQueueToAvoidDeadLocks(List<LuceneWork> queue, Workspace luceneWorkspace)
{
queue.Sort(delegate(LuceneWork x, LuceneWork y)
{
@@ -38,29 +38,33 @@ private static void SortQueueToAvoidDeadLocks(List<LuceneWork> queue, LuceneWork
});
}
- private static long GetWorkHashCode(LuceneWork luceneWork, LuceneWorkspace luceneWorkspace)
+ private static long GetWorkHashCode(LuceneWork luceneWork, Workspace luceneWorkspace)
{
long h = luceneWorkspace.GetDocumentBuilder(luceneWork.EntityClass).GetHashCode()*2;
if (luceneWork is AddLuceneWork)
h += 1; //addwork after deleteWork
return h;
}
- public void Run(object ignored)
+ /// <summary>
+ ///
+ /// </summary>
+ /// <param name="ignore">Ignored, used to keep the delegate signature that WaitWithCallback requires</param>
+ public void Run(object ignore)
{
- LuceneWorkspace luceneWorkspace = new LuceneWorkspace(searchFactory);
- LuceneWorker worker = new LuceneWorker(luceneWorkspace);
+ Workspace workspace = new Workspace(searchFactory);
+ LuceneWorker worker = new LuceneWorker(workspace);
try
{
- SortQueueToAvoidDeadLocks(queue, luceneWorkspace);
+ SortQueueToAvoidDeadLocks(queue, workspace);
foreach (LuceneWork luceneWork in queue)
{
- worker.PerformWork(luceneWork);
+ worker.PerformWork(new LuceneWorker.WorkWithPayload(luceneWork, null));
}
}
finally
{
- luceneWorkspace.Dispose();
+ workspace.Dispose();
queue.Clear();
}
}
@@ -6,22 +6,49 @@
using NHibernate.Search.Backend;
using NHibernate.Search.Engine;
using NHibernate.Search.Impl;
+using NHibernate.Search.Storage;
namespace NHibernate.Search.Backend.Impl.Lucene
{
public class LuceneWorker
{
private static readonly ILog log = LogManager.GetLogger(typeof(LuceneWorker));
- private readonly LuceneWorkspace workspace;
+ private readonly Workspace workspace;
- public LuceneWorker(LuceneWorkspace workspace)
+ public LuceneWorker(Workspace workspace)
{
this.workspace = workspace;
}
+ #region Nested classes: WorkWithPayload
+
+ public class WorkWithPayload
+ {
+ private readonly LuceneWork work;
+ private readonly IDirectoryProvider provider;
+
+ public WorkWithPayload(LuceneWork work, IDirectoryProvider provider)
+ {
+ this.work = work;
+ this.provider = provider;
+ }
+
+ public LuceneWork Work
+ {
+ get { return work; }
+ }
+
+ public IDirectoryProvider Provider
+ {
+ get { return provider; }
+ }
+ }
+
+ #endregion
+
#region Private methods
- private void Add(System.Type entity, object id, Document document)
+ private void Add(System.Type entity, object id, Document document, IDirectoryProvider provider)
{
if (log.IsDebugEnabled)
log.Debug("Add to Lucene index: " + entity + "#" + id + ": " + document);
@@ -36,13 +63,13 @@ private void Add(System.Type entity, object id, Document document)
}
}
- private void Remove(System.Type entity, object id)
+ private void Remove(System.Type entity, object id, IDirectoryProvider provider)
{
- /**
- * even with Lucene 2.1, use of indexWriter to delte is not an option
- * We can only delete by term, and the index doesn't have a termt that
- * uniquely identify the entry. See logic below
- */
+ /*
+ * even with Lucene 2.1, use of indexWriter to delte is not an option
+ * We can only delete by term, and the index doesn't have a termt that
+ * uniquely identify the entry. See logic below
+ */
log.DebugFormat("remove from Lucene index: {0}#{1}", entity, id);
DocumentBuilder builder = workspace.GetDocumentBuilder(entity);
Term term = builder.GetTerm(id);
@@ -85,30 +112,64 @@ private void Remove(System.Type entity, object id)
#endregion
- public void PerformWork(AddLuceneWork work)
+ public void PerformWork(WorkWithPayload luceneWork)
{
- Add(work.EntityClass, work.Id, work.Document);
+ if (luceneWork.Work is AddLuceneWork)
+ PerformWork((AddLuceneWork)luceneWork.Work, luceneWork.Provider);
+ else if (luceneWork.Work is DeleteLuceneWork)
+ PerformWork((DeleteLuceneWork)luceneWork.Work, luceneWork.Provider);
+ else if (luceneWork.Work is OptimizeLuceneWork)
+ PerformWork((OptimizeLuceneWork)luceneWork.Work, luceneWork.Provider);
+ else if (luceneWork.Work is PurgeAllLuceneWork)
+ PerformWork((PurgeAllLuceneWork)luceneWork.Work, luceneWork.Provider);
+ else
+ throw new AssertionFailure("Unknown work type: " + luceneWork.GetType());
}
- public void PerformWork(LuceneWork luceneWork)
+ public void PerformWork(AddLuceneWork work, IDirectoryProvider provider)
{
- if (luceneWork is AddLuceneWork)
- {
- PerformWork((AddLuceneWork)luceneWork);
- }
- else if (luceneWork is DeleteLuceneWork)
+ Add(work.EntityClass, work.Id, work.Document, provider);
+ }
+
+ public void PerformWork(DeleteLuceneWork work, IDirectoryProvider provider)
+ {
+ Remove(work.EntityClass, work.Id, provider);
+ }
+
+ public void PerformWork(OptimizeLuceneWork work, IDirectoryProvider provider)
+ {
+ System.Type entity = work.EntityClass;
+ if (log.IsDebugEnabled)
+ log.Debug("Optimize Lucene index: " + entity);
+ IndexWriter writer = workspace.GetIndexWriter(entity);
+
+ try
{
- PerformWork((DeleteLuceneWork)luceneWork);
+ writer.Optimize();
+ //workspace.Optimize(provider);
}
- else
+ catch (IOException e)
{
- throw new AssertionFailure("Unknown work type: " + luceneWork.GetType());
+ throw new SearchException("Unable to optimize Lucene index: " + entity, e);
}
}
- public void PerformWork(DeleteLuceneWork work)
+ public void PerformWork(PurgeAllLuceneWork work, IDirectoryProvider provider)
{
- Remove(work.EntityClass, work.Id);
+ System.Type entity = work.EntityClass;
+ if (log.IsDebugEnabled)
+ log.Debug("PurgeAll Lucene index: " + entity);
+
+ IndexReader reader = workspace.GetIndexReader(entity);
+ try
+ {
+ Term term = new Term(DocumentBuilder.CLASS_FIELDNAME, entity.Name);
+ reader.DeleteDocuments(term);
+ }
+ catch (Exception e)
+ {
+ throw new SearchException("Unable to purge all from Lucene index: " + entity, e);
+ }
}
}
}
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace NHibernate.Search.Backend
+{
+ public class OptimizeLuceneWork : LuceneWork
+ {
+ public OptimizeLuceneWork(System.Type entity) : base(null, null, entity)
+ {
+ }
+ }
+}
@@ -19,15 +19,15 @@ namespace NHibernate.Search.Impl
/// The recommended approach is to execute all the modifications on the IndexReaders, {@link #Dispose()} }, and acquire the
/// index writers
/// </summary>
- public class LuceneWorkspace : IDisposable
+ public class Workspace : IDisposable
{
- private static ILog log = LogManager.GetLogger(typeof (LuceneWorkspace));
+ private static ILog log = LogManager.GetLogger(typeof(Workspace));
private Dictionary<IDirectoryProvider, IndexReader> readers = new Dictionary<IDirectoryProvider, IndexReader>();
private Dictionary<IDirectoryProvider, IndexWriter> writers = new Dictionary<IDirectoryProvider, IndexWriter>();
private List<IDirectoryProvider> lockedProviders = new List<IDirectoryProvider>();
private SearchFactory searchFactory;
- public LuceneWorkspace(SearchFactory searchFactory)
+ public Workspace(SearchFactory searchFactory)
{
this.searchFactory = searchFactory;
}
@@ -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\OptimizeLuceneWork.cs" />
<Compile Include="Backend\PurgeAllLuceneWork.cs" />
<Compile Include="Query\FullTextSearchHelper.cs" />
<Compile Include="LuceneQueryExpression.cs" />
@@ -90,7 +91,7 @@
<Compile Include="Bridge\TwoWayString2FieldBridgeAdaptor.cs" />
<Compile Include="Bridge\Builtin\ValueTypeBridge.cs" />
<Compile Include="Backend\Work.cs" />
- <Compile Include="Backend\LuceneWorkspace.cs" />
+ <Compile Include="Backend\Workspace.cs" />
<Compile Include="Backend\WorkType.cs" />
<Compile Include="Bridge\IStringBridge.cs" />
<Compile Include="Bridge\ITwoWayFieldBridge.cs" />

0 comments on commit f44afcd

Please sign in to comment.