@@ -311,6 +311,39 @@ public boolean addDirectory(String dir) {
311
311
return false ;
312
312
}
313
313
314
+ private int getFileCount (File sourceRoot , String dir ) throws IOException {
315
+ int file_cnt = 0 ;
316
+ if (RuntimeEnvironment .getInstance ().isPrintProgress ()) {
317
+ LOGGER .log (Level .INFO , "Counting files in {0} ..." , dir );
318
+ file_cnt = indexDown (sourceRoot , dir , true , 0 , 0 );
319
+ LOGGER .log (Level .INFO ,
320
+ "Need to process: {0} files for {1}" ,
321
+ new Object []{file_cnt , dir });
322
+ }
323
+
324
+ return file_cnt ;
325
+ }
326
+
327
+ private void markProjectIndexed (Project project ) throws IOException {
328
+ RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
329
+
330
+ // Successfully indexed the directory. If this is a project
331
+ // that has just been indexed for the first time mark it so
332
+ // by sending special message to the webapp.
333
+ if (project != null && !project .isIndexed ()) {
334
+ if (env .getConfigHost () != null && env .getConfigPort () > 0 ) {
335
+ Message m = Message .createMessage ("project" );
336
+ m .addTag (project .getName ());
337
+ m .setText ("indexed" );
338
+ m .write (env .getConfigHost (), env .getConfigPort ());
339
+ }
340
+
341
+ // Also need to store the correct value in configuration
342
+ // when indexer writes it to a file.
343
+ project .setIndexed (true );
344
+ }
345
+ }
346
+
314
347
/**
315
348
* Update the content of this index database
316
349
*
@@ -337,7 +370,7 @@ public void update() throws IOException, HistoryException {
337
370
}
338
371
339
372
if (ctags != null ) {
340
- String filename = RuntimeEnvironment . getInstance () .getCTagsExtraOptionsFile ();
373
+ String filename = env .getCTagsExtraOptionsFile ();
341
374
if (filename != null ) {
342
375
ctags .setCTagsExtraOptionsFile (filename );
343
376
}
@@ -391,44 +424,22 @@ public void update() throws IOException, HistoryException {
391
424
startuid );
392
425
}
393
426
}
394
- // The code below traverses the tree to get total count.
395
- int file_cnt = 0 ;
396
- if (env .isPrintProgress ()) {
397
- LOGGER .log (Level .INFO , "Counting files in {0} ..." , dir );
398
- file_cnt = indexDown (sourceRoot , dir , true , 0 , 0 );
399
- LOGGER .log (Level .INFO ,
400
- "Need to process: {0} files for {1}" ,
401
- new Object []{file_cnt , dir });
402
- }
403
427
404
428
// The actual indexing happens in indexDown().
405
- indexDown (sourceRoot , dir , false , 0 , file_cnt );
429
+ indexDown (sourceRoot , dir , false , 0 ,
430
+ getFileCount (sourceRoot , dir ));
406
431
407
432
while (uidIter != null && uidIter .term () != null
408
433
&& uidIter .term ().utf8ToString ().startsWith (startuid )) {
409
434
410
- removeFile ();
435
+ removeFile (true );
411
436
BytesRef next = uidIter .next ();
412
- if (next == null ) {
437
+ if (next == null ) {
413
438
uidIter =null ;
414
439
}
415
440
}
416
441
417
- // Successfully indexed the directory. If this is a project
418
- // that has just been indexed for the first time mark it so
419
- // by sending special message to the webapp.
420
- if (project != null && !project .isIndexed ()) {
421
- if (env .getConfigHost () != null && env .getConfigPort () > 0 ) {
422
- Message m = Message .createMessage ("project" );
423
- m .addTag (project .getName ());
424
- m .setText ("indexed" );
425
- m .write (env .getConfigHost (), env .getConfigPort ());
426
- }
427
-
428
- // Also need to store the correct value in configuration
429
- // when indexer writes it to a file.
430
- project .setIndexed (true );
431
- }
442
+ markProjectIndexed (project );
432
443
} finally {
433
444
reader .close ();
434
445
}
@@ -602,9 +613,10 @@ private void removeHistoryFile(String path) {
602
613
* Remove a stale file (uidIter.term().text()) from the index database,
603
614
* history cache and xref.
604
615
*
616
+ * @param removeHistory if false, do not remove history cache for this file
605
617
* @throws java.io.IOException if an error occurs
606
618
*/
607
- private void removeFile () throws IOException {
619
+ private void removeFile (boolean removeHistory ) throws IOException {
608
620
String path = Util .uid2url (uidIter .term ().utf8ToString ());
609
621
610
622
for (IndexChangedListener listener : listeners ) {
@@ -616,7 +628,9 @@ private void removeFile() throws IOException {
616
628
writer .commit ();
617
629
618
630
removeXrefFile (path );
619
- removeHistoryFile (path );
631
+ if (removeHistory ) {
632
+ removeHistoryFile (path );
633
+ }
620
634
621
635
setDirty ();
622
636
for (IndexChangedListener listener : listeners ) {
@@ -837,16 +851,25 @@ private boolean isLocal(String path) {
837
851
return local ;
838
852
}
839
853
854
+ private void printProgress (int currentCount , int totalCount ) {
855
+ if (RuntimeEnvironment .getInstance ().isPrintProgress ()
856
+ && totalCount > 0 && LOGGER .isLoggable (Level .INFO )) {
857
+ LOGGER .log (Level .INFO , "Progress: {0} ({1}%)" ,
858
+ new Object []{currentCount ,
859
+ (currentCount * 100.0f / totalCount )});
860
+ }
861
+ }
862
+
840
863
/**
841
864
* Generate indexes recursively
842
865
*
843
866
* @param dir the root indexDirectory to generate indexes for
844
- * @param path the path
867
+ * @param parent path to parent directory
845
868
* @param count_only if true will just traverse the source root and count
846
869
* files
847
- * @param cur_count current count during the traversal of the tree
870
+ * @param cur_count current file count during the traversal of the tree
848
871
* @param est_total estimate total files to process
849
- *
872
+ * @return current file count
850
873
*/
851
874
private int indexDown (File dir , String parent , boolean count_only ,
852
875
int cur_count , int est_total ) throws IOException {
@@ -880,30 +903,31 @@ private int indexDown(File dir, String parent, boolean count_only,
880
903
continue ;
881
904
}
882
905
883
- if (RuntimeEnvironment .getInstance ().isPrintProgress ()
884
- && est_total > 0 && LOGGER .isLoggable (Level .INFO )) {
885
- LOGGER .log (Level .INFO , "Progress: {0} ({1}%)" ,
886
- new Object []{lcur_count ,
887
- (lcur_count * 100.0f / est_total )});
888
- }
906
+ printProgress (lcur_count , est_total );
889
907
890
908
if (uidIter != null ) {
891
909
String uid = Util .path2uid (path ,
892
910
DateTools .timeToString (file .lastModified (),
893
911
DateTools .Resolution .MILLISECOND )); // construct uid for doc
894
- BytesRef buid = new BytesRef (uid );
912
+ BytesRef buid = new BytesRef (uid );
895
913
while (uidIter != null && uidIter .term () != null
896
- && uidIter .term ().compareTo (emptyBR ) !=0
914
+ && uidIter .term ().compareTo (emptyBR ) != 0
897
915
&& uidIter .term ().compareTo (buid ) < 0 ) {
898
- removeFile ();
916
+
917
+ String termPath = Util .uid2url (uidIter .term ().utf8ToString ());
918
+ removeFile (!termPath .equals (path ));
899
919
BytesRef next = uidIter .next ();
900
- if (next ==null ) {uidIter =null ;}
920
+ if (next == null ) {
921
+ uidIter = null ;
922
+ }
901
923
}
902
924
903
925
if (uidIter != null && uidIter .term () != null
904
926
&& uidIter .term ().bytesEquals (buid )) {
905
927
BytesRef next = uidIter .next (); // keep matching docs
906
- if (next ==null ) {uidIter =null ;}
928
+ if (next == null ) {
929
+ uidIter = null ;
930
+ }
907
931
continue ;
908
932
}
909
933
}
0 commit comments