use of eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference in project hale by halestudio.
the class InstanceIndexUpdateServiceImpl method reindex.
private void reindex() {
getIndexService().clearIndexedValues();
InstanceService is = serviceProvider.getService(InstanceService.class);
InstanceCollection source = is.getInstances(DataSet.SOURCE);
try (ResourceIterator<Instance> it = source.iterator()) {
while (it.hasNext()) {
Instance i = it.next();
InstanceReference ref = source.getReference(i);
if (Identifiable.is(ref)) {
ref = new IdentifiableInstanceReference(ref, Identifiable.getId(ref));
}
ResolvableInstanceReference rir = new ResolvableInstanceReference(ref, source);
getIndexService().add(i, rir);
}
}
}
use of eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference in project hale by halestudio.
the class IndexJoinHandler method partitionInstances.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.InstanceHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
* java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
if (transformationParameters == null || !transformationParameters.containsKey(PARAMETER_JOIN) || transformationParameters.get(PARAMETER_JOIN).isEmpty()) {
throw new TransformationException("No join parameter defined");
}
JoinHandler fallbackHandler = new JoinHandler();
InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
if (indexService == null) {
log.warn(MessageFormat.format("Index service not available, falling back to join handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
JoinParameter joinParameter = transformationParameters.get(PARAMETER_JOIN).get(0).as(JoinParameter.class);
String validation = joinParameter.validate();
if (validation != null) {
throw new TransformationException("Join parameter invalid: " + validation);
}
List<TypeEntityDefinition> types = joinParameter.getTypes();
JoinDefinition joinDefinition = JoinUtil.getJoinDefinition(joinParameter);
// remember instances of first type to start join afterwards
Collection<ResolvableInstanceReference> startInstances = new LinkedList<ResolvableInstanceReference>();
List<Object> inputInstanceIds = new ArrayList<>();
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext()) {
Instance i = InstanceDecorator.getRoot(it.next());
// remember instances of first type
if (i.getDefinition().equals(types.get(0).getDefinition())) {
startInstances.add(new ResolvableInstanceReference(instances.getReference(i), instances));
}
if (!Identifiable.is(i)) {
log.warn(MessageFormat.format("At least one instance does not have an ID, falling back to join handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
inputInstanceIds.add(Identifiable.getId(i));
}
}
return new IndexJoinIterator(startInstances, joinDefinition, indexService);
}
use of eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference in project hale by halestudio.
the class IndexMergeHandler method partitionInstances.
/**
* @see eu.esdihumboldt.cst.functions.core.merge.AbstractMergeHandler#partitionInstances(eu.esdihumboldt.hale.common.instance.model.InstanceCollection,
* java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
public ResourceIterator<FamilyInstance> partitionInstances(InstanceCollection instances, String transformationIdentifier, TransformationEngine engine, ListMultimap<String, ParameterValue> transformationParameters, Map<String, String> executionParameters, TransformationLog log) throws TransformationException {
PropertiesMergeHandler fallbackHandler = new PropertiesMergeHandler();
InstanceIndexService indexService = serviceProvider.getService(InstanceIndexService.class);
if (indexService == null) {
log.warn(MessageFormat.format("Index service not available, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
final IndexMergeConfig mergeConfig = createMergeConfiguration(transformationParameters);
QName typeName;
try (ResourceIterator<Instance> it = instances.iterator()) {
if (it.hasNext()) {
typeName = it.next().getDefinition().getName();
} else {
// Nothing to partition
return new ResourceIterator<FamilyInstance>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public FamilyInstance next() {
return null;
}
@Override
public void close() {
// Do nothing
}
};
}
}
// Querying the index will yield a result over all instances. We must,
// however, be able to operate only on the given input instances instead
// of all instances.
// We must, therefore, be able to uniquely identify every instance in
// the index, so that we can retain from the index query only the
// relevant instances.
List<Object> inputInstanceIds = new ArrayList<>();
try (ResourceIterator<Instance> it = instances.iterator()) {
while (it.hasNext()) {
Instance i = InstanceDecorator.getRoot(it.next());
if (!Identifiable.is(i)) {
log.warn(MessageFormat.format("At least one instance does not have an ID, falling back to merge handler {0}", fallbackHandler.getClass().getCanonicalName()));
return fallbackHandler.partitionInstances(instances, transformationIdentifier, engine, transformationParameters, executionParameters, log);
}
inputInstanceIds.add(Identifiable.getId(i));
}
}
Collection<Collection<ResolvableInstanceReference>> partitionedIndex = indexService.groupBy(typeName, mergeConfig.keyProperties);
// Remove instance groups from the partitioned index where none of the
// instances in the group are in the processed instances.
partitionedIndex.removeIf(part -> !part.stream().map(ref -> ref.getId()).anyMatch(id -> inputInstanceIds.contains(id)));
Iterator<Collection<ResolvableInstanceReference>> it = partitionedIndex.iterator();
return new ResourceIterator<FamilyInstance>() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public FamilyInstance next() {
Collection<ResolvableInstanceReference> instanceRefs = it.next();
InstanceCollection instancesToBeMerged = new DefaultInstanceCollection(instanceRefs.stream().map(ref -> ref.resolve()).collect(Collectors.toList()));
return new FamilyInstanceImpl(merge(instancesToBeMerged, mergeConfig));
}
@Override
public void close() {
// TODO Auto-generated method stub
}
};
}
use of eu.esdihumboldt.hale.common.instance.model.ResolvableInstanceReference in project hale by halestudio.
the class InstanceIndexServiceImpl method add.
/**
* @see eu.esdihumboldt.hale.common.instance.index.InstanceIndexService#add(eu.esdihumboldt.hale.common.instance.model.Instance,
* eu.esdihumboldt.hale.common.instance.model.InstanceCollection)
*/
@Override
public void add(Instance instance, InstanceCollection instances) {
InstanceReference ref;
if (Identifiable.is(instance)) {
ref = new IdentifiableInstanceReference(instances.getReference(instance), Identifiable.getId(instance));
} else {
ref = instances.getReference(instance);
}
ResolvableInstanceReference rir = new ResolvableInstanceReference(ref, instances);
getIndex(instance.getDefinition().getName()).add(rir, instance);
}
Aggregations