Beispiel #1
0
 public override void ClearLock(string name)
 {
     lock (_locks)
     {
         if (_locks.ContainsKey(name))
         {
             _locks[name].BreakLock();
         }
     }
     _cacheDirectory.ClearLock(name);
 }
        public void Rebuild()
        {
            if (Rebuilding)
            {
                return;
            }
            Rebuilt = 0;
            Task.Run(() =>
            {
                try
                {
                    Rebuilding = true;
                    lock (_locker)
                    {
                        _dir.ClearLock("rebuilding");
                        using (var writer = new IndexWriter(_dir, Analyzer, IndexWriter.MaxFieldLength.UNLIMITED))
                        {
                            writer.DeleteAll();
                        }
                    }

                    DateTime start = DateTime.Now.Subtract(TimeSpan.FromDays(_recordDays));
                    while (start < DateTime.Now.AddDays(1))
                    {
                        string file = _dataDirectory + "/source/" + start.ToString("yyyy-MMM-dd") + "/source.src";
                        if (File.Exists(file))
                        {
                            byte[] txt = File.ReadAllBytes(file);
                            foreach (string entry in StringZipper.Unzip(txt).Split(new[] { "<|||>" }, StringSplitOptions.RemoveEmptyEntries))
                            {
                                AuditSourceRecord record = _jsonSerializationService.DeserializeObject <AuditSourceRecord>(entry);
                                Log(record.Entry, record.Content, false);
                                Rebuilt++;
                            }
                        }

                        start = start.AddDays(1);
                    }
                }
                catch (Exception e)
                {
                    Sitecore.Diagnostics.Log.Error(e.ToString(), this);
                }
                finally
                {
                    Rebuilt    = -1;
                    Rebuilding = false;
                }
            });
        }
Beispiel #3
0
        public LuceneIndexer(bool useRamDirectory)
        {
            // Create directory
            string    path            = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
            var       luceneDirectory = new DirectoryInfo(Path.Combine(path, "LuceneIndex"));
            Directory directory       = useRamDirectory ? (Directory) new RAMDirectory() : new SimpleFSDirectory(luceneDirectory);

            directory.ClearLock("write");

            // Create index writer
            _indexWriter = new IndexWriter(directory, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);

            // Figure out the fields to index
            var dummyObj = new T();
            List <PropertyInfo> foundProps = dummyObj.LookForCustomAttribute(typeof(FullTextAttribute));

            FullTextFields  = foundProps.Select(x => x.Name).ToArray();
            PrimaryKeyField = PrimaryKeyProperty();
            ItemCache       = new ConcurrentDictionary <string, T>();
        }
Beispiel #4
0
 public override void ClearLock(string name)
 {
     @delegate.ClearLock(name);
 }
Beispiel #5
0
        /*
         * Phương thức tìm kiếm văn bản
         */
        public static List <Location> SearchQuery(string keyword)
        {
            // Biến toàn cục: Nơi lưu trữ các index
            Console.WriteLine("\n\n\n\n");
            Console.WriteLine("Keyword: " + keyword);
            using (var analyzer = new StandardAnalyzer(Version.LUCENE_29))
            {
                Lucene.Net.Store.Directory indexStore = FSDirectory.Open(indexDirIndex.FullName);
                if (indexStore.FileExists(IndexWriter.WRITE_LOCK_NAME))
                {
                    try
                    {
                        indexStore.ClearLock(IndexWriter.WRITE_LOCK_NAME);
                    }catch (IOException ioe)
                    {
                        Console.WriteLine("write.lock are being use by other process");
                    }
                }

                // Truyền query vào IndexSearcher
                Searcher search = new IndexSearcher(IndexReader.Open(indexStore, true));

                var fields      = new[] { "Name", "Type", "Content", "Path" };
                var queryParser = new MultiFieldQueryParser(Version.LUCENE_29, fields, analyzer);
                // Tạo truy vấn tìm kiếm
                var query = queryParser.Parse($"{keyword.ToLower()}*");

                /*Bắt đầu tìm kiếm. Có rất nhiều cách tìm kiếm @@
                 * Cách 1: Tìm dựa theo số lần xuất hiện*/
                TopScoreDocCollector cllctr = TopScoreDocCollector.Create(10, true); // true: bật sắp xếp theo thứ tự
                // Lấy các kết quả đạt yêu cầu query
                var hits = search.Search(query, 1000).ScoreDocs;
                // ScoreDoc[] hits = cllctr.TopDocs().ScoreDocs;

                // Vòng lặp lấy kết quả
                Console.WriteLine("Đã tìm thấy: {0} kết quả", hits.Length);

                List <Location> results = new List <Location>(hits.Length);
                foreach (var hit in hits)
                {
                    var foundDoc = search.Doc(hit.Doc);
                    Console.WriteLine("Folder: {0}, Path: {1}", foundDoc.Get("Name"), foundDoc.Get("Path"));
                    results.Add(new Location {
                        Name = foundDoc.Get("Name"), Path = foundDoc.Get("Path"), Type = foundDoc.Get("Type"), Content = foundDoc.Get("Content")
                    });
                }
                return(results);

                /*for(int hit_idx=0; hit_idx < hits.Length; hit_idx++)
                 * {
                 *  if(hit_idx == 0)
                 *  {
                 *      var foundDoc = search.Doc(hits[hit_idx].Doc);
                 *      Console.WriteLine("Root folder: {0}, File name: {1}", foundDoc.Get("Path"), foundDoc.Get("Filename"));
                 *  } else
                 *  {
                 *      var foundDoc = search.Doc(hits[hit_idx].Doc);
                 *      Console.WriteLine("Folder name: {0}", foundDoc.Get("Path"));
                 *  }
                 * }*/
            }
        }
Beispiel #6
0
 public override void ClearLock(string name)
 {
     m_input.ClearLock(name);
 }
Beispiel #7
0
        public override void Run()
        {
            Trace.WriteLine(DateTime.Now.ToString() + " [INIT]");

            // Create azure account credentials
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
            {
                configSetter(CloudSettingsResolver.GetConfigSetting(configName));
            });
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.FromConfigurationSetting("BlobStorageEndpoint");

            // Create reference to index queue
            CloudQueueClient client = cloudStorageAccount.CreateCloudQueueClient();
            CloudQueue queue = client.GetQueueReference("searchindexqueue");
            queue.CreateIfNotExist();
            queue.Clear();

            // Create lucene index writer and optimize index (only on startup)
            _Directory = new AzureDirectory(cloudStorageAccount, "LuceneStorage", new RAMDirectory());

            while (true)
            {
                try
                {
                    int queueItems = queue.RetrieveApproximateMessageCount();
                    Trace.WriteLine(DateTime.Now.ToString() + " [QUEUE_CHECK] " + queueItems + " items.");
                    if (queueItems > 0)
                    {
                        // Get index writer
                        if (IndexWriter.IsLocked(_Directory)) _Directory.ClearLock("write.lock");
                        IndexWriter indexWriter = new IndexWriter(_Directory, new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), false, new IndexWriter.MaxFieldLength(1000));
                        indexWriter.SetRAMBufferSizeMB(10.0);
                        indexWriter.SetUseCompoundFile(false);
                        indexWriter.SetMaxMergeDocs(10000);
                        indexWriter.SetMergeFactor(100);

                        try
                        {
                            // Get dictionary to skip duplicated queue items
                            Dictionary<long, DateTime> updatedSummaries = new Dictionary<long, DateTime>();

                            // Retrieve batch of messages to iterate
                            var msgs = queue.GetMessages(32);
                            while (msgs.Count() > 0)
                            {
                                foreach (var msg in msgs)
                                {
                                    Trace.WriteLine(DateTime.Now.ToString() + " [MSG] " + msg.AsString);
                                    long summaryId = long.Parse(msg.AsString);

                                    // zero = re-index the search index
                                    // positive number = add/update PointDataSummary to index
                                    // negative number = delete PointDataSummary from index
                                    if (summaryId == 0)
                                    {
                                        Trace.WriteLine(DateTime.Now.ToString() + " [REINDEX]");
                                        indexWriter.Close();
                                        indexWriter = new IndexWriter(_Directory, new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, new IndexWriter.MaxFieldLength(1000));
                                        indexWriter.SetRAMBufferSizeMB(10.0);
                                        indexWriter.SetUseCompoundFile(false);
                                        indexWriter.SetMaxMergeDocs(10000);
                                        indexWriter.SetMergeFactor(100);
                                        List<PointDataSummary> summaries = PointDataSummary.All().ToList();
                                        updatedSummaries = new Dictionary<long, DateTime>();
                                        foreach (var summary in summaries)
                                        {
                                            Index(indexWriter, summary);
                                            Trace.WriteLine(DateTime.Now.ToString() + " [INDEX] " + summary.Id);
                                            updatedSummaries.Add(summaryId, DateTime.UtcNow);
                                        }
                                    }
                                    else if (updatedSummaries.ContainsKey(summaryId) && ((DateTime)updatedSummaries[summaryId]) >= msg.InsertionTime)
                                    {
                                        Trace.WriteLine(DateTime.Now.ToString() + " [SKIPPED] " + msg.AsString);
                                    }
                                    else
                                    {
                                        if (summaryId < 0)
                                        {
                                            indexWriter.DeleteDocuments(new Lucene.Net.Index.Term("point_id", (-summaryId).ToString()));
                                        }
                                        else
                                        {
                                            var summary = PointDataSummary.SingleOrDefault(p => p.Id == summaryId);
                                            if (summary != null)
                                            {
                                                Index(indexWriter, summary);
                                                Trace.WriteLine(DateTime.Now.ToString() + " [INDEX] " + summary.Id);
                                            }
                                        }
                                        updatedSummaries.Remove(summaryId);
                                        updatedSummaries.Add(summaryId, DateTime.UtcNow);
                                    }
                                    // Delete message from queue
                                    queue.DeleteMessage(msg);
                                }
                                // Retrieve batch of messages to iterate
                                msgs = queue.GetMessages(32);
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine(ex.ToString());
                        }
                        finally
                        {
                            indexWriter.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(DateTime.Now + " [ERROR] " + ex.Message);
                }
                finally
                {
                    Trace.WriteLine(DateTime.Now.ToString() + " [QUEUE_CHECK] End");
                    Thread.Sleep(_SleepInMinutes * 60 * 1000);
                }
            }
        }
Beispiel #8
0
 public override void ClearLock(string name)
 {
     @in.ClearLock(name);
 }