Search in sources :

Example 96 with Function

use of com.google.common.base.Function in project GeoGig by boundlessgeo.

the class OSMExportPG method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) {
    Preconditions.checkNotNull(mappingFile != null, "A data mapping file must be specified");
    final Mapping mapping = Mapping.fromFile(mappingFile);
    List<MappingRule> rules = mapping.getRules();
    checkParameter(!rules.isEmpty(), "No rules are defined in the specified mapping");
    for (final MappingRule rule : mapping.getRules()) {
        Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

            @Override
            @Nullable
            public Optional<Feature> apply(@Nullable Feature feature) {
                Optional<Feature> mapped = rule.apply(feature);
                if (mapped.isPresent()) {
                    return Optional.of(mapped.get());
                }
                return Optional.absent();
            }
        };
        SimpleFeatureType outputFeatureType = rule.getFeatureType();
        String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
        DataStore dataStore = null;
        try {
            dataStore = getDataStore();
            String tableName = outputFeatureType.getName().getLocalPart();
            if (Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
                if (!overwrite) {
                    throw new CommandFailedException("A table named '" + tableName + "'already exists. Use -o to overwrite");
                }
            } else {
                try {
                    dataStore.createSchema(outputFeatureType);
                } catch (IOException e) {
                    throw new CommandFailedException("Cannot create new table in database", e);
                }
            }
            final SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
            if (!(featureSource instanceof SimpleFeatureStore)) {
                throw new CommandFailedException("Could not create feature store. Data source is read only.");
            }
            final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
            if (overwrite) {
                featureStore.removeFeatures(Filter.INCLUDE);
            }
            ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFeatureTypeConversionFunction(function);
            try {
                op.setProgressListener(cli.getProgressListener()).call();
                cli.getConsole().println("OSM data exported successfully to " + tableName);
            } catch (IllegalArgumentException iae) {
                throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
            } catch (GeoToolsOpException e) {
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
            }
        } catch (IOException e) {
            throw new IllegalStateException("Cannot connect to database: " + e.getMessage(), e);
        } finally {
            if (dataStore != null) {
                dataStore.dispose();
            }
        }
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Mapping(org.locationtech.geogig.osm.internal.Mapping) IOException(java.io.IOException) MappingRule(org.locationtech.geogig.osm.internal.MappingRule) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) Function(com.google.common.base.Function) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) Nullable(javax.annotation.Nullable)

Example 97 with Function

use of com.google.common.base.Function in project GeoGig by boundlessgeo.

the class OSMExportSL method exportRule.

private void exportRule(final MappingRule rule, final GeogigCLI cli) throws IOException {
    Function<Feature, Optional<Feature>> function = new Function<Feature, Optional<Feature>>() {

        @Override
        public Optional<Feature> apply(Feature feature) {
            Optional<Feature> mapped = rule.apply(feature);
            return mapped;
        }
    };
    SimpleFeatureType outputFeatureType = rule.getFeatureType();
    String path = getOriginTreesFromOutputFeatureType(outputFeatureType);
    DataStore dataStore = getDataStore();
    try {
        final String tableName = ensureTableExists(outputFeatureType, dataStore);
        final SimpleFeatureSource source = dataStore.getFeatureSource(tableName);
        if (!(source instanceof SimpleFeatureStore)) {
            throw new CommandFailedException("Could not create feature store.");
        }
        final SimpleFeatureStore store = (SimpleFeatureStore) source;
        if (overwrite) {
            try {
                store.removeFeatures(Filter.INCLUDE);
            } catch (IOException e) {
                throw new CommandFailedException("Error truncating table " + tableName, e);
            }
        }
        ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(store).setPath(path).setFeatureTypeConversionFunction(function);
        try {
            op.setProgressListener(cli.getProgressListener()).call();
            cli.getConsole().println("OSM data exported successfully to " + tableName);
        } catch (IllegalArgumentException iae) {
            throw new InvalidParameterException(iae.getMessage(), iae);
        } catch (GeoToolsOpException e) {
            throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    } finally {
        dataStore.dispose();
    }
}
Also used : Optional(com.google.common.base.Optional) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) Function(com.google.common.base.Function) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp)

Example 98 with Function

use of com.google.common.base.Function in project drill by apache.

the class LocalPersistentStore method getRange.

@Override
public Iterator<Map.Entry<String, V>> getRange(int skip, int take) {
    try (AutoCloseableLock lock = readLock.open()) {
        try {
            List<FileStatus> f = fs.list(false, basePath);
            if (f == null || f.isEmpty()) {
                return Collections.emptyIterator();
            }
            List<String> files = Lists.newArrayList();
            for (FileStatus stat : f) {
                String s = stat.getPath().getName();
                if (s.endsWith(DRILL_SYS_FILE_SUFFIX)) {
                    files.add(s.substring(0, s.length() - DRILL_SYS_FILE_SUFFIX.length()));
                }
            }
            Collections.sort(files);
            return Iterables.transform(Iterables.limit(Iterables.skip(files, skip), take), new Function<String, Entry<String, V>>() {

                @Nullable
                @Override
                public Entry<String, V> apply(String key) {
                    return new ImmutableEntry<>(key, get(key));
                }
            }).iterator();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : Function(com.google.common.base.Function) FileStatus(org.apache.hadoop.fs.FileStatus) ImmutableEntry(org.apache.drill.common.collections.ImmutableEntry) AutoCloseableLock(org.apache.drill.common.concurrent.AutoCloseableLock) IOException(java.io.IOException)

Example 99 with Function

use of com.google.common.base.Function in project GeoGig by boundlessgeo.

the class Log method run.

/**
     * Runs the command and builds the appropriate response
     * 
     * @param context - the context to use for this command
     * 
     * @throws IllegalArgumentException
     */
@Override
public void run(final CommandContext context) {
    final Context geogig = this.getCommandLocator(context);
    LogOp op = geogig.command(LogOp.class).setFirstParentOnly(firstParentOnly);
    if (skip != null) {
        op.setSkip(skip.intValue());
    }
    if (limit != null) {
        op.setLimit(limit.intValue());
    }
    if (this.sinceTime != null || this.untilTime != null) {
        Date since = new Date(0);
        Date until = new Date();
        if (this.sinceTime != null) {
            since = new Date(geogig.command(ParseTimestamp.class).setString(this.sinceTime).call());
        }
        if (this.untilTime != null) {
            until = new Date(geogig.command(ParseTimestamp.class).setString(this.untilTime).call());
        }
        op.setTimeRange(new Range<Date>(Date.class, since, until));
    }
    if (this.since != null) {
        Optional<ObjectId> since;
        since = geogig.command(RevParse.class).setRefSpec(this.since).call();
        Preconditions.checkArgument(since.isPresent(), "Object not found '%s'", this.since);
        op.setSince(since.get());
    }
    if (this.until != null) {
        Optional<ObjectId> until;
        until = geogig.command(RevParse.class).setRefSpec(this.until).call();
        Preconditions.checkArgument(until.isPresent(), "Object not found '%s'", this.until);
        op.setUntil(until.get());
    }
    if (paths != null && !paths.isEmpty()) {
        for (String path : paths) {
            op.addPath(path);
        }
    }
    final Iterator<RevCommit> log = op.call();
    Iterators.advance(log, page * elementsPerPage);
    if (countChanges) {
        final String pathFilter;
        if (paths != null && !paths.isEmpty()) {
            pathFilter = paths.get(0);
        } else {
            pathFilter = null;
        }
        Function<RevCommit, CommitWithChangeCounts> changeCountFunctor = new Function<RevCommit, CommitWithChangeCounts>() {

            @Override
            public CommitWithChangeCounts apply(RevCommit input) {
                ObjectId parent = ObjectId.NULL;
                if (input.getParentIds().size() > 0) {
                    parent = input.getParentIds().get(0);
                }
                int added = 0;
                int modified = 0;
                int removed = 0;
                // If it's a shallow clone, the commit may not exist
                if (parent.equals(ObjectId.NULL) || geogig.stagingDatabase().exists(parent)) {
                    final Iterator<DiffEntry> diff = geogig.command(DiffOp.class).setOldVersion(parent).setNewVersion(input.getId()).setFilter(pathFilter).call();
                    while (diff.hasNext()) {
                        DiffEntry entry = diff.next();
                        if (entry.changeType() == DiffEntry.ChangeType.ADDED) {
                            added++;
                        } else if (entry.changeType() == DiffEntry.ChangeType.MODIFIED) {
                            modified++;
                        } else {
                            removed++;
                        }
                    }
                }
                return new CommitWithChangeCounts(input, added, modified, removed);
            }
        };
        final Iterator<CommitWithChangeCounts> summarizedLog = Iterators.transform(log, changeCountFunctor);
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeCommitsWithChangeCounts(summarizedLog, elementsPerPage);
                out.finish();
            }
        });
    } else if (summary) {
        if (paths != null && paths.size() > 0) {
            context.setResponseContent(new StreamResponse() {

                @Override
                public void write(Writer out) throws Exception {
                    writeCSV(context.getGeoGIG(), out, log);
                }
            });
        } else {
            throw new CommandSpecException("You must specify a feature type path when getting a summary.");
        }
    } else {
        final boolean rangeLog = returnRange;
        context.setResponseContent(new CommandResponse() {

            @Override
            public void write(ResponseWriter out) throws Exception {
                out.start();
                out.writeCommits(log, elementsPerPage, rangeLog);
                out.finish();
            }
        });
    }
}
Also used : Context(org.locationtech.geogig.api.Context) CommandContext(org.locationtech.geogig.web.api.CommandContext) ObjectId(org.locationtech.geogig.api.ObjectId) LogOp(org.locationtech.geogig.api.porcelain.LogOp) StreamResponse(org.locationtech.geogig.web.api.StreamResponse) CommandResponse(org.locationtech.geogig.web.api.CommandResponse) Date(java.util.Date) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) Function(com.google.common.base.Function) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) RevParse(org.locationtech.geogig.api.plumbing.RevParse) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) ResponseWriter(org.locationtech.geogig.web.api.ResponseWriter) Writer(java.io.Writer) RevCommit(org.locationtech.geogig.api.RevCommit) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry)

Example 100 with Function

use of com.google.common.base.Function in project metron by apache.

the class SolrIndexingIntegrationTest method getSearchComponent.

@Override
public InMemoryComponent getSearchComponent(final Properties topologyProperties) throws Exception {
    SolrComponent solrComponent = new SolrComponent.Builder().addCollection(collection, "../metron-solr/src/test/resources/solr/conf").withPostStartCallback(new Function<SolrComponent, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable SolrComponent solrComponent) {
            topologyProperties.setProperty("solr.zk", solrComponent.getZookeeperUrl());
            try {
                String testZookeeperUrl = topologyProperties.getProperty(ZKServerComponent.ZOOKEEPER_PROPERTY);
                Configurations configurations = SampleUtil.getSampleConfigs();
                Map<String, Object> globalConfig = configurations.getGlobalConfig();
                globalConfig.put("solr.zookeeper", solrComponent.getZookeeperUrl());
                ConfigurationsUtils.writeGlobalConfigToZookeeper(JSONUtils.INSTANCE.toJSONPretty(globalConfig), testZookeeperUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }).build();
    return solrComponent;
}
Also used : Function(com.google.common.base.Function) SolrComponent(org.apache.metron.solr.integration.components.SolrComponent) Configurations(org.apache.metron.common.configuration.Configurations) Nullable(javax.annotation.Nullable) IOException(java.io.IOException)

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