{
assert node.name.equals("constraint");
String name = node.attributes.get("name");
String typeName = node.attributes.get("constrainttype");
ConstraintType type = ConstraintType.valueOf(typeName);
if (type == ConstraintType.LIMIT) {
int tupleLimit = Integer.parseInt(node.attributes.get("rowslimit"));
if (tupleLimit < 0) {
throw m_compiler.new VoltCompilerException("Invalid constraint limit number '" + tupleLimit + "'");
}
if (tableLimitConstraintCounter.contains(table.getTypeName())) {
throw m_compiler.new VoltCompilerException("Too many table limit constraints for table " + table.getTypeName());
} else {
tableLimitConstraintCounter.add(table.getTypeName());
}
table.setTuplelimit(tupleLimit);
return;
}
if (type == ConstraintType.CHECK) {
String msg = "VoltDB does not enforce check constraints. ";
msg += "Constraint on table " + table.getTypeName() + " will be ignored.";
m_compiler.addWarn(msg);
return;
}
else if (type == ConstraintType.FOREIGN_KEY) {
String msg = "VoltDB does not enforce foreign key references and constraints. ";
msg += "Constraint on table " + table.getTypeName() + " will be ignored.";
m_compiler.addWarn(msg);
return;
}
else if (type == ConstraintType.MAIN) {
// should never see these
assert(false);
}
else if (type == ConstraintType.NOT_NULL) {
// these get handled by table metadata inspection
return;
}
else if (type != ConstraintType.PRIMARY_KEY && type != ConstraintType.UNIQUE) {
throw m_compiler.new VoltCompilerException("Invalid constraint type '" + typeName + "'");
}
// else, create the unique index below
// primary key code is in other places as well
// The constraint is backed by an index, therefore we need to create it
// TODO: We need to be able to use indexes for foreign keys. I am purposely
// leaving those out right now because HSQLDB just makes too many of them.
Constraint catalog_const = table.getConstraints().add(name);
String indexName = node.attributes.get("index");
assert(indexName != null);
// handle replacements from duplicate index pruning
if (indexReplacementMap.containsKey(indexName)) {
indexName = indexReplacementMap.get(indexName);
}
Index catalog_index = indexMap.get(indexName);
// TODO(xin): It seems that indexes have already been set up well, the next whole block is redundant.
// Remove them?
if (catalog_index != null) {
// if the constraint name contains index type hints, exercise them (giant hack)
String constraintNameNoCase = name.toLowerCase();
if (constraintNameNoCase.contains("tree"))
catalog_index.setType(IndexType.BALANCED_TREE.getValue());
if (constraintNameNoCase.contains("hash"))
catalog_index.setType(IndexType.HASH_TABLE.getValue());
catalog_const.setIndex(catalog_index);
catalog_index.setUnique(true);
boolean assumeUnique = Boolean.parseBoolean(node.attributes.get("assumeunique"));
catalog_index.setAssumeunique(assumeUnique);
}
catalog_const.setType(type.getValue());
}