66use Illuminate \Console \Command ;
77use Nqxcode \LuceneSearch \Locker \Locker ;
88use Nqxcode \LuceneSearch \Search ;
9+ use Nqxcode \LuceneSearch \Support \SearchIndexRotator ;
910use Symfony \Component \Console \Helper \ProgressBar ;
1011use Symfony \Component \Console \Input \InputOption ;
1112use Symfony \Component \Console \Output \NullOutput ;
@@ -21,6 +22,11 @@ class RebuildCommand extends Command
2122 */
2223 private $ search ;
2324
25+ /**
26+ * @var SearchIndexRotator
27+ */
28+ private $ searchIndexRotator ;
29+
2430 protected function getOptions ()
2531 {
2632 return [
@@ -34,7 +40,7 @@ public function fire()
3440 $ this ->output = new NullOutput ;
3541 }
3642
37- $ lockFilePath = sys_get_temp_dir () . ' / laravel-lucene-search/rebuild.lock ' ;
43+ $ lockFilePath = storage_path ( ' laravel-lucene-search/rebuild.lock ') ;
3844
3945 $ locker = new Locker ($ lockFilePath );
4046
@@ -44,10 +50,11 @@ public function fire()
4450
4551 $ locker ->doLocked (function () {
4652 if ($ this ->option ('force ' )) {
47- $ this ->forceRebuild ();
53+ $ this ->call ('search:clear ' );
54+ $ this ->rebuild ();
4855
4956 } else {
50- $ this ->softRebuild ();
57+ $ this ->rebuild ();
5158 }
5259 });
5360 }
@@ -57,6 +64,10 @@ private function rebuild()
5764 /** @var Search $search */
5865 $ this ->search = App::make ('search ' );
5966
67+ $ this ->searchIndexRotator = App::make ('search.index.rotator ' );
68+ $ queue = Config::get ('laravel-lucene-search::queue ' );
69+ $ indexPath = Config::get ('laravel-lucene-search::index.path ' );
70+
6071 $ modelRepositories = $ this ->search ->config ()->repositories ();
6172
6273 if (count ($ modelRepositories ) > 0 ) {
@@ -74,19 +85,21 @@ private function rebuild()
7485 $ progress = new ProgressBar ($ this ->getOutput (), $ count / $ chunkCount );
7586 $ progress ->start ();
7687
77- $ modelRepository ->chunk ($ chunkCount , function ($ chunk ) use ($ progress ) {
78- $ queue = Config::get ('laravel-lucene-search::queue ' );
88+ $ modelRepository ->chunk ($ chunkCount , function ($ chunk ) use ($ progress , $ queue ) {
89+ $ newIndexPath = $ this ->searchIndexRotator ->getNewIndexPath ();
90+
7991 if ($ queue ) {
8092 Queue::push (
8193 'Nqxcode\LuceneSearch\Job\MassUpdateSearchIndex ' ,
8294 [
8395 'modelClass ' => get_class ($ chunk [0 ]),
8496 'modelKeys ' => $ chunk ->lists ($ chunk [0 ]->getKeyName ()),
85- 'indexPath ' => Config:: get ( ' laravel-lucene-search::index.path ' ) ,
97+ 'indexPath ' => $ newIndexPath ,
8698 ],
8799 $ queue );
88100
89101 } else {
102+ Config::set ('laravel-lucene-search::index.path ' , $ newIndexPath );
90103 foreach ($ chunk as $ model ) {
91104 $ this ->search ->update ($ model );
92105 }
@@ -102,30 +115,16 @@ private function rebuild()
102115 } else {
103116 $ this ->error ('No models found in config.php file.. ' );
104117 }
105- }
106-
107- private function softRebuild ()
108- {
109- $ oldIndexPath = Config::get ('laravel-lucene-search::index.path ' );
110- $ newIndexPath = sys_get_temp_dir () . '/laravel-lucene-search/ ' . uniqid ('index- ' , true );
111-
112- Config::set ('laravel-lucene-search::index.path ' , $ newIndexPath );
113-
114- $ this ->rebuild ();
115118
116119 $ this ->search ->destroyConnection ();
117120
118- File::cleanDirectory ($ oldIndexPath );
119- File::copyDirectory ($ newIndexPath , $ oldIndexPath );
120- File::cleanDirectory ($ newIndexPath );
121+ if ($ queue ) {
122+ Queue::push ('Nqxcode\LuceneSearch\Job\RotateSearchIndex ' , $ queue );
121123
122- Config:: set ( ' laravel-lucene-search::index.path ' , $ oldIndexPath );
123-
124- }
124+ } else {
125+ $ this -> searchIndexRotator -> rotate ();
126+ }
125127
126- private function forceRebuild ()
127- {
128- $ this ->call ('search:clear ' );
129- $ this ->rebuild ();
128+ Config::set ('laravel-lucene-search::index.path ' , $ indexPath );
130129 }
131130}
0 commit comments