Search in sources :

Example 11 with Column

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);
        }
    }
}
Also used : TableAlreadyExistsException(herddb.model.TableAlreadyExistsException) DataStorageManagerException(herddb.storage.DataStorageManagerException) Table(herddb.model.Table) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) Index(herddb.model.Index) CommitLogResult(herddb.log.CommitLogResult) StatementExecutionException(herddb.model.StatementExecutionException) TableCheckpoint(herddb.core.AbstractTableManager.TableCheckpoint) IndexAlreadyExistsException(herddb.model.IndexAlreadyExistsException) Column(herddb.model.Column) ForeignKeyDef(herddb.model.ForeignKeyDef) LogEntry(herddb.log.LogEntry) DumpedLogEntry(herddb.backup.DumpedLogEntry) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 12 with Column

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;
    }
}
Also used : Column(herddb.model.Column) ByteArrayCursor(herddb.utils.ByteArrayCursor)

Example 13 with Column

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();
}
Also used : Column(herddb.model.Column) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) IOException(java.io.IOException) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Example 14 with Column

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++;
        }
    }
}
Also used : Column(herddb.model.Column) RawString(herddb.utils.RawString)

Example 15 with Column

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;
}
Also used : Column(herddb.model.Column) ByteArrayCursor(herddb.utils.ByteArrayCursor)

Aggregations

Column (herddb.model.Column)68 StatementExecutionException (herddb.model.StatementExecutionException)25 ArrayList (java.util.ArrayList)24 Table (herddb.model.Table)23 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)19 DataAccessor (herddb.utils.DataAccessor)18 HashMap (java.util.HashMap)14 PlannerOp (herddb.model.planner.PlannerOp)12 List (java.util.List)12 Bytes (herddb.utils.Bytes)10 AbstractTableManager (herddb.core.AbstractTableManager)9 RecordFunction (herddb.model.RecordFunction)9 RawString (herddb.utils.RawString)9 IOException (java.io.IOException)9 HashSet (java.util.HashSet)9 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)9 Test (org.junit.Test)9 DataScanner (herddb.model.DataScanner)8 Tuple (herddb.model.Tuple)8 InsertStatement (herddb.model.commands.InsertStatement)8