use of java.util.Collections in project graylog2-server by Graylog2.
the class QuerySuggestionsES6 method suggest.
@Override
public SuggestionResponse suggest(SuggestionRequest req) {
final Set<String> affectedIndices = indexLookup.indexNamesForStreamsInTimeRange(req.streams(), req.timerange());
final SearchSourceBuilder search = new SearchSourceBuilder().query(QueryBuilders.prefixQuery(req.field(), req.input())).size(0).aggregation(AggregationBuilders.terms("fieldvalues").field(req.field()).size(req.size())).suggest(new SuggestBuilder().addSuggestion("corrections", SuggestBuilders.termSuggestion(req.field()).text(req.input()).size(req.size())));
final Search.Builder searchBuilder = new Search.Builder(search.toString()).addType(IndexMapping.TYPE_MESSAGE).addIndex(affectedIndices.isEmpty() ? Collections.singleton("") : affectedIndices).allowNoIndices(false).ignoreUnavailable(false);
try {
final SearchResult result = JestUtils.execute(jestClient, searchBuilder.build(), () -> "Unable to perform aggregation: ");
final TermsAggregation aggregation = result.getAggregations().getTermsAggregation("fieldvalues");
final List<SuggestionEntry> entries = aggregation.getBuckets().stream().map(b -> new SuggestionEntry(b.getKeyAsString(), b.getCount())).collect(Collectors.toList());
if (!entries.isEmpty()) {
return SuggestionResponse.forSuggestions(req.field(), req.input(), entries, aggregation.getSumOtherDocCount());
} else {
final List<SuggestionEntry> corrections = Optional.of(result.getJsonObject()).map(o -> o.get("suggest")).map(o -> o.get("corrections")).map(o -> o.get(0)).map(o -> o.get("options")).map(options -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(options.elements(), Spliterator.ORDERED), false).map(option -> new SuggestionEntry(option.get("text").textValue(), option.get("freq").longValue())).collect(Collectors.toList())).orElseGet(Collections::emptyList);
return SuggestionResponse.forSuggestions(req.field(), req.input(), corrections, null);
}
} catch (Exception e) {
final SuggestionError err = SuggestionError.create(e.getClass().getSimpleName(), e.getMessage());
return SuggestionResponse.forError(req.field(), req.input(), err);
}
}
use of java.util.Collections in project mybatis-3 by mybatis.
the class DefaultObjectFactory method instantiateClass.
private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
try {
Constructor<T> constructor;
if (constructorArgTypes == null || constructorArgs == null) {
constructor = type.getDeclaredConstructor();
try {
return constructor.newInstance();
} catch (IllegalAccessException e) {
if (Reflector.canControlMemberAccessible()) {
constructor.setAccessible(true);
return constructor.newInstance();
} else {
throw e;
}
}
}
constructor = type.getDeclaredConstructor(constructorArgTypes.toArray(new Class[0]));
try {
return constructor.newInstance(constructorArgs.toArray(new Object[0]));
} catch (IllegalAccessException e) {
if (Reflector.canControlMemberAccessible()) {
constructor.setAccessible(true);
return constructor.newInstance(constructorArgs.toArray(new Object[0]));
} else {
throw e;
}
}
} catch (Exception e) {
String argTypes = Optional.ofNullable(constructorArgTypes).orElseGet(Collections::emptyList).stream().map(Class::getSimpleName).collect(Collectors.joining(","));
String argValues = Optional.ofNullable(constructorArgs).orElseGet(Collections::emptyList).stream().map(String::valueOf).collect(Collectors.joining(","));
throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes + ") or values (" + argValues + "). Cause: " + e, e);
}
}
use of java.util.Collections in project mycore by MyCoRe-Org.
the class MCRSword method initConfig.
private static void initConfig() {
if (collections == null) {
collections = new Hashtable<>();
workspaceCollectionTable = new Hashtable<>();
final MCRConfiguration mcrConfiguration = MCRConfiguration.instance();
Map<String, String> propertiesMap = mcrConfiguration.getPropertiesMap();
final int lenghtOfPropertyPrefix = MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX.length();
LOGGER.info("--- INITIALIZE SWORD SERVER ---");
propertiesMap.keySet().stream().filter(// remove all which are not collections
prop -> prop.startsWith(MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX)).filter(// remove all which have no suffix
prop -> !prop.trim().equals(MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX)).map(// remove MCR_SWORD_COLLECTION_PREFIX
prop -> prop.substring(lenghtOfPropertyPrefix)).map(// split to workspace name and collection name
prop -> prop.split(Pattern.quote("."), 2)).filter(// remove all whith no workspace or collection name
prop -> prop.length == 2).forEach(workspaceCollectionEntry -> {
final String collection = workspaceCollectionEntry[1];
final String workspace = workspaceCollectionEntry[0];
LOGGER.info("Found collection: {} in workspace {}", collection, workspace);
String name = MCRSwordConstants.MCR_SWORD_COLLECTION_PREFIX + workspace + "." + collection;
LOGGER.info("Try to init : {}", name);
MCRSwordCollectionProvider collectionProvider = mcrConfiguration.getInstanceOf(name);
collections.put(collection, collectionProvider);
final MCRSwordLifecycleConfiguration lifecycleConfiguration = new MCRSwordLifecycleConfiguration(collection);
collectionProvider.init(lifecycleConfiguration);
// This Map is needed to speed up the build of the {@link org.mycore.mir.sword2.manager.MCRServiceDocumentManager}
List<String> collectionsOfWorkspace;
if (workspaceCollectionTable.containsKey(workspace)) {
collectionsOfWorkspace = workspaceCollectionTable.get(workspace);
} else {
collectionsOfWorkspace = new ArrayList<>();
workspaceCollectionTable.put(workspace, collectionsOfWorkspace);
}
collectionsOfWorkspace.add(collection);
});
addCollectionShutdownHook();
}
}
use of java.util.Collections in project alluxio by Alluxio.
the class UfsJournalIntegrationTest method deleteFsMasterJournalLogs.
private void deleteFsMasterJournalLogs() throws Exception {
String journalFolder = mLocalAlluxioCluster.getLocalAlluxioMaster().getJournalFolder();
UfsJournal journal = new UfsJournal(new URI(PathUtils.concatPath(journalFolder, Constants.FILE_SYSTEM_MASTER_NAME)), new NoopMaster(), 0, Collections::emptySet);
if (UfsJournalSnapshot.getCurrentLog(journal) != null) {
UnderFileSystem.Factory.create(journalFolder, ServerConfiguration.global()).deleteFile(UfsJournalSnapshot.getCurrentLog(journal).getLocation().toString());
}
}
use of java.util.Collections in project alluxio by Alluxio.
the class TriggeredCheckpointTest method ufsJournal.
@Test
public void ufsJournal() throws Exception {
int numFiles = 100;
MultiProcessCluster cluster = MultiProcessCluster.newBuilder(PortCoordination.TRIGGERED_UFS_CHECKPOINT).setClusterName("TriggeredUfsCheckpointTest").addProperty(PropertyKey.MASTER_JOURNAL_TYPE, JournalType.UFS.toString()).addProperty(PropertyKey.MASTER_JOURNAL_CHECKPOINT_PERIOD_ENTRIES, String.valueOf(numFiles)).setNumMasters(1).setNumWorkers(1).build();
try {
cluster.start();
cluster.waitForAllNodesRegistered(20 * Constants.SECOND_MS);
// Get enough journal entries
createFiles(cluster, numFiles);
// Trigger checkpoint
Assert.assertEquals(cluster.getMasterAddresses().get(0).getHostname(), cluster.getMetaMasterClient().checkpoint());
String journalLocation = cluster.getJournalDir();
UfsJournal ufsJournal = new UfsJournal(URIUtils.appendPathOrDie(new URI(journalLocation), Constants.FILE_SYSTEM_MASTER_NAME), new NoopMaster(""), 0, Collections::emptySet);
Assert.assertEquals(1, UfsJournalSnapshot.getSnapshot(ufsJournal).getCheckpoints().size());
validateCheckpointInClusterRestart(cluster);
cluster.notifySuccess();
} finally {
cluster.destroy();
}
}
Aggregations