Search in sources :

Example 51 with Stream

use of java.util.stream.Stream in project karaf by apache.

the class FilesStream method files.

private static Stream<Path> files(Path cur, String glob) {
    String prefix;
    String rem;
    int idx = glob.lastIndexOf('/');
    if (idx >= 0) {
        prefix = glob.substring(0, idx + 1);
        rem = glob.substring(idx + 1);
    } else {
        prefix = "";
        rem = glob;
    }
    Path dir = cur.resolve(prefix);
    final PathMatcher matcher = dir.getFileSystem().getPathMatcher("glob:" + rem);
    Stream.Builder<Path> stream = Stream.builder();
    try {
        Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.equals(dir)) {
                    return FileVisitResult.CONTINUE;
                }
                if (Files.isHidden(file)) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                Path r = dir.relativize(file);
                if (matcher.matches(r)) {
                    stream.add(file);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (!Files.isHidden(file)) {
                    Path r = dir.relativize(file);
                    if (matcher.matches(r)) {
                        stream.add(file);
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        LOGGER.warn("Error generating filenames", e);
    }
    return stream.build();
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) Stream(java.util.stream.Stream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 52 with Stream

use of java.util.stream.Stream in project lucene-solr by apache.

the class RamUsageTester method measureObjectSize.

/*
   * Non-recursive version of object descend. This consumes more memory than recursive in-depth
   * traversal but prevents stack overflows on long chains of objects
   * or complex graphs (a max. recursion depth on my machine was ~5000 objects linked in a chain
   * so not too much).
   */
private static long measureObjectSize(Object root, Accumulator accumulator) {
    // Objects seen so far.
    final Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>());
    // Class cache with reference Field and precalculated shallow size. 
    final IdentityHashMap<Class<?>, ClassCache> classCache = new IdentityHashMap<>();
    // Stack of objects pending traversal. Recursion caused stack overflows. 
    final ArrayList<Object> stack = new ArrayList<>();
    stack.add(root);
    long totalSize = 0;
    while (!stack.isEmpty()) {
        final Object ob = stack.remove(stack.size() - 1);
        if (ob == null || seen.contains(ob)) {
            continue;
        }
        seen.add(ob);
        final Class<?> obClazz = ob.getClass();
        assert obClazz != null : "jvm bug detected (Object.getClass() == null). please report this to your vendor";
        if (obClazz.isArray()) {
            /*
         * Consider an array, possibly of primitive types. Push any of its references to
         * the processing stack and accumulate this array's shallow size. 
         */
            final long shallowSize = RamUsageEstimator.shallowSizeOf(ob);
            final int len = Array.getLength(ob);
            final List<Object> values;
            Class<?> componentClazz = obClazz.getComponentType();
            if (componentClazz.isPrimitive()) {
                values = Collections.emptyList();
            } else {
                values = new AbstractList<Object>() {

                    @Override
                    public Object get(int index) {
                        return Array.get(ob, index);
                    }

                    @Override
                    public int size() {
                        return len;
                    }
                };
            }
            totalSize += accumulator.accumulateArray(ob, shallowSize, values, stack);
        } else {
            /*
         * Consider an object. Push any references it has to the processing stack
         * and accumulate this object's shallow size. 
         */
            try {
                ClassCache cachedInfo = classCache.get(obClazz);
                if (cachedInfo == null) {
                    classCache.put(obClazz, cachedInfo = createCacheEntry(obClazz));
                }
                boolean needsReflection = true;
                if (Constants.JRE_IS_MINIMUM_JAVA9 && obClazz.getName().startsWith("java.")) {
                    // Java 9: Best guess for some known types, as we cannot precisely look into runtime classes:
                    final ToLongFunction<Object> func = SIMPLE_TYPES.get(obClazz);
                    if (func != null) {
                        // some simple type like String where the size is easy to get from public properties
                        totalSize += accumulator.accumulateObject(ob, cachedInfo.alignedShallowInstanceSize + func.applyAsLong(ob), Collections.emptyMap(), stack);
                        needsReflection = false;
                    } else if (ob instanceof Iterable) {
                        final List<Object> values = StreamSupport.stream(((Iterable<?>) ob).spliterator(), false).collect(Collectors.toList());
                        totalSize += accumulator.accumulateArray(ob, cachedInfo.alignedShallowInstanceSize + RamUsageEstimator.NUM_BYTES_ARRAY_HEADER, values, stack);
                        needsReflection = false;
                    } else if (ob instanceof Map) {
                        final List<Object> values = ((Map<?, ?>) ob).entrySet().stream().flatMap(e -> Stream.of(e.getKey(), e.getValue())).collect(Collectors.toList());
                        totalSize += accumulator.accumulateArray(ob, cachedInfo.alignedShallowInstanceSize + RamUsageEstimator.NUM_BYTES_ARRAY_HEADER, values, stack);
                        totalSize += RamUsageEstimator.NUM_BYTES_ARRAY_HEADER;
                        needsReflection = false;
                    }
                }
                if (needsReflection) {
                    final Map<Field, Object> fieldValues = new HashMap<>();
                    for (Field f : cachedInfo.referenceFields) {
                        fieldValues.put(f, f.get(ob));
                    }
                    totalSize += accumulator.accumulateObject(ob, cachedInfo.alignedShallowInstanceSize, fieldValues, stack);
                }
            } catch (IllegalAccessException e) {
                // this should never happen as we enabled setAccessible().
                throw new RuntimeException("Reflective field access failed?", e);
            }
        }
    }
    // Help the GC (?).
    seen.clear();
    stack.clear();
    classCache.clear();
    return totalSize;
}
Also used : Array(java.lang.reflect.Array) IdentityHashMap(java.util.IdentityHashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Collection(java.util.Collection) AbstractList(java.util.AbstractList) Set(java.util.Set) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) PrivilegedAction(java.security.PrivilegedAction) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) AccessController(java.security.AccessController) ToLongFunction(java.util.function.ToLongFunction) Path(java.nio.file.Path) Collections(java.util.Collections) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) AbstractList(java.util.AbstractList) ArrayList(java.util.ArrayList) List(java.util.List) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 53 with Stream

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

the class HibernateSecondLevelCache method addSecondLevelCacheDependencies.

public static void addSecondLevelCacheDependencies(Properties mutableProperties, String scopedPersistenceUnitName) {
    if (mutableProperties.getProperty(AvailableSettings.CACHE_REGION_PREFIX) == null) {
        if (scopedPersistenceUnitName != null) {
            mutableProperties.setProperty(AvailableSettings.CACHE_REGION_PREFIX, scopedPersistenceUnitName);
        }
    }
    String regionFactory = mutableProperties.getProperty(AvailableSettings.CACHE_REGION_FACTORY);
    if (regionFactory == null) {
        regionFactory = DEFAULT_REGION_FACTORY;
        mutableProperties.setProperty(AvailableSettings.CACHE_REGION_FACTORY, regionFactory);
    }
    if (regionFactory.equals(DEFAULT_REGION_FACTORY)) {
        // Set infinispan defaults
        String container = mutableProperties.getProperty(CACHE_CONTAINER);
        if (container == null) {
            container = DEFAULT_CACHE_CONTAINER;
            mutableProperties.setProperty(CACHE_CONTAINER, container);
        }
        /**
             * AS will need the ServiceBuilder<?> builder that used to be passed to PersistenceProviderAdaptor.addProviderDependencies
             */
        Properties cacheSettings = new Properties();
        cacheSettings.put(CONTAINER, container);
        cacheSettings.put(ENTITY, mutableProperties.getProperty(ENTITY_CACHE_RESOURCE_PROP, DEF_ENTITY_RESOURCE));
        cacheSettings.put(IMMUTABLE_ENTITY, mutableProperties.getProperty(IMMUTABLE_ENTITY_CACHE_RESOURCE_PROP, DEF_ENTITY_RESOURCE));
        cacheSettings.put(COLLECTION, mutableProperties.getProperty(COLLECTION_CACHE_RESOURCE_PROP, DEF_ENTITY_RESOURCE));
        cacheSettings.put(NATURAL_ID, mutableProperties.getProperty(NATURAL_ID_CACHE_RESOURCE_PROP, DEF_ENTITY_RESOURCE));
        if (mutableProperties.getProperty(PENDING_PUTS_CACHE_RESOURCE_PROP) != null) {
            cacheSettings.put(PENDING_PUTS, mutableProperties.getProperty(PENDING_PUTS_CACHE_RESOURCE_PROP));
        }
        if (Boolean.parseBoolean(mutableProperties.getProperty(AvailableSettings.USE_QUERY_CACHE))) {
            cacheSettings.put(QUERY, mutableProperties.getProperty(QUERY_CACHE_RESOURCE_PROP, DEF_QUERY_RESOURCE));
            cacheSettings.put(TIMESTAMPS, mutableProperties.getProperty(TIMESTAMPS_CACHE_RESOURCE_PROP, DEF_QUERY_RESOURCE));
        }
        // Collect distinct cache configurations for standard regions
        Set<String> standardRegionConfigs = Stream.of(ENTITY, IMMUTABLE_ENTITY, COLLECTION, NATURAL_ID, PENDING_PUTS, QUERY, TIMESTAMPS).map(region -> cacheSettings.getProperty(region)).filter(Objects::nonNull).collect(Collectors.toSet());
        int length = INFINISPAN_CONFIG_RESOURCE_PROP.length();
        String customRegionPrefix = INFINISPAN_CONFIG_RESOURCE_PROP.substring(0, length - 3) + mutableProperties.getProperty(AvailableSettings.CACHE_REGION_PREFIX, "");
        String customRegionSuffix = INFINISPAN_CONFIG_RESOURCE_PROP.substring(length - 4, length);
        // Collect distinct cache configurations for custom regions
        Set<String> customRegionConfigs = mutableProperties.stringPropertyNames().stream().filter(name -> name.startsWith(customRegionPrefix) && name.endsWith(customRegionSuffix)).map(name -> mutableProperties.getProperty(name)).filter(config -> !standardRegionConfigs.contains(config)).collect(Collectors.toSet());
        if (!customRegionConfigs.isEmpty()) {
            cacheSettings.setProperty(CUSTOM, String.join(" ", customRegionConfigs));
        }
        Notification.addCacheDependencies(Classification.INFINISPAN, cacheSettings);
    }
}
Also used : NATURAL_ID_CACHE_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.NATURAL_ID_CACHE_RESOURCE_PROP) COLLECTION_CACHE_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.COLLECTION_CACHE_RESOURCE_PROP) DEF_QUERY_RESOURCE(org.hibernate.cache.infinispan.InfinispanRegionFactory.DEF_QUERY_RESOURCE) INFINISPAN_CONFIG_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.INFINISPAN_CONFIG_RESOURCE_PROP) Properties(java.util.Properties) Notification(org.jipijapa.event.impl.internal.Notification) DEFAULT_CACHE_CONTAINER(org.jboss.as.jpa.hibernate5.infinispan.InfinispanRegionFactory.DEFAULT_CACHE_CONTAINER) DEF_ENTITY_RESOURCE(org.hibernate.cache.infinispan.InfinispanRegionFactory.DEF_ENTITY_RESOURCE) AvailableSettings(org.hibernate.cfg.AvailableSettings) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) SharedInfinispanRegionFactory(org.jboss.as.jpa.hibernate5.infinispan.SharedInfinispanRegionFactory) PENDING_PUTS_CACHE_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.PENDING_PUTS_CACHE_RESOURCE_PROP) CACHE_CONTAINER(org.jboss.as.jpa.hibernate5.infinispan.InfinispanRegionFactory.CACHE_CONTAINER) IMMUTABLE_ENTITY_CACHE_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.IMMUTABLE_ENTITY_CACHE_RESOURCE_PROP) Stream(java.util.stream.Stream) QUERY_CACHE_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.QUERY_CACHE_RESOURCE_PROP) TIMESTAMPS_CACHE_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.TIMESTAMPS_CACHE_RESOURCE_PROP) ENTITY_CACHE_RESOURCE_PROP(org.hibernate.cache.infinispan.InfinispanRegionFactory.ENTITY_CACHE_RESOURCE_PROP) Classification(org.jipijapa.cache.spi.Classification) Properties(java.util.Properties)

Example 54 with Stream

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

the class AddStepHandler method recordCapabilitiesAndRequirements.

@Override
protected void recordCapabilitiesAndRequirements(OperationContext context, ModelNode operation, Resource resource) throws OperationFailedException {
    PathAddress address = context.getCurrentAddress();
    ModelNode model = resource.getModel();
    // The super implementation assumes that the capability name is a simple extension of the base name - we do not.
    // Only register capabilities when allowed by the associated predicate
    this.descriptor.getCapabilities().entrySet().stream().filter(entry -> entry.getValue().test(model)).map(Map.Entry::getKey).forEach(capability -> context.registerCapability(capability.resolve(address)));
    ImmutableManagementResourceRegistration registration = context.getResourceRegistration();
    registration.getAttributeNames(PathAddress.EMPTY_ADDRESS).stream().map(name -> registration.getAttributeAccess(PathAddress.EMPTY_ADDRESS, name)).filter(Objects::nonNull).map(AttributeAccess::getAttributeDefinition).filter(Objects::nonNull).filter(AttributeDefinition::hasCapabilityRequirements).forEach(attribute -> attribute.addCapabilityRequirements(context, model.get(attribute.getName())));
    this.descriptor.getResourceCapabilityReferences().forEach((reference, resolver) -> reference.addCapabilityRequirements(context, (String) null, resolver.apply(address)));
}
Also used : OperationEntry(org.jboss.as.controller.registry.OperationEntry) OperationStepHandler(org.jboss.as.controller.OperationStepHandler) Resource(org.jboss.as.controller.registry.Resource) PathAddress(org.jboss.as.controller.PathAddress) Collection(java.util.Collection) SimpleAttributeDefinitionBuilder(org.jboss.as.controller.SimpleAttributeDefinitionBuilder) PathElement(org.jboss.as.controller.PathElement) ImmutableManagementResourceRegistration(org.jboss.as.controller.registry.ImmutableManagementResourceRegistration) AttributeAccess(org.jboss.as.controller.registry.AttributeAccess) Objects(java.util.Objects) BiPredicate(java.util.function.BiPredicate) ManagementResourceRegistration(org.jboss.as.controller.registry.ManagementResourceRegistration) OperationContext(org.jboss.as.controller.OperationContext) Stream(java.util.stream.Stream) AbstractAddStepHandler(org.jboss.as.controller.AbstractAddStepHandler) OperationFailedException(org.jboss.as.controller.OperationFailedException) Util(org.jboss.as.controller.operations.common.Util) Map(java.util.Map) Optional(java.util.Optional) ModelNode(org.jboss.dmr.ModelNode) SimpleOperationDefinitionBuilder(org.jboss.as.controller.SimpleOperationDefinitionBuilder) ModelDescriptionConstants(org.jboss.as.controller.descriptions.ModelDescriptionConstants) AttributeDefinition(org.jboss.as.controller.AttributeDefinition) ModelType(org.jboss.dmr.ModelType) PathAddress(org.jboss.as.controller.PathAddress) Objects(java.util.Objects) ImmutableManagementResourceRegistration(org.jboss.as.controller.registry.ImmutableManagementResourceRegistration) ModelNode(org.jboss.dmr.ModelNode) Map(java.util.Map)

Example 55 with Stream

use of java.util.stream.Stream in project gerrit by GerritCodeReview.

the class Schema_135 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
    try (Repository git = repoManager.openRepository(allProjectsName);
        MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allProjectsName, git)) {
        ProjectConfig config = ProjectConfig.read(md);
        AccessSection meta = config.getAccessSection(RefNames.REFS_CONFIG, true);
        Permission createRefsMetaConfigPermission = meta.getPermission(Permission.CREATE, true);
        Set<GroupReference> groups = Stream.concat(config.getAccessSection(AccessSection.GLOBAL_CAPABILITIES, true).getPermission(GlobalCapability.ADMINISTRATE_SERVER, true).getRules().stream().map(PermissionRule::getGroup), Stream.of(systemGroupBackend.getGroup(PROJECT_OWNERS))).filter(g -> createRefsMetaConfigPermission.getRule(g) == null).collect(toSet());
        for (GroupReference group : groups) {
            createRefsMetaConfigPermission.add(new PermissionRule(config.resolve(group)));
        }
        md.getCommitBuilder().setAuthor(serverUser);
        md.getCommitBuilder().setCommitter(serverUser);
        md.setMessage(COMMIT_MSG);
        config.commit(md);
    } catch (ConfigInvalidException | IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : PermissionRule(com.google.gerrit.common.data.PermissionRule) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GlobalCapability(com.google.gerrit.common.data.GlobalCapability) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate) Inject(com.google.inject.Inject) AccessSection(com.google.gerrit.common.data.AccessSection) SystemGroupBackend(com.google.gerrit.server.group.SystemGroupBackend) PROJECT_OWNERS(com.google.gerrit.server.group.SystemGroupBackend.PROJECT_OWNERS) Collectors.toSet(java.util.stream.Collectors.toSet) Permission(com.google.gerrit.common.data.Permission) Set(java.util.Set) IOException(java.io.IOException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) Provider(com.google.inject.Provider) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) Stream(java.util.stream.Stream) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) RefNames(com.google.gerrit.reviewdb.client.RefNames) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) GroupReference(com.google.gerrit.common.data.GroupReference) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) Repository(org.eclipse.jgit.lib.Repository) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) PermissionRule(com.google.gerrit.common.data.PermissionRule) IOException(java.io.IOException) AccessSection(com.google.gerrit.common.data.AccessSection) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) Permission(com.google.gerrit.common.data.Permission) GroupReference(com.google.gerrit.common.data.GroupReference) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

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