use of org.locationtech.geowave.service.grpc.protobuf.FeatureAttributeProtos in project geowave by locationtech.
the class GeoWaveGrpcVectorService method vectorIngest.
@Override
public StreamObserver<VectorIngestParametersProtos> vectorIngest(final StreamObserver<StringResponseProtos> responseObserver) {
return new StreamObserver<VectorIngestParametersProtos>() {
private boolean firstFeature = true;
private String storeName = null;
private DataStore dataStore = null;
private String typeName = null;
private String indexName = null;
private Writer<SimpleFeature> writer = null;
private DataTypeAdapter adapter = null;
private Index pIndex = null;
private SimpleFeatureTypeBuilder typeBuilder = null;
private SimpleFeatureBuilder featureBuilder = null;
private static final int batchSize = 100;
private int batchCount = 0;
private int totalCount = 0;
@Override
public void onNext(final VectorIngestParametersProtos f) {
if (firstFeature) {
firstFeature = false;
// parse top level required parameters
storeName = f.getBaseParams().getStoreName();
final StoreLoader storeLoader = new StoreLoader(storeName);
typeName = f.getBaseParams().getTypeName();
indexName = f.getBaseParams().getIndexName();
// This only needs to happen once
if (typeBuilder == null) {
typeBuilder = new SimpleFeatureTypeBuilder();
for (final Map.Entry<String, FeatureAttributeProtos> mapEntry : f.getFeatureMap().entrySet()) {
switch(mapEntry.getValue().getValueCase()) {
case VALSTRING:
{
typeBuilder.add(mapEntry.getKey(), String.class);
break;
}
case VALINT32:
{
typeBuilder.add(mapEntry.getKey(), Integer.class);
break;
}
case VALINT64:
{
typeBuilder.add(mapEntry.getKey(), Long.class);
break;
}
case VALFLOAT:
{
typeBuilder.add(mapEntry.getKey(), Float.class);
break;
}
case VALDOUBLE:
{
typeBuilder.add(mapEntry.getKey(), Double.class);
break;
}
case VALDATE:
{
typeBuilder.add(mapEntry.getKey(), Date.class);
break;
}
case VALGEOMETRY:
{
typeBuilder.add(mapEntry.getKey(), Geometry.class);
break;
}
default:
break;
}
;
}
}
// This a factory class that builds simple feature objects
// based
// on the
// type
typeBuilder.setName(typeName);
final SimpleFeatureType featureType = typeBuilder.buildFeatureType();
featureBuilder = new SimpleFeatureBuilder(featureType);
// get a handle to the relevant stores
if (!storeLoader.loadFromConfig(GeoWaveGrpcServiceOptions.geowaveConfigFile)) {
throw new ParameterException("Cannot find store name: " + storeLoader.getStoreName());
}
dataStore = storeLoader.createDataStore();
final PersistentAdapterStore adapterStore = storeLoader.createAdapterStore();
final InternalAdapterStore internalAdapterStore = storeLoader.createInternalAdapterStore();
final Short internalAdapterId = internalAdapterStore.getAdapterId(typeName);
if (internalAdapterId != null) {
adapter = adapterStore.getAdapter(internalAdapterId);
} else {
adapter = null;
}
if (adapter == null) {
adapter = new FeatureDataAdapter(featureType);
}
// Load the Indexes
final List<Index> indices = DataStoreUtils.loadIndices(storeLoader.createIndexStore(), indexName);
// assuming one index for now
// (PrimaryIndex)
pIndex = indices.get(0);
// indexStore.getIndex(indexId);
if (pIndex == null) {
throw new ParameterException("Failed to instantiate primary index");
}
// create index writer to actually write data
dataStore.addType(adapter, pIndex);
writer = dataStore.createWriter(adapter.getTypeName());
}
// Set the values for all the attributes in the feature
for (final Map.Entry<String, FeatureAttributeProtos> attribute : f.getFeatureMap().entrySet()) {
switch(attribute.getValue().getValueCase()) {
case VALSTRING:
{
featureBuilder.set(attribute.getKey(), attribute.getValue().getValString());
break;
}
case VALINT32:
{
featureBuilder.set(attribute.getKey(), attribute.getValue().getValInt32());
break;
}
case VALINT64:
{
featureBuilder.set(attribute.getKey(), attribute.getValue().getValInt64());
break;
}
case VALFLOAT:
{
featureBuilder.set(attribute.getKey(), attribute.getValue().getValFloat());
break;
}
case VALDOUBLE:
{
featureBuilder.set(attribute.getKey(), attribute.getValue().getValDouble());
break;
}
case VALDATE:
{
featureBuilder.set(attribute.getKey(), new Date(Timestamps.toMillis(attribute.getValue().getValDate())));
break;
}
case VALGEOMETRY:
{
Geometry geom = null;
try {
geom = new WKBReader(JTSFactoryFinder.getGeometryFactory()).read(attribute.getValue().getValGeometry().toByteArray());
} catch (FactoryRegistryException | org.locationtech.jts.io.ParseException e) {
LOGGER.error("Failed to parse string for geometry", e);
}
if (geom != null) {
featureBuilder.set(attribute.getKey(), geom);
}
break;
}
default:
break;
}
;
}
final SimpleFeature sf = featureBuilder.buildFeature(String.valueOf(totalCount));
writer.write(sf);
// onComplete and onError
if (++batchCount >= batchSize) {
// writer.flush();
batchCount = 0;
}
final StringResponseProtos resp = StringResponseProtos.newBuilder().setResponseValue(String.valueOf(++totalCount)).build();
responseObserver.onNext(resp);
}
@Override
public void onError(final Throwable t) {
LOGGER.error("Exception encountered during vectorIngest", t);
writer.flush();
writer.close();
final StringResponseProtos resp = StringResponseProtos.newBuilder().setResponseValue("Error during ingest: ").build();
responseObserver.onNext(resp);
responseObserver.onCompleted();
}
@Override
public void onCompleted() {
writer.flush();
writer.close();
final StringResponseProtos resp = StringResponseProtos.newBuilder().setResponseValue("Ingest completed successfully").build();
responseObserver.onNext(resp);
responseObserver.onCompleted();
}
};
}
Aggregations