Skip to content

Commit

Permalink
[#1393] Use TupleTransformer for SUBSELECT and MULTISET fetching to a…
Browse files Browse the repository at this point in the history
…llow streaming
  • Loading branch information
beikov committed Dec 7, 2021
1 parent 8c65963 commit 117e4bd
Show file tree
Hide file tree
Showing 18 changed files with 555 additions and 620 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void addAll(Collection<?> container, Collection<?> collection, boolean re
}
for (int i = listEnd; i < other.size(); i++) {
Object valueContainer = valueAccumulator.createContainer(false, 1);
valueAccumulator.add(valueContainer, null, other.get(i), false);
valueAccumulator.addAll(valueContainer, other.get(i), false);
list.add(i, valueContainer);
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ public TupleTransformatorFactory getTupleTransformatorFactory() {
return tupleTransformatorFactory;
}

public void setTupleListTransformer(TupleListTransformer tupleListTransformer) {
public void addTupleListTransformer(TupleListTransformer tupleListTransformer) {
tupleTransformatorFactory.add(tupleListTransformer);
}

public void setTupleListTransformerFactory(TupleListTransformerFactory tupleListTransformerFactory) {
public void addTupleListTransformerFactory(TupleListTransformerFactory tupleListTransformerFactory) {
tupleTransformatorFactory.add(tupleListTransformerFactory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @author Christian Beikov
* @since 1.5.0
*/
public class CollectionMultisetTupleListTransformerFactory implements TupleListTransformerFactory {
public class CollectionMultisetTupleTransformerFactory implements TupleTransformerFactory {

private final int startIndex;
private final String mapping;
Expand All @@ -53,8 +53,8 @@ public class CollectionMultisetTupleListTransformerFactory implements TupleListT
private final BasicUserTypeStringSupport<?> indexBasicTypeSupport;
private final boolean hasSelectOrSubselectFetchedAttributes;

public CollectionMultisetTupleListTransformerFactory(int startIndex, String mapping, String attributePath, String multisetResultAlias, TypeConverter<Object, Object> elementConverter, ContainerAccumulator<?> containerAccumulator, boolean dirtyTracking, ViewTypeObjectBuilderTemplate<Object[]> template,
ViewTypeObjectBuilderTemplate<Object[]> indexTemplate, boolean hasSelectOrSubselectFetchedAttributes, TupleTransformerFactory subviewTupleTransformerFactory, TupleTransformerFactory indexSubviewTupleTransformerFactory, BasicUserTypeStringSupport<?> valueBasicTypeSupport, BasicUserTypeStringSupport<?> indexBasicTypeSupport) {
public CollectionMultisetTupleTransformerFactory(int startIndex, String mapping, String attributePath, String multisetResultAlias, TypeConverter<Object, Object> elementConverter, ContainerAccumulator<?> containerAccumulator, boolean dirtyTracking, ViewTypeObjectBuilderTemplate<Object[]> template,
ViewTypeObjectBuilderTemplate<Object[]> indexTemplate, boolean hasSelectOrSubselectFetchedAttributes, TupleTransformerFactory subviewTupleTransformerFactory, TupleTransformerFactory indexSubviewTupleTransformerFactory, BasicUserTypeStringSupport<?> valueBasicTypeSupport, BasicUserTypeStringSupport<?> indexBasicTypeSupport) {
this.startIndex = startIndex;
this.mapping = mapping;
this.attributePath = attributePath;
Expand Down Expand Up @@ -93,12 +93,17 @@ public CollectionMultisetTupleListTransformerFactory(int startIndex, String mapp
}

@Override
public int getConsumableIndex() {
public int getConsumeStartIndex() {
return startIndex;
}

@Override
public TupleListTransformer create(ParameterHolder<?> parameterHolder, Map<String, Object> optionalParameters, EntityViewConfiguration entityViewConfiguration) {
public int getConsumeEndIndex() {
return startIndex + 1;
}

@Override
public TupleTransformer create(ParameterHolder<?> parameterHolder, Map<String, Object> optionalParameters, EntityViewConfiguration entityViewConfiguration) {
if (mapping != null) {
if (parameterHolder instanceof FullQueryBuilder<?, ?>) {
FullQueryBuilder<?, ?> queryBuilder = (FullQueryBuilder<?, ?>) parameterHolder;
Expand All @@ -115,7 +120,7 @@ public TupleListTransformer create(ParameterHolder<?> parameterHolder, Map<Strin
TupleTransformator tupleTransformator = template == null ? null : template.getTupleTransformatorFactory().create(parameterHolder, optionalParameters, entityViewConfiguration);
TupleTransformer subviewTupleTransformer = subviewTupleTransformerFactory == null ? null : subviewTupleTransformerFactory.create(parameterHolder, optionalParameters, entityViewConfiguration);
TupleTransformer indexSubviewTupleTransformer = indexSubviewTupleTransformerFactory == null ? null : indexSubviewTupleTransformerFactory.create(parameterHolder, optionalParameters, entityViewConfiguration);
return new MultisetTupleListTransformer(startIndex, hasSelectOrSubselectFetchedAttributes, tupleTransformator, subviewTupleTransformer, indexSubviewTupleTransformer, indexBasicTypeSupport == null ? -1 : fieldConverters.length - 1, fieldConverters, elementConverter, containerAccumulator, dirtyTracking);
return new MultisetTupleTransformer(startIndex, hasSelectOrSubselectFetchedAttributes, tupleTransformator, subviewTupleTransformer, indexSubviewTupleTransformer, indexBasicTypeSupport == null ? -1 : fieldConverters.length - 1, fieldConverters, elementConverter, containerAccumulator, dirtyTracking);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2014 - 2021 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blazebit.persistence.view.impl.objectbuilder.transformer;

import com.blazebit.persistence.view.impl.objectbuilder.ContainerAccumulator;
import com.blazebit.persistence.view.impl.objectbuilder.transformator.TupleTransformator;
import com.blazebit.persistence.view.impl.objectbuilder.transformator.UpdatableViewMap;
import com.blazebit.persistence.view.spi.type.BasicUserTypeStringSupport;
import com.blazebit.persistence.view.spi.type.TypeConverter;

import java.util.List;

/**
*
* @author Christian Beikov
* @since 1.5.0
*/
public class MultisetTupleTransformer implements TupleTransformer {

private final int startIndex;
private final boolean hasSelectOrSubselectFetchedAttributes;
private final TupleTransformator tupleTransformator;
private final TupleTransformer subviewTupleTransformer;
private final TupleTransformer indexSubviewTupleTransformer;
private final int indexStartIndex;
private final BasicUserTypeStringSupport<Object>[] fieldConverters;
private final TypeConverter<Object, Object> elementConverter;
private final ContainerAccumulator<Object> containerAccumulator;
private final boolean dirtyTracking;

public MultisetTupleTransformer(int startIndex, boolean hasSelectOrSubselectFetchedAttributes, TupleTransformator tupleTransformator, TupleTransformer subviewTupleTransformer, TupleTransformer indexSubviewTupleTransformer, int indexStartIndex,
BasicUserTypeStringSupport<Object>[] fieldConverters, TypeConverter<Object, Object> elementConverter, ContainerAccumulator<Object> containerAccumulator, boolean dirtyTracking) {
this.startIndex = startIndex;
this.hasSelectOrSubselectFetchedAttributes = hasSelectOrSubselectFetchedAttributes;
this.tupleTransformator = tupleTransformator;
this.subviewTupleTransformer = subviewTupleTransformer;
this.indexSubviewTupleTransformer = indexSubviewTupleTransformer;
this.indexStartIndex = indexStartIndex;
this.elementConverter = elementConverter;
this.fieldConverters = fieldConverters;
this.containerAccumulator = containerAccumulator;
this.dirtyTracking = dirtyTracking;
}

@Override
public int getConsumeStartIndex() {
return startIndex;
}

@Override
public int getConsumeEndIndex() {
return startIndex + 1;
}

@Override
public Object[] transform(Object[] tuple, UpdatableViewMap updatableViewMap) {
Object collection = containerAccumulator.createContainer(dirtyTracking, 0);
if (tuple[startIndex] != null) {
List<Object[]> objects = (List<Object[]>) tuple[startIndex];
for (int i = 0; i < objects.size(); i++) {
Object[] elementTuple = objects.get(i);
for (int j = 0; j < fieldConverters.length; j++) {
if (elementTuple[j] instanceof CharSequence) {
elementTuple[j] = fieldConverters[j].fromString((CharSequence) elementTuple[j]);
}
}
}
if (tupleTransformator != null) {
// Before building the subviews, apply the tuple transformator on the nested set in isolation
tupleTransformator.transformAll(objects);
}
for (int i = 0; i < objects.size(); i++) {
Object indexObject = null;
if (indexSubviewTupleTransformer != null) {
indexObject = indexSubviewTupleTransformer.transform(objects.get(i), updatableViewMap)[indexSubviewTupleTransformer.getConsumeStartIndex() - 1];
} else if (indexStartIndex != -1) {
indexObject = objects.get(i)[indexStartIndex];
}
if (subviewTupleTransformer == null) {
add(collection, indexObject, objects.get(i)[startIndex]);
} else {
Object[] transformedTuple = subviewTupleTransformer.transform(objects.get(i), updatableViewMap);
add(collection, indexObject, transformedTuple[0]);
}
}
}
tuple[startIndex] = collection;
return tuple;
}

protected void add(Object collection, Object index, Object value) {
if (elementConverter != null) {
value = elementConverter.convertToViewType(value);
}
containerAccumulator.add(collection, index, value, dirtyTracking);
}
}
Loading

0 comments on commit 117e4bd

Please sign in to comment.