public void Start()
        {
            string refreshPeriod = properties.ContainsKey("refresh") ? properties["refresh"] : "3600";
            long   period;

            if (!long.TryParse(refreshPeriod, out period))
            {
                period = 3600;
                log.Warn("Error parsing refresh period, defaulting to 1 hour");
            }

            log.DebugFormat("Refresh period {0} seconds", period);
            period *= 1000;  // per second

            try
            {
                // Copy to source
                if (File.Exists(Path.Combine(source, "current1")))
                {
                    current = 2;
                }
                else if (File.Exists(Path.Combine(source, "current2")))
                {
                    current = 1;
                }
                else
                {
                    log.DebugFormat("Source directory for '{0}' will be initialized", indexName);
                    current = 1;
                }

                string        currentString = current.ToString();
                DirectoryInfo subDir        = new DirectoryInfo(Path.Combine(source, currentString));
                FileHelper.Synchronize(indexDir, subDir, true);
                File.Delete(Path.Combine(source, "current1"));
                File.Delete(Path.Combine(source, "current2"));
                log.Debug("Current directory: " + current);
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }

            task  = new TriggerTask(this, indexName, source);
            timer = new Timer(task.Run, null, period, period);
        }
        public void Start()
        {
            string refreshPeriod = properties.ContainsKey("refresh") ? properties["refresh"] : "3600";
            long period;
            if (!long.TryParse(refreshPeriod, out period))
            {
                period = 3600;
                log.Warn("Error parsing refresh period, defaulting to 1 hour");
            }
            log.DebugFormat("Refresh period {0} seconds", period);
            period *= 1000;  // per second
            try
            {
                bool create;

                DirectoryInfo subDir = new DirectoryInfo(Path.Combine(indexName, "1"));
                create = !IndexReader.IndexExists(subDir.FullName);
                directory1 = FSDirectory.GetDirectory(subDir.FullName, create);
                if (create)
                {
                    log.DebugFormat("Initialize index: '{0}'", subDir.FullName);
                    IndexWriter iw1 = new IndexWriter(directory1, new StandardAnalyzer(), create);
                    iw1.Close();
                }

                subDir = new DirectoryInfo(Path.Combine(indexName, "2"));
                create = !IndexReader.IndexExists(subDir.FullName);
                directory2 = FSDirectory.GetDirectory(subDir.FullName, create);
                if (create)
                {
                    log.DebugFormat("Initialize index: '{0}'", subDir.FullName);
                    IndexWriter iw2 = new IndexWriter(directory2, new StandardAnalyzer(), create);
                    iw2.Close();
                }

                string current1Marker = Path.Combine(indexName, "current1");
                string current2Marker = Path.Combine(indexName, "current2");
                if (File.Exists(current1Marker))
                {
                    current = 1;
                }
                else if (File.Exists(current2Marker))
                {
                    current = 2;
                }
                else
                {
                    // no default
                    log.Debug("Setting directory 1 as current");
                    current = 1;
                    DirectoryInfo srcDir = new DirectoryInfo(source);
                    DirectoryInfo destDir = new DirectoryInfo(Path.Combine(indexName, current.ToString()));
                    int sourceCurrent = -1;
                    if (File.Exists(Path.Combine(srcDir.Name, "current1")))
                    {
                        sourceCurrent = 1;
                    }
                    else if (File.Exists(Path.Combine(srcDir.Name, "current2")))
                    {
                        sourceCurrent = 2;
                    }

                    if (sourceCurrent != -1)
                    {
                        try
                        {
                            FileHelper.Synchronize(
                                    new DirectoryInfo(Path.Combine(source, sourceCurrent.ToString())), destDir, true);
                        }
                        catch (IOException e)
                        {
                            throw new HibernateException("Umable to synchonize directory: " + indexName, e);
                        }
                    }

                    try
                    {
                        File.Create(current1Marker).Dispose();
                    }
                    catch (IOException e)
                    {
                        throw new HibernateException("Unable to create the directory marker file: " + indexName, e);
                    }
                }
                log.Debug("Current directory: " + current);
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }

            task = new TriggerTask(this, source, indexName);
            timer = new Timer(task.Run, null, period, period);
        }
        public void Start()
        {
            string refreshPeriod = properties.ContainsKey("refresh") ? properties["refresh"] : "3600";
            long period;
            if (!long.TryParse(refreshPeriod, out period))
            {
                period = 3600;
                log.Warn("Error parsing refresh period, defaulting to 1 hour");
            }
            log.DebugFormat("Refresh period {0} seconds", period);
            period *= 1000;  // per second

            try
            {
                //copy to source
                if (File.Exists(Path.Combine(source, "current1")))
                    current = 2;
                else if (File.Exists(Path.Combine(source, "current2")))
                    current = 1;
                else
                {
                    log.DebugFormat("Source directory for '{0}' will be initialized", indexName);
                    current = 1;
                }
                String currentString = current.ToString();
                DirectoryInfo subDir = new DirectoryInfo(Path.Combine(source, currentString));
                FileHelper.Synchronize(indexDir, subDir, true);
                File.Delete(Path.Combine(source, "current1"));
                File.Delete(Path.Combine(source, "current2"));
                log.Debug("Current directory: " + current);

            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }

            task = new TriggerTask(this, indexName, source);
            timer = new Timer(task.Run, null, period, period);
        }
        public void Start()
        {
            string refreshPeriod = properties.ContainsKey("refresh") ? properties["refresh"] : "3600";
            long   period;

            if (!long.TryParse(refreshPeriod, out period))
            {
                period = 3600;
                log.Warn("Error parsing refresh period, defaulting to 1 hour");
            }
            log.DebugFormat("Refresh period {0} seconds", period);
            period *= 1000;  // per second
            try
            {
                var config = new IndexWriterConfig(_luceneVersion, new StandardAnalyzer(_luceneVersion));

                var indexPath1 = new DirectoryInfo(Path.Combine(indexName, "1"));
                directory1 = FSDirectory.Open(indexPath1);
                if (!DirectoryReader.IndexExists(directory1))
                {
                    log.DebugFormat("Initialize index: '{0}'", indexPath1);
                    var iw1 = new IndexWriter(directory1, config);
                    try
                    {
                        iw1.Dispose();
                    }
                    finally
                    {
                        if (IndexWriter.IsLocked(directory1))
                        {
                            IndexWriter.Unlock(directory1);
                        }
                    }
                }

                var indexPath2 = Path.Combine(indexName, "2");
                directory2 = FSDirectory.Open(indexPath2);
                if (!DirectoryReader.IndexExists(directory2))
                {
                    log.DebugFormat("Initialize index: '{0}'", indexPath2);
                    var iw2 = new IndexWriter(directory2, config);
                    try
                    {
                        iw2.Dispose();
                    }
                    finally
                    {
                        if (IndexWriter.IsLocked(directory2))
                        {
                            IndexWriter.Unlock(directory2);
                        }
                    }
                }

                string current1Marker = Path.Combine(indexName, "current1");
                string current2Marker = Path.Combine(indexName, "current2");
                if (File.Exists(current1Marker))
                {
                    current = 1;
                }
                else if (File.Exists(current2Marker))
                {
                    current = 2;
                }
                else
                {
                    // no default
                    log.Debug("Setting directory 1 as current");
                    current = 1;
                    DirectoryInfo srcDir        = new DirectoryInfo(source);
                    DirectoryInfo destDir       = new DirectoryInfo(Path.Combine(indexName, current.ToString()));
                    int           sourceCurrent = -1;
                    if (File.Exists(Path.Combine(srcDir.Name, "current1")))
                    {
                        sourceCurrent = 1;
                    }
                    else if (File.Exists(Path.Combine(srcDir.Name, "current2")))
                    {
                        sourceCurrent = 2;
                    }

                    if (sourceCurrent != -1)
                    {
                        try
                        {
                            FileHelper.Synchronize(
                                new DirectoryInfo(Path.Combine(source, sourceCurrent.ToString())), destDir, true);
                        }
                        catch (IOException e)
                        {
                            throw new HibernateException("Umable to synchonize directory: " + indexName, e);
                        }
                    }

                    try
                    {
                        File.Create(current1Marker).Dispose();
                    }
                    catch (IOException e)
                    {
                        throw new HibernateException("Unable to create the directory marker file: " + indexName, e);
                    }
                }
                log.Debug("Current directory: " + current);
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }

            task  = new TriggerTask(this, source, indexName);
            timer = new Timer(task.Run, null, period, period);
        }
Ejemplo n.º 5
0
        public override void Start()
        {
            string refreshPeriod = properties.ContainsKey("refresh") ? properties["refresh"] : "3600";
            long   period;

            if (!long.TryParse(refreshPeriod, out period))
            {
                period = 3600;
                log.Warn("Error parsing refresh period, defaulting to 1 hour");
            }
            log.DebugFormat("Refresh period {0} seconds", period);
            period *= 1000;  // per second
            try
            {
                // Initialize
                this.directory1 = InitializeIndex(new DirectoryInfo(Path.Combine(indexName, "1")));
                this.directory2 = InitializeIndex(new DirectoryInfo(Path.Combine(indexName, "2")));

                string current1Marker = Path.Combine(indexName, "current1");
                string current2Marker = Path.Combine(indexName, "current2");

                if (File.Exists(current1Marker))
                {
                    current = 1;
                }
                else if (File.Exists(current2Marker))
                {
                    current = 2;
                }
                else
                {
                    // no default
                    log.Debug("Setting directory 1 as current");
                    current = 1;
                    DirectoryInfo srcDir        = new DirectoryInfo(source);
                    DirectoryInfo destDir       = new DirectoryInfo(Path.Combine(indexName, current.ToString()));
                    int           sourceCurrent = -1;
                    if (File.Exists(Path.Combine(srcDir.Name, "current1")))
                    {
                        sourceCurrent = 1;
                    }
                    else if (File.Exists(Path.Combine(srcDir.Name, "current2")))
                    {
                        sourceCurrent = 2;
                    }

                    if (sourceCurrent != -1)
                    {
                        try
                        {
                            FileHelper.Synchronize(
                                new DirectoryInfo(Path.Combine(source, sourceCurrent.ToString())), destDir, true);
                        }
                        catch (IOException e)
                        {
                            throw new HibernateException("Umable to synchonize directory: " + indexName, e);
                        }
                    }

                    try
                    {
                        File.Create(current1Marker).Dispose();
                    }
                    catch (IOException e)
                    {
                        throw new HibernateException("Unable to create the directory marker file: " + indexName, e);
                    }
                }
                log.Debug("Current directory: " + current);
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }

            task  = new TriggerTask(this, source, indexName);
            timer = new Timer(task.Run, null, period, period);
        }