All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.fw.common.mithra.transaction.BatchInsertOperation Maven / Gradle / Ivy

There is a newer version: 18.1.0
Show newest version
/*
 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.transaction;

import com.gs.fw.common.mithra.MithraDatabaseException;
import com.gs.fw.common.mithra.MithraManagerProvider;
import com.gs.fw.common.mithra.MithraObjectPortal;
import com.gs.fw.common.mithra.MithraTransactionalObject;
import com.gs.fw.common.mithra.cache.FullUniqueIndex;

import com.gs.collections.impl.list.mutable.FastList;
import java.util.List;



public class BatchInsertOperation extends TransactionOperation
{

    private final FastList objects;
    private transient FullUniqueIndex index;

    public BatchInsertOperation(MithraTransactionalObject first, MithraTransactionalObject second, MithraObjectPortal portal)
    {
        super(first, portal);
        objects = new FastList();
        addObject(first);
        addObject(second);
    }

    public BatchInsertOperation(MithraTransactionalObject mithraObject, List allObjects, MithraObjectPortal portal)
    {
        super(mithraObject, portal);
        objects = new FastList(allObjects);
        addObject(mithraObject);
    }

    private void addAll(TransactionOperation op)
    {
        List allObjects = op.getAllObjects();
        this.objects.addAll(allObjects);
        if (index != null) index.addAll(allObjects);
    }

    private void addObject(MithraTransactionalObject first)
    {
        objects.add(first);
        if (index != null) index.put(first);
    }

    @Override
    protected FullUniqueIndex getIndexedObjects()
    {
        if (index == null)
        {
            index = createFullUniqueIndex(objects);
        }
        return index;
    }

    @Override
    public int getTotalOperationsSize()
    {
        return objects.size();
    }

    @Override
    public void execute() throws MithraDatabaseException
    {
        if (objects.size() > 0)
        {
            int bulkInsertThreshold = MithraManagerProvider.getMithraManager().getCurrentTransaction().getBulkInsertThreshold();
            this.getPortal().getMithraObjectPersister().batchInsert(objects, bulkInsertThreshold);
            for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy