<commit_before>package com.splicemachine.pipeline.writehandler;

import com.carrotsearch.hppc.ObjectOpenHashSet;
import com.splicemachine.hbase.KVPair;
import com.splicemachine.pipeline.api.WriteContext;
import com.splicemachine.pipeline.api.WriteHandler;
import com.splicemachine.pipeline.constraint.Constraint;
import com.splicemachine.pipeline.impl.WriteResult;
import org.apache.hadoop.hbase.NotServingRegionException;
import org.apache.hadoop.hbase.regionserver.HRegion;

import java.io.IOException;
import java.util.List;

/**
 * @author <NAME>
 *         Created on: 4/30/13
 */
public class ConstraintWriteHandler implements WriteHandler {

    private static final WriteResult additiveWriteConflict = WriteResult.failed("Additive WriteConflict");

    private final Constraint localConstraint;
    private boolean failed;
    private ObjectOpenHashSet<KVPair> visitedRows;
    private final WriteResult invalidResult;

    public ConstraintWriteHandler(Constraint localConstraint) {
        this.localConstraint = localConstraint;
        this.invalidResult = new WriteResult(WriteResult.convertType(localConstraint.getType()), localConstraint.getConstraintContext());
    }

    @Override
    public void next(KVPair mutation, WriteContext ctx) {
        if (visitedRows == null) {
            visitedRows = new ObjectOpenHashSet<>();
        }
        if (failed) {
            ctx.notRun(mutation);
        }
        if (!HRegion.rowIsInRange(ctx.getRegion().getRegionInfo(), mutation.getRowKey())) {
            //we can't check the mutation, it'll explode
            ctx.failed(mutation, WriteResult.wrongRegion());
            return;
        }
        try {
            Constraint.Result validate = localConstraint.validate(mutation, ctx.getTxn(), ctx.getCoprocessorEnvironment(), visitedRows);
            switch (validate) {
                case FAILURE:
                    ctx.result(mutation, invalidResult);
                    break;
                case ADDITIVE_WRITE_CONFLICT:
                    ctx.result(mutation, additiveWriteConflict);
                    break;
                default:
                    ctx.sendUpstream(mutation);
            }
            visitedRows.add(mutation);
        } catch (NotServingRegionException nsre) {
            ctx.failed(mutation, WriteResult.notServingRegion());
            failed = true;
        } catch (Exception e) {
            failed = true;
            ctx.failed(mutation, WriteResult.failed(e.getClass().getSimpleName() + ":" + e.getMessage()));
        }
    }<commit_msg>DB-2983 Fix hashset resizing by passing in expected writes<commit_after>package com.splicemachine.pipeline.writehandler;

import com.carrotsearch.hppc.ObjectOpenHashSet;
import com.splicemachine.hbase.KVPair;
import com.splicemachine.pipeline.api.WriteContext;
import com.splicemachine.pipeline.api.WriteHandler;
import com.splicemachine.pipeline.constraint.Constraint;
import com.splicemachine.pipeline.impl.WriteResult;

import org.apache.hadoop.hbase.NotServingRegionException;
import org.apache.hadoop.hbase.regionserver.HRegion;

import java.io.IOException;
import java.util.List;

/**
 * @author <NAME>
 *         Created on: 4/30/13
 */
public class ConstraintWriteHandler implements WriteHandler {

    private static final WriteResult additiveWriteConflict = WriteResult.failed("Additive WriteConflict");

    private final Constraint localConstraint;
    private boolean failed;
    private ObjectOpenHashSet<KVPair> visitedRows;
    private final WriteResult invalidResult;
    private int expectedWrites;

    public ConstraintWriteHandler(Constraint localConstraint, int expectedWrites) {
    	this.expectedWrites = expectedWrites;
    	this.localConstraint = localConstraint;
        this.invalidResult = new WriteResult(WriteResult.convertType(localConstraint.getType()), localConstraint.getConstraintContext());
    }

    @Override
    public void next(KVPair mutation, WriteContext ctx) {
        if (visitedRows == null) {        	
        	int initialCapacity = (int) Math.ceil(expectedWrites/0.9f);
            visitedRows = new ObjectOpenHashSet<>(initialCapacity,0.9f);
        }
        if (failed) {
            ctx.notRun(mutation);
        }
        if (!HRegion.rowIsInRange(ctx.getRegion().getRegionInfo(), mutation.getRowKey())) {
            //we can't check the mutation, it'll explode
            ctx.failed(mutation, WriteResult.wrongRegion());
            return;
        }
        try {
            Constraint.Result validate = localConstraint.validate(mutation, ctx.getTxn(), ctx.getCoprocessorEnvironment(), visitedRows);
            switch (validate) {
                case FAILURE:
                    ctx.result(mutation, invalidResult);
                    break;
                case ADDITIVE_WRITE_CONFLICT:
                    ctx.result(mutation, additiveWriteConflict);
                    break;
                default:
                    ctx.sendUpstream(mutation);
            }
            visitedRows.add(mutation);
        } catch (NotServingRegionException nsre) {
            ctx.failed(mutation, WriteResult.notServingRegion());
            failed = true;
        } catch (Exception e) {
            failed = true;
            ctx.failed(mutation, WriteResult.failed(e.getClass().getSimpleName() + ":" + e.getMessage()));
        }
    }
{'fraction_non_alphanumeric': 0.09469555472543892, 'fraction_numerical': 0.0033619723571161747, 'mean_word_length': 2.731707317073171, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2567121', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1408, 'n_words': 275}