Search in sources :

Example 1 with Function

use of com.google.common.base.Function in project druid by druid-io.

the class BoundFilterBenchmark method setup.

@Setup
public void setup() throws IOException {
    step = (END_INT - START_INT) / cardinality;
    final BitmapFactory bitmapFactory = new RoaringBitmapFactory();
    final BitmapSerdeFactory serdeFactory = new RoaringBitmapSerdeFactory(null);
    final List<Integer> ints = generateInts();
    final GenericIndexed<String> dictionary = GenericIndexed.fromIterable(FluentIterable.from(ints).transform(new Function<Integer, String>() {

        @Override
        public String apply(Integer i) {
            return i.toString();
        }
    }), GenericIndexed.STRING_STRATEGY);
    final BitmapIndex bitmapIndex = new BitmapIndexColumnPartSupplier(bitmapFactory, GenericIndexed.fromIterable(FluentIterable.from(ints).transform(new Function<Integer, ImmutableBitmap>() {

        @Override
        public ImmutableBitmap apply(Integer i) {
            final MutableBitmap mutableBitmap = bitmapFactory.makeEmptyMutableBitmap();
            mutableBitmap.add((i - START_INT) / step);
            return bitmapFactory.makeImmutableBitmap(mutableBitmap);
        }
    }), serdeFactory.getObjectStrategy()), dictionary).get();
    selector = new BitmapIndexSelector() {

        @Override
        public Indexed<String> getDimensionValues(String dimension) {
            return dictionary;
        }

        @Override
        public int getNumRows() {
            throw new UnsupportedOperationException();
        }

        @Override
        public BitmapFactory getBitmapFactory() {
            return bitmapFactory;
        }

        @Override
        public ImmutableBitmap getBitmapIndex(String dimension, String value) {
            return bitmapIndex.getBitmap(bitmapIndex.getIndex(value));
        }

        @Override
        public BitmapIndex getBitmapIndex(String dimension) {
            return bitmapIndex;
        }

        @Override
        public ImmutableRTree getSpatialIndex(String dimension) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : ImmutableBitmap(io.druid.collections.bitmap.ImmutableBitmap) MutableBitmap(io.druid.collections.bitmap.MutableBitmap) BitmapIndex(io.druid.segment.column.BitmapIndex) Function(com.google.common.base.Function) ImmutableRTree(io.druid.collections.spatial.ImmutableRTree) RoaringBitmapSerdeFactory(io.druid.segment.data.RoaringBitmapSerdeFactory) BitmapIndexColumnPartSupplier(io.druid.segment.serde.BitmapIndexColumnPartSupplier) BitmapIndexSelector(io.druid.query.filter.BitmapIndexSelector) RoaringBitmapFactory(io.druid.collections.bitmap.RoaringBitmapFactory) BitmapFactory(io.druid.collections.bitmap.BitmapFactory) RoaringBitmapFactory(io.druid.collections.bitmap.RoaringBitmapFactory) Indexed(io.druid.segment.data.Indexed) GenericIndexed(io.druid.segment.data.GenericIndexed) RoaringBitmapSerdeFactory(io.druid.segment.data.RoaringBitmapSerdeFactory) BitmapSerdeFactory(io.druid.segment.data.BitmapSerdeFactory) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with Function

use of com.google.common.base.Function in project druid by druid-io.

the class JsonConfigurator method configurate.

public <T> T configurate(Properties props, String propertyPrefix, Class<T> clazz) throws ProvisionException {
    verifyClazzIsConfigurable(jsonMapper, clazz);
    // Make it end with a period so we only include properties with sub-object thingies.
    final String propertyBase = propertyPrefix.endsWith(".") ? propertyPrefix : propertyPrefix + ".";
    Map<String, Object> jsonMap = Maps.newHashMap();
    for (String prop : props.stringPropertyNames()) {
        if (prop.startsWith(propertyBase)) {
            final String propValue = props.getProperty(prop);
            Object value;
            try {
                // If it's a String Jackson wants it to be quoted, so check if it's not an object or array and quote.
                String modifiedPropValue = propValue;
                if (!(modifiedPropValue.startsWith("[") || modifiedPropValue.startsWith("{"))) {
                    modifiedPropValue = jsonMapper.writeValueAsString(propValue);
                }
                value = jsonMapper.readValue(modifiedPropValue, Object.class);
            } catch (IOException e) {
                log.info(e, "Unable to parse [%s]=[%s] as a json object, using as is.", prop, propValue);
                value = propValue;
            }
            jsonMap.put(prop.substring(propertyBase.length()), value);
        }
    }
    final T config;
    try {
        config = jsonMapper.convertValue(jsonMap, clazz);
    } catch (IllegalArgumentException e) {
        throw new ProvisionException(String.format("Problem parsing object at prefix[%s]: %s.", propertyPrefix, e.getMessage()), e);
    }
    final Set<ConstraintViolation<T>> violations = validator.validate(config);
    if (!violations.isEmpty()) {
        List<String> messages = Lists.newArrayList();
        for (ConstraintViolation<T> violation : violations) {
            String path = "";
            try {
                Class<?> beanClazz = violation.getRootBeanClass();
                final Iterator<Path.Node> iter = violation.getPropertyPath().iterator();
                while (iter.hasNext()) {
                    Path.Node next = iter.next();
                    if (next.getKind() == ElementKind.PROPERTY) {
                        final String fieldName = next.getName();
                        final Field theField = beanClazz.getDeclaredField(fieldName);
                        if (theField.getAnnotation(JacksonInject.class) != null) {
                            path = String.format(" -- Injected field[%s] not bound!?", fieldName);
                            break;
                        }
                        JsonProperty annotation = theField.getAnnotation(JsonProperty.class);
                        final boolean noAnnotationValue = annotation == null || Strings.isNullOrEmpty(annotation.value());
                        final String pathPart = noAnnotationValue ? fieldName : annotation.value();
                        if (path.isEmpty()) {
                            path += pathPart;
                        } else {
                            path += "." + pathPart;
                        }
                    }
                }
            } catch (NoSuchFieldException e) {
                throw Throwables.propagate(e);
            }
            messages.add(String.format("%s - %s", path, violation.getMessage()));
        }
        throw new ProvisionException(Iterables.transform(messages, new Function<String, Message>() {

            @Override
            public Message apply(String input) {
                return new Message(String.format("%s%s", propertyBase, input));
            }
        }));
    }
    log.info("Loaded class[%s] from props[%s] as [%s]", clazz, propertyBase, config);
    return config;
}
Also used : Path(javax.validation.Path) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Message(com.google.inject.spi.Message) IOException(java.io.IOException) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField) Field(java.lang.reflect.Field) Function(com.google.common.base.Function) ProvisionException(com.google.inject.ProvisionException) JacksonInject(com.fasterxml.jackson.annotation.JacksonInject) ConstraintViolation(javax.validation.ConstraintViolation)

Example 3 with Function

use of com.google.common.base.Function in project druid by druid-io.

the class ResponseCookieHandler method handleResponse.

@Override
public ClientResponse<Intermediate> handleResponse(HttpResponse httpResponse) {
    try {
        final HttpHeaders headers = httpResponse.headers();
        manager.put(uri, Maps.asMap(headers.names(), new Function<String, List<String>>() {

            @Override
            public List<String> apply(String input) {
                return headers.getAll(input);
            }
        }));
    } catch (IOException e) {
        log.error(e, "Error while processing Cookies from header");
    } finally {
        return delegate.handleResponse(httpResponse);
    }
}
Also used : HttpHeaders(org.jboss.netty.handler.codec.http.HttpHeaders) Function(com.google.common.base.Function) IOException(java.io.IOException)

Example 4 with Function

use of com.google.common.base.Function in project sharding-jdbc by dangdangdotcom.

the class ShardingJdbcDataSourceBeanDefinitionParser method parseTableRuleConfig.

private BeanDefinition parseTableRuleConfig(final Element tableElement) {
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(TableRuleConfig.class);
    String dynamic = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DYNAMIC_TABLE_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(dynamic)) {
        factory.addPropertyValue("dynamic", dynamic);
    }
    String actualTables = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.ACTUAL_TABLES_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(actualTables)) {
        factory.addPropertyValue("actualTables", actualTables);
    }
    String dataSourceNames = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATA_SOURCE_NAMES_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(dataSourceNames)) {
        factory.addPropertyValue("dataSourceNames", dataSourceNames);
    }
    String databaseStrategy = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATABASE_STRATEGY_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(databaseStrategy)) {
        factory.addPropertyReference("databaseStrategy", databaseStrategy);
    }
    String tableStrategy = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.TABLE_STRATEGY_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(tableStrategy)) {
        factory.addPropertyReference("tableStrategy", tableStrategy);
    }
    List<Element> autoIncrementColumns = DomUtils.getChildElementsByTagName(tableElement, ShardingJdbcDataSourceBeanDefinitionParserTag.AUTO_INCREMENT_COLUMN);
    if (null == autoIncrementColumns || autoIncrementColumns.isEmpty()) {
        return factory.getBeanDefinition();
    }
    factory.addPropertyValue("autoIncrementColumns", Lists.transform(autoIncrementColumns, new Function<Element, AutoIncrementColumnConfig>() {

        @Override
        public AutoIncrementColumnConfig apply(final Element input) {
            AutoIncrementColumnConfig result = new AutoIncrementColumnConfig();
            result.setColumnName(input.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_NAME));
            result.setColumnIdGeneratorClass(input.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_ID_GENERATOR_CLASS));
            return result;
        }
    }));
    return factory.getBeanDefinition();
}
Also used : Function(com.google.common.base.Function) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AutoIncrementColumnConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.AutoIncrementColumnConfig) Element(org.w3c.dom.Element)

Example 5 with Function

use of com.google.common.base.Function in project buck by facebook.

the class ProjectGeneratorTest method getBuildRuleResolverNodeFunction.

private Function<TargetNode<?, ?>, BuildRuleResolver> getBuildRuleResolverNodeFunction(final TargetGraph targetGraph) {
    BuildRuleResolver resolver = new BuildRuleResolver(targetGraph, new DefaultTargetNodeToBuildRuleTransformer());
    AbstractBottomUpTraversal<TargetNode<?, ?>, RuntimeException> bottomUpTraversal = new AbstractBottomUpTraversal<TargetNode<?, ?>, RuntimeException>(targetGraph) {

        @Override
        @SuppressWarnings("PMD.EmptyCatchBlock")
        public void visit(TargetNode<?, ?> node) {
            try {
                resolver.requireRule(node.getBuildTarget());
            } catch (Exception e) {
            // NOTE(agallagher): A large number of the tests appear to setup their target nodes
            // incorrectly, causing action graph creation to fail with lots of missing expected
            // Apple C/C++ platform flavors.  This is gross, but to support tests that need a
            // complete sub-action graph, just skip over the errors.
            }
        }
    };
    bottomUpTraversal.traverse();
    return input -> resolver;
}
Also used : ProjectGeneratorTestUtils.assertTargetExistsAndReturnTarget(com.facebook.buck.apple.project_generator.ProjectGeneratorTestUtils.assertTargetExistsAndReturnTarget) SourceTreePath(com.facebook.buck.apple.xcode.xcodeproj.SourceTreePath) AppleBundleExtension(com.facebook.buck.apple.AppleBundleExtension) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) AppleLibraryBuilder(com.facebook.buck.apple.AppleLibraryBuilder) InternalFlavor(com.facebook.buck.model.InternalFlavor) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FlavorDomain(com.facebook.buck.model.FlavorDomain) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) ReactNativeBuckConfig(com.facebook.buck.js.ReactNativeBuckConfig) Path(java.nio.file.Path) CxxDescriptionEnhancer(com.facebook.buck.cxx.CxxDescriptionEnhancer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) NSDictionary(com.dd.plist.NSDictionary) FileAttribute(java.nio.file.attribute.FileAttribute) BuildTarget(com.facebook.buck.model.BuildTarget) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) SettableFakeClock(com.facebook.buck.timing.SettableFakeClock) Assert.assertFalse(org.junit.Assert.assertFalse) StringWithMacrosUtils(com.facebook.buck.rules.macros.StringWithMacrosUtils) ByteStreams(com.google.common.io.ByteStreams) CxxLibraryBuilder(com.facebook.buck.cxx.CxxLibraryBuilder) CxxBuckConfig(com.facebook.buck.cxx.CxxBuckConfig) BuckEventBus(com.facebook.buck.event.BuckEventBus) PBXSourcesBuildPhase(com.facebook.buck.apple.xcode.xcodeproj.PBXSourcesBuildPhase) Iterables(com.google.common.collect.Iterables) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) PBXHeadersBuildPhase(com.facebook.buck.apple.xcode.xcodeproj.PBXHeadersBuildPhase) SourcePath(com.facebook.buck.rules.SourcePath) Either(com.facebook.buck.model.Either) XCBuildConfiguration(com.facebook.buck.apple.xcode.xcodeproj.XCBuildConfiguration) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) HalideLibraryDescription(com.facebook.buck.halide.HalideLibraryDescription) BuildTargetFactory(com.facebook.buck.model.BuildTargetFactory) AppleBinaryBuilder(com.facebook.buck.apple.AppleBinaryBuilder) PBXBuildPhase(com.facebook.buck.apple.xcode.xcodeproj.PBXBuildPhase) StringWithMacros(com.facebook.buck.rules.macros.StringWithMacros) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Before(org.junit.Before) AppleAssetCatalogBuilder(com.facebook.buck.apple.AppleAssetCatalogBuilder) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Assert.assertTrue(org.junit.Assert.assertTrue) ProductType(com.facebook.buck.apple.xcode.xcodeproj.ProductType) AppleResourceBuilder(com.facebook.buck.apple.AppleResourceBuilder) Test(org.junit.Test) IOException(java.io.IOException) PBXResourcesBuildPhase(com.facebook.buck.apple.xcode.xcodeproj.PBXResourcesBuildPhase) SceneKitAssetsBuilder(com.facebook.buck.apple.SceneKitAssetsBuilder) CxxSource(com.facebook.buck.cxx.CxxSource) FakeBuckConfig(com.facebook.buck.cli.FakeBuckConfig) HalideLibraryBuilder(com.facebook.buck.halide.HalideLibraryBuilder) Paths(java.nio.file.Paths) AppleLibraryDescription(com.facebook.buck.apple.AppleLibraryDescription) PBXShellScriptBuildPhase(com.facebook.buck.apple.xcode.xcodeproj.PBXShellScriptBuildPhase) Assert.assertEquals(org.junit.Assert.assertEquals) IosReactNativeLibraryBuilder(com.facebook.buck.js.IosReactNativeLibraryBuilder) CoreMatchers.is(org.hamcrest.CoreMatchers.is) AppleDependenciesCache(com.facebook.buck.apple.AppleDependenciesCache) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PatternMatchedCollection(com.facebook.buck.rules.coercer.PatternMatchedCollection) XcodePostbuildScriptBuilder(com.facebook.buck.apple.XcodePostbuildScriptBuilder) Matchers.hasKey(org.hamcrest.Matchers.hasKey) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) PBXBuildFile(com.facebook.buck.apple.xcode.xcodeproj.PBXBuildFile) Assert.assertThat(org.junit.Assert.assertThat) AppleConfig(com.facebook.buck.apple.AppleConfig) BuckConfig(com.facebook.buck.cli.BuckConfig) CxxPlatformUtils(com.facebook.buck.cxx.CxxPlatformUtils) PBXFileReference(com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Cell(com.facebook.buck.rules.Cell) SwiftBuckConfig(com.facebook.buck.swift.SwiftBuckConfig) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) AppleBundleBuilder(com.facebook.buck.apple.AppleBundleBuilder) ImmutableMap(com.google.common.collect.ImmutableMap) TargetGraph(com.facebook.buck.rules.TargetGraph) Collection(java.util.Collection) Platform(com.facebook.buck.util.environment.Platform) PBXGroup(com.facebook.buck.apple.xcode.xcodeproj.PBXGroup) XcodePrebuildScriptBuilder(com.facebook.buck.apple.XcodePrebuildScriptBuilder) List(java.util.List) AlwaysFoundExecutableFinder(com.facebook.buck.io.AlwaysFoundExecutableFinder) SourceWithFlags(com.facebook.buck.rules.SourceWithFlags) HalideBuckConfig(com.facebook.buck.halide.HalideBuckConfig) FakeAppleRuleDescriptions(com.facebook.buck.apple.FakeAppleRuleDescriptions) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) PathSourcePath(com.facebook.buck.rules.PathSourcePath) ExportFileDescription(com.facebook.buck.shell.ExportFileDescription) Optional(java.util.Optional) Assume.assumeTrue(org.junit.Assume.assumeTrue) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) Pattern(java.util.regex.Pattern) CoreMatchers.not(org.hamcrest.CoreMatchers.not) FrameworkPath(com.facebook.buck.rules.coercer.FrameworkPath) AbstractBottomUpTraversal(com.facebook.buck.graph.AbstractBottomUpTraversal) PBXVariantGroup(com.facebook.buck.apple.xcode.xcodeproj.PBXVariantGroup) BuckEventBusFactory(com.facebook.buck.event.BuckEventBusFactory) FakeSourcePath(com.facebook.buck.rules.FakeSourcePath) ImmutableList(com.google.common.collect.ImmutableList) AllExistingProjectFilesystem(com.facebook.buck.testutil.AllExistingProjectFilesystem) NSString(com.dd.plist.NSString) PBXTarget(com.facebook.buck.apple.xcode.xcodeproj.PBXTarget) ExpectedException(org.junit.rules.ExpectedException) MoreCollectors(com.facebook.buck.util.MoreCollectors) Assert.assertNotNull(org.junit.Assert.assertNotNull) TargetNode(com.facebook.buck.rules.TargetNode) ExportFileBuilder(com.facebook.buck.shell.ExportFileBuilder) Matchers(org.hamcrest.Matchers) PBXProject(com.facebook.buck.apple.xcode.xcodeproj.PBXProject) PBXReference(com.facebook.buck.apple.xcode.xcodeproj.PBXReference) CxxPlatform(com.facebook.buck.cxx.CxxPlatform) HumanReadableException(com.facebook.buck.util.HumanReadableException) DefaultBuildTargetSourcePath(com.facebook.buck.rules.DefaultBuildTargetSourcePath) TimeUnit(java.util.concurrent.TimeUnit) PBXCopyFilesBuildPhase(com.facebook.buck.apple.xcode.xcodeproj.PBXCopyFilesBuildPhase) Rule(org.junit.Rule) Ordering(com.google.common.collect.Ordering) TargetGraphFactory(com.facebook.buck.testutil.TargetGraphFactory) AppleTestBuilder(com.facebook.buck.apple.AppleTestBuilder) TestCellBuilder(com.facebook.buck.rules.TestCellBuilder) CoreDataModelBuilder(com.facebook.buck.apple.CoreDataModelBuilder) CopyFilePhaseDestinationSpec(com.facebook.buck.apple.xcode.xcodeproj.CopyFilePhaseDestinationSpec) Flavor(com.facebook.buck.model.Flavor) HeaderMap(com.facebook.buck.apple.clang.HeaderMap) InputStream(java.io.InputStream) TargetNode(com.facebook.buck.rules.TargetNode) AbstractBottomUpTraversal(com.facebook.buck.graph.AbstractBottomUpTraversal) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) NoSuchBuildTargetException(com.facebook.buck.parser.NoSuchBuildTargetException) IOException(java.io.IOException) ExpectedException(org.junit.rules.ExpectedException) HumanReadableException(com.facebook.buck.util.HumanReadableException)

Aggregations

Function (com.google.common.base.Function)616 List (java.util.List)138 ArrayList (java.util.ArrayList)114 Nullable (javax.annotation.Nullable)103 Map (java.util.Map)97 IOException (java.io.IOException)89 HashMap (java.util.HashMap)78 Test (org.junit.Test)73 ImmutableList (com.google.common.collect.ImmutableList)49 File (java.io.File)46 Set (java.util.Set)46 Collection (java.util.Collection)35 ImmutableMap (com.google.common.collect.ImmutableMap)34 DateTime (org.joda.time.DateTime)33 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)29 HashSet (java.util.HashSet)27 Iterator (java.util.Iterator)27 LinkedList (java.util.LinkedList)26 ExecutionException (java.util.concurrent.ExecutionException)24 Collectors (java.util.stream.Collectors)15