use of com.google.common.base.Function in project google-cloud-java by GoogleCloudPlatform.
the class HttpBigQueryRpc method listTables.
@Override
public Tuple<String, Iterable<Table>> listTables(String projectId, String datasetId, Map<Option, ?> options) {
try {
TableList tableList = bigquery.tables().list(projectId, datasetId).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).execute();
Iterable<TableList.Tables> tables = tableList.getTables();
return Tuple.of(tableList.getNextPageToken(), Iterables.transform(tables != null ? tables : ImmutableList.<TableList.Tables>of(), new Function<TableList.Tables, Table>() {
@Override
public Table apply(TableList.Tables tablePb) {
return new Table().setFriendlyName(tablePb.getFriendlyName()).setId(tablePb.getId()).setKind(tablePb.getKind()).setTableReference(tablePb.getTableReference()).setType(tablePb.getType());
}
}));
} catch (IOException ex) {
throw translate(ex);
}
}
use of com.google.common.base.Function in project GeoGig by boundlessgeo.
the class DeepMove method moveObjects.
private void moveObjects(final ObjectDatabase from, final ObjectDatabase to, final Supplier<Iterator<Node>> nodesToMove, final Set<ObjectId> metadataIds) {
Iterable<ObjectId> ids = new Iterable<ObjectId>() {
final Function<Node, ObjectId> asId = new Function<Node, ObjectId>() {
@Override
public ObjectId apply(Node input) {
Optional<ObjectId> metadataId = input.getMetadataId();
if (metadataId.isPresent()) {
metadataIds.add(input.getMetadataId().get());
}
ObjectId id = input.getObjectId();
return id;
}
};
@Override
public Iterator<ObjectId> iterator() {
Iterator<Node> iterator = nodesToMove.get();
Iterator<ObjectId> ids = Iterators.transform(iterator, asId);
return ids;
}
};
final ExecutorService deletingService = Executors.newSingleThreadExecutor();
try {
final DeletingListener deletingListener = new DeletingListener(deletingService, from);
// store objects into the target db and remove them from the origin db in one shot
to.putAll(from.getAll(ids), deletingListener);
// in case there are some deletes pending cause the iterator finished and the listener
// didn't fill its buffer
deletingListener.deleteInserted();
} finally {
deletingService.shutdown();
while (!deletingService.isTerminated()) {
try {
deletingService.awaitTermination(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// ok, still awaiting for delete tasks to finish
}
}
}
}
use of com.google.common.base.Function in project GeoGig by boundlessgeo.
the class OSMMapOp method getFeatures.
private Iterator<Feature> getFeatures(String ref) {
Optional<ObjectId> id = command(RevParse.class).setRefSpec(ref).call();
if (!id.isPresent()) {
return Iterators.emptyIterator();
}
LsTreeOp op = command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
Iterator<NodeRef> iterator = op.call();
Function<NodeRef, Feature> nodeRefToFeature = new Function<NodeRef, Feature>() {
private final //
Map<String, FeatureBuilder> builders = //
ImmutableMap.<//
String, //
FeatureBuilder>of(//
OSMUtils.NODE_TYPE_NAME, //
new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.nodeType())), //
OSMUtils.WAY_TYPE_NAME, new FeatureBuilder(RevFeatureTypeImpl.build(OSMUtils.wayType())));
private final RevObjectParse parseCommand = command(RevObjectParse.class);
@Override
@Nullable
public Feature apply(@Nullable NodeRef ref) {
RevFeature revFeature = parseCommand.setObjectId(ref.objectId()).call(RevFeature.class).get();
final String parentPath = ref.getParentPath();
FeatureBuilder featureBuilder = builders.get(parentPath);
String fid = ref.name();
Feature feature = featureBuilder.build(fid, revFeature);
return feature;
}
};
return Iterators.transform(iterator, nodeRefToFeature);
}
use of com.google.common.base.Function in project GeoGig by boundlessgeo.
the class MongoGraphDatabase method getChildren.
@Override
public ImmutableList<ObjectId> getChildren(ObjectId id) {
DBObject query = new BasicDBObject();
query.put("_label", Relationship.PARENT.name());
query.put("_out", id.toString());
DBCursor cursor = collection.find(query);
Function<DBObject, ObjectId> idMapper = new Function<DBObject, ObjectId>() {
@Override
public ObjectId apply(DBObject o) {
return ObjectId.valueOf((String) o.get("_in"));
}
};
return ImmutableList.copyOf(Iterators.transform(cursor.iterator(), idMapper));
}
use of com.google.common.base.Function in project GeoGig by boundlessgeo.
the class OSMExport method getFeatures.
private Iterator<EntityContainer> getFeatures(String ref) {
Optional<ObjectId> id = geogig.command(RevParse.class).setRefSpec(ref).call();
if (!id.isPresent()) {
return Iterators.emptyIterator();
}
LsTreeOp op = geogig.command(LsTreeOp.class).setStrategy(Strategy.DEPTHFIRST_ONLY_FEATURES).setReference(ref);
if (bbox != null) {
final Envelope env;
try {
env = new Envelope(Double.parseDouble(bbox.get(0)), Double.parseDouble(bbox.get(2)), Double.parseDouble(bbox.get(1)), Double.parseDouble(bbox.get(3)));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Wrong bbox definition");
}
Predicate<Bounded> filter = new Predicate<Bounded>() {
@Override
public boolean apply(final Bounded bounded) {
boolean intersects = bounded.intersects(env);
return intersects;
}
};
op.setBoundsFilter(filter);
}
Iterator<NodeRef> iterator = op.call();
final EntityConverter converter = new EntityConverter();
Function<NodeRef, EntityContainer> function = new Function<NodeRef, EntityContainer>() {
@Override
@Nullable
public EntityContainer apply(@Nullable NodeRef ref) {
RevFeature revFeature = geogig.command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
SimpleFeatureType featureType;
if (ref.path().startsWith(OSMUtils.NODE_TYPE_NAME)) {
featureType = OSMUtils.nodeType();
} else {
featureType = OSMUtils.wayType();
}
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(featureType);
RevFeatureType revFeatureType = RevFeatureTypeImpl.build(featureType);
List<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
ImmutableList<Optional<Object>> values = revFeature.getValues();
for (int i = 0; i < descriptors.size(); i++) {
PropertyDescriptor descriptor = descriptors.get(i);
Optional<Object> value = values.get(i);
featureBuilder.set(descriptor.getName(), value.orNull());
}
SimpleFeature feature = featureBuilder.buildFeature(ref.name());
Entity entity = converter.toEntity(feature, null);
EntityContainer container;
if (entity instanceof Node) {
container = new NodeContainer((Node) entity);
} else {
container = new WayContainer((Way) entity);
}
return container;
}
};
return Iterators.transform(iterator, function);
}
Aggregations