Search in sources :

Example 96 with Stream

use of java.util.stream.Stream in project neo4j by neo4j.

the class SwitchToSlaveCopyThenBranchTest method newSwitchToSlaveSpy.

private SwitchToSlaveCopyThenBranch newSwitchToSlaveSpy() throws Exception {
    PageCache pageCacheMock = mock(PageCache.class);
    PagedFile pagedFileMock = mock(PagedFile.class);
    when(pagedFileMock.getLastPageId()).thenReturn(1L);
    when(pageCacheMock.map(any(File.class), anyInt())).thenReturn(pagedFileMock);
    StoreCopyClient storeCopyClient = mock(StoreCopyClient.class);
    Stream mockStream = mock(Stream.class);
    when(mockStream.filter(any(Predicate.class))).thenReturn(mock(Stream.class));
    when(pageCacheMock.streamFilesRecursive(any(File.class))).thenReturn(mockStream);
    return newSwitchToSlaveSpy(pageCacheMock, storeCopyClient);
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) StoreCopyClient(org.neo4j.com.storecopy.StoreCopyClient) Stream(java.util.stream.Stream) PagedFile(org.neo4j.io.pagecache.PagedFile) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Predicate(java.util.function.Predicate)

Example 97 with Stream

use of java.util.stream.Stream in project sonarqube by SonarSource.

the class SearchProjectsAction method addFacets.

private static SearchProjectsWsResponse.Builder addFacets(SearchResults searchResults, SearchProjectsWsResponse.Builder wsResponse) {
    Facets esFacets = searchResults.facets;
    EsToWsFacet esToWsFacet = new EsToWsFacet();
    searchResults.query.getLanguages().ifPresent(languages -> addMandatoryValuesToFacet(esFacets, FILTER_LANGUAGES, languages));
    searchResults.query.getTags().ifPresent(tags -> addMandatoryValuesToFacet(esFacets, FILTER_TAGS, tags));
    Common.Facets wsFacets = esFacets.getAll().entrySet().stream().map(esToWsFacet).collect(Collector.of(Common.Facets::newBuilder, Common.Facets.Builder::addFacets, (result1, result2) -> {
        throw new IllegalStateException("Parallel execution forbidden");
    }, Common.Facets.Builder::build));
    wsResponse.setFacets(wsFacets);
    return wsResponse;
}
Also used : Facets(org.sonar.server.es.Facets) Collectors.toSet(org.sonar.core.util.stream.Collectors.toSet) DbSession(org.sonar.db.DbSession) Request(org.sonar.api.server.ws.Request) SUPPORTED_FACETS(org.sonar.server.measure.index.ProjectMeasuresIndex.SUPPORTED_FACETS) DEFAULT_PAGE_SIZE(org.sonarqube.ws.client.component.SearchProjectsRequest.DEFAULT_PAGE_SIZE) Param(org.sonar.api.server.ws.WebService.Param) WebService(org.sonar.api.server.ws.WebService) PropertyQuery(org.sonar.db.property.PropertyQuery) Map(java.util.Map) ProjectMeasuresQueryFactory.newProjectMeasuresQuery(org.sonar.server.component.ws.ProjectMeasuresQueryFactory.newProjectMeasuresQuery) WsUtils.checkFoundWithOptional(org.sonar.server.ws.WsUtils.checkFoundWithOptional) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) PARAM_FILTER(org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_FILTER) Collector(java.util.stream.Collector) OrganizationDto(org.sonar.db.organization.OrganizationDto) Criterion(org.sonar.server.component.ws.FilterParser.Criterion) FILTER_LANGUAGES(org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_LANGUAGES) ImmutableMap(com.google.common.collect.ImmutableMap) SearchIdResult(org.sonar.server.es.SearchIdResult) SORT_BY_NAME(org.sonar.server.measure.index.ProjectMeasuresQuery.SORT_BY_NAME) Set(java.util.Set) Collectors(org.sonar.core.util.stream.Collectors) SearchProjectsRequest(org.sonarqube.ws.client.component.SearchProjectsRequest) NotFoundException(org.sonar.server.exceptions.NotFoundException) String.format(java.lang.String.format) Common(org.sonarqube.ws.Common) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) SearchOptions(org.sonar.server.es.SearchOptions) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) Stream(java.util.stream.Stream) ProjectMeasuresQuery(org.sonar.server.measure.index.ProjectMeasuresQuery) Entry(java.util.Map.Entry) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) FIELDS(org.sonar.api.server.ws.WebService.Param.FIELDS) MAX_PAGE_SIZE(org.sonarqube.ws.client.component.SearchProjectsRequest.MAX_PAGE_SIZE) PropertyDto(org.sonar.db.property.PropertyDto) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) PARAM_ORGANIZATION(org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION) SearchProjectsWsResponse(org.sonarqube.ws.WsComponents.SearchProjectsWsResponse) IS_FAVORITE_CRITERION(org.sonar.server.component.ws.ProjectMeasuresQueryFactory.IS_FAVORITE_CRITERION) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) ProjectMeasuresIndex(org.sonar.server.measure.index.ProjectMeasuresIndex) DateUtils(org.sonar.api.utils.DateUtils) Response(org.sonar.api.server.ws.Response) Nullable(javax.annotation.Nullable) Collections.emptyMap(java.util.Collections.emptyMap) Component(org.sonarqube.ws.WsComponents.Component) Ordering(com.google.common.collect.Ordering) Qualifiers(org.sonar.api.resources.Qualifiers) Change(org.sonar.api.server.ws.Change) UserSession(org.sonar.server.user.UserSession) NCLOC_KEY(org.sonar.api.measures.CoreMetrics.NCLOC_KEY) FILTER_TAGS(org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_TAGS) SnapshotDto(org.sonar.db.component.SnapshotDto) Collections(java.util.Collections) Facets(org.sonar.server.es.Facets) Common(org.sonarqube.ws.Common)

Example 98 with Stream

use of java.util.stream.Stream in project processing by processing.

the class ChangeDetector method checkFiles.

// Synchronize, we are running async and touching fields
private synchronized void checkFiles() {
    List<String> filenames = new ArrayList<>();
    sketch.getSketchCodeFiles(filenames, null);
    SketchCode[] codes = sketch.getCode();
    // Separate codes with and without files
    Map<Boolean, List<SketchCode>> existsMap = Arrays.stream(codes).collect(Collectors.groupingBy(code -> filenames.contains(code.getFileName())));
    // ADDED FILES
    List<String> codeFilenames = Arrays.stream(codes).map(SketchCode::getFileName).collect(Collectors.toList());
    // Get filenames which are in filesystem but don't have code
    List<String> addedFilenames = filenames.stream().filter(f -> !codeFilenames.contains(f)).collect(Collectors.toList());
    // Show prompt if there are any added files which were not previously ignored
    boolean added = addedFilenames.stream().anyMatch(f -> !ignoredAdditions.contains(f));
    // REMOVED FILES
    // Get codes which don't have file
    List<SketchCode> removedCodes = Optional.ofNullable(existsMap.get(Boolean.FALSE)).orElse(Collections.emptyList());
    // Show prompt if there are any removed codes which were not previously ignored
    boolean removed = removedCodes.stream().anyMatch(code -> !ignoredRemovals.contains(code));
    /// MODIFIED FILES
    // Get codes which have file with different modification time
    List<SketchCode> modifiedCodes = Optional.ofNullable(existsMap.get(Boolean.TRUE)).orElse(Collections.emptyList()).stream().filter(code -> {
        long fileLastModified = code.getFile().lastModified();
        long codeLastModified = code.getLastModified();
        long diff = fileLastModified - codeLastModified;
        return fileLastModified == 0L || diff > MODIFICATION_WINDOW_MILLIS;
    }).collect(Collectors.toList());
    // Show prompt if any open codes were modified
    boolean modified = !modifiedCodes.isEmpty();
    boolean ask = added || removed || modified;
    if (DEBUG) {
        System.out.println("ask: " + ask + "\n" + "added filenames: " + addedFilenames + ",\n" + "ignored added: " + ignoredAdditions + ",\n" + "removed codes: " + removedCodes + ",\n" + "ignored removed: " + ignoredRemovals + ",\n" + "modified codes: " + modifiedCodes);
    }
    // dismissing the prompt and we get another prompt before we even finished.
    try {
        // Wait for EDT to finish its business
        // We need to stay in synchronized scope because of ignore lists
        EventQueue.invokeAndWait(() -> {
            // Show prompt if something interesting happened
            if (ask && showReloadPrompt()) {
                // She said yes!!!
                if (sketch.getMainFile().exists()) {
                    sketch.reload();
                    editor.rebuildHeader();
                } else {
                    // Mark everything as modified so that it saves properly
                    for (SketchCode code : codes) {
                        code.setModified(true);
                    }
                    try {
                        sketch.save();
                    } catch (Exception e) {
                        //if that didn't work, tell them it's un-recoverable
                        Messages.showError("Reload Failed", "The main file for this sketch was deleted\n" + "and could not be rewritten.", e);
                    }
                }
                // Sketch was reloaded, clear ignore lists
                ignoredAdditions.clear();
                ignoredRemovals.clear();
                return;
            }
            // Update ignore lists to get rid of old stuff
            ignoredAdditions = addedFilenames;
            ignoredRemovals = removedCodes;
            // If something changed, set modified flags and modification times
            if (!removedCodes.isEmpty() || !modifiedCodes.isEmpty()) {
                Stream.concat(removedCodes.stream(), modifiedCodes.stream()).forEach(code -> {
                    code.setModified(true);
                    code.setLastModified();
                });
                // Not sure if this is needed
                editor.rebuildHeader();
            }
        });
    } catch (InterruptedException ignore) {
    } catch (InvocationTargetException e) {
        Messages.loge("exception in ChangeDetector", e);
    }
}
Also used : EventQueue(java.awt.EventQueue) Arrays(java.util.Arrays) Messages(processing.app.Messages) Sketch(processing.app.Sketch) WindowFocusListener(java.awt.event.WindowFocusListener) JOptionPane(javax.swing.JOptionPane) Collectors(java.util.stream.Collectors) WindowEvent(java.awt.event.WindowEvent) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) ForkJoinPool(java.util.concurrent.ForkJoinPool) Map(java.util.Map) Optional(java.util.Optional) Preferences(processing.app.Preferences) Collections(java.util.Collections) SketchCode(processing.app.SketchCode) SketchCode(processing.app.SketchCode) ArrayList(java.util.ArrayList) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 99 with Stream

use of java.util.stream.Stream in project presto by prestodb.

the class AbstractTestQueries method testNonDeterministic.

@Test
public void testNonDeterministic() {
    MaterializedResult materializedResult = computeActual("SELECT rand() FROM orders LIMIT 10");
    long distinctCount = materializedResult.getMaterializedRows().stream().map(row -> row.getField(0)).distinct().count();
    assertTrue(distinctCount >= 8, "rand() must produce different rows");
    materializedResult = computeActual("SELECT apply(1, x -> x + rand()) FROM orders LIMIT 10");
    distinctCount = materializedResult.getMaterializedRows().stream().map(row -> row.getField(0)).distinct().count();
    assertTrue(distinctCount >= 8, "rand() must produce different rows");
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) DateTimeZoneIndex(com.facebook.presto.util.DateTimeZoneIndex) TestingAccessControlManager.privilege(com.facebook.presto.testing.TestingAccessControlManager.privilege) DateTimeZone(org.joda.time.DateTimeZone) Time(java.sql.Time) Iterables.transform(com.google.common.collect.Iterables.transform) MISSING_SCHEMA(com.facebook.presto.sql.analyzer.SemanticErrorCode.MISSING_SCHEMA) QueryTemplate.queryTemplate(com.facebook.presto.tests.QueryTemplate.queryTemplate) IntStream.range(java.util.stream.IntStream.range) APPLY_FUNCTION(com.facebook.presto.operator.scalar.ApplyFunction.APPLY_FUNCTION) MaterializedResult.resultBuilder(com.facebook.presto.testing.MaterializedResult.resultBuilder) Test(org.testng.annotations.Test) PropertyMetadata(com.facebook.presto.spi.session.PropertyMetadata) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) FunctionListBuilder(com.facebook.presto.metadata.FunctionListBuilder) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) SELECT_TABLE(com.facebook.presto.testing.TestingAccessControlManager.TestingPrivilegeType.SELECT_TABLE) QueryTemplate.parameter(com.facebook.presto.tests.QueryTemplate.parameter) Assert.assertFalse(org.testng.Assert.assertFalse) Decimals(com.facebook.presto.spi.type.Decimals) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Timestamp(java.sql.Timestamp) Set(java.util.Set) VARCHAR(com.facebook.presto.spi.type.VarcharType.VARCHAR) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) TpchTable(io.airlift.tpch.TpchTable) List(java.util.List) Stream(java.util.stream.Stream) TESTING_CATALOG(com.facebook.presto.testing.TestingSession.TESTING_CATALOG) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) Optional(java.util.Optional) DISTRIBUTED(com.facebook.presto.sql.tree.ExplainType.Type.DISTRIBUTED) IntStream(java.util.stream.IntStream) TimeZoneKey(com.facebook.presto.spi.type.TimeZoneKey) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) DataProvider(org.testng.annotations.DataProvider) TIMESTAMP_WITH_TIME_ZONE(com.facebook.presto.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE) ORDERS(io.airlift.tpch.TpchTable.ORDERS) Assert.assertEquals(org.testng.Assert.assertEquals) LOGICAL(com.facebook.presto.sql.tree.ExplainType.Type.LOGICAL) Multimap(com.google.common.collect.Multimap) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) Multimaps(com.google.common.collect.Multimaps) HashSet(java.util.HashSet) INFORMATION_SCHEMA(com.facebook.presto.connector.informationSchema.InformationSchemaMetadata.INFORMATION_SCHEMA) QueryAssertions.assertEqualsIgnoreOrder(com.facebook.presto.tests.QueryAssertions.assertEqualsIgnoreOrder) ImmutableList(com.google.common.collect.ImmutableList) QueryAssertions.assertContains(com.facebook.presto.tests.QueryAssertions.assertContains) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) TIMESTAMP(com.facebook.presto.spi.type.TimestampType.TIMESTAMP) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) DELETE_TABLE(com.facebook.presto.testing.TestingAccessControlManager.TestingPrivilegeType.DELETE_TABLE) Language(org.intellij.lang.annotations.Language) Session(com.facebook.presto.Session) MUST_BE_AGGREGATE_OR_GROUP_BY(com.facebook.presto.sql.analyzer.SemanticErrorCode.MUST_BE_AGGREGATE_OR_GROUP_BY) Assert.fail(org.testng.Assert.fail) DateTime(org.joda.time.DateTime) TIME(com.facebook.presto.spi.type.TimeType.TIME) INVALID_PARAMETER_USAGE(com.facebook.presto.sql.analyzer.SemanticErrorCode.INVALID_PARAMETER_USAGE) INSERT_TABLE(com.facebook.presto.testing.TestingAccessControlManager.TestingPrivilegeType.INSERT_TABLE) Arguments(com.facebook.presto.testing.Arguments) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) TIME_WITH_TIME_ZONE(com.facebook.presto.spi.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE) Maps(com.google.common.collect.Maps) Date(java.sql.Date) Arguments.toArgumentsArrays(com.facebook.presto.testing.Arguments.toArgumentsArrays) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Collectors.toList(java.util.stream.Collectors.toList) Ordering(com.google.common.collect.Ordering) DATE(com.facebook.presto.spi.type.DateType.DATE) CREATE_TABLE(com.facebook.presto.testing.TestingAccessControlManager.TestingPrivilegeType.CREATE_TABLE) MaterializedRow(com.facebook.presto.testing.MaterializedRow) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) SqlFunction(com.facebook.presto.metadata.SqlFunction) Assert.assertTrue(org.testng.Assert.assertTrue) TpchTable.tableNameGetter(io.airlift.tpch.TpchTable.tableNameGetter) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 100 with Stream

use of java.util.stream.Stream in project aerosolve by airbnb.

the class Util method flattenFeatureWithDropoutAsStream.

/**
   * Convert a feature vector from example to a nested stream(feature family, stream(feature name.
   * feature value)) with dropout
   *
   * @apiNote Understand Stream can only be iterated once just like iterator, it is crucial to set a
   * random seed if one wants to reproduce consistent dropout result.
   */
public static Stream<? extends Map.Entry<String, Stream<? extends Map.Entry<String, Double>>>> flattenFeatureWithDropoutAsStream(FeatureVector featureVector, double dropout, long seed) {
    // collect string features into a stream
    Stream<? extends Map.Entry<String, Stream<? extends Map.Entry<String, Double>>>> stringFeatures = Stream.empty();
    if (featureVector.stringFeatures != null) {
        stringFeatures = featureVector.stringFeatures.entrySet().stream().map(entry -> {
            Stream<? extends Map.Entry<String, Double>> values = entry.getValue().stream().map(feature -> new HashMap.SimpleImmutableEntry<>(feature, 1.0));
            return new HashMap.SimpleImmutableEntry<>(entry.getKey(), values);
        });
    }
    // collect float features into a stream
    Stream<? extends Map.Entry<String, Stream<? extends Map.Entry<String, Double>>>> floatFeatures = Stream.empty();
    if (featureVector.floatFeatures != null) {
        floatFeatures = featureVector.floatFeatures.entrySet().stream().map(entry -> new HashMap.SimpleImmutableEntry<>(entry.getKey(), entry.getValue().entrySet().stream()));
    }
    // concat string and float features and apply dropout if necessary
    Stream<? extends Map.Entry<String, Stream<? extends Map.Entry<String, Double>>>> flatFeatures = Stream.concat(stringFeatures, floatFeatures);
    if (dropout > 0) {
        Random random = new Random(seed);
        // dropout needs to be applied in the inner most stream
        return flatFeatures.map(entry -> new HashMap.SimpleImmutableEntry<>(entry.getKey(), entry.getValue().filter(x -> random.nextDouble() >= dropout)));
    } else {
        return flatFeatures;
    }
}
Also used : Arrays(java.util.Arrays) GZIPInputStream(java.util.zip.GZIPInputStream) ModelRecord(com.airbnb.aerosolve.core.ModelRecord) HashMap(java.util.HashMap) Random(java.util.Random) Hashing(com.google.common.hash.Hashing) TSerializer(org.apache.thrift.TSerializer) Base64(org.apache.commons.codec.binary.Base64) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TDeserializer(org.apache.thrift.TDeserializer) FeatureVector(com.airbnb.aerosolve.core.FeatureVector) Map(java.util.Map) DebugScoreRecord(com.airbnb.aerosolve.core.DebugScoreRecord) Hasher(com.google.common.hash.Hasher) TBase(org.apache.thrift.TBase) HashCode(com.google.common.hash.HashCode) Set(java.util.Set) IOException(java.io.IOException) KDTreeNode(com.airbnb.aerosolve.core.KDTreeNode) DebugScoreDiffRecord(com.airbnb.aerosolve.core.DebugScoreDiffRecord) InputStreamReader(java.io.InputStreamReader) Serializable(java.io.Serializable) Example(com.airbnb.aerosolve.core.Example) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Stream(java.util.stream.Stream) BufferedReader(java.io.BufferedReader) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) Random(java.util.Random) HashMap(java.util.HashMap) GZIPInputStream(java.util.zip.GZIPInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Stream (java.util.stream.Stream)161 Collectors (java.util.stream.Collectors)98 List (java.util.List)89 ArrayList (java.util.ArrayList)66 Map (java.util.Map)66 Set (java.util.Set)59 IOException (java.io.IOException)58 Optional (java.util.Optional)45 Collections (java.util.Collections)43 HashMap (java.util.HashMap)43 Arrays (java.util.Arrays)33 HashSet (java.util.HashSet)33 File (java.io.File)32 Path (java.nio.file.Path)32 Function (java.util.function.Function)28 Logger (org.slf4j.Logger)26 LoggerFactory (org.slf4j.LoggerFactory)26 java.util (java.util)25 Predicate (java.util.function.Predicate)23 Objects (java.util.Objects)22