use of org.locationtech.geowave.core.store.api.QueryBuilder in project geowave by locationtech.
the class GeoWaveEnumIndexIT method getResults.
private void getResults(final EnumSearch search) {
final DataStore ds = dataStoreOptions.createDataStore();
final Set<String> expectedResults = new HashSet<>();
final QueryBuilder queryBldr = QueryBuilder.newBuilder().addTypeName(TYPE_NAME);
// query everything and apply manual filtering
final AtomicInteger everythingResults = new AtomicInteger(0);
try (final CloseableIterator<SimpleFeature> it = ds.query((Query) queryBldr.build())) {
it.forEachRemaining(f -> {
final String timezone = f.getAttribute(TIMEZONE_ATTR_NAME).toString();
if (search.getSearchTerm().equals(timezone)) {
expectedResults.add(f.getID());
}
everythingResults.incrementAndGet();
});
}
// ensure that the full set of results exceeds the expected results (ie. that we successfully
// queried everything in the previous step)
Assert.assertTrue(everythingResults.get() > expectedResults.size());
LOGGER.info("Expecting '" + expectedResults.size() + "' in timezone " + search.getSearchTerm());
queryBldr.indexName(TEST_ENUM_INDEX_NAME);
try (final CloseableIterator<SimpleFeature> it = ds.query((Query) queryBldr.constraints(queryBldr.constraintsFactory().customConstraints(search)).build())) {
it.forEachRemaining(f -> {
final String timezone = f.getAttribute(TIMEZONE_ATTR_NAME).toString();
Assert.assertEquals(search.getSearchTerm(), timezone);
expectedResults.remove(f.getID());
});
}
Assert.assertTrue(expectedResults.isEmpty());
}
use of org.locationtech.geowave.core.store.api.QueryBuilder in project geowave by locationtech.
the class BaseDataStore method copyTo.
@Override
public void copyTo(final DataStore other, final Query<?> query) {
// check for 'everything' query
if (query == null) {
copyTo(other);
return;
}
final String[] typeNames = query.getDataTypeQueryOptions().getTypeNames();
final String indexName = query.getIndexQueryOptions().getIndexName();
final boolean isAllIndices = query.getIndexQueryOptions().isAllIndices();
final List<DataTypeAdapter<?>> typesToCopy;
// if typeNames are not specified, then it means 'everything' as well
if (((typeNames == null) || (typeNames.length == 0))) {
if ((query.getQueryConstraints() == null) || (query.getQueryConstraints() instanceof EverythingQuery)) {
copyTo(other);
return;
} else {
typesToCopy = Arrays.asList(getTypes());
}
} else {
// make sure the types requested exist in the source store (this)
// before trying to copy!
final DataTypeAdapter<?>[] sourceTypes = getTypes();
typesToCopy = new ArrayList<>();
for (int i = 0; i < typeNames.length; i++) {
boolean found = false;
for (int k = 0; k < sourceTypes.length; k++) {
if (sourceTypes[k].getTypeName().compareTo(typeNames[i]) == 0) {
found = true;
typesToCopy.add(sourceTypes[k]);
break;
}
}
if (!found) {
throw new IllegalArgumentException("Some type names specified in the query do not exist in the source database and thus cannot be copied.");
}
}
}
// if there is an index requested in the query, make sure it exists in
// the source store before trying to copy as well!
final Index[] sourceIndices = getIndices();
Index indexToCopy = null;
if (!isAllIndices) {
// just add the one index specified by the query
// first make sure source index exists though
boolean found = false;
for (int i = 0; i < sourceIndices.length; i++) {
if (sourceIndices[i].getName().compareTo(indexName) == 0) {
found = true;
indexToCopy = sourceIndices[i];
break;
}
}
if (!found) {
throw new IllegalArgumentException("The index specified in the query does not exist in the source database and thus cannot be copied.");
}
// also make sure the types/index mapping for the query are legit
for (int i = 0; i < typeNames.length; i++) {
final short adapterId = internalAdapterStore.getAdapterId(typeNames[i]);
final AdapterToIndexMapping[] indexMappings = indexMappingStore.getIndicesForAdapter(adapterId);
found = false;
for (int k = 0; k < indexMappings.length; k++) {
if (indexMappings[k].getIndexName().compareTo(indexName) == 0) {
found = true;
break;
}
}
if (!found) {
throw new IllegalArgumentException("The index " + indexName + " and the type " + typeNames[i] + " specified by the query are not associated in the source database");
}
}
}
// add all the types that the destination store doesn't have yet
final DataTypeAdapter<?>[] destTypes = other.getTypes();
for (int i = 0; i < typesToCopy.size(); i++) {
boolean found = false;
for (int k = 0; k < destTypes.length; k++) {
if (destTypes[k].getTypeName().compareTo(typesToCopy.get(i).getTypeName()) == 0) {
found = true;
break;
}
}
if (!found) {
other.addType(typesToCopy.get(i));
}
}
// add all the indices that the destination store doesn't have yet
if (isAllIndices) {
// in this case, all indices from the types requested by the query
for (int i = 0; i < typesToCopy.size(); i++) {
final String typeName = typesToCopy.get(i).getTypeName();
final short adapterId = internalAdapterStore.getAdapterId(typeName);
final AdapterToIndexMapping[] indexMappings = indexMappingStore.getIndicesForAdapter(adapterId);
final Index[] indices = Arrays.stream(indexMappings).map(mapping -> mapping.getIndex(indexStore)).toArray(Index[]::new);
other.addIndex(typeName, indices);
final QueryBuilder<?, ?> qb = QueryBuilder.newBuilder().addTypeName(typeName).constraints(query.getQueryConstraints());
try (CloseableIterator<?> it = query(qb.build())) {
try (Writer<Object> writer = other.createWriter(typeName)) {
while (it.hasNext()) {
writer.write(it.next());
}
}
}
}
} else {
// query
for (int i = 0; i < typesToCopy.size(); i++) {
other.addIndex(typesToCopy.get(i).getTypeName(), indexToCopy);
}
// we can write appropriately
for (int k = 0; k < typesToCopy.size(); k++) {
final InternalDataAdapter<?> adapter = adapterStore.getAdapter(internalAdapterStore.getAdapterId(typesToCopy.get(k).getTypeName()));
final QueryBuilder<?, ?> qb = QueryBuilder.newBuilder().addTypeName(adapter.getTypeName()).indexName(indexToCopy.getName()).constraints(query.getQueryConstraints());
try (CloseableIterator<?> it = query(qb.build())) {
try (Writer<Object> writer = other.createWriter(adapter.getTypeName())) {
while (it.hasNext()) {
writer.write(it.next());
}
}
}
}
}
}
use of org.locationtech.geowave.core.store.api.QueryBuilder in project geowave by locationtech.
the class BaseDataStore method copyTo.
@Override
public void copyTo(final DataStore other) {
if (other instanceof BaseDataStore) {
// efficiently copy underlying GeoWaveRow and GeoWaveMetadata
for (final MetadataType metadataType : MetadataType.values()) {
try (MetadataWriter writer = ((BaseDataStore) other).baseOperations.createMetadataWriter(metadataType)) {
final MetadataReader reader = baseOperations.createMetadataReader(metadataType);
try (CloseableIterator<GeoWaveMetadata> it = reader.query(new MetadataQuery())) {
while (it.hasNext()) {
writer.write(it.next());
}
}
} catch (final Exception e) {
LOGGER.error("Unable to write metadata on copy", e);
}
}
final InternalDataAdapter<?>[] adapters = adapterStore.getAdapters();
for (final InternalDataAdapter<?> adapter : adapters) {
final AdapterToIndexMapping[] mappings = indexMappingStore.getIndicesForAdapter(adapter.getAdapterId());
for (final AdapterToIndexMapping mapping : mappings) {
final Index index = mapping.getIndex(indexStore);
final boolean rowMerging = BaseDataStoreUtils.isRowMerging(adapter);
final ReaderParamsBuilder<GeoWaveRow> bldr = new ReaderParamsBuilder<>(index, adapterStore, indexMappingStore, internalAdapterStore, rowMerging ? new GeoWaveRowMergingTransform(BaseDataStoreUtils.getRowMergingAdapter(adapter), adapter.getAdapterId()) : GeoWaveRowIteratorTransformer.NO_OP_TRANSFORMER);
bldr.adapterIds(new short[] { adapter.getAdapterId() });
bldr.isClientsideRowMerging(rowMerging);
try (RowReader<GeoWaveRow> reader = baseOperations.createReader(bldr.build())) {
try (RowWriter writer = ((BaseDataStore) other).baseOperations.createWriter(index, adapter)) {
while (reader.hasNext()) {
writer.write(reader.next());
}
}
} catch (final Exception e) {
LOGGER.error("Unable to write metadata on copy", e);
}
}
}
} else {
final DataTypeAdapter<?>[] sourceTypes = getTypes();
// add all the types that the destination store doesn't have yet
final DataTypeAdapter<?>[] destTypes = other.getTypes();
for (int i = 0; i < sourceTypes.length; i++) {
boolean found = false;
for (int k = 0; k < destTypes.length; k++) {
if (destTypes[k].getTypeName().compareTo(sourceTypes[i].getTypeName()) == 0) {
found = true;
break;
}
}
if (!found) {
other.addType(sourceTypes[i]);
}
}
// add the indices for each type
for (int i = 0; i < sourceTypes.length; i++) {
final String typeName = sourceTypes[i].getTypeName();
final short adapterId = internalAdapterStore.getAdapterId(typeName);
final AdapterToIndexMapping[] indexMappings = indexMappingStore.getIndicesForAdapter(adapterId);
final Index[] indices = Arrays.stream(indexMappings).map(mapping -> mapping.getIndex(indexStore)).toArray(Index[]::new);
other.addIndex(typeName, indices);
final QueryBuilder<?, ?> qb = QueryBuilder.newBuilder().addTypeName(typeName);
try (CloseableIterator<?> it = query(qb.build())) {
try (final Writer<Object> writer = other.createWriter(typeName)) {
while (it.hasNext()) {
writer.write(it.next());
}
}
}
}
}
}
use of org.locationtech.geowave.core.store.api.QueryBuilder in project geowave by locationtech.
the class SelectStatement method execute.
@Override
public ResultSet execute(final String... authorizations) {
final String typeName = adapter.getTypeName();
if (isAggregation()) {
final AggregationQueryBuilder<PersistableList, List<Object>, T, ?> bldr = AggregationQueryBuilder.newBuilder();
bldr.setAuthorizations(authorizations);
if (filter != null) {
bldr.filter(filter);
}
if (limit != null) {
bldr.limit(limit);
}
final CompositeAggregation<T> composite = new CompositeAggregation<>();
final List<String> columnNames = Lists.newArrayListWithCapacity(selectors.size());
final List<Class<?>> columnTypes = Lists.newArrayListWithCapacity(selectors.size());
for (final Selector selector : selectors) {
final AggregationSelector aggregation = (AggregationSelector) selector;
final AggregationFunction<?> function = GWQLExtensionRegistry.instance().getAggregationFunction(aggregation.functionName());
if (function == null) {
throw new RuntimeException("No aggregation function called '" + aggregation.functionName() + "' was found.");
}
composite.add(function.getAggregation(adapter, aggregation.functionArgs()));
columnNames.add(selector.name());
columnTypes.add(function.getReturnType());
}
bldr.aggregate(typeName, composite);
return new SingletonResultSet(columnNames, columnTypes, dataStore.aggregate(bldr.build()));
} else {
final QueryBuilder<T, ?> bldr = QueryBuilder.newBuilder(adapter.getDataClass()).addTypeName(typeName);
bldr.setAuthorizations(authorizations);
if (filter != null) {
bldr.filter(filter);
}
if ((selectors != null) && !selectors.isEmpty()) {
final Set<String> usedAttributes = Sets.newHashSet();
selectors.forEach(s -> usedAttributes.add(((ColumnSelector) s).columnName()));
if (filter != null) {
filter.addReferencedFields(usedAttributes);
}
for (final String attribute : usedAttributes) {
if (adapter.getFieldDescriptor(attribute) == null) {
throw new RuntimeException("No column named " + attribute + " was found in " + typeName);
}
}
bldr.subsetFields(typeName, usedAttributes.toArray(new String[usedAttributes.size()]));
} else {
selectors = Lists.transform(Arrays.asList(adapter.getFieldDescriptors()), f -> new ColumnSelector(f.fieldName()));
}
if (limit != null) {
bldr.limit(limit);
}
return new AdapterEntryResultSet<>(selectors, adapter, dataStore.query(bldr.build()));
}
}
use of org.locationtech.geowave.core.store.api.QueryBuilder in project geowave by locationtech.
the class GeoWaveTextIndexIT method getResults.
private Set<String> getResults(final TextSearch search) {
final DataStore ds = dataStoreOptions.createDataStore();
final Set<String> results = new HashSet<>();
final QueryBuilder queryBldr = QueryBuilder.newBuilder().addTypeName(TYPE_NAME).indexName(TEST_TEXT_INDEX_NAME);
try (final CloseableIterator<SimpleFeature> it = ds.query((Query) queryBldr.constraints(queryBldr.constraintsFactory().customConstraints(search)).build())) {
it.forEachRemaining(f -> {
final String cityName = f.getAttribute(CITY_ATTR_NAME).toString();
Assert.assertFalse(results.contains(cityName));
results.add(cityName);
});
}
return results;
}
Aggregations