Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit beecad5

Browse files
committed
Refactory code
1 parent f2f4f56 commit beecad5

File tree

3 files changed

+104
-91
lines changed

3 files changed

+104
-91
lines changed

src/main/java/com/searchcode/app/service/index/IndexService.java

Lines changed: 73 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,17 @@ public class IndexService extends IndexBaseService {
6363

6464
private final int MAX_INDEX_SIZE, MAX_LINES_INDEX_SIZE;
6565
private final Path INDEX_A_LOCATION, INDEX_B_LOCATION, FACET_A_LOCATION, FACET_B_LOCATION;
66-
private Path INDEX_READ_LOCATION, INDEX_WRITE_LOCATION, FACET_WRITE_LOCATION;
67-
68-
private int CHILD_FACET_LIMIT;
69-
7066
private final Queue<CodeIndexDocument> codeIndexDocumentQueue;
7167
private final UniqueRepoQueue uniqueGitRepoQueue, uniqueFileRepoQueue, uniqueSvnRepoQueue;
72-
68+
private Path INDEX_READ_LOCATION, INDEX_WRITE_LOCATION, FACET_WRITE_LOCATION;
69+
private int CHILD_FACET_LIMIT;
7370
private boolean repoAdderPause = false; // Controls if repo add job should pause, controlled through the UI
7471
private boolean repoJobExit = false; // Controls if repo indexing jobs should exit instantly
7572
private int codeIndexLinesCount = 0;
7673

7774
///////////////////////////////////////////////////////////////////////
7875
// The below store state for when the reindexing + flip should occur
79-
//////////////////////////////////////////////////////////////////////
76+
/// ///////////////////////////////////////////////////////////////////
8077
private boolean reindexingAll = false; // Are we in reindexing state waiting to flip over
8178
private int repoJobsCount = 0; // If we are reindexing then how many jobs need to finish
8279

@@ -142,7 +139,8 @@ public IndexService(Data data, StatsService statsService, SearchCodeLib searchco
142139

143140
//////////////////////////////////////////////////////////////
144141
// Methods for controlling the index
145-
//////////////////////////////////////////////////////////////
142+
143+
/// ///////////////////////////////////////////////////////////
146144

147145
@Override
148146
public boolean getRepoAdderPause() {
@@ -222,14 +220,13 @@ public synchronized void indexDocument(Queue<CodeIndexDocument> codeIndexDocumen
222220
this.logger.severe(String.format("b824ed70::error in class %s exception %s", ex.getClass(), ex.getMessage()));
223221
}
224222
});
225-
}
226-
finally {
223+
} finally {
227224
this.helpers.closeQuietly(writer);
228225
this.helpers.closeQuietly(taxonomyWriter);
229226
this.logger.info("f32cef3e::closing writers");
230227
}
231228
}
232-
229+
233230
/**
234231
* Builds a document ready to be indexed by lucene
235232
*/
@@ -258,29 +255,29 @@ public Document buildDocument(CodeIndexDocument codeIndexDocument) {
258255
this.searchcodeLib.addToSpellingCorrector(codeIndexDocument.getContents());
259256
String indexContents = this.indexContentPipeline(codeIndexDocument);
260257

261-
document.add(new TextField(Values.REPONAME, codeIndexDocument.getRepoName().replace(" ", "_"), Field.Store.YES));
262-
document.add(new TextField(Values.REPO_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getRepoName()).toLowerCase(), Field.Store.NO));
263-
document.add(new TextField(Values.FILENAME, codeIndexDocument.getFileName(), Field.Store.YES));
264-
document.add(new TextField(Values.FILE_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getFileName()).toLowerCase(), Field.Store.NO));
265-
document.add(new TextField(Values.FILELOCATION, codeIndexDocument.getFileLocation(), Field.Store.YES));
266-
document.add(new TextField(Values.FILELOCATIONFILENAME, codeIndexDocument.getFileLocationFilename(), Field.Store.YES));
267-
document.add(new TextField(Values.DISPLAY_LOCATION, codeIndexDocument.getDisplayLocation(), Field.Store.YES));
258+
document.add(new TextField(Values.REPONAME, codeIndexDocument.getRepoName().replace(" ", "_"), Field.Store.YES));
259+
document.add(new TextField(Values.REPO_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getRepoName()).toLowerCase(), Field.Store.NO));
260+
document.add(new TextField(Values.FILENAME, codeIndexDocument.getFileName(), Field.Store.YES));
261+
document.add(new TextField(Values.FILE_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getFileName()).toLowerCase(), Field.Store.NO));
262+
document.add(new TextField(Values.FILELOCATION, codeIndexDocument.getFileLocation(), Field.Store.YES));
263+
document.add(new TextField(Values.FILELOCATIONFILENAME, codeIndexDocument.getFileLocationFilename(), Field.Store.YES));
264+
document.add(new TextField(Values.DISPLAY_LOCATION, codeIndexDocument.getDisplayLocation(), Field.Store.YES));
268265
document.add(new TextField(Values.DISPLAY_LOCATION_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getDisplayLocation()).toLowerCase(), Field.Store.NO));
269-
document.add(new TextField(Values.MD5HASH, codeIndexDocument.getMd5hash(), Field.Store.YES));
270-
document.add(new TextField(Values.LANGUAGENAME, codeIndexDocument.getLanguageName().replace(" ", "_"), Field.Store.YES));
271-
document.add(new TextField(Values.LANGUAGE_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getLanguageName()).toLowerCase(), Field.Store.NO));
272-
document.add(new IntField(Values.LINES, codeIndexDocument.getLines(), Field.Store.YES));
273-
document.add(new IntField(Values.CODELINES, codeIndexDocument.getCodeLines(), Field.Store.YES));
274-
document.add(new IntField(Values.BLANKLINES, codeIndexDocument.getBlankLines(), Field.Store.YES));
275-
document.add(new IntField(Values.COMMENTLINES, codeIndexDocument.getCommentLines(), Field.Store.YES));
276-
document.add(new IntField(Values.COMPLEXITY, codeIndexDocument.getComplexity(), Field.Store.YES));
277-
document.add(new TextField(Values.CONTENTS, indexContents.toLowerCase(), Field.Store.NO));
278-
document.add(new TextField(Values.REPOLOCATION, codeIndexDocument.getRepoRemoteLocation(), Field.Store.YES));
279-
document.add(new TextField(Values.CODEOWNER, codeIndexDocument.getCodeOwner().replace(" ", "_"), Field.Store.YES));
280-
document.add(new TextField(Values.OWNER_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getCodeOwner()).toLowerCase(), Field.Store.NO));
281-
document.add(new TextField(Values.CODEID, codeIndexDocument.getHash(), Field.Store.YES));
282-
document.add(new TextField(Values.SCHASH, codeIndexDocument.getSchash(), Field.Store.YES));
283-
document.add(new TextField(Values.SOURCE, this.helpers.replaceForIndex(codeIndexDocument.getSource()), Field.Store.YES));
266+
document.add(new TextField(Values.MD5HASH, codeIndexDocument.getMd5hash(), Field.Store.YES));
267+
document.add(new TextField(Values.LANGUAGENAME, codeIndexDocument.getLanguageName().replace(" ", "_"), Field.Store.YES));
268+
document.add(new TextField(Values.LANGUAGE_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getLanguageName()).toLowerCase(), Field.Store.NO));
269+
document.add(new IntField(Values.LINES, codeIndexDocument.getLines(), Field.Store.YES));
270+
document.add(new IntField(Values.CODELINES, codeIndexDocument.getCodeLines(), Field.Store.YES));
271+
document.add(new IntField(Values.BLANKLINES, codeIndexDocument.getBlankLines(), Field.Store.YES));
272+
document.add(new IntField(Values.COMMENTLINES, codeIndexDocument.getCommentLines(), Field.Store.YES));
273+
document.add(new IntField(Values.COMPLEXITY, codeIndexDocument.getComplexity(), Field.Store.YES));
274+
document.add(new TextField(Values.CONTENTS, indexContents.toLowerCase(), Field.Store.NO));
275+
document.add(new TextField(Values.REPOLOCATION, codeIndexDocument.getRepoRemoteLocation(), Field.Store.YES));
276+
document.add(new TextField(Values.CODEOWNER, codeIndexDocument.getCodeOwner().replace(" ", "_"), Field.Store.YES));
277+
document.add(new TextField(Values.OWNER_NAME_LITERAL, this.helpers.replaceForIndex(codeIndexDocument.getCodeOwner()).toLowerCase(), Field.Store.NO));
278+
document.add(new TextField(Values.CODEID, codeIndexDocument.getHash(), Field.Store.YES));
279+
document.add(new TextField(Values.SCHASH, codeIndexDocument.getSchash(), Field.Store.YES));
280+
document.add(new TextField(Values.SOURCE, this.helpers.replaceForIndex(codeIndexDocument.getSource()), Field.Store.YES));
284281

285282
// Extra metadata in this case when it was last indexed
286283
document.add(new LongField(Values.MODIFIED, new Date().getTime(), Field.Store.YES));
@@ -423,7 +420,7 @@ public boolean shouldPause(JobType jobType) {
423420

424421
@Override
425422
public synchronized boolean shouldExit(JobType jobType) {
426-
switch(jobType) {
423+
switch (jobType) {
427424
case REPO_PARSER:
428425
return this.repoJobExit;
429426
}
@@ -455,8 +452,7 @@ public void incrementCodeIndexLinesCount(int incrementBy) {
455452

456453
try {
457454
this.codeIndexLinesCount = this.codeIndexLinesCount + incrementBy;
458-
}
459-
finally {
455+
} finally {
460456
codeIndexLinesCountLock.unlock();
461457
}
462458
}
@@ -471,32 +467,29 @@ public void decrementCodeIndexLinesCount(int decrementBy) {
471467
if (this.codeIndexLinesCount < 0) {
472468
this.codeIndexLinesCount = 0;
473469
}
474-
}
475-
finally {
470+
} finally {
476471
codeIndexLinesCountLock.unlock();
477472
}
478473
}
479474

480475
@Override
481-
public void setCodeIndexLinesCount(int value) {
476+
public int getCodeIndexLinesCount() {
482477
codeIndexLinesCountLock.lock();
483478

484479
try {
485-
this.codeIndexLinesCount = value;
486-
}
487-
finally {
480+
return this.codeIndexLinesCount;
481+
} finally {
488482
codeIndexLinesCountLock.unlock();
489483
}
490484
}
491485

492486
@Override
493-
public int getCodeIndexLinesCount() {
487+
public void setCodeIndexLinesCount(int value) {
494488
codeIndexLinesCountLock.lock();
495489

496490
try {
497-
return this.codeIndexLinesCount;
498-
}
499-
finally {
491+
this.codeIndexLinesCount = value;
492+
} finally {
500493
codeIndexLinesCountLock.unlock();
501494
}
502495
}
@@ -509,11 +502,9 @@ public int getIndexedDocumentCount() {
509502
try {
510503
reader = DirectoryReader.open(FSDirectory.open(this.INDEX_READ_LOCATION));
511504
numDocs = reader.numDocs();
512-
}
513-
catch (Exception ex) {
505+
} catch (Exception ex) {
514506
this.logger.severe(String.format("f02f5a23::error in class %s exception %s unable to delete index locations", ex.getClass(), ex.getMessage()));
515-
}
516-
finally {
507+
} finally {
517508
this.helpers.closeQuietly(reader);
518509
}
519510

@@ -551,11 +542,9 @@ public CodeResult getCodeResultByCodeId(String codeId) {
551542

552543
codeResult = this.createCodeResult(code, filePath, doc, hits[0].doc, hits[0].score);
553544
}
554-
}
555-
catch (Exception ex) {
545+
} catch (Exception ex) {
556546
this.logger.severe(String.format("4e5f00d0::error in class %s exception %s", ex.getClass(), ex.getMessage()));
557-
}
558-
finally {
547+
} finally {
559548
this.helpers.closeQuietly(reader);
560549
}
561550

@@ -591,8 +580,7 @@ public List<String> getRepoDocuments(String repoName, int page) {
591580
}
592581

593582
reader.close();
594-
}
595-
catch (Exception ex) {
583+
} catch (Exception ex) {
596584
this.logger.severe(String.format("44df9064::error in class %s exception %s", ex.getClass(), ex.getMessage()));
597585
}
598586

@@ -638,25 +626,22 @@ public ProjectStats getProjectStats(String repoName, int repoId) {
638626

639627
if (linesCount.containsKey(languageName)) {
640628
linesCount.put(languageName, linesCount.get(languageName) + lines);
641-
}
642-
else {
629+
} else {
643630
linesCount.put(languageName, lines);
644631
}
645632
}
646633

647-
for (var key: linesCount.keySet()) {
634+
for (var key : linesCount.keySet()) {
648635
codeByLines.add(new CodeFacetLanguage(key, linesCount.get(key)));
649636
}
650637
codeByLines.sort((a, b) -> b.getCount() - a.getCount());
651638

652639
totalFiles = results.totalHits;
653640
codeFacetLanguages = this.getLanguageFacetResults(searcher, reader, query);
654641
repoFacetOwners = this.getOwnerFacetResults(searcher, reader, query);
655-
}
656-
catch (Exception ex) {
642+
} catch (Exception ex) {
657643
this.logger.severe(String.format("61b491eb::error in class %s exception %s", ex.getClass(), ex.getMessage()));
658-
}
659-
finally {
644+
} finally {
660645
this.helpers.closeQuietly(reader);
661646
}
662647

@@ -699,11 +684,9 @@ public SearchResult getProjectFileTree(String repoName) {
699684
break;
700685
}
701686
}
702-
}
703-
catch (Exception ex) {
687+
} catch (Exception ex) {
704688
this.logger.severe(String.format("1c90b7f9::error in class %s exception %s", ex.getClass(), ex.getMessage()));
705-
}
706-
finally {
689+
} finally {
707690
this.helpers.closeQuietly(reader);
708691
}
709692

@@ -725,14 +708,15 @@ public SearchResult search(String queryString, HashMap<String, String[]> facets,
725708
this.statsService.incrementSearchCount();
726709
IndexReader reader = null;
727710

728-
// Required to ensure that results work the way we expect for the index
729-
if (!isLiteral) {
730-
queryString = this.searchcodeLib.formatQueryString(queryString);
731-
}
711+
try {
732712

733-
queryString += this.buildFacets(facets);
713+
// Required to ensure that results work the way we expect for the index
714+
if (!isLiteral) {
715+
queryString = this.searchcodeLib.formatQueryString(queryString);
716+
} else {
717+
queryString = this.searchcodeLib.lowcase(queryString);
718+
}
734719

735-
try {
736720
reader = DirectoryReader.open(FSDirectory.open(this.INDEX_READ_LOCATION));
737721
IndexSearcher searcher = new IndexSearcher(reader);
738722

@@ -747,14 +731,15 @@ public SearchResult search(String queryString, HashMap<String, String[]> facets,
747731
this.logger.searchLog("a8895274::query " + query.toString(Values.CONTENTS) + " page " + page);
748732

749733
searchResult = this.doPagingSearch(reader, searcher, query, page);
750-
}
751-
catch (Exception ex) {
734+
} catch (Exception ex) {
752735
this.logger.severe(String.format("bc93074f::error in class %s exception %s", ex.getClass(), ex.getMessage()));
753-
}
754-
finally {
736+
} finally {
755737
this.helpers.closeQuietly(reader);
756738
}
757739

740+
queryString += this.buildFacets(facets);
741+
742+
758743
return searchResult;
759744
}
760745

@@ -765,7 +750,7 @@ public String buildFacets(HashMap<String, String[]> facets) {
765750

766751
var filters = new StringBuilder(Values.EMPTYSTRING);
767752

768-
for (var key: facets.keySet()) {
753+
for (var key : facets.keySet()) {
769754
switch (key) {
770755
case "repo":
771756
List<String> reposList = Arrays.stream(facets.get(key))
@@ -886,8 +871,7 @@ private SearchResult doPagingSearch(IndexReader reader, IndexSearcher searcher,
886871
List<String> code = new ArrayList<>();
887872
try {
888873
code = this.helpers.readFileLinesGuessEncoding(filePath, this.helpers.tryParseInt(Properties.getProperties().getProperty(Values.MAXFILELINEDEPTH, Values.DEFAULTMAXFILELINEDEPTH), Values.DEFAULTMAXFILELINEDEPTH));
889-
}
890-
catch (Exception ex) {
874+
} catch (Exception ex) {
891875
this.logger.severe(String.format("cbd1868a::error in class %s exception %s", ex.getClass(), ex.getMessage()));
892876
}
893877

@@ -900,8 +884,8 @@ private SearchResult doPagingSearch(IndexReader reader, IndexSearcher searcher,
900884

901885
List<CodeFacetLanguage> codeFacetLanguages = this.getLanguageFacetResults(searcher, reader, query);
902886
List<CodeFacetRepo> repoFacetLanguages = this.getRepoFacetResults(searcher, reader, query);
903-
List<CodeFacetOwner> repoFacetOwner= this.getOwnerFacetResults(searcher, reader, query);
904-
List<CodeFacetSource> repoFacetSource= this.getSourceFacetResults(searcher, reader, query);
887+
List<CodeFacetOwner> repoFacetOwner = this.getOwnerFacetResults(searcher, reader, query);
888+
List<CodeFacetSource> repoFacetSource = this.getSourceFacetResults(searcher, reader, query);
905889

906890
return new SearchResult(numTotalHits, page, query.toString(), codeResults, pages, codeFacetLanguages, repoFacetLanguages, repoFacetOwner, repoFacetSource);
907891
}
@@ -957,7 +941,8 @@ private List<CodeFacetLanguage> getLanguageFacetResults(IndexSearcher searcher,
957941
}
958942
}
959943
}
960-
} catch (Exception ignore) {}
944+
} catch (Exception ignore) {
945+
}
961946

962947
return codeFacetLanguages;
963948
}
@@ -986,7 +971,8 @@ private List<CodeFacetRepo> getRepoFacetResults(IndexSearcher searcher, IndexRea
986971
}
987972
}
988973
}
989-
} catch (Exception ignore) {}
974+
} catch (Exception ignore) {
975+
}
990976

991977
return codeFacetRepo;
992978
}
@@ -1015,7 +1001,8 @@ private List<CodeFacetOwner> getOwnerFacetResults(IndexSearcher searcher, IndexR
10151001
}
10161002
}
10171003
}
1018-
} catch (Exception ignore) {}
1004+
} catch (Exception ignore) {
1005+
}
10191006

10201007
return codeFacetRepo;
10211008
}
@@ -1044,7 +1031,8 @@ private List<CodeFacetSource> getSourceFacetResults(IndexSearcher searcher, Inde
10441031
}
10451032
}
10461033
}
1047-
} catch (Exception ignore) {}
1034+
} catch (Exception ignore) {
1035+
}
10481036

10491037
return codeFacetSource;
10501038
}

src/main/java/com/searchcode/app/util/Helpers.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@
1919
import org.apache.commons.io.FileUtils;
2020
import org.apache.commons.io.IOUtils;
2121

22-
// TODO REMOVE
23-
//import org.apache.http.client.methods.HttpPost;
24-
//import org.apache.http.entity.StringEntity;
25-
//import org.apache.http.impl.client.DefaultHttpClient;
26-
2722
import org.apache.hc.core5.http.io.entity.StringEntity;
2823
import org.apache.hc.client5.http.classic.methods.HttpPost;
29-
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
3024
import org.apache.hc.client5.http.impl.classic.HttpClients;
3125

3226
import org.apache.lucene.facet.taxonomy.TaxonomyWriter;

0 commit comments

Comments
 (0)