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

it.tidalwave.integritychecker.archive.spi.CommitAfterRecordBatch Maven / Gradle / Ivy

/*
 * #%L
 * *********************************************************************************************************************
 * 
 * SolidBlue - open source safe data
 * http://solidblue.tidalwave.it - hg clone https://bitbucket.org/tidalwave/solidblue-src
 * %%
 * Copyright (C) 2011 - 2014 Tidalwave s.a.s. (http://tidalwave.it)
 * %%
 * *********************************************************************************************************************
 * 
 * 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.
 * 
 * *********************************************************************************************************************
 * 
 * $Id$
 * 
 * *********************************************************************************************************************
 * #L%
 */
package it.tidalwave.integritychecker.archive.spi;

import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;

/***********************************************************************************************************************
 *
 * A commit policy that performs a commit after a configurable number of records have been imported. It also commits
 * after a file has been imported.
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@Slf4j
public class CommitAfterRecordBatch extends CommitPolicySupport
  {
    @Nonnegative
    private final int batchSize;
    
    private final ThreadLocal pendingRecordCount = new ThreadLocal()
      {
        @Override
        protected AtomicInteger initialValue() 
          {
            return new AtomicInteger(0);
          }
      };
    
    public CommitAfterRecordBatch (final @Nonnull Committer committer, final @Nonnegative int batchSize)
      {
        super(committer);
        this.batchSize = batchSize;
      }

    @Override
    public void afterRecord() 
      {
//         log.debug("{} vs {}", pendingRecordCount, batchSize);
        if (pendingRecordCount.get().incrementAndGet() >= batchSize)
          {
            pendingRecordCount.get().set(0);
            commit();
          }
      }
    
    @Override
    public void afterFile() 
      {
        pendingRecordCount.get().set(0);
        commit();
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy