*/
package org.sonar.server.computation.task.projectanalysis.issue;
-import com.google.common.base.Function;
import com.google.common.base.Joiner;
-import com.google.common.collect.FluentIterable;
import com.google.common.collect.Ordering;
import java.util.Collection;
import java.util.List;
import java.util.Map;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.server.user.index.UserDoc;
import org.sonar.server.user.index.UserIndex;
import org.sonar.server.util.cache.CacheLoader;
if (!users.isEmpty()) {
// multiple users are associated to the same SCM account, for example
// the same email
- Collection<String> logins = FluentIterable.from(users).transform(UserDocToLogin.INSTANCE).toSortedList(Ordering.natural());
+ Collection<String> logins = users.stream()
+ .map(UserDoc::login)
+ .sorted(Ordering.natural())
+ .collect(MoreCollectors.toList(users.size()));
LOGGER.warn(String.format("Multiple users share the SCM account '%s': %s", scmAccount, Joiner.on(", ").join(logins)));
}
return null;
public Map<String, String> loadAll(Collection<? extends String> scmAccounts) {
throw new UnsupportedOperationException("Loading by multiple scm accounts is not supported yet");
}
-
- private enum UserDocToLogin implements Function<UserDoc, String> {
- INSTANCE;
- @Nullable
- @Override
- public String apply(@Nonnull UserDoc user) {
- return user.login();
- }
- }
}
*/
package org.sonar.server.es;
-import com.google.common.base.Function;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
+import java.util.function.Function;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
*/
package org.sonar.server.es;
-import com.google.common.base.Function;
import java.util.List;
import java.util.Map;
+import java.util.function.Function;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.elasticsearch.action.search.SearchResponse;
*/
package org.sonar.server.issue.index;
-import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
-import org.sonar.core.util.NonNullInputFunction;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
private static final Duration TWENTY_WEEKS = Duration.standardDays(20L * 7L);
private static final Duration TWENTY_MONTHS = Duration.standardDays(20L * 30L);
- /**
- * Convert an Elasticsearch result (a map) to an {@link org.sonar.server.issue.index.IssueDoc}. It's
- * used for {@link org.sonar.server.es.SearchResult}.
- */
- private static final Function<Map<String, Object>, IssueDoc> DOC_CONVERTER = new NonNullInputFunction<Map<String, Object>, IssueDoc>() {
- @Override
- protected IssueDoc doApply(Map<String, Object> input) {
- return new IssueDoc(input);
- }
- };
public static final String AGGREGATION_NAME_FOR_TAGS = "tags__issues";
private final Sorting sorting;
configureStickyFacets(query, options, filters, esQuery, requestBuilder);
SearchResponse response = requestBuilder.get();
- return new SearchResult<>(response, DOC_CONVERTER);
+ return new SearchResult<>(response, IssueDoc::new);
}
/**
.setQuery(boolQuery().must(matchAllQuery()).filter(filter));
SearchResponse response = requestBuilder.get();
- return EsUtils.scroll(client, response.getScrollId(), DOC_CONVERTER);
+ return EsUtils.scroll(client, response.getScrollId(), IssueDoc::new);
}
}
*/
package org.sonar.server.test.index;
-import com.google.common.base.Function;
import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.search.SearchHit;
-import org.sonar.core.util.NonNullInputFunction;
import org.sonar.server.es.EsClient;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.es.SearchResult;
import static org.sonar.server.test.index.TestIndexDefinition.FIELD_TEST_UUID;
public class TestIndex {
- private static final Function<Map<String, Object>, TestDoc> CONVERTER = new NonNullInputFunction<Map<String, Object>, TestDoc>() {
- @Override
- protected TestDoc doApply(Map<String, Object> fields) {
- return new TestDoc(fields);
- }
- };
private final EsClient client;
public TestIndex(EsClient client) {
.setFrom(searchOptions.getOffset())
.setQuery(boolQuery().must(matchAllQuery()).filter(termQuery(FIELD_FILE_UUID, testFileUuid)));
- return new SearchResult<>(searchRequest.get(), CONVERTER);
+ return new SearchResult<>(searchRequest.get(), TestDoc::new);
}
public SearchResult<TestDoc> searchBySourceFileUuidAndLineNumber(String sourceFileUuid, int lineNumber, SearchOptions searchOptions) {
.must(termQuery(FIELD_COVERED_FILES + "." + FIELD_COVERED_FILE_UUID, sourceFileUuid))
.must(termQuery(FIELD_COVERED_FILES + "." + FIELD_COVERED_FILE_LINES, lineNumber))));
- return new SearchResult<>(searchRequest.get(), CONVERTER);
+ return new SearchResult<>(searchRequest.get(), TestDoc::new);
}
public TestDoc getByTestUuid(String testUuid) {
}
public Optional<TestDoc> getNullableByTestUuid(String testUuid) {
- for (SearchHit hit : client.prepareSearch(TestIndexDefinition.INDEX_TYPE_TEST)
+ SearchHit[] hits = client.prepareSearch(TestIndexDefinition.INDEX_TYPE_TEST)
.setSize(1)
.setQuery(boolQuery().must(matchAllQuery()).filter(termQuery(FIELD_TEST_UUID, testUuid)))
- .get().getHits().getHits()) {
- return Optional.of(new TestDoc(hit.sourceAsMap()));
+ .get().getHits().getHits();
+ if (hits.length > 0) {
+ return Optional.of(new TestDoc(hits[0].sourceAsMap()));
}
-
return Optional.absent();
}
.setFrom(searchOptions.getOffset())
.setQuery(boolQuery().must(matchAllQuery()).filter(termQuery(FIELD_TEST_UUID, testUuid)));
- return new SearchResult<>(searchRequest.get(), CONVERTER);
+ return new SearchResult<>(searchRequest.get(), TestDoc::new);
}
}
*/
package org.sonar.server.user.index;
-import com.google.common.base.Function;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
import javax.annotation.CheckForNull;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.search.sort.SortOrder;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
-import org.sonar.core.util.NonNullInputFunction;
import org.sonar.server.es.EsClient;
import org.sonar.server.es.EsUtils;
import org.sonar.server.es.SearchOptions;
@ComputeEngineSide
public class UserIndex {
- /**
- * Convert an Elasticsearch result (a map) to an {@link UserDoc}. It's
- * used for {@link org.sonar.server.es.SearchResult}.
- */
- private static final Function<Map<String, Object>, UserDoc> DOC_CONVERTER = new NonNullInputFunction<Map<String, Object>, UserDoc>() {
- @Override
- protected UserDoc doApply(Map<String, Object> input) {
- return new UserDoc(input);
- }
- };
-
private final EsClient esClient;
public UserIndex(EsClient esClient) {
.setRouting(login);
GetResponse response = request.get();
if (response.isExists()) {
- return DOC_CONVERTER.apply(response.getSource());
+ return new UserDoc(response.getSource());
}
return null;
}
.should(termQuery(FIELD_SCM_ACCOUNTS, scmAccount))))
.setSize(3);
for (SearchHit hit : request.get().getHits().getHits()) {
- result.add(DOC_CONVERTER.apply(hit.sourceAsMap()));
+ result.add(new UserDoc(hit.sourceAsMap()));
}
}
return result;
.setQuery(filter);
SearchResponse response = requestBuilder.get();
- return EsUtils.scroll(esClient, response.getScrollId(), DOC_CONVERTER);
+ return EsUtils.scroll(esClient, response.getScrollId(), UserDoc::new);
}
public SearchResult<UserDoc> search(UserQuery userQuery, SearchOptions options) {
request.setQuery(boolQuery().must(esQuery).filter(filter));
- return new SearchResult<>(request.get(), DOC_CONVERTER);
+ return new SearchResult<>(request.get(), UserDoc::new);
}
}
*/
package org.sonar.server.es;
-import com.google.common.base.Function;
import java.util.Date;
import java.util.List;
-import java.util.Map;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.Test;
@Test
public void convertToDocs_empty() {
SearchHits hits = mock(SearchHits.class, Mockito.RETURNS_MOCKS);
- List<BaseDoc> docs = EsUtils.convertToDocs(hits, new Function<Map<String, Object>, BaseDoc>() {
- @Override
- public BaseDoc apply(Map<String, Object> input) {
- return new IssueDoc(input);
- }
- });
+ List<BaseDoc> docs = EsUtils.convertToDocs(hits, IssueDoc::new);
assertThat(docs).isEmpty();
}
@Test
public void convertToDocs() {
SearchHits hits = mock(SearchHits.class, Mockito.RETURNS_MOCKS);
- when(hits.getHits()).thenReturn(new SearchHit[]{mock(SearchHit.class)});
- List<BaseDoc> docs = EsUtils.convertToDocs(hits, new Function<Map<String, Object>, BaseDoc>() {
- @Override
- public BaseDoc apply(Map<String, Object> input) {
- return new IssueDoc(input);
- }
- });
+ when(hits.getHits()).thenReturn(new SearchHit[] {mock(SearchHit.class)});
+ List<BaseDoc> docs = EsUtils.convertToDocs(hits, IssueDoc::new);
assertThat(docs).hasSize(1);
}