This project is read-only.

Processing the index queue

Important: SimpleLucene does NOT process the index queue for you. It only provides a queue for storing index tasks.

The following test demonstrates how you can process the queue. Since you only want to have one IIndexService instance per index location (to prevent locking issues and for better performance) you can use the "WithOptions" extension method to evaluate a list of IndexServices to determine whether one exists that has the same IndexOptions (or specifically, location) as the task you are attempting to execute.

        [Test]
        public void Can_resolve_index_services_when_processing_index_queue()
        {
            var t1 = new EntityUpdateTask<Product>(new Product(), new ProductIndexDefinition(), new TestIndexLocation("products"));
            var t2 = new EntityUpdateTask<Product>(new Product(), new ProductIndexDefinition(), new TestIndexLocation("products"));
            var t3 = new EntityUpdateTask<Product>(new Product(), new ProductIndexDefinition(), new TestIndexLocation("products2"));

            Action<IIndexTask> queue = t => IndexQueue.Instance.Queue(t);

            queue(t1);
            queue(t2);
            queue(t3);

            var services = new HashSet<IIndexService>();

            IIndexTask task;
            while (IndexQueue.Instance.TryDequeue(out task))
            {
                var service = services.WithOptions(task.IndexOptions);

                if (service == null) {
                    service = new IndexService(new DirectoryIndexWriter(task.IndexOptions.IndexLocation.GetDirectory(), task.IndexOptions.RecreateIndex));
                    services.Add(service);
                }

                // process the task
                task.Execute(service);
            }

            Assert.IsTrue(services.Count == 2);
        }

Since the IndexQueue is essentially a wrapper around a .NET ConcurrentQueue, we use the TryDequeue method to remove an item from the queue. Next we try to locate a matching IIndexService instance and create one if necessary. Finally we process the task by calling Execute and passing in the appropriate IIndexService.

The assertion here is that only 2 services were created since 2 of the 3 tasks are for the same index location.

Last edited Apr 20, 2011 at 3:49 PM by retroviz, version 1

Comments

No comments yet.