• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oracle / opengrok / #3716

30 Nov 2023 08:55AM UTC coverage: 66.158% (+0.05%) from 66.106%
#3716

push

web-flow
Refactoring to reduce sonar code smell fixes (#4485)

---------

Signed-off-by: Gino Augustine <ginoaugustine@gmail.com>
Co-authored-by: Vladimir Kotal <vlada@kotalovi.cz>

389 of 478 new or added lines in 51 files covered. (81.38%)

11 existing lines in 9 files now uncovered.

38764 of 58593 relevant lines covered (66.16%)

0.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/suggester/src/main/java/org/opengrok/suggest/SuggestResultCollector.java
1
/*
2
 * CDDL HEADER START
3
 *
4
 * The contents of this file are subject to the terms of the
5
 * Common Development and Distribution License (the "License").
6
 * You may not use this file except in compliance with the License.
7
 *
8
 * See LICENSE.txt included in this distribution for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing Covered Code, include this CDDL HEADER in each
12
 * file and include the License file at LICENSE.txt.
13
 * If applicable, add the following below this CDDL HEADER, with the
14
 * fields enclosed by brackets "[]" replaced with your own identifying
15
 * information: Portions Copyright [yyyy] [name of copyright owner]
16
 *
17
 * CDDL HEADER END
18
 */
19

20
/*
21
 * Copyright (c) 2023, Oracle and/or its affiliates.
22
 * Portions Copyright (c) 2023, Gino Augustine <gino.augustine@oracle.com>.
23
 */
24
package org.opengrok.suggest;
25

26
import org.apache.lucene.index.LeafReaderContext;
27
import org.apache.lucene.index.StoredFields;
28
import org.apache.lucene.search.CollectionTerminatedException;
29
import org.apache.lucene.search.Collector;
30
import org.apache.lucene.search.IndexSearcher;
31
import org.apache.lucene.search.LeafCollector;
32
import org.apache.lucene.search.Scorable;
33
import org.apache.lucene.search.ScoreMode;
34
import org.opengrok.suggest.query.PhraseScorer;
35
import org.opengrok.suggest.query.data.BitIntsHolder;
36

37
import java.io.IOException;
38

39
/**
40
 * Collects Suggester query results.
41
 * @author Gino Augustine
42
 */
43
class SuggestResultCollector implements Collector {
44
    private final LeafReaderContext leafReaderContext;
45
    private final ComplexQueryData data;
46
    private final BitIntsHolder documentIds;
47

48
    SuggestResultCollector(LeafReaderContext leafReaderContext, ComplexQueryData data,
49
                                  BitIntsHolder documentIds) {
1✔
50
        this.leafReaderContext = leafReaderContext;
1✔
51
        this.data = data;
1✔
52
        this.documentIds = documentIds;
1✔
53
    }
1✔
54

55
    /**
56
     * Create a new {@link LeafCollector collector} to collect the given context.
57
     *
58
     * @param context next atomic reader context
59
     */
60
    @Override
61
    public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
62
        return new SuggesterLeafCollector(context);
1✔
63
    }
64

65
    /**
66
     * Indicates what features are required from the scorer.
67
     */
68
    @Override
69
    public ScoreMode scoreMode() {
70
        return ScoreMode.COMPLETE_NO_SCORES;
1✔
71
    }
72

73
    private final class SuggesterLeafCollector implements LeafCollector {
74
        private final LeafReaderContext context;
75
        private final int docBase;
76

77
        private SuggesterLeafCollector(LeafReaderContext context) {
1✔
78
            this.context = context;
1✔
79
            docBase = this.context.docBase;
1✔
80
        }
1✔
81

82
        /**
83
         * Called before successive calls to {@link #collect(int)}. Implementations that need the score of
84
         * the current document (passed-in to {@link #collect(int)}), should save the passed-in Scorer and
85
         * call scorer.score() when needed.
86
         *
87
         * @param scorer scorer
88
         */
89
        @Override
90
        public void setScorer(Scorable scorer) throws IOException {
91
            if (leafReaderContext == context) {
1✔
92
                if (scorer instanceof PhraseScorer) {
1✔
93
                    data.scorer = (PhraseScorer) scorer;
1✔
94
                } else {
95
                    try {
96
                        // it is mentioned in the documentation that #getChildren should not be called
97
                        // in #setScorer but no better way was found
98
                        for (var childScorer : scorer.getChildren()) {
1✔
NEW
99
                            if (childScorer.child instanceof PhraseScorer) {
×
NEW
100
                                data.scorer = (PhraseScorer) childScorer.child;
×
101
                            }
NEW
102
                        }
×
NEW
103
                    } catch (Exception e) {
×
104
                        // ignore
105
                    }
1✔
106
                }
107
            }
108
        }
1✔
109

110
        /**
111
         * Called once for every document matching a query, with the unbased document number.
112
         *
113
         * <p>Note: The collection of the current segment can be terminated by throwing a {@link
114
         * CollectionTerminatedException}. In this case, the last docs of the current {@link
115
         * LeafReaderContext} will be skipped and {@link IndexSearcher} will
116
         * swallow the exception and continue collection with the next leaf.
117
         *
118
         * <p>Note: This is called in an inner search loop. For good search performance, implementations
119
         * of this method should not call {@link StoredFields#document} on every hit. Doing so can slow
120
         * searches by an order of magnitude or more.
121
         *
122
         * @param doc documentId
123
         */
124
        @Override
125
        public void collect(int doc) throws IOException {
126
            if (leafReaderContext == context) {
1✔
127
                documentIds.set(docBase + doc);
1✔
128
            }
129
        }
1✔
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc