use of herddb.model.Index in project herddb by diennea.
the class TableSpaceManager method writeTablesOnDataStorageManager.
private Collection<PostCheckpointAction> writeTablesOnDataStorageManager(CommitLogResult writeLog, boolean prepareActions) throws DataStorageManagerException, LogNotAvailableException {
LogSequenceNumber logSequenceNumber = writeLog.getLogSequenceNumber();
List<Table> tablelist = new ArrayList<>();
List<Index> indexlist = new ArrayList<>();
for (AbstractTableManager tableManager : tables.values()) {
if (!tableManager.isSystemTable()) {
tablelist.add(tableManager.getTable());
}
}
for (AbstractIndexManager indexManager : indexes.values()) {
indexlist.add(indexManager.getIndex());
}
return dataStorageManager.writeTables(tableSpaceUUID, logSequenceNumber, tablelist, indexlist, prepareActions);
}
use of herddb.model.Index in project herddb by diennea.
the class TableSpaceManager method restoreTableFinished.
public void restoreTableFinished(String table, List<Index> indexes) {
TableManager tableManager = (TableManager) tables.get(table);
tableManager.restoreFinished();
for (Index index : indexes) {
bootIndex(index, tableManager, false, 0, true, true);
}
}
use of herddb.model.Index 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.Index in project herddb by diennea.
the class TableManager method checkForeignKeyConstraintsAsChildTable.
private void checkForeignKeyConstraintsAsChildTable(ForeignKeyDef fk, DataAccessor values, StatementEvaluationContext context, Transaction transaction) throws StatementExecutionException {
// We are creating a SQL query and then using DBManager
// using an SQL query will let us leverage the SQL Planner
// and use the best index to perform the execution
// the SQL Planner will cache the plan, and the plan will also be
// invalidated consistently during DML operations.
String query = childForeignKeyQueries.computeIfAbsent(fk.name, (l -> {
Table parentTable = tableSpaceManager.getTableManagerByUUID(fk.parentTableId).getTable();
// with '*' we are not going to perform projections or copies
StringBuilder q = new StringBuilder("SELECT * FROM ");
q.append(delimit(parentTable.tablespace));
q.append(".");
q.append(delimit(parentTable.name));
q.append(" WHERE ");
for (int i = 0; i < fk.parentTableColumns.length; i++) {
if (i > 0) {
q.append(" AND ");
}
q.append(delimit(fk.parentTableColumns[i]));
q.append("=?");
}
return q.toString();
}));
final List<Object> valuesToMatch = new ArrayList<>(fk.columns.length);
boolean allNulls = true;
for (int i = 0; i < fk.columns.length; i++) {
Object value = values.get(fk.columns[i]);
allNulls = allNulls && value == null;
valuesToMatch.add(value);
}
if (allNulls) {
// all of the values are null, so no check on the parent table
return;
}
TransactionContext tx = transaction != null ? new TransactionContext(transaction.transactionId) : TransactionContext.NO_TRANSACTION;
boolean fkOk;
try (DataScanner scan = tableSpaceManager.getDbmanager().executeSimpleQuery(tableSpaceManager.getTableSpaceName(), query, valuesToMatch, // only one record
1, // keep read locks in TransactionContext
true, tx, null)) {
List<DataAccessor> resultSet = scan.consume();
fkOk = !resultSet.isEmpty();
} catch (DataScannerException err) {
throw new StatementExecutionException(err);
}
if (!fkOk) {
throw new ForeignKeyViolationException(fk.name, "foreignKey " + table.name + "." + fk.name + " violated");
}
}
use of herddb.model.Index in project herddb by diennea.
the class SysindexcolumnsTableManager method buildVirtualRecordList.
@Override
protected Iterable<Record> buildVirtualRecordList(Transaction transaction) {
List<Table> tables = tableSpaceManager.getAllVisibleTables(transaction);
List<Record> result = new ArrayList<>();
tables.forEach(r -> {
// PK
int posPk = 0;
for (String pk : r.getPrimaryKey()) {
Column column = r.getColumn(pk);
String indexName = BLinkKeyToPageIndex.deriveIndexName(r.name);
result.add(RecordSerializer.makeRecord(table, "index_type", "pk", "column_name", column.name, "ordinal_position", posPk++, "clustered", 1, "unique", 1, "tablespace", r.tablespace, "table_name", r.name, "index_name", indexName, // uuid = index_name in BLink !
"index_uuid", // uuid = index_name in BLink !
indexName));
}
Map<String, AbstractIndexManager> indexesOnTable = tableSpaceManager.getIndexesOnTable(r.name);
if (indexesOnTable != null) {
indexesOnTable.values().forEach(indexManager -> {
Index index = indexManager.getIndex();
int pos = 0;
for (Column cc : index.getColumns()) {
result.add(RecordSerializer.makeRecord(table, "tablespace", r.tablespace, "table_name", r.name, "index_name", index.name, "index_uuid", index.uuid, "index_type", index.type, "column_name", cc.name, "ordinal_position", pos++, "clustered", 0, "unique", index.unique ? 1 : 0));
}
});
}
});
return result;
}
Aggregations