use of com.predic8.schema.Schema in project core by s4.
the class DefaultPartitioner method partition.
public List<CompoundKeyInfo> partition(String streamName, List<List<String>> compoundKeyNames, Object event, int partitionCount) {
if (streamName != null && streamNameSet != null && !streamNameSet.contains(streamName)) {
return null;
}
// Some event types that need special handling
if (event instanceof io.s4.message.Request) {
// construct key from request's target
io.s4.message.Request r = (io.s4.message.Request) event;
return r.partition(hasher, delimiter, partitionCount);
} else if (event instanceof io.s4.message.Response) {
// partition id is encoded in Response, so use it directly.
io.s4.message.Response r = (io.s4.message.Response) event;
return r.partition(partitionCount);
} else if (compoundKeyNames == null) {
// if compoundKeyNames is null, then assign to a random partition.
return partitionRandom(partitionCount);
}
// have to compute key value and
// partition based on hash of that value
Schema schema = schemaContainer.getSchema(event.getClass());
if (debug) {
System.out.println(schema);
}
List<CompoundKeyInfo> partitionInfoList = new ArrayList<CompoundKeyInfo>();
// fast path for single top-level key
if (fastPath || (compoundKeyNames.size() == 1 && compoundKeyNames.get(0).size() == 1)) {
String simpleKeyName = compoundKeyNames.get(0).get(0);
if (debug) {
System.out.println("Using fast path!");
}
fastPath = true;
KeyInfo keyInfo = new KeyInfo();
Property property = schema.getProperties().get(simpleKeyName);
if (property == null) {
return null;
}
Object value = null;
try {
value = property.getGetterMethod().invoke(event);
} catch (Exception e) {
if (debug) {
e.printStackTrace();
}
}
if (value == null) {
if (debug) {
System.out.println("Fast path: Null value encountered");
}
return null;
}
keyInfo.addElementToPath(simpleKeyName);
String stringValue = String.valueOf(value);
keyInfo.setValue(stringValue);
CompoundKeyInfo partitionInfo = new CompoundKeyInfo();
partitionInfo.addKeyInfo(keyInfo);
int partitionId = (int) (hasher.hash(stringValue) % partitionCount);
partitionInfo.setPartitionId(partitionId);
partitionInfo.setCompoundValue(stringValue);
partitionInfoList.add(partitionInfo);
if (debug) {
System.out.printf("Value %s, partition id %d\n", stringValue, partitionInfo.getPartitionId());
}
return partitionInfoList;
}
List<List<KeyInfo>> valueLists = new ArrayList<List<KeyInfo>>();
int maxSize = 0;
for (List<String> simpleKeyPath : compoundKeyNames) {
List<KeyInfo> keyInfoList = new ArrayList<KeyInfo>();
KeyInfo keyInfo = new KeyInfo();
keyInfoList = getKeyValues(event, schema, simpleKeyPath, 0, keyInfoList, keyInfo);
if (keyInfoList == null || keyInfoList.size() == 0) {
if (debug) {
System.out.println("Null value encountered");
}
// do no partitioning if any simple key's value
return null;
// resolves to null
}
valueLists.add(keyInfoList);
maxSize = Math.max(maxSize, keyInfoList.size());
if (debug) {
printKeyInfoList(keyInfoList);
}
}
for (int i = 0; i < maxSize; i++) {
String compoundValue = "";
CompoundKeyInfo partitionInfo = new CompoundKeyInfo();
for (List<KeyInfo> keyInfoList : valueLists) {
if (i < keyInfoList.size()) {
compoundValue += (compoundValue.length() > 0 ? delimiter : "") + keyInfoList.get(i).getValue();
partitionInfo.addKeyInfo(keyInfoList.get(i));
} else {
compoundValue += (compoundValue.length() > 0 ? delimiter : "") + keyInfoList.get(keyInfoList.size() - 1).getValue();
partitionInfo.addKeyInfo(keyInfoList.get(keyInfoList.size() - 1));
}
}
// get the partition id
int partitionId = (int) (hasher.hash(compoundValue) % partitionCount);
partitionInfo.setPartitionId(partitionId);
partitionInfo.setCompoundValue(compoundValue);
partitionInfoList.add(partitionInfo);
if (debug) {
System.out.printf("Value %s, partition id %d\n", compoundValue, partitionInfo.getPartitionId());
}
}
return partitionInfoList;
}
use of com.predic8.schema.Schema in project core by s4.
the class LoadGenerator method createEventTypeInfo.
@SuppressWarnings("unchecked")
public void createEventTypeInfo(JSONObject classInfo) {
String className = "";
try {
for (Iterator it = classInfo.keys(); it.hasNext(); ) {
className = (String) it.next();
JSONObject jsonEventTypeInfo = classInfo.getJSONObject(className);
int classIndex = (Integer) jsonEventTypeInfo.getInt("classIndex");
String streamName = jsonEventTypeInfo.getString("streamName");
Class clazz = Class.forName(className);
Schema schema = new Schema(clazz);
eventTypeInfoMap.put(classIndex, new EventTypeInfo(schema, streamName));
}
} catch (JSONException je) {
je.printStackTrace();
} catch (ClassNotFoundException cnfe) {
System.err.println("Count not locate class " + className);
}
}
use of com.predic8.schema.Schema in project ignite by apache.
the class GridSqlQueryParser method parseAddColumn.
/**
* Parse {@code ALTER TABLE ... ADD COLUMN} statement.
* @param addCol H2 statement.
* @return Grid SQL statement.
*
* @see <a href="http://www.h2database.com/html/grammar.html#alter_table_add"></a>
*/
private GridSqlStatement parseAddColumn(AlterTableAlterColumn addCol) {
assert addCol.getType() == CommandInterface.ALTER_TABLE_ADD_COLUMN;
if (ALTER_COLUMN_BEFORE_COL.get(addCol) != null)
throw new IgniteSQLException("BEFORE keyword is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (ALTER_COLUMN_AFTER_COL.get(addCol) != null)
throw new IgniteSQLException("AFTER keyword is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
if (ALTER_COLUMN_FIRST.get(addCol))
throw new IgniteSQLException("FIRST keyword is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
GridSqlAlterTableAddColumn res = new GridSqlAlterTableAddColumn();
ArrayList<Column> h2NewCols = ALTER_COLUMN_NEW_COLS.get(addCol);
GridSqlColumn[] gridNewCols = new GridSqlColumn[h2NewCols.size()];
for (int i = 0; i < h2NewCols.size(); i++) {
Column col = h2NewCols.get(i);
if (col.getDefaultExpression() != null) {
throw new IgniteSQLException("ALTER TABLE ADD COLUMN with DEFAULT value is not supported " + "[col=" + col.getName() + ']', IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
gridNewCols[i] = parseColumn(h2NewCols.get(i));
}
res.columns(gridNewCols);
if (gridNewCols.length == 1)
res.ifNotExists(ALTER_COLUMN_IF_NOT_EXISTS.get(addCol));
res.ifTableExists(ALTER_COLUMN_IF_TBL_EXISTS.get(addCol));
Schema schema = SCHEMA_COMMAND_SCHEMA.get(addCol);
res.schemaName(schema.getName());
res.tableName(ALTER_COLUMN_TBL_NAME.get(addCol));
return res;
}
use of com.predic8.schema.Schema in project ignite by apache.
the class GridSqlQueryParser method parseDropTable.
/**
* Parse {@code DROP TABLE} statement.
*
* @param dropTbl {@code DROP TABLE} statement.
* @see <a href="http://h2database.com/html/grammar.html#drop_table">H2 {@code DROP TABLE} spec.</a>
*/
private GridSqlDropTable parseDropTable(DropTable dropTbl) {
GridSqlDropTable res = new GridSqlDropTable();
Schema schema = SCHEMA_COMMAND_SCHEMA.get(dropTbl);
res.schemaName(schema.getName());
res.ifExists(DROP_TABLE_IF_EXISTS.get(dropTbl));
res.tableName(DROP_TABLE_NAME.get(dropTbl));
return res;
}
use of com.predic8.schema.Schema in project ignite by apache.
the class GridSqlQueryParser method parseCreateTable.
/**
* Parse {@code CREATE TABLE} statement.
*
* @param createTbl {@code CREATE TABLE} statement.
* @see <a href="http://h2database.com/html/grammar.html#create_table">H2 {@code CREATE TABLE} spec.</a>
*/
private GridSqlCreateTable parseCreateTable(CreateTable createTbl) {
GridSqlCreateTable res = new GridSqlCreateTable();
res.templateName(QueryUtils.TEMPLATE_PARTITIONED);
Query qry = CREATE_TABLE_QUERY.get(createTbl);
if (qry != null) {
throw new IgniteSQLException("CREATE TABLE ... AS ... syntax is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
List<DefineCommand> constraints = CREATE_TABLE_CONSTRAINTS.get(createTbl);
if (F.isEmpty(constraints)) {
throw new IgniteSQLException("No PRIMARY KEY defined for CREATE TABLE", IgniteQueryErrorCode.PARSING);
}
if (constraints.size() > 1) {
throw new IgniteSQLException("Too many constraints - only PRIMARY KEY is supported for CREATE TABLE", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
DefineCommand constraint = constraints.get(0);
if (!(constraint instanceof AlterTableAddConstraint)) {
throw new IgniteSQLException("Unsupported type of constraint for CREATE TABLE - only PRIMARY KEY " + "is supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
AlterTableAddConstraint alterTbl = (AlterTableAddConstraint) constraint;
if (alterTbl.getType() != Command.ALTER_TABLE_ADD_CONSTRAINT_PRIMARY_KEY) {
throw new IgniteSQLException("Unsupported type of constraint for CREATE TABLE - only PRIMARY KEY " + "is supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
Schema schema = SCHEMA_COMMAND_SCHEMA.get(createTbl);
res.schemaName(schema.getName());
CreateTableData data = CREATE_TABLE_DATA.get(createTbl);
if (data.globalTemporary) {
throw new IgniteSQLException("GLOBAL TEMPORARY keyword is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
if (data.temporary) {
throw new IgniteSQLException("TEMPORARY keyword is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
if (data.isHidden) {
throw new IgniteSQLException("HIDDEN keyword is not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
if (!data.persistIndexes) {
throw new IgniteSQLException("MEMORY and NOT PERSISTENT keywords are not supported", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
LinkedHashMap<String, GridSqlColumn> cols = new LinkedHashMap<>(data.columns.size());
for (Column col : data.columns) {
if (cols.put(col.getName(), parseColumn(col)) != null)
throw new IgniteSQLException("Duplicate column name: " + col.getName(), IgniteQueryErrorCode.PARSING);
}
if (cols.containsKey(QueryUtils.KEY_FIELD_NAME.toUpperCase()) || cols.containsKey(QueryUtils.VAL_FIELD_NAME.toUpperCase())) {
throw new IgniteSQLException("Direct specification of _KEY and _VAL columns is forbidden", IgniteQueryErrorCode.PARSING);
}
IndexColumn[] pkIdxCols = CREATE_TABLE_PK.get(createTbl);
if (F.isEmpty(pkIdxCols))
throw new AssertionError("No PRIMARY KEY columns specified");
LinkedHashSet<String> pkCols = new LinkedHashSet<>();
for (IndexColumn pkIdxCol : pkIdxCols) {
GridSqlColumn gridCol = cols.get(pkIdxCol.columnName);
if (gridCol == null) {
throw new IgniteSQLException("PRIMARY KEY column is not defined: " + pkIdxCol.columnName, IgniteQueryErrorCode.PARSING);
}
pkCols.add(gridCol.columnName());
}
int keyColsNum = pkCols.size();
int valColsNum = cols.size() - keyColsNum;
if (valColsNum == 0) {
throw new IgniteSQLException("Table must have at least one non PRIMARY KEY column.", IgniteQueryErrorCode.UNSUPPORTED_OPERATION);
}
res.columns(cols);
res.primaryKeyColumns(pkCols);
res.tableName(data.tableName);
res.ifNotExists(CREATE_TABLE_IF_NOT_EXISTS.get(createTbl));
List<String> extraParams = data.tableEngineParams != null ? new ArrayList<String>() : null;
if (data.tableEngineParams != null)
for (String s : data.tableEngineParams) extraParams.addAll(F.asList(s.split(",")));
res.params(extraParams);
if (!F.isEmpty(extraParams)) {
Map<String, String> params = new HashMap<>();
for (String p : extraParams) {
String[] parts = p.split(PARAM_NAME_VALUE_SEPARATOR);
if (parts.length > 2) {
throw new IgniteSQLException("Invalid parameter (key[=value] expected): " + p, IgniteQueryErrorCode.PARSING);
}
String name = parts[0].trim().toUpperCase();
String val = parts.length > 1 ? parts[1].trim() : null;
if (F.isEmpty(name)) {
throw new IgniteSQLException("Invalid parameter (key[=value] expected): " + p, IgniteQueryErrorCode.PARSING);
}
if (params.put(name, val) != null)
throw new IgniteSQLException("Duplicate parameter: " + p, IgniteQueryErrorCode.PARSING);
}
for (Map.Entry<String, String> e : params.entrySet()) processExtraParam(e.getKey(), e.getValue(), res);
}
// Process key wrapping.
Boolean wrapKey = res.wrapKey();
if (wrapKey != null && !wrapKey) {
if (keyColsNum > 1) {
throw new IgniteSQLException(PARAM_WRAP_KEY + " cannot be false when composite primary key exists.", IgniteQueryErrorCode.PARSING);
}
if (!F.isEmpty(res.keyTypeName())) {
throw new IgniteSQLException(PARAM_WRAP_KEY + " cannot be false when " + PARAM_KEY_TYPE + " is set.", IgniteQueryErrorCode.PARSING);
}
}
boolean wrapKey0 = (res.wrapKey() != null && res.wrapKey()) || !F.isEmpty(res.keyTypeName()) || keyColsNum > 1;
res.wrapKey(wrapKey0);
// Process value wrapping.
Boolean wrapVal = res.wrapValue();
if (wrapVal != null && !wrapVal) {
if (valColsNum > 1) {
throw new IgniteSQLException(PARAM_WRAP_VALUE + " cannot be false when multiple non-primary key " + "columns exist.", IgniteQueryErrorCode.PARSING);
}
if (!F.isEmpty(res.valueTypeName())) {
throw new IgniteSQLException(PARAM_WRAP_VALUE + " cannot be false when " + PARAM_VAL_TYPE + " is set.", IgniteQueryErrorCode.PARSING);
}
res.wrapValue(false);
} else
// By default value is always wrapped to allow for ALTER TABLE ADD COLUMN commands.
res.wrapValue(true);
if (!F.isEmpty(res.valueTypeName()) && F.eq(res.keyTypeName(), res.valueTypeName())) {
throw new IgniteSQLException("Key and value type names " + "should be different for CREATE TABLE: " + res.valueTypeName(), IgniteQueryErrorCode.PARSING);
}
if (res.affinityKey() == null) {
LinkedHashSet<String> pkCols0 = res.primaryKeyColumns();
if (!F.isEmpty(pkCols0) && pkCols0.size() == 1 && wrapKey0)
res.affinityKey(pkCols0.iterator().next());
}
return res;
}
Aggregations