use of herddb.model.Column in project herddb by diennea.
the class TableSpaceManager method createTable.
private StatementExecutionResult createTable(CreateTableStatement statement, Transaction transaction, StatementEvaluationContext context) throws StatementExecutionException {
boolean lockAcquired = false;
if (context.getTableSpaceLock() == 0) {
long lockStamp = acquireWriteLock(statement);
context.setTableSpaceLock(lockStamp);
lockAcquired = true;
}
try {
if (tables.containsKey(statement.getTableDefinition().name)) {
if (statement.isIfExistsClause()) {
return new DDLStatementExecutionResult(transaction != null ? transaction.transactionId : 0);
}
throw new TableAlreadyExistsException(statement.getTableDefinition().name);
}
for (Index additionalIndex : statement.getAdditionalIndexes()) {
AbstractIndexManager exists = indexes.get(additionalIndex.name);
if (exists != null) {
LOGGER.log(Level.INFO, "Error while creating index " + additionalIndex.name + ", there is already an index " + exists.getIndex().name + " on table " + exists.getIndex().table);
throw new IndexAlreadyExistsException(additionalIndex.name);
}
}
Table table = statement.getTableDefinition();
// validate foreign keys
if (table.foreignKeys != null) {
for (ForeignKeyDef def : table.foreignKeys) {
AbstractTableManager parentTableManager = null;
for (AbstractTableManager ab : tables.values()) {
if (ab.getTable().uuid.equals(def.parentTableId)) {
parentTableManager = ab;
break;
}
}
if (parentTableManager == null) {
throw new StatementExecutionException("Table " + def.parentTableId + " does not exist in tablespace " + tableSpaceName);
}
Table parentTable = parentTableManager.getTable();
int i = 0;
for (String col : def.columns) {
Column column = table.getColumn(col);
Column parentColumn = parentTable.getColumn(def.parentTableColumns[i]);
if (column == null) {
throw new StatementExecutionException("Cannot find column " + col);
}
if (parentColumn == null) {
throw new StatementExecutionException("Cannot find column " + def.parentTableColumns[i]);
}
if (!ColumnTypes.sameRawDataType(column.type, parentColumn.type)) {
throw new StatementExecutionException("Column " + table.name + "." + column.name + " is not the same tyepe of column " + parentTable.name + "." + parentColumn.name);
}
i++;
}
}
}
LogEntry entry = LogEntryFactory.createTable(statement.getTableDefinition(), transaction);
CommitLogResult pos = log.log(entry, entry.transactionId <= 0);
apply(pos, entry, false);
for (Index additionalIndex : statement.getAdditionalIndexes()) {
LogEntry index_entry = LogEntryFactory.createIndex(additionalIndex, transaction);
CommitLogResult index_pos = log.log(index_entry, index_entry.transactionId <= 0);
apply(index_pos, index_entry, false);
}
return new DDLStatementExecutionResult(entry.transactionId);
} catch (DataStorageManagerException | LogNotAvailableException err) {
throw new StatementExecutionException(err);
} finally {
if (lockAcquired) {
releaseWriteLock(context.getTableSpaceLock(), statement);
context.setTableSpaceLock(0);
}
}
}
use of herddb.model.Column in project herddb by diennea.
the class RecordSerializer method accessRawDataFromValue.
static Object accessRawDataFromValue(String property, Bytes value, Table table) throws IOException {
if (table.getColumn(property) == null) {
throw new herddb.utils.IllegalDataAccessException("table " + table.tablespace + "." + table.name + " does not define column " + property);
}
try (ByteArrayCursor din = value.newCursor()) {
while (!din.isEof()) {
int serialPosition;
serialPosition = din.readVIntNoEOFException();
if (din.isEof()) {
return null;
}
Column col = table.getColumnBySerialPosition(serialPosition);
if (col != null && col.name.equals(property)) {
return deserializeTypeAndValue(din);
} else {
// we have to deserialize always the value, even the column is no more present
skipTypeAndValue(din);
}
}
return null;
}
}
use of herddb.model.Column in project herddb by diennea.
the class RecordSerializer method buildRecord.
public static byte[] buildRecord(int expectedSize, Table table, Function<String, Object> evaluator) {
VisibleByteArrayOutputStream value = new VisibleByteArrayOutputStream(expectedSize <= 0 ? INITIAL_BUFFER_SIZE : expectedSize);
try (ExtendedDataOutputStream doo = new ExtendedDataOutputStream(value)) {
for (Column c : table.columns) {
if (!table.isPrimaryKeyColumn(c.name)) {
Object v = evaluator.apply(c.name);
if (v != null) {
doo.writeVInt(c.serialPosition);
serializeTypeAndValue(v, c.type, doo);
}
}
}
} catch (IOException err) {
throw new RuntimeException(err);
}
return value.toByteArrayNoCopy();
}
use of herddb.model.Column in project herddb by diennea.
the class RecordSerializer method validateIndexableValue.
/**
* Like {@link #serializeIndexKey(herddb.utils.DataAccessor, herddb.model.ColumnsList, java.lang.String[])
* } but without return a value and/or creating temporary byte[]
*
* @param record
* @param indexDefinition
* @param columns
*/
public static void validateIndexableValue(DataAccessor record, ColumnsList indexDefinition, String[] columns) {
String[] columnListForIndex = indexDefinition.getPrimaryKey();
if (columnListForIndex.length == 1) {
String pkColumn = columnListForIndex[0];
if (columns.length != 1 && !columns[0].equals(pkColumn)) {
throw new IllegalArgumentException("SQLTranslator error, " + Arrays.toString(columns) + " != " + Arrays.asList(pkColumn));
}
Column c = indexDefinition.getColumn(pkColumn);
Object v = record.get(c.name);
if (v == null && !indexDefinition.allowNullsForIndexedValues()) {
throw new IllegalArgumentException("key field " + pkColumn + " cannot be null. Record data: " + record);
}
validate(v, c.type);
} else {
// beware that we can serialize even only a part of the PK, for instance of a prefix index scan
int i = 0;
for (String pkColumn : columns) {
if (!pkColumn.equals(columnListForIndex[i])) {
throw new IllegalArgumentException("SQLTranslator error, " + Arrays.toString(columns) + " != " + Arrays.asList(columnListForIndex));
}
Column c = indexDefinition.getColumn(pkColumn);
Object v = record.get(c.name);
if (v == null && !indexDefinition.allowNullsForIndexedValues()) {
throw new IllegalArgumentException("key field " + pkColumn + " cannot be null. Record data: " + record);
}
validate(v, c.type);
i++;
}
}
}
use of herddb.model.Column in project herddb by diennea.
the class RecordSerializer method compareRawDataFromValue.
static SQLRecordPredicateFunctions.CompareResult compareRawDataFromValue(int index, Bytes value, Table table, Object cvalue) throws IOException {
Column column = table.getColumn(index);
try (ByteArrayCursor din = value.newCursor()) {
while (!din.isEof()) {
int serialPosition;
serialPosition = din.readVIntNoEOFException();
if (din.isEof()) {
return CompareResult.NULL;
}
Column col = table.getColumnBySerialPosition(serialPosition);
if (col != null && col.serialPosition == column.serialPosition) {
return compareDeserializeTypeAndValue(din, cvalue);
} else {
// we have to deserialize always the value, even the column is no more present
skipTypeAndValue(din);
}
}
}
return CompareResult.NULL;
}
Aggregations