Search in sources :

Example 36 with Consumer

use of java.util.function.Consumer in project elasticsearch by elastic.

the class DiversifiedSamplerTests method testDiversifiedSampler.

public void testDiversifiedSampler() throws Exception {
    String[] data = { // "id,cat,name,price,inStock,author_t,series_t,sequence_i,genre_s,genre_id",
    "0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,A Song of Ice and Fire,1,fantasy,0", "0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,A Song of Ice and Fire,2,fantasy,0", "055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,A Song of Ice and Fire,3,fantasy,0", "0553293354,book,Foundation,17.99,true,Isaac Asimov,Foundation Novels,1,scifi,1", "0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy,0", "0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi,1", "0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy,0", "0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy,0", "0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy,0", "080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy,0" };
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    for (String entry : data) {
        String[] parts = entry.split(",");
        Document document = new Document();
        document.add(new SortedDocValuesField("id", new BytesRef(parts[0])));
        document.add(new StringField("cat", parts[1], Field.Store.NO));
        document.add(new TextField("name", parts[2], Field.Store.NO));
        document.add(new DoubleDocValuesField("price", Double.valueOf(parts[3])));
        document.add(new StringField("inStock", parts[4], Field.Store.NO));
        document.add(new StringField("author", parts[5], Field.Store.NO));
        document.add(new StringField("series", parts[6], Field.Store.NO));
        document.add(new StringField("sequence", parts[7], Field.Store.NO));
        document.add(new SortedDocValuesField("genre", new BytesRef(parts[8])));
        document.add(new NumericDocValuesField("genre_id", Long.valueOf(parts[9])));
        indexWriter.addDocument(document);
    }
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    MappedFieldType genreFieldType = new KeywordFieldMapper.KeywordFieldType();
    genreFieldType.setName("genre");
    genreFieldType.setHasDocValues(true);
    Consumer<InternalSampler> verify = result -> {
        Terms terms = result.getAggregations().get("terms");
        assertEquals(2, terms.getBuckets().size());
        assertEquals("0805080481", terms.getBuckets().get(0).getKeyAsString());
        assertEquals("0812550706", terms.getBuckets().get(1).getKeyAsString());
    };
    testCase(indexSearcher, genreFieldType, "map", verify);
    testCase(indexSearcher, genreFieldType, "global_ordinals", verify);
    testCase(indexSearcher, genreFieldType, "bytes_hash", verify);
    genreFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    genreFieldType.setName("genre_id");
    testCase(indexSearcher, genreFieldType, null, verify);
    // wrong field:
    genreFieldType = new KeywordFieldMapper.KeywordFieldType();
    genreFieldType.setName("wrong_field");
    genreFieldType.setHasDocValues(true);
    testCase(indexSearcher, genreFieldType, null, result -> {
        Terms terms = result.getAggregations().get("terms");
        assertEquals(1, terms.getBuckets().size());
        assertEquals("0805080481", terms.getBuckets().get(0).getKeyAsString());
    });
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) StringField(org.apache.lucene.document.StringField) Index(org.elasticsearch.index.Index) Document(org.apache.lucene.document.Document) SortedNumericDVIndexFieldData(org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Directory(org.apache.lucene.store.Directory) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) FieldValueFactorFunction(org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) DirectoryReader(org.apache.lucene.index.DirectoryReader) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Consumer(java.util.function.Consumer) AggregatorTestCase(org.elasticsearch.search.aggregations.AggregatorTestCase) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) Field(org.apache.lucene.document.Field) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) TextField(org.apache.lucene.document.TextField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) NumberFieldMapper(org.elasticsearch.index.mapper.NumberFieldMapper) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Document(org.apache.lucene.document.Document) KeywordFieldMapper(org.elasticsearch.index.mapper.KeywordFieldMapper) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) StringField(org.apache.lucene.document.StringField) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) TextField(org.apache.lucene.document.TextField) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Example 37 with Consumer

use of java.util.function.Consumer in project elasticsearch by elastic.

the class DateHistogramAggregatorTests method testNoDocs.

public void testNoDocs() throws IOException {
    Query query = new MatchNoDocsQuery();
    List<String> dates = Collections.emptyList();
    Consumer<DateHistogramAggregationBuilder> aggregation = agg -> agg.dateHistogramInterval(DateHistogramInterval.YEAR).field(DATE_FIELD);
    testSearchCase(query, dates, aggregation, histogram -> assertEquals(0, histogram.getBuckets().size()));
    testSearchAndReduceCase(query, dates, aggregation, histogram -> assertNull(histogram));
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) LongPoint(org.apache.lucene.document.LongPoint) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) DateFieldMapper(org.elasticsearch.index.mapper.DateFieldMapper) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Consumer(java.util.function.Consumer) Document(org.apache.lucene.document.Document) AggregatorTestCase(org.elasticsearch.search.aggregations.AggregatorTestCase) List(java.util.List) Directory(org.apache.lucene.store.Directory) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Collections(java.util.Collections) IndexReader(org.apache.lucene.index.IndexReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery)

Example 38 with Consumer

use of java.util.function.Consumer in project POL-POM-5 by PlayOnLinux.

the class NashornEngineFactory method createEngine.

NashornEngine createEngine() {
    final Set<List<String>> includedScripts = new HashSet<>();
    final NashornEngine nashornEngine = new NashornEngine(new ScriptEngineManager().getEngineByName("nashorn"));
    nashornEngine.eval(new InputStreamReader(getClass().getResourceAsStream("utils.js")), this::throwException);
    nashornEngine.put("Bean", (Function<String, Object>) title -> applicationContext.getBean(title), this::throwException);
    nashornEngine.put("SetupWizard", (Function<String, UiSetupWizardImplementation>) (name) -> {
        final UiSetupWizardImplementation uiSetupWizardImplementation = uiSetupWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiSetupWizardImplementation.close());
        return uiSetupWizardImplementation;
    }, this::throwException);
    nashornEngine.put("EngineProgressUi", (Function<String, UiProgressWizardImplementation>) (name) -> {
        final UiProgressWizardImplementation uiProgressWizardImplementation = uiProgressWizardFactory.create(name);
        nashornEngine.addErrorHandler(e -> uiProgressWizardImplementation.close());
        return uiProgressWizardImplementation;
    }, this::throwException);
    nashornEngine.put("include", (Consumer<ScriptObjectMirror>) args -> {
        final String[] arguments = args.to(String[].class);
        final String script = scriptFetcher.getScript(arguments);
        if (script == null) {
            throwException(new ScriptException(Arrays.asList(arguments).toString() + " is not found"));
        }
        if (includedScripts.add(Arrays.asList(arguments))) {
            nashornEngine.eval("//# sourceURL=" + Arrays.asList(arguments).toString() + "\n" + script, this::throwException);
        }
    }, this::throwException);
    return nashornEngine;
}
Also used : Arrays(java.util.Arrays) UiProgressWizardFactory(org.phoenicis.scripts.wizard.UiProgressWizardFactory) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiSetupWizardFactory(org.phoenicis.scripts.wizard.UiSetupWizardFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ScriptEngineManager(javax.script.ScriptEngineManager) Set(java.util.Set) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) ApplicationContext(org.springframework.context.ApplicationContext) InputStreamReader(java.io.InputStreamReader) Function(java.util.function.Function) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InputStreamReader(java.io.InputStreamReader) UiSetupWizardImplementation(org.phoenicis.scripts.wizard.UiSetupWizardImplementation) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptException(org.phoenicis.scripts.interpreter.ScriptException) UiProgressWizardImplementation(org.phoenicis.scripts.wizard.UiProgressWizardImplementation) List(java.util.List) HashSet(java.util.HashSet)

Example 39 with Consumer

use of java.util.function.Consumer in project POL-POM-5 by PlayOnLinux.

the class WinePrefixContainerController method repairPrefix.

public void repairPrefix(WinePrefixContainerDTO winePrefix, Runnable doneCallback, Consumer<Exception> errorCallback) {
    // FIXME
    final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
    interactiveScriptSession.eval("include([\"Functions\", \"Engines\", \"Wine\"]);", ignored -> interactiveScriptSession.eval("new Wine()", output -> {
        final ScriptObjectMirror wine = (ScriptObjectMirror) output;
        wine.callMember("prefix", winePrefix.getName());
        wine.callMember("run", "wineboot");
        wine.callMember("wait");
        doneCallback.run();
    }, errorCallback), errorCallback);
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) RegistryWriter(org.phoenicis.win32.registry.RegistryWriter) Logger(org.slf4j.Logger) FileUtilities(org.phoenicis.tools.files.FileUtilities) LoggerFactory(org.slf4j.LoggerFactory) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Consumer(java.util.function.Consumer) TerminalOpener(org.phoenicis.tools.system.terminal.TerminalOpener) List(java.util.List) RegistryParameter(org.phoenicis.containers.wine.parameters.RegistryParameter) ShortcutManager(org.phoenicis.library.ShortcutManager) Map(java.util.Map) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) OperatingSystemFetcher(org.phoenicis.tools.system.OperatingSystemFetcher) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) LibraryManager(org.phoenicis.library.LibraryManager) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 40 with Consumer

use of java.util.function.Consumer in project POL-POM-5 by PlayOnLinux.

the class WinePrefixContainerController method deletePrefix.

public void deletePrefix(WinePrefixContainerDTO winePrefix, Consumer<Exception> errorCallback) {
    try {
        fileUtilities.remove(new File(winePrefix.getPath()));
    } catch (IOException e) {
        LOGGER.error("Cannot delete Wine prefix (" + winePrefix.getPath() + ")! Exception: " + e.toString());
        errorCallback.accept(e);
    }
    List<ShortcutCategoryDTO> categories = libraryManager.fetchShortcuts();
    categories.stream().flatMap(shortcutCategoryDTO -> shortcutCategoryDTO.getShortcuts().stream()).forEach(shortcutDTO -> {
        final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
        interactiveScriptSession.eval("include([\"Functions\", \"Shortcuts\", \"Reader\"]);", ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> {
            final ScriptObjectMirror shortcutReader = (ScriptObjectMirror) output;
            shortcutReader.callMember("of", shortcutDTO);
            final String container = (String) shortcutReader.callMember("container");
            if (container.equals(winePrefix.getName())) {
                shortcutManager.deleteShortcut(shortcutDTO);
            }
        }, errorCallback), errorCallback);
    });
}
Also used : ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) RegistryWriter(org.phoenicis.win32.registry.RegistryWriter) Logger(org.slf4j.Logger) FileUtilities(org.phoenicis.tools.files.FileUtilities) LoggerFactory(org.slf4j.LoggerFactory) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) HashMap(java.util.HashMap) File(java.io.File) Consumer(java.util.function.Consumer) TerminalOpener(org.phoenicis.tools.system.terminal.TerminalOpener) List(java.util.List) RegistryParameter(org.phoenicis.containers.wine.parameters.RegistryParameter) ShortcutManager(org.phoenicis.library.ShortcutManager) Map(java.util.Map) WinePrefixContainerDTO(org.phoenicis.containers.dto.WinePrefixContainerDTO) OperatingSystemFetcher(org.phoenicis.tools.system.OperatingSystemFetcher) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) LibraryManager(org.phoenicis.library.LibraryManager) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ShortcutCategoryDTO(org.phoenicis.library.dto.ShortcutCategoryDTO) IOException(java.io.IOException) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) File(java.io.File)

Aggregations

Consumer (java.util.function.Consumer)193 List (java.util.List)81 Test (org.junit.Test)55 IOException (java.io.IOException)53 ArrayList (java.util.ArrayList)52 Map (java.util.Map)49 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)46 LoggerFactory (org.slf4j.LoggerFactory)41 Logger (org.slf4j.Logger)40 HashMap (java.util.HashMap)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 File (java.io.File)31 Collections (java.util.Collections)27 Set (java.util.Set)25 Collectors (java.util.stream.Collectors)25 TimeUnit (java.util.concurrent.TimeUnit)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 CountDownLatch (java.util.concurrent.CountDownLatch)23 Arrays (java.util.Arrays)21 Supplier (java.util.function.Supplier)21