Exemplo n.º 1
0
        protected override void Load(Autofac.ContainerBuilder builder)
        {
            base.Load(builder);

            builder
                .Register<Service>(c => new Service(c.Resolve<Func<IndexWriter>>(), c.Resolve<SearcherManager>()))
                .AsSelf()
                .AsImplementedInterfaces()
                .SingleInstance();

            builder
                .Register<ServiceQueue>(c => new ServiceQueue(c.Resolve<Service>()))
                .AsImplementedInterfaces()
                .SingleInstance();

            builder
                .Register<Listener>(c => new Listener(c.Resolve<IQueue>(), c.Resolve<Common.Fulltext.DataObjectFormatter>(), c.Resolve<IMetaDataResolver>()))
                .AsImplementedInterfaces()
                .SingleInstance();

            builder
                .Register<Lucene.Net.Store.Directory>(c =>
                {
                    var cfg = c.Resolve<ZetboxConfig>();
                    return SimpleFSDirectory.Open(Path.Combine(cfg.Server.DocumentStore, "LuceneIndex"));
                })
                .SingleInstance();

            builder
                .Register<StandardAnalyzer>(c => new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                .As<Analyzer>()
                .Named<Analyzer>("en")
                .SingleInstance();

            builder
                .Register<GermanAnalyzer>(c => new GermanAnalyzer(Lucene.Net.Util.Version.LUCENE_30))
                .Named<Analyzer>("de")
                .SingleInstance();

            builder
                .Register<IndexWriter>(c =>
                {
                    var directory = c.Resolve<Lucene.Net.Store.Directory>();
                    var analyzer = c.IsRegisteredWithName<Analyzer>(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
                        ? c.ResolveNamed<Analyzer>(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
                        : c.Resolve<Analyzer>();

                    return new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.LIMITED);
                })
                .InstancePerDependency();

            builder
                .Register<SearcherManager>(c => new SearcherManager(c.Resolve<Lucene.Net.Store.Directory>()))
                .SingleInstance();

            builder
               .Register<QueryParser>(c => new QueryParser(Lucene.Net.Util.Version.LUCENE_30, FIELD_BODY, c.Resolve<Analyzer>()))
               .InstancePerLifetimeScope();

            builder
                .Register<LuceneSearchDeps>(c => new LuceneSearchDeps(c.Resolve<SearcherManager>(), c.Resolve<QueryParser>(), c.Resolve<IMetaDataResolver>(), c.Resolve<IQueue>()))
                .InstancePerDependency();

            builder
                .Register<Rebuilder>(c => new Rebuilder(c.Resolve<ILifetimeScope>(), c.Resolve<Func<IndexWriter>>(), c.Resolve<Common.Fulltext.DataObjectFormatter>(), c.Resolve<IMetaDataResolver>()))
                .InstancePerDependency();

            builder
                .RegisterCmdLineAction("rebuild-fulltext-index=", "Rebuild and optimize the fulltext index. You can add a comma separated list of class names (including the namespace) to rebuild the index for only those classes. When no filter is given, all classes are rebuilt.",
                (scope, v) =>
                {
                    var rebuilder = scope.Resolve<Rebuilder>();
                    if (!string.IsNullOrWhiteSpace(v))
                    {
                        rebuilder.Rebuild(v.Split(','));
                    }
                    else
                    {
                        rebuilder.Rebuild();
                    }
                });

            builder
                 .RegisterCmdLineAction("optimize-fulltext-index=", "Optimize the fulltext index.",
                 (scope, v) =>
                 {
                     var Log = log4net.LogManager.GetLogger("Zetbox.API.Server.Fulltext.Optimizer");
                     using (Log.InfoTraceMethodCall("Optimize"))
                     {
                         using (var indexWriter = scope.Resolve<IndexWriter>())
                         {
                             indexWriter.Optimize();
                         }
                     }
                 });
        }