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();
}
}
}
}
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();
}
}
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);
}
}
}
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();
}
});
}
}
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;
}
Aggregations