Permalink
Browse files

Cleaned up a bit on storage

  • Loading branch information...
1 parent f6c305e commit 2638e22de6ef7824b97d10976cea1dacf4897275 @brunomlopes committed Nov 27, 2011
Showing with 43 additions and 23 deletions.
  1. +3 −3 src/Core/Lucene/LuceneStorage.cs
  2. +31 −19 src/Core/Lucene/SourceStorage.cs
  3. +9 −1 src/Plugins/Diagnostics/DumpAllIndexes.cs
@@ -38,7 +38,7 @@ public void UpdateDocumentForItem<T>(IndexWriter writer, IItemSource source, str
var documentId = id.GetId();
var learningId = id.GetLearningId();
- PopDocument(writer, documentId); //deleting the old version of the doc
+ PopDocumentIfExists(writer, documentId); //deleting the old version of the doc
document.SetLearnings(_learningRepository.LearningsFor(learningId));
document.Tag(tag);
@@ -60,7 +60,7 @@ public void LearnCommandForInput(IndexWriter writer, DocumentId completionId, st
// fickle command, isn't learnable
if (completionId == null) return;
- var document = CoreDocument.Rehydrate(PopDocument(writer, completionId.GetId()));
+ var document = CoreDocument.Rehydrate(PopDocumentIfExists(writer, completionId.GetId()));
if (document == null)
throw new InvalidOperationException(string.Format("Didn't find command {0}", completionId));
@@ -83,7 +83,7 @@ public void DeleteDocumentsForSourceWithoutTag(IndexWriter indexWriter, IItemSou
indexWriter.DeleteDocuments(query);
}
- private Document PopDocument(IndexWriter writer, string sha1)
+ private Document PopDocumentIfExists(IndexWriter writer, string sha1)
{
var searcher = new IndexSearcher(writer.GetDirectory(), false);
try
@@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Core.API;
using Core.Abstractions;
+using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.Store;
@@ -15,14 +16,15 @@ public class SourceStorage
private readonly IItemSource _source;
private readonly Directory _indexDirectory;
private readonly LuceneStorage _storage;
+ private object _sourceWriteLock = new object();
public SourceStorage(IItemSource source, Directory indexDirectory, LuceneStorage storage)
{
_source = source;
_indexDirectory = indexDirectory;
_storage = storage;
-
- EnsureIndexExists();
+
+ EnsureIndexExistsAndThereIsNoWriteLock();
}
public IItemSource Source
@@ -39,34 +41,44 @@ public Task IndexItems()
.ContinueWith(task => IndexItems(_source, task.Result));
}
- private void EnsureIndexExists()
+ private void EnsureIndexExistsAndThereIsNoWriteLock()
{
+ var createDir = true;
var dir = _indexDirectory as FSDirectory;
if (dir != null)
- new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_29), !dir.GetDirectory().Exists,
- IndexWriter.MaxFieldLength.UNLIMITED).Close();
+ {
+ createDir = !dir.GetDirectory().Exists;
+ }
+
+ _indexDirectory.ClearLock("write.lock");
+ new IndexWriter(_indexDirectory, new SimpleAnalyzer(),
+ createDir,
+ IndexWriter.MaxFieldLength.UNLIMITED).Close();
}
private void IndexItems(IItemSource source, IEnumerable<object> items)
{
- IndexWriter indexWriter = null;
- try
+ lock (_sourceWriteLock)
{
- indexWriter = GetIndexWriter();
- var newTag = Guid.NewGuid().ToString();
-
- foreach (var item in items)
+ IndexWriter indexWriter = null;
+ try
{
- _storage.UpdateDocumentForObject(indexWriter, source, newTag, item);
- }
+ indexWriter = GetIndexWriter();
+ var newTag = Guid.NewGuid().ToString();
- _storage.DeleteDocumentsForSourceWithoutTag(indexWriter, source, newTag);
+ foreach (var item in items)
+ {
+ _storage.UpdateDocumentForObject(indexWriter, source, newTag, item);
+ }
- indexWriter.Commit();
- }
- finally
- {
- if (indexWriter != null) indexWriter.Close();
+ _storage.DeleteDocumentsForSourceWithoutTag(indexWriter, source, newTag);
+
+ indexWriter.Commit();
+ }
+ finally
+ {
+ if (indexWriter != null) indexWriter.Close();
+ }
}
}
@@ -25,7 +25,15 @@ public class DumpAllIndexes : BaseCommand
public override void Act()
{
var indexDirectory = Path.Combine(CoreConfiguration.DataDirectory, "MergedIndexes");
- var mergedDirectory = new SimpleFSDirectory(new DirectoryInfo(indexDirectory));
+
+ var directoryInfo = new DirectoryInfo(indexDirectory);
+ if(directoryInfo.Exists)
+ {
+ directoryInfo.Delete(true);
+ directoryInfo.Refresh();
+ }
+
+ var mergedDirectory = new SimpleFSDirectory(directoryInfo);
var mergedIndex = new IndexWriter(mergedDirectory, new SimpleAnalyzer(), true,
IndexWriter.MaxFieldLength.UNLIMITED);

0 comments on commit 2638e22

Please sign in to comment.