use of javax.inject.Provider in project presto by prestodb.
the class VerifierTestUtil method createChecksumValidator.
public static ChecksumValidator createChecksumValidator(VerifierConfig verifierConfig) {
Map<Column.Category, Provider<ColumnValidator>> lazyValidators = new HashMap<>();
Map<Column.Category, Provider<ColumnValidator>> validators = ImmutableMap.of(Column.Category.SIMPLE, SimpleColumnValidator::new, Column.Category.FLOATING_POINT, () -> new FloatingPointColumnValidator(verifierConfig), Column.Category.ARRAY, ArrayColumnValidator::new, Column.Category.ROW, () -> new RowColumnValidator(lazyValidators), Column.Category.MAP, MapColumnValidator::new);
lazyValidators.putAll(validators);
return new ChecksumValidator(validators);
}
use of javax.inject.Provider in project graylog2-server by Graylog2.
the class ElasticsearchBackend method doRun.
@Override
public QueryResult doRun(SearchJob job, Query query, ESGeneratedQueryContext queryContext) {
if (query.searchTypes().isEmpty()) {
return QueryResult.builder().query(query).searchTypes(Collections.emptyMap()).errors(new HashSet<>(queryContext.errors())).build();
}
LOG.debug("Running query {} for job {}", query.id(), job.getId());
final HashMap<String, SearchType.Result> resultsMap = Maps.newHashMap();
final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(query.usedStreamIds(), query.timerange());
final Map<String, SearchSourceBuilder> searchTypeQueries = queryContext.searchTypeQueries();
final List<String> searchTypeIds = new ArrayList<>(searchTypeQueries.keySet());
final List<Search> searches = searchTypeIds.stream().map(searchTypeId -> {
final Set<String> affectedIndicesForSearchType = query.searchTypes().stream().filter(s -> s.id().equalsIgnoreCase(searchTypeId)).findFirst().flatMap(searchType -> {
if (searchType.effectiveStreams().isEmpty() && !query.globalOverride().flatMap(GlobalOverride::timerange).isPresent() && !searchType.timerange().isPresent()) {
return Optional.empty();
}
final Set<String> usedStreamIds = searchType.effectiveStreams().isEmpty() ? query.usedStreamIds() : searchType.effectiveStreams();
return Optional.of(indexLookup.indexNamesForStreamsInTimeRange(usedStreamIds, query.effectiveTimeRange(searchType)));
}).orElse(affectedIndices);
return new Search.Builder(searchTypeQueries.get(searchTypeId).toString()).addType(IndexMapping.TYPE_MESSAGE).addIndex(affectedIndicesForSearchType.isEmpty() ? Collections.singleton("") : affectedIndicesForSearchType).allowNoIndices(false).ignoreUnavailable(false).build();
}).collect(Collectors.toList());
final MultiSearch.Builder multiSearchBuilder = new MultiSearch.Builder(searches);
final MultiSearchResult result = JestUtils.execute(jestClient, multiSearchBuilder.build(), () -> "Unable to perform search query: ");
for (SearchType searchType : query.searchTypes()) {
final String searchTypeId = searchType.id();
final Provider<ESSearchTypeHandler<? extends SearchType>> handlerProvider = elasticsearchSearchTypeHandlers.get(searchType.type());
if (handlerProvider == null) {
LOG.error("Unknown search type '{}', cannot convert query result.", searchType.type());
// no need to add another error here, as the query generation code will have added the error about the missing handler already
continue;
}
if (isSearchTypeWithError(queryContext, searchTypeId)) {
LOG.error("Failed search type '{}', cannot convert query result, skipping.", searchType.type());
// no need to add another error here, as the query generation code will have added the error about the missing handler already
continue;
}
// we create a new instance because some search type handlers might need to track information between generating the query and
// processing its result, such as aggregations, which depend on the name and type
final ESSearchTypeHandler<? extends SearchType> handler = handlerProvider.get();
final int searchTypeIndex = searchTypeIds.indexOf(searchTypeId);
final MultiSearchResult.MultiSearchResponse multiSearchResponse = result.getResponses().get(searchTypeIndex);
if (multiSearchResponse.isError) {
ElasticsearchException e = JestUtils.specificException(() -> "Search type returned error: ", multiSearchResponse.error);
queryContext.addError(SearchTypeErrorParser.parse(query, searchTypeId, e));
} else if (checkForFailedShards(multiSearchResponse.searchResult).isPresent()) {
ElasticsearchException e = checkForFailedShards(multiSearchResponse.searchResult).get();
queryContext.addError(SearchTypeErrorParser.parse(query, searchTypeId, e));
} else {
final SearchType.Result searchTypeResult = handler.extractResult(job, query, searchType, multiSearchResponse.searchResult, queryContext);
if (searchTypeResult != null) {
resultsMap.put(searchTypeId, searchTypeResult);
}
}
}
LOG.debug("Query {} ran for job {}", query.id(), job.getId());
return QueryResult.builder().query(query).searchTypes(resultsMap).errors(new HashSet<>(queryContext.errors())).build();
}
use of javax.inject.Provider in project graylog2-server by Graylog2.
the class ElasticsearchBackend method doRun.
@Override
public QueryResult doRun(SearchJob job, Query query, ESGeneratedQueryContext queryContext) {
if (query.searchTypes().isEmpty()) {
return QueryResult.builder().query(query).searchTypes(Collections.emptyMap()).errors(new HashSet<>(queryContext.errors())).build();
}
LOG.debug("Running query {} for job {}", query.id(), job.getId());
final HashMap<String, SearchType.Result> resultsMap = Maps.newHashMap();
final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(query.usedStreamIds(), query.timerange());
final Map<String, SearchSourceBuilder> searchTypeQueries = queryContext.searchTypeQueries();
final List<String> searchTypeIds = new ArrayList<>(searchTypeQueries.keySet());
final List<SearchRequest> searches = searchTypeIds.stream().map(searchTypeId -> {
final Set<String> affectedIndicesForSearchType = query.searchTypes().stream().filter(s -> s.id().equalsIgnoreCase(searchTypeId)).findFirst().flatMap(searchType -> {
if (searchType.effectiveStreams().isEmpty() && !query.globalOverride().flatMap(GlobalOverride::timerange).isPresent() && !searchType.timerange().isPresent()) {
return Optional.empty();
}
final Set<String> usedStreamIds = searchType.effectiveStreams().isEmpty() ? query.usedStreamIds() : searchType.effectiveStreams();
return Optional.of(indexLookup.indexNamesForStreamsInTimeRange(usedStreamIds, query.effectiveTimeRange(searchType)));
}).orElse(affectedIndices);
Set<String> indices = affectedIndicesForSearchType.isEmpty() ? Collections.singleton("") : affectedIndicesForSearchType;
return new SearchRequest().source(searchTypeQueries.get(searchTypeId)).indices(indices.toArray(new String[0])).indicesOptions(IndicesOptions.fromOptions(false, false, true, false));
}).collect(Collectors.toList());
final List<MultiSearchResponse.Item> results = client.msearch(searches, "Unable to perform search query: ");
for (SearchType searchType : query.searchTypes()) {
final String searchTypeId = searchType.id();
final Provider<ESSearchTypeHandler<? extends SearchType>> handlerProvider = elasticsearchSearchTypeHandlers.get(searchType.type());
if (handlerProvider == null) {
LOG.error("Unknown search type '{}', cannot convert query result.", searchType.type());
// no need to add another error here, as the query generation code will have added the error about the missing handler already
continue;
}
if (isSearchTypeWithError(queryContext, searchTypeId)) {
LOG.error("Failed search type '{}', cannot convert query result, skipping.", searchType.type());
// no need to add another error here, as the query generation code will have added the error about the missing handler already
continue;
}
// we create a new instance because some search type handlers might need to track information between generating the query and
// processing its result, such as aggregations, which depend on the name and type
final ESSearchTypeHandler<? extends SearchType> handler = handlerProvider.get();
final int searchTypeIndex = searchTypeIds.indexOf(searchTypeId);
final MultiSearchResponse.Item multiSearchResponse = results.get(searchTypeIndex);
if (multiSearchResponse.isFailure()) {
ElasticsearchException e = new ElasticsearchException("Search type returned error: ", multiSearchResponse.getFailure());
queryContext.addError(SearchTypeErrorParser.parse(query, searchTypeId, e));
} else if (checkForFailedShards(multiSearchResponse).isPresent()) {
ElasticsearchException e = checkForFailedShards(multiSearchResponse).get();
queryContext.addError(SearchTypeErrorParser.parse(query, searchTypeId, e));
} else {
final SearchType.Result searchTypeResult = handler.extractResult(job, query, searchType, multiSearchResponse.getResponse(), queryContext);
if (searchTypeResult != null) {
resultsMap.put(searchTypeId, searchTypeResult);
}
}
}
LOG.debug("Query {} ran for job {}", query.id(), job.getId());
return QueryResult.builder().query(query).searchTypes(resultsMap).errors(new HashSet<>(queryContext.errors())).build();
}
use of javax.inject.Provider in project roboguice by roboguice.
the class Jsr330Test method testGuicifyWithDependencies.
public void testGuicifyWithDependencies() {
Provider<String> jsr330Provider = new Provider<String>() {
@Inject
double d;
int i;
@Inject
void injectMe(int i) {
this.i = i;
}
public String get() {
return d + "-" + i;
}
};
final com.google.inject.Provider<String> guicified = Providers.guicify(jsr330Provider);
assertTrue(guicified instanceof HasDependencies);
Set<Dependency<?>> actual = ((HasDependencies) guicified).getDependencies();
validateDependencies(actual, jsr330Provider.getClass());
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toProvider(guicified);
bind(int.class).toInstance(1);
bind(double.class).toInstance(2.0d);
}
});
Binding<String> binding = injector.getBinding(String.class);
assertEquals("2.0-1", binding.getProvider().get());
validateDependencies(actual, jsr330Provider.getClass());
}
use of javax.inject.Provider in project roboguice by roboguice.
the class MiniGuice method addJitBinding.
private void addJitBinding(Key key, Object requiredBy) {
Class<?> type = (Class<?>) key.type;
/*
* Lookup the injectable fields and their corresponding keys.
*/
final List<Field> injectedFields = new ArrayList<Field>();
List<Object> fieldKeysList = new ArrayList<Object>();
for (Class<?> c = type; c != Object.class; c = c.getSuperclass()) {
for (Field field : c.getDeclaredFields()) {
if (!field.isAnnotationPresent(javax.inject.Inject.class)) {
continue;
}
field.setAccessible(true);
injectedFields.add(field);
Key fieldKey = key(field, field.getGenericType(), field.getAnnotations());
fieldKeysList.add(fieldKey);
requireKey(fieldKey, field);
}
}
final Key[] fieldKeys = fieldKeysList.toArray(new Key[fieldKeysList.size()]);
/*
* Lookup @Inject-annotated constructors. If there's no @Inject-annotated
* constructor, use a default constructor if the class has other injections.
*/
Constructor<?> injectedConstructor = null;
for (Constructor<?> constructor : type.getDeclaredConstructors()) {
if (!constructor.isAnnotationPresent(javax.inject.Inject.class)) {
continue;
}
if (injectedConstructor != null) {
throw new IllegalArgumentException("Too many injectable constructors on " + type);
}
constructor.setAccessible(true);
injectedConstructor = constructor;
}
if (injectedConstructor == null) {
if (fieldKeys.length == 0) {
throw new IllegalArgumentException("No injectable constructor on " + type + " required by " + requiredBy);
}
try {
injectedConstructor = type.getConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("No injectable constructor on " + type + " required by " + requiredBy);
}
}
/*
* Create a provider that invokes the constructor and sets its fields.
*/
final Constructor<?> constructor = injectedConstructor;
final Key[] parameterKeys = parametersToKeys(constructor, constructor.getGenericParameterTypes(), constructor.getParameterAnnotations());
final Provider<Object> unscoped = new Provider<Object>() {
public Object get() {
Object[] constructorParameters = keysToValues(parameterKeys);
try {
Object result = constructor.newInstance(constructorParameters);
Object[] fieldValues = keysToValues(fieldKeys);
for (int i = 0; i < fieldValues.length; i++) {
injectedFields.get(i).set(result, fieldValues[i]);
}
return result;
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getCause());
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getCause());
} catch (InstantiationException e) {
throw new RuntimeException(e);
}
}
};
boolean singleton = type.isAnnotationPresent(javax.inject.Singleton.class);
putBinding(new Key(type, null), unscoped, singleton);
}
Aggregations