class AsyncIndexer
{
public static int debugPause;
Queue<string> urlQueue = new Queue<string>();
public int QueueSize { get { return urlQueue.Count; } }
bool busy = false;
static AsyncIndexer instance = null;
public static AsyncIndexer GetInstance()
{
if (instance == null) instance = new AsyncIndexer();
return instance;
}
delegate void IndexDelegate(Keyoti.SearchEngine.Configuration config);
public void QueueForIndexing(string documentURL, Keyoti.SearchEngine.Configuration config)
{
//add new URL to queue
urlQueue.Enqueue(documentURL);
IndexDelegate indexDelegate = new IndexDelegate(delegate(Keyoti.SearchEngine.Configuration configuration)
{
//do indexing work here
if (!busy)
{
busy = true;
//this outer 'while' just takes care of anything added to the queue during the documentIndex.Close() call.
while (urlQueue.Count > 0)
ProcessQueueItems(configuration);
busy = false;
}
});
IAsyncResult ar = indexDelegate.BeginInvoke(config, new AsyncCallback(MyCallback), null);
}
void ProcessQueueItems(Keyoti.SearchEngine.Configuration configration)
{
Keyoti.SearchEngine.Index.DocumentIndex documentIndex = new Keyoti.SearchEngine.Index.DocumentIndex(configration);
try
{
//while anything is waiting in the queue
while (urlQueue.Count > 0)
{
documentIndex.AddDocument(new Keyoti.SearchEngine.Documents.Document(urlQueue.Dequeue(), configration));
System.Threading.Thread.Sleep(debugPause);
}
}
finally
{
documentIndex.Close();
}
}
void MyCallback(IAsyncResult ar)
{
AsyncResult aResult = (AsyncResult)ar;
IndexDelegate idxDelegate = (IndexDelegate)aResult.AsyncDelegate;
idxDelegate.EndInvoke(ar);
}
}