Search in sources :

Example 1 with Constraint

use of org.apache.accumulo.core.data.constraints.Constraint in project accumulo by apache.

the class ConstraintCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
    final String tableName;
    final String namespace;
    if (cl.hasOption(namespaceOpt.getOpt())) {
        namespace = cl.getOptionValue(namespaceOpt.getOpt());
    } else {
        namespace = null;
    }
    if (cl.hasOption(OptUtil.tableOpt().getOpt()) || !shellState.getTableName().isEmpty()) {
        tableName = OptUtil.getTableOpt(cl, shellState);
    } else {
        tableName = null;
    }
    int i;
    switch(OptUtil.getAldOpt(cl)) {
        case ADD:
            for (String constraint : cl.getArgs()) {
                if (namespace != null) {
                    if (!shellState.getAccumuloClient().namespaceOperations().testClassLoad(namespace, constraint, Constraint.class.getName())) {
                        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + constraint + " as type " + Constraint.class.getName());
                    }
                    i = shellState.getAccumuloClient().namespaceOperations().addConstraint(namespace, constraint);
                    shellState.getWriter().println("Added constraint " + constraint + " to namespace " + namespace + " with number " + i);
                } else if (tableName != null && !tableName.isEmpty()) {
                    if (!shellState.getAccumuloClient().tableOperations().testClassLoad(tableName, constraint, Constraint.class.getName())) {
                        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + constraint + " as type " + Constraint.class.getName());
                    }
                    i = shellState.getAccumuloClient().tableOperations().addConstraint(tableName, constraint);
                    shellState.getWriter().println("Added constraint " + constraint + " to table " + tableName + " with number " + i);
                } else {
                    throw new IllegalArgumentException("Please specify either a table or a namespace");
                }
            }
            break;
        case DELETE:
            for (String constraint : cl.getArgs()) {
                i = Integer.parseInt(constraint);
                if (namespace != null) {
                    shellState.getAccumuloClient().namespaceOperations().removeConstraint(namespace, i);
                    shellState.getWriter().println("Removed constraint " + i + " from namespace " + namespace);
                } else if (tableName != null) {
                    shellState.getAccumuloClient().tableOperations().removeConstraint(tableName, i);
                    shellState.getWriter().println("Removed constraint " + i + " from table " + tableName);
                } else {
                    throw new IllegalArgumentException("Please specify either a table or a namespace");
                }
            }
            break;
        case LIST:
            if (namespace != null) {
                for (Entry<String, Integer> property : shellState.getAccumuloClient().namespaceOperations().listConstraints(namespace).entrySet()) {
                    shellState.getWriter().println(property.toString());
                }
            } else if (tableName != null) {
                for (Entry<String, Integer> property : shellState.getAccumuloClient().tableOperations().listConstraints(tableName).entrySet()) {
                    shellState.getWriter().println(property.toString());
                }
            } else {
                throw new IllegalArgumentException("Please specify either a table or a namespace");
            }
    }
    return 0;
}
Also used : Entry(java.util.Map.Entry) Constraint(org.apache.accumulo.core.data.constraints.Constraint) ShellCommandException(org.apache.accumulo.shell.ShellCommandException) Constraint(org.apache.accumulo.core.data.constraints.Constraint)

Example 2 with Constraint

use of org.apache.accumulo.core.data.constraints.Constraint in project accumulo by apache.

the class ConstraintChecker method check.

public Violations check(Environment env, Mutation m) {
    if (!tabletContains(env.getTablet(), new ComparableBytes(m.getRow()))) {
        Violations violations = new Violations();
        ConstraintViolationSummary cvs = new ConstraintViolationSummary(SystemConstraint.class.getName(), (short) -1, "Mutation outside of tablet extent", 1);
        violations.add(cvs);
        // do not bother with further checks since this mutation does not go with this tablet
        return violations;
    }
    // violations is intentionally initialized as null for performance
    Violations violations = null;
    for (Constraint constraint : getConstraints()) {
        try {
            List<Short> violationCodes = constraint.check(env, m);
            if (violationCodes != null) {
                String className = constraint.getClass().getName();
                for (Short vcode : violationCodes) {
                    violations = addViolation(violations, new ConstraintViolationSummary(className, vcode, constraint.getViolationDescription(vcode), 1));
                }
            }
        } catch (Exception e) {
            log.warn("CONSTRAINT FAILED : {}", e.getMessage(), e);
            // constraint failed in some way, do not allow mutation to pass
            short vcode;
            String msg;
            if (e instanceof NullPointerException) {
                vcode = -1;
                msg = "threw NullPointerException";
            } else if (e instanceof ArrayIndexOutOfBoundsException) {
                vcode = -2;
                msg = "threw ArrayIndexOutOfBoundsException";
            } else if (e instanceof NumberFormatException) {
                vcode = -3;
                msg = "threw NumberFormatException";
            } else {
                vcode = -100;
                msg = "threw some Exception";
            }
            violations = addViolation(violations, new ConstraintViolationSummary(constraint.getClass().getName(), vcode, "CONSTRAINT FAILED : " + msg, 1));
        }
    }
    return violations;
}
Also used : Violations(org.apache.accumulo.core.constraints.Violations) Constraint(org.apache.accumulo.core.data.constraints.Constraint) ComparableBytes(org.apache.accumulo.core.dataImpl.ComparableBytes) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary)

Example 3 with Constraint

use of org.apache.accumulo.core.data.constraints.Constraint in project accumulo by apache.

the class ConstraintCheckerTest method makeFailureConstraint.

private Constraint makeFailureConstraint() {
    Constraint c = createMock(Constraint.class);
    short code1 = 2;
    short code2 = 4;
    List<Short> vCodes = List.of(code1, code2);
    expect(c.getViolationDescription(code1)).andReturn("ViolationCode1");
    expect(c.getViolationDescription(code2)).andReturn("ViolationCode2");
    expect(c.check(env, m)).andReturn(vCodes);
    replay(c);
    return c;
}
Also used : Constraint(org.apache.accumulo.core.data.constraints.Constraint)

Example 4 with Constraint

use of org.apache.accumulo.core.data.constraints.Constraint in project accumulo by apache.

the class ConstraintCheckerTest method makeExceptionConstraint.

private Constraint makeExceptionConstraint() {
    Constraint c = createMock(Constraint.class);
    expect(c.check(env, m)).andThrow(new RuntimeException("some exception"));
    replay(c);
    return c;
}
Also used : Constraint(org.apache.accumulo.core.data.constraints.Constraint)

Example 5 with Constraint

use of org.apache.accumulo.core.data.constraints.Constraint in project accumulo by apache.

the class ConstraintCheckerTest method makeSuccessConstraint.

private Constraint makeSuccessConstraint() {
    Constraint c = createMock(Constraint.class);
    // no violations
    expect(c.check(env, m)).andReturn(null);
    replay(c);
    return c;
}
Also used : Constraint(org.apache.accumulo.core.data.constraints.Constraint)

Aggregations

Constraint (org.apache.accumulo.core.data.constraints.Constraint)5 Entry (java.util.Map.Entry)1 Violations (org.apache.accumulo.core.constraints.Violations)1 ConstraintViolationSummary (org.apache.accumulo.core.data.ConstraintViolationSummary)1 ComparableBytes (org.apache.accumulo.core.dataImpl.ComparableBytes)1 ShellCommandException (org.apache.accumulo.shell.ShellCommandException)1