use of io.crate.expression.udf.UserDefinedFunctionsMetadata in project crate by crate.
the class DocSchemaInfo method update.
@Override
public void update(ClusterChangedEvent event) {
assert event.metadataChanged() : "metadataChanged must be true if update is called";
// search for aliases of deleted and created indices, they must be invalidated also
Metadata prevMetadata = event.previousState().metadata();
for (Index index : event.indicesDeleted()) {
invalidateFromIndex(index, prevMetadata);
}
Metadata newMetadata = event.state().metadata();
for (String index : event.indicesCreated()) {
invalidateAliases(newMetadata.index(index).getAliases());
}
// search for templates with changed meta data => invalidate template aliases
ImmutableOpenMap<String, IndexTemplateMetadata> newTemplates = newMetadata.templates();
ImmutableOpenMap<String, IndexTemplateMetadata> prevTemplates = prevMetadata.templates();
if (!newTemplates.equals(prevTemplates)) {
for (ObjectCursor<IndexTemplateMetadata> cursor : newTemplates.values()) {
invalidateAliases(cursor.value.aliases());
}
for (ObjectCursor<IndexTemplateMetadata> cursor : prevTemplates.values()) {
invalidateAliases(cursor.value.aliases());
}
}
// search indices with changed meta data
Iterator<String> currentTablesIt = docTableByName.keySet().iterator();
ObjectLookupContainer<String> templates = newTemplates.keys();
ImmutableOpenMap<String, IndexMetadata> indices = newMetadata.indices();
while (currentTablesIt.hasNext()) {
String tableName = currentTablesIt.next();
String indexName = getIndexName(tableName);
IndexMetadata newIndexMetadata = newMetadata.index(indexName);
if (newIndexMetadata == null) {
docTableByName.remove(tableName);
} else {
IndexMetadata oldIndexMetadata = prevMetadata.index(indexName);
if (oldIndexMetadata != null && ClusterChangedEvent.indexMetadataChanged(oldIndexMetadata, newIndexMetadata)) {
docTableByName.remove(tableName);
// invalidate aliases of changed indices
invalidateAliases(newIndexMetadata.getAliases());
invalidateAliases(oldIndexMetadata.getAliases());
} else {
// this is the case if a single partition has been modified using alter table <t> partition (...)
String possibleTemplateName = PartitionName.templateName(name(), tableName);
if (templates.contains(possibleTemplateName)) {
for (ObjectObjectCursor<String, IndexMetadata> indexEntry : indices) {
if (IndexParts.isPartitioned(indexEntry.key)) {
docTableByName.remove(tableName);
break;
}
}
}
}
}
}
// re register UDFs for this schema
UserDefinedFunctionsMetadata udfMetadata = newMetadata.custom(UserDefinedFunctionsMetadata.TYPE);
if (udfMetadata != null) {
udfService.updateImplementations(schemaName, udfMetadata.functionsMetadata().stream().filter(f -> schemaName.equals(f.schema())));
}
}
use of io.crate.expression.udf.UserDefinedFunctionsMetadata in project crate by crate.
the class PgCatalogSchemaInfo method update.
@Override
public void update(ClusterChangedEvent event) {
assert event.metadataChanged() : "metadataChanged must be true if update is called";
Metadata newMetadata = event.state().metadata();
// re register UDFs for this schema
UserDefinedFunctionsMetadata udfMetadata = newMetadata.custom(UserDefinedFunctionsMetadata.TYPE);
if (udfMetadata != null) {
udfService.updateImplementations(NAME, udfMetadata.functionsMetadata().stream().filter(f -> NAME.equals(f.schema())));
}
}
use of io.crate.expression.udf.UserDefinedFunctionsMetadata in project crate by crate.
the class Schemas method getNewCurrentSchemas.
@VisibleForTesting
static Set<String> getNewCurrentSchemas(Metadata metadata) {
Set<String> schemas = new HashSet<>();
// 'doc' schema is always available and has the special property that its indices
// don't have to be prefixed with the schema name
schemas.add(DOC_SCHEMA_NAME);
for (String index : metadata.getConcreteAllIndices()) {
addIfSchema(schemas, index);
}
for (ObjectCursor<String> cursor : metadata.templates().keys()) {
addIfSchema(schemas, cursor.value);
}
UserDefinedFunctionsMetadata udfMetadata = metadata.custom(UserDefinedFunctionsMetadata.TYPE);
if (udfMetadata != null) {
udfMetadata.functionsMetadata().stream().map(UserDefinedFunctionMetadata::schema).forEach(schemas::add);
}
ViewsMetadata viewsMetadata = metadata.custom(ViewsMetadata.TYPE);
if (viewsMetadata != null) {
StreamSupport.stream(viewsMetadata.names().spliterator(), false).map(IndexParts::new).map(IndexParts::getSchema).forEach(schemas::add);
}
return schemas;
}
use of io.crate.expression.udf.UserDefinedFunctionsMetadata in project crate by crate.
the class DocSchemaInfoTest method testInvalidFunction.
@Test
public void testInvalidFunction() throws Exception {
UserDefinedFunctionMetadata invalid = new UserDefinedFunctionMetadata("my_schema", "invalid", List.of(), DataTypes.INTEGER, "burlesque", "this is not valid burlesque code");
UserDefinedFunctionMetadata valid = new UserDefinedFunctionMetadata("my_schema", "valid", List.of(), DataTypes.INTEGER, "burlesque", "\"Hello, World!\"Q");
UserDefinedFunctionsMetadata metadata = UserDefinedFunctionsMetadata.of(invalid, valid);
// if a functionImpl can't be created, it won't be registered
udfService.updateImplementations("my_schema", metadata.functionsMetadata().stream());
assertThat(nodeCtx.functions().get("my_schema", "valid", List.of(), pathWithPGCatalogAndDoc()), Matchers.notNullValue());
expectedException.expectMessage("Unknown function: my_schema.invalid()");
nodeCtx.functions().get("my_schema", "invalid", List.of(), pathWithPGCatalogAndDoc());
}
Aggregations