com.gs.fw.common.mithra.overlap.OverlapFixer 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.overlap;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.fw.common.mithra.MithraDataObject;
import com.gs.fw.common.mithra.MithraManagerProvider;
import com.gs.fw.common.mithra.MithraObjectPortal;
import com.gs.fw.common.mithra.MithraTransaction;
import com.gs.fw.common.mithra.MithraTransactionalObject;
import com.gs.fw.common.mithra.TransactionalCommand;
import com.gs.fw.common.mithra.attribute.AsOfAttribute;
import com.gs.fw.common.mithra.attribute.Attribute;
import com.gs.fw.common.mithra.finder.RelatedFinder;
import com.gs.fw.common.mithra.finder.orderby.OrderBy;
import com.gs.fw.common.mithra.transaction.BatchDeleteOperation;
import com.gs.fw.common.mithra.transaction.BatchInsertOperation;
import com.gs.fw.common.mithra.transaction.InTransactionDatedTransactionalObject;
import com.gs.fw.common.mithra.util.dbextractor.MilestoneRectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OverlapFixer implements OverlapHandler
{
private static final Logger LOGGER = LoggerFactory.getLogger(OverlapFixer.class.getName());
private final int batchSize;
private BatchInsertOperation insertBatch;
private BatchDeleteOperation deleteBatch;
private long startTime;
private int totalInsertCount;
private int totalDeleteCount;
public OverlapFixer()
{
this(1000);
}
public OverlapFixer(int batchSize)
{
this.batchSize = batchSize;
}
@Override
public void overlapProcessingStarted(Object connectionManager, String mithraClassName)
{
if (this.getPendingOperationCount() > 0)
{
throw new IllegalStateException("There are pending batch operations!");
}
LOGGER.info("Fixing overlaps for " + mithraClassName);
this.startTime = System.currentTimeMillis();
}
private int getPendingOperationCount()
{
return this.getInsertBatchSize() + this.getDeleteBatchSize();
}
private int getInsertBatchSize()
{
return this.insertBatch == null ? 0 : this.insertBatch.getTotalOperationsSize();
}
private int getDeleteBatchSize()
{
return this.deleteBatch == null ? 0 : this.deleteBatch.getTotalOperationsSize();
}
@Override
public void overlapProcessingFinished(Object connectionManager, String mithraClassName)
{
if (this.getPendingOperationCount() > 0)
{
this.executePendingOperations();
}
long time = System.currentTimeMillis() - this.startTime;
LOGGER.info("Fixed overlaps for " + mithraClassName + " in " + time + "ms (deletes=" + this.totalDeleteCount + ", inserts=" + this.totalInsertCount + ')');
}
private void executePendingOperations()
{
int deleteCount = this.getDeleteBatchSize();
int insertCount = this.getInsertBatchSize();
MithraManagerProvider.getMithraManager().executeTransactionalCommand(new TransactionalCommand