com.gs.fw.common.mithra.finder.MultiInOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reladomo Show documentation
Show all versions of reladomo Show documentation
Reladomo is an object-relational mapping framework.
/*
Copyright 2016 Goldman Sachs.
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.gs.fw.common.mithra.finder;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.fw.common.mithra.MithraObjectPortal;
import com.gs.fw.common.mithra.attribute.AsOfAttribute;
import com.gs.fw.common.mithra.attribute.Attribute;
import com.gs.fw.common.mithra.attribute.SingleColumnAttribute;
import com.gs.fw.common.mithra.attribute.TimestampAttribute;
import com.gs.fw.common.mithra.cache.*;
import com.gs.fw.common.mithra.extractor.Extractor;
import com.gs.fw.common.mithra.extractor.TimestampExtractor;
import com.gs.fw.common.mithra.finder.asofop.AsOfTimestampEqualityMapper;
import com.gs.fw.common.mithra.finder.sqcache.ExactMatchSmr;
import com.gs.fw.common.mithra.finder.sqcache.NoMatchRequiresExactSmr;
import com.gs.fw.common.mithra.finder.sqcache.ShapeMatchResult;
import com.gs.fw.common.mithra.notification.MithraDatabaseIdentifierExtractor;
import com.gs.fw.common.mithra.tempobject.TupleTempContext;
import com.gs.fw.common.mithra.util.*;
public class MultiInOperation implements Operation, SqlParameterSetter
{
private Attribute[] attributes;
private MithraTupleSet mithraTupleSet;
private transient IndexReference bestIndexRef;
private transient int hashCode;
private transient List attributeList;
private transient EqualityOperation equalityOperation; // only used for full cache evaluation
public MultiInOperation(Attribute[] attributes, List dataHolders, Extractor[] extractors)
{
this.attributes = attributes;
this.mithraTupleSet = new MithraDataHolderTupleSet(dataHolders, extractors);
this.mithraTupleSet.markAsReadOnly();
}
public MultiInOperation(Attribute[] attributes, MithraTupleSet mithraTupleSet)
{
this.attributes = attributes;
mithraTupleSet.markAsReadOnly();
this.mithraTupleSet = mithraTupleSet;
}
public EqualityOperation zExtractEqualityOperations()
{
return null;
}
public Operation setExtraOperationAndReturnLeftOver(Operation extraOperationOnResult)
{
if (extraOperationOnResult != null)
{
equalityOperation = extraOperationOnResult.zExtractEqualityOperations();
if (equalityOperation == extraOperationOnResult)
{
return null;
}
}
return extraOperationOnResult; // todo: optimize this
}
protected Cache getCache()
{
return this.getResultObjectPortal().getCache();
}
protected void findBestIndex()
{
createAttributeList();
if (this.bestIndexRef == null || !this.bestIndexRef.isForCache(this.getCache()))
{
this.bestIndexRef = this.getCache().getBestIndexReference(this.attributeList);
}
}
private void createAttributeList()
{
if (this.attributeList == null)
{
if (this.equalityOperation == null)
{
this.attributeList = Arrays.asList(this.attributes);
}
else
{
this.attributeList = FastList.newList(this.attributes.length + this.equalityOperation.getEqualityOpCount());
for(int i=0;i 0) toStringContext.append(", ");
attributes[i].zAppendToString(toStringContext);
}
toStringContext.append(") in ()");
}
public void addDependentPortalsToSet(Set set)
{
if (this.equalityOperation != null)
{
this.equalityOperation.addDependentPortalsToSet(set);
}
set.add(getFirstLeftAttribute().getOwnerPortal());
}
public void addDepenedentAttributesToSet(Set set)
{
if (this.equalityOperation != null)
{
this.equalityOperation.addDepenedentAttributesToSet(set);
}
for (int i = 0; i < attributes.length; i++)
{
set.add(attributes[i]);
}
}
public boolean isJoinedWith(MithraObjectPortal portal)
{
return false;
}
protected int getAttributeIndex(Attribute attribute)
{
for (int i = 0; i < attributes.length; i++)
{
if (attribute.equals(attributes[i]))
{
return i;
}
}
return -1;
}
public List applyOperationToFullCache()
{
return this.applyOperation(false);
}
public List applyOperationToPartialCache()
{
if (this.usesUniqueIndex())
{
return this.applyOperation(true);
}
return null;
}
protected List applyOperation(boolean returnNullIfNotFoundInIndex)
{
this.findBestIndex();
Cache cache = this.getCache();
Extractor[] extractors = mithraTupleSet.getExtractors();
if (this.bestIndexRef.isValid())
{
Attribute[] bestAttributes = cache.getIndexAttributes(this.bestIndexRef.indexReference);
Extractor[] sortedRightIndexAttributes = new Extractor[bestAttributes.length];
int extractorPos = 0;
int usedTupleAttributes = 0;
int usedEqualityAttributes = 0;
for (int i = 0; i < bestAttributes.length; i++)
{
int attributeIndex = this.getAttributeIndex(bestAttributes[i]);
if (attributeIndex >= 0)
{
sortedRightIndexAttributes[extractorPos] = extractors[attributeIndex];
extractorPos++;
usedTupleAttributes++;
}
else
{
sortedRightIndexAttributes[extractorPos] = this.equalityOperation.getParameterExtractorFor(bestAttributes[i]);
extractorPos++;
usedEqualityAttributes++;
}
}
List result;
if (usedTupleAttributes < this.attributes.length)
{
Extractor[] unusedLeft = new Extractor[attributes.length - usedTupleAttributes];
Extractor[] unusedRight = new Extractor[attributes.length - usedTupleAttributes];
int count = 0;
for(int i=0;i result)
{
for(Attribute a: attributes) result.add(a);
}
public Operation zSubstituteForTempJoin(Map attributeMap, Object prototypeObject)
{
throw new RuntimeException("not implemented");
}
public Operation zGetAsOfOp(AsOfAttribute asOfAttribute)
{
for(int i=0;i 0) query.appendWhereClause(" or ");
query.appendWhereClause("(");
for(int i=0;i 0) query.appendWhereClause(" and ");
query.appendWhereClause(attributes[i].getFullyQualifiedLeftHandExpression(query));
query.appendWhereClause(" = ?");
}
query.appendWhereClause(")");
}
query.appendWhereClause(")");
query.addSqlParameterSetter(this);
}
else
{
TupleTempContext tempContext = query.getMultiInTempContext(this);
query.addTempContext(tempContext);
Object source = null;
if (this.attributes[0].getSourceAttribute() != null)
{
source = query.getSourceAttributeValue(query.getCurrentMapperList(), query.getCurrentSourceNumber());
}
//this doesn't require retry handling; the context is destroyed before retry code is called.
tempContext.insert(this.attributes[0].getOwnerPortal(), 100, source, mithraTupleSet.getTupleList(), query.isParallel());
Mapper mapper = createMapper(tempContext);
query.registerTempTupleMapper(mapper);
mapper.generateSql(query);
if (source != null)
{
Attribute sourceAttribute = this.attributes[0].getSourceAttribute();
query.setSourceOperation((SourceOperation) tempContext.getSourceOperation(source, sourceAttribute));
}
mapper.popMappers(query);
}
}
private Mapper createMapper(TupleTempContext tempContext)
{
InternalList equalityMappers = new InternalList(this.attributes.length);
for(int i=0;i