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);
        }
            //[MethodImpl(MethodImplOptions.Synchronized)]
            public void Run()
            {
                // TODO get rid of current and use the marker file instead?
                DateTime start = DateTime.Now;

                inProgress = true;
                if (directoryProviderLock == null)
                {
                    directoryProviderLock = parent.searchFactory.GetLockableDirectoryProviders()[directoryProvider];
                    directoryProvider     = null;
                }

                try
                {
                    lock (directoryProviderLock)
                    {
                        int           oldIndex        = parent.current;
                        int           index           = parent.current == 1 ? 2 : 1;
                        DirectoryInfo sourceFile      = new DirectoryInfo(source);
                        DirectoryInfo destinationFile = new DirectoryInfo(Path.Combine(destination, index.ToString()));

                        // TODO make smart a parameter
                        try
                        {
                            log.Info("Copying " + sourceFile + " into " + destinationFile);
                            FileHelper.Synchronize(sourceFile, destinationFile, true);
                            parent.current = index;
                        }
                        catch (IOException e)
                        {
                            // Don't change current
                            log.Error("Unable to synchronize source of " + parent.indexName, e);
                            return;
                        }

                        try
                        {
                            File.Delete(Path.Combine(destination, "current" + oldIndex));
                        }
                        catch (IOException e)
                        {
                            log.Warn("Unable to remove previous marker file from source of " + parent.indexName, e);
                        }

                        try
                        {
                            File.Create(Path.Combine(destination, "current" + index)).Dispose();
                        }
                        catch (IOException e)
                        {
                            log.Warn("Unable to create current marker in source of " + parent.indexName, e);
                        }
                    }
                }
                finally
                {
                    inProgress = false;
                }

                log.InfoFormat("Copy for {0} took {1}.", parent.indexName, (DateTime.Now - start));
            }
        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);
        }
            //[MethodImpl(MethodImplOptions.Synchronized)]
            public void Run()
            {
                DateTime start = DateTime.Now;

                try
                {
                    inProgress = true;
                    int           oldIndex = parent.current;
                    int           index    = parent.current == 1 ? 2 : 1;
                    DirectoryInfo sourceFile;
                    string        current1Slave = Path.Combine(source, "current1");
                    string        current2Slave = Path.Combine(source, "current2");
                    if (File.Exists(current1Slave))
                    {
                        sourceFile = new DirectoryInfo(Path.Combine(source, "1"));
                    }
                    else if (File.Exists(current2Slave))
                    {
                        sourceFile = new DirectoryInfo(Path.Combine(source, "2"));
                    }
                    else
                    {
                        log.Warn("Unable to determine current in source directory");
                        inProgress = false;
                        return;
                    }

                    DirectoryInfo destinationFile = new DirectoryInfo(Path.Combine(destination, index.ToString()));

                    // TODO make smart a parameter
                    try
                    {
                        log.Info("Copying " + sourceFile + " into " + destinationFile);
                        FileHelper.Synchronize(sourceFile, destinationFile, true);
                        parent.current = index;
                    }
                    catch (IOException e)
                    {
                        //don't change current
                        log.Error("Unable to synchronize " + parent.indexName, e);
                        inProgress = false;
                        return;
                    }

                    try
                    {
                        File.Delete(Path.Combine(parent.indexName, "current" + oldIndex));
                    }
                    catch (Exception e)
                    {
                        log.Warn("Unable to remove previous marker file in " + parent.indexName, e);
                    }

                    try
                    {
                        File.Create(Path.Combine(parent.indexName, "current" + index)).Dispose();
                    }
                    catch (IOException e)
                    {
                        log.Warn("Unable to create current marker file in " + parent.indexName, e);
                    }
                }
                finally
                {
                    inProgress = false;
                }
                log.Info("Copy for " + parent.indexName + " took " + (DateTime.Now - start) + ".");
            }
Пример #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);
        }