com.gs.fw.common.mithra.cache.TransactionalNonUniqueIndex 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.cache;
import java.sql.Timestamp;
import java.util.List;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.fw.common.mithra.MithraManagerProvider;
import com.gs.fw.common.mithra.MithraTransaction;
import com.gs.fw.common.mithra.extractor.Extractor;
import com.gs.fw.common.mithra.extractor.RelationshipHashStrategy;
import com.gs.fw.common.mithra.transaction.TransactionLocal;
import com.gs.fw.common.mithra.util.DoUntilProcedure;
import com.gs.fw.common.mithra.util.Filter2;
import org.slf4j.Logger;
public class TransactionalNonUniqueIndex implements IterableNonUniqueIndex, TransactionalIndex
{
private ExtractorBasedHashStrategy hashStrategy;
private ExtractorBasedHashStrategy pkHashStrategy;
private Extractor[] pkExtractors;
private Extractor[] indexExtractors;
private NonUniqueIndex mainIndex;
private static final TransactionalUnderlyingObjectGetter transactionalUnderlyingObjectGetter = new TransactionalUnderlyingObjectGetter();
private static final NonTransactionalUnderlyingObjectGetter nonTransactionalUnderlyingObjectGetter = new NonTransactionalUnderlyingObjectGetter();
private TransactionLocal perTransactionStorage = new TransactionLocal();
public TransactionalNonUniqueIndex(String indexName, Extractor[] pkExtractors, Extractor[] indexExtractors)
{
this.pkExtractors = pkExtractors;
this.indexExtractors = indexExtractors;
this.pkHashStrategy = ExtractorBasedHashStrategy.create(pkExtractors);
this.hashStrategy = ExtractorBasedHashStrategy.create(indexExtractors);
this.mainIndex = new NonUniqueIndex(indexName, pkExtractors, indexExtractors, pkHashStrategy, hashStrategy);
this.mainIndex.setUnderlyingObjectGetter(nonTransactionalUnderlyingObjectGetter);
}
@Override
public boolean isInitialized()
{
return true;
}
@Override
public Index getInitialized(IterableIndex iterableIndex)
{
return this;
}
@Override
public void destroy()
{
this.mainIndex.destroy();
this.mainIndex = null;
}
@Override
public void reportSpaceUsage(Logger logger, String className)
{
this.mainIndex.reportSpaceUsage(logger, className);
}
@Override
public void ensureExtraCapacity(int size)
{
this.mainIndex.ensureExtraCapacity(size);
}
protected List removeDeletedFromMainResult(Object fromMain, TransactionLocalStorage txStorage)
{
if (fromMain == null)
{
return null;
}
List result = null;
FullUniqueIndex deletedPerThread = getDeletedFromLocalStorage(txStorage);
if (deletedPerThread == null)
{
if (fromMain instanceof FullUniqueIndex)
{
result = ((FullUniqueIndex)fromMain).getAll();
}
else
{
result = new FastList(1);
result.add(fromMain);
}
}
else
{
if (fromMain instanceof FullUniqueIndex)
{
result = new FastList();
((FullUniqueIndex)fromMain).forAll(new AddNonDeleted(result, deletedPerThread));
}
else
{
if (deletedPerThread.getFromData(this.nonTransactionalUnderlyingObjectGetter.getUnderlyingObject(fromMain)) == null)
{
result = new FastList(1);
result.add(fromMain);
}
}
}
return result;
}
private FullUniqueIndex getDeletedFromLocalStorage(TransactionLocalStorage txStorage)
{
return txStorage == null ? null : txStorage.deleted;
}
protected Object combineResults(Object fromPerThread, Object fromMain, TransactionLocalStorage txStorage)
{
List result = removeDeletedFromMainResult(fromMain, txStorage);
if (fromPerThread != null)
{
if (result == null)
{
result = new FastList();
}
if (fromPerThread instanceof FullUniqueIndex)
{
((FullUniqueIndex) fromPerThread).forAll(new AddAll(result));
}
else
{
result.add(fromPerThread);
}
}
return result;
}
// for int indicies:
public Object get(int indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(char indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(Object indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(byte[] indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(long indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(double indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(float indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(boolean indexValue)
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(indexValue);
}
Object mainIndexResult = this.mainIndex.get(indexValue);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(Object dataHolder, List extractors) // for multi attribute indicies
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(dataHolder, extractors);
}
Object mainIndexResult = this.mainIndex.get(dataHolder, extractors);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public Object get(Object dataHolder, Extractor[] extractors) // for multi attribute indicies
{
Object perThreadResult = null;
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
if (perThreadAdded != null)
{
perThreadResult = perThreadAdded.get(dataHolder, extractors);
}
Object mainIndexResult = this.mainIndex.get(dataHolder, extractors);
return this.combineResults(perThreadResult, mainIndexResult, txStorage);
}
public boolean contains(Object keyHolder, Extractor[] extractors, Filter2 filter)
{
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
if (this.getDeletedFromLocalStorage(txStorage) != null)
{
Object candidate = this.get(keyHolder, extractors);
return candidate != null && (filter == null || filter.matches(candidate, keyHolder));
}
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
return (perThreadAdded != null && perThreadAdded.contains(keyHolder, extractors, filter))
|| this.mainIndex.contains(keyHolder, extractors, filter);
}
private NonUniqueIndex getAddedFromLocalStorage(TransactionLocalStorage txStorage)
{
return txStorage == null ? null : txStorage.added;
}
@Override
public void findAndExecute(Object dataHolder, Extractor[] extractors, DoUntilProcedure procedure)
{
TransactionLocalStorage txStorage = (TransactionLocalStorage) perTransactionStorage.get(MithraManagerProvider.getMithraManager().zGetCurrentTransactionWithNoCheck());
Index perThreadAdded = getAddedFromLocalStorage(txStorage);
Index perThreadDeleted = getDeletedFromLocalStorage(txStorage);
if (perThreadAdded == null && perThreadDeleted == null)
{
this.mainIndex.findAndExecute(dataHolder, extractors, procedure);
}
else
{
Object result = this.get(dataHolder, extractors);
if (result instanceof List)
{
List list = (List) result;
boolean done = false;
for(int i=0;i