/**
 * Copyright (C) 2009-2013 FoundationDB, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.foundationdb.qp.rowtype;

import com.foundationdb.ais.model.TableName;
import com.foundationdb.ais.model.Table;
import com.foundationdb.qp.row.Row;
import com.foundationdb.server.error.InvalidOperationException;
import com.foundationdb.server.error.NotNullViolationException;

import java.util.BitSet;

public class TableRowChecker implements ConstraintChecker
{
    @Override
    public void checkConstraints(Row row) throws InvalidOperationException
    {
        for (int f = 0; f < fields; f++) {
            if (notNull.get(f) && isNull(row, f)) {
                TableName tableName = table.getName();
                throw new NotNullViolationException(tableName.getSchemaName(),
                                                    tableName.getTableName(),
                                                    table.getColumnsIncludingInternal().get(f).getName());
            }
        }
    }

    private boolean isNull(Row row, int f) {
        return row.value(f).isNull();
    }

    public TableRowChecker(RowType rowType)
    {
        assert rowType.hasTable() : rowType;
        fields = rowType.nFields();
        table = rowType.table();
        notNull = table.notNull();
    }

    public TableRowChecker(Table table)
    {
        this.table = table;
        fields = table.getColumnsIncludingInternal().size();
        notNull = table.notNull();
    }

    private final Table table;
    private final int fields;
    private final BitSet notNull;
}

@@ -1151,41 +1151,8 @@
 row)
- throws InvalidOperationException
 %0A   
@@ -1165,17 +1165,16 @@
      for
- 
 (int f =
@@ -1178,77 +1178,239 @@
 f = 
-0; f %3C fields; f++) %7B%0A            if (notNull.get(f) && isNull(row, f
+notNull.nextSetBit(0); f %3E= 0; f = notNull.nextSetBit(f+1)) %7B%0A            // Delicate: Hidden columns aren't populated until much later.%0A            if(isNull(row, f) && !table.getColumnsIncludingInternal().get(f).isAkibanPKColumn(
 )) %7B
@@ -1912,80 +1912,85 @@
     
-assert rowType.has
+this(get
 Table(
-) : 
 rowType
+))
 ;%0A    
+%7D%0A%0A
     
-fields = rowType.nFields();
+public TableRowChecker(Table table)%0A    %7B
 %0A   
@@ -1994,24 +1994,29 @@
         
+this.
 table = 
 rowType.
@@ -2011,23 +2011,13 @@
 e = 
-rowType.
 table
-()
 ;%0A  
@@ -2014,32 +2014,37 @@
  table;%0A        
+this.
 notNull = table.
@@ -2070,169 +2070,129 @@
    p
-ublic TableRowChecker(Table table)%0A    %7B%0A        this.table = table;%0A        fields = table.getColumnsIncludingInternal().size();%0A        notNull = table.notNull
+rivate static Table getTable(RowType rowType) %7B%0A        assert rowType.hasTable() : rowType;%0A        return rowType.table
 ();%0A
@@ -2233,38 +2233,8 @@
 le;%0A
-    private final int fields;%0A
     

Streamline TableRowChecker