Esempio n. 1
0
        private void ProcessQueue(DataTable q, int siteId, string indexPath)
        {
            rowsProcessed = 0;
            rowsToProcess = q.Rows.Count;

            // first process deletes with reader
            try
            {
                using (Lucene.Net.Store.Directory searchDirectory = IndexHelper.GetDirectory(siteId))
                {
                    using (IndexReader reader = IndexReader.Open(searchDirectory, false))
                    {
                        foreach (DataRow row in q.Rows)
                        {
                            Term term = new Term("Key", row["ItemKey"].ToString());
                            try
                            {
                                reader.DeleteDocuments(term);
                                log.Debug("reader.DeleteDocuments(term) for Key " + row["ItemKey"].ToString());
                            }
                            catch (Exception ge)
                            {
                                // TODO: monitor what real exceptions if any occur and then
                                // change this catch to catch only the expected ones
                                // instead of non specific exception
                                log.Error(ge);
                            }

                            bool removeOnly = Convert.ToBoolean(row["RemoveOnly"]);
                            if (removeOnly)
                            {
                                Int64 rowId = Convert.ToInt64(row["RowId"]);
                                IndexingQueue.Delete(rowId);
                            }


                            if (DateTime.UtcNow > nextStatusUpdateTime)
                            {
                                // don't mark as complete because there may be more qu items
                                //for different index paths in a multi site installation
                                bool markAsComplete = false;
                                ReportStatus(markAsComplete);
                            }
                        }
                    }
                }
            }
            catch (System.IO.IOException ex)
            {
                log.Info("IndexWriter swallowed exception this is not unexpected if building or rebuilding the search index ", ex);
                errorCount += 1;
            }
            catch (TypeInitializationException ex)
            {
                log.Info("IndexWriter swallowed exception ", ex);
                errorCount += 1;
            }


            // next add items with writer
            using (Lucene.Net.Store.Directory searchDirectory = IndexHelper.GetDirectory(siteId))
            {
                using (IndexWriter indexWriter = GetWriter(siteId, searchDirectory))
                {
                    if (indexWriter == null)
                    {
                        log.Error("failed to get IndexWriter for path: " + indexPath);
                        errorCount += 1;
                        return;
                    }

                    foreach (DataRow row in q.Rows)
                    {
                        bool removeOnly = Convert.ToBoolean(row["RemoveOnly"]);
                        if (!removeOnly)
                        {
                            try
                            {
                                IndexItem indexItem
                                    = (IndexItem)SerializationHelper.DeserializeFromString(typeof(IndexItem), row["SerializedItem"].ToString());
                                // if the content is locked down to only admins it is a special case
                                // we just won't add it to the search index
                                // because at search time we avoid the role check for all admins, content admins and siteeditors
                                // we don't have a good way to prevent content admins and site editors from seeing the content
                                // in search
                                if ((indexItem.ViewRoles != "Admins;") && (indexItem.ModuleViewRoles != "Admins;"))
                                {
                                    if (indexItem.ViewPage.Length > 0)
                                    {
                                        Document doc = GetDocument(indexItem);
                                        WriteToIndex(doc, indexWriter);
                                        log.Debug("called WriteToIndex(doc, indexWriter) for key " + indexItem.Key);
                                    }
                                }

                                Int64 rowId = Convert.ToInt64(row["RowId"]);
                                IndexingQueue.Delete(rowId);
                            }
                            catch (Exception ex)
                            {
                                log.Error(ex);
                            }
                        }

                        if (DateTime.UtcNow > nextStatusUpdateTime)
                        {
                            // don't mark as complete because there may be more qu items
                            //for different index paths in a multi site installation
                            bool markAsComplete = false;
                            ReportStatus(markAsComplete);
                        }
                    }

                    try
                    {
                        indexWriter.Optimize();
                    }
                    catch (System.IO.IOException ex)
                    {
                        log.Error(ex);
                    }
                }
            }
        }