Search in sources :

Example 1 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class InsertOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) {
    StatementExecutionResult input = this.input.execute(tableSpaceManager, transactionContext, context, true, true);
    ScanResult downstreamScanResult = (ScanResult) input;
    final Table table = tableSpaceManager.getTableManager(tableName).getTable();
    long transactionId = transactionContext.transactionId;
    int updateCount = 0;
    Bytes key = null;
    Bytes newValue = null;
    try (DataScanner inputScanner = downstreamScanResult.dataScanner) {
        while (inputScanner.hasNext()) {
            DataAccessor row = inputScanner.next();
            long transactionIdFromScanner = inputScanner.getTransactionId();
            if (transactionIdFromScanner > 0 && transactionIdFromScanner != transactionId) {
                transactionId = transactionIdFromScanner;
                transactionContext = new TransactionContext(transactionId);
            }
            int index = 0;
            List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
            List<String> keyExpressionToColumn = new ArrayList<>();
            List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
            List<String> valuesColumns = new ArrayList<>();
            for (Column column : table.getColumns()) {
                Object value = row.get(index++);
                if (value != null) {
                    ConstantExpression exp = new ConstantExpression(value);
                    if (table.isPrimaryKeyColumn(column.name)) {
                        keyExpressionToColumn.add(column.name);
                        keyValueExpression.add(exp);
                    }
                    valuesColumns.add(column.name);
                    valuesExpressions.add(exp);
                }
            }
            RecordFunction keyfunction;
            if (keyValueExpression.isEmpty() && table.auto_increment) {
                keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
            } else {
                if (keyValueExpression.size() != table.primaryKey.length) {
                    throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
                }
                keyfunction = new SQLRecordKeyFunction(keyExpressionToColumn, keyValueExpression, table);
            }
            RecordFunction valuesfunction = new SQLRecordFunction(valuesColumns, table, valuesExpressions);
            DMLStatement insertStatement = new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues);
            DMLStatementExecutionResult _result = (DMLStatementExecutionResult) tableSpaceManager.executeStatement(insertStatement, context, transactionContext);
            updateCount += _result.getUpdateCount();
            if (_result.transactionId > 0 && _result.transactionId != transactionId) {
                transactionId = _result.transactionId;
                transactionContext = new TransactionContext(transactionId);
            }
            key = _result.getKey();
            newValue = _result.getNewvalue();
        }
        if (updateCount > 1 && returnValues) {
            if (transactionId > 0) {
                // usually the first record will be rolledback with transaction failure
                throw new StatementExecutionException("cannot 'return values' on multi-values insert");
            } else {
                throw new StatementExecutionException("cannot 'return values' on multi-values insert, at least record could have been written because autocommit=true");
            }
        }
        return new DMLStatementExecutionResult(transactionId, updateCount, key, newValue);
    } catch (DataScannerException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) ConstantExpression(herddb.sql.expressions.ConstantExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) Bytes(herddb.utils.Bytes) DataScanner(herddb.model.DataScanner) Column(herddb.model.Column) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) StatementExecutionResult(herddb.model.StatementExecutionResult) SQLRecordFunction(herddb.sql.SQLRecordFunction) SQLRecordFunction(herddb.sql.SQLRecordFunction) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) SQLRecordKeyFunction(herddb.sql.SQLRecordKeyFunction) DataScannerException(herddb.model.DataScannerException) ScanResult(herddb.model.ScanResult) Table(herddb.model.Table) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) TransactionContext(herddb.model.TransactionContext) DMLStatement(herddb.model.DMLStatement) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction)

Example 2 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class JoinOp method resultProjection.

private Function2<DataAccessor, DataAccessor, DataAccessor> resultProjection(String[] fieldNamesFromLeft, String[] fieldNamesFromRight) {
    DataAccessor nullsOnLeft = DataAccessor.ALL_NULLS(fieldNamesFromLeft);
    DataAccessor nullsOnRight = DataAccessor.ALL_NULLS(fieldNamesFromRight);
    return (DataAccessor a, DataAccessor b) -> new ConcatenatedDataAccessor(fieldNames, a != null ? a : nullsOnLeft, b != null ? b : nullsOnRight);
}
Also used : DataAccessor(herddb.utils.DataAccessor) AbstractDataAccessor(herddb.utils.AbstractDataAccessor)

Example 3 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class BRINIndexManager method rebuild.

@Override
public void rebuild() throws DataStorageManagerException {
    long _start = System.currentTimeMillis();
    LOGGER.log(Level.SEVERE, "rebuilding index {0}", index.name);
    data.clear();
    Table table = tableManager.getTable();
    tableManager.scanForIndexRebuild(r -> {
        DataAccessor values = r.getDataAccessor(table);
        Bytes key = RecordSerializer.serializePrimaryKey(values, table, table.primaryKey);
        // LOGGER.log(Level.SEVERE, "adding " + key + " -> " + values);
        recordInserted(key, values);
    });
    long _stop = System.currentTimeMillis();
    LOGGER.log(Level.SEVERE, "rebuilding index {0} took {1}", new Object[] { index.name, (_stop - _start) + " ms" });
}
Also used : Bytes(herddb.utils.Bytes) Table(herddb.model.Table) DataAccessor(herddb.utils.DataAccessor)

Example 4 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class SemiJoinOp method execute.

@Override
public StatementExecutionResult execute(TableSpaceManager tableSpaceManager, TransactionContext transactionContext, StatementEvaluationContext context, boolean lockRequired, boolean forWrite) throws StatementExecutionException {
    ScanResult resLeft = (ScanResult) left.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    transactionContext = new TransactionContext(resLeft.transactionId);
    ScanResult resRight = (ScanResult) right.execute(tableSpaceManager, transactionContext, context, lockRequired, forWrite);
    final long resTransactionId = resRight.transactionId;
    Enumerable<DataAccessor> result = EnumerableDefaults.semiJoin(resLeft.dataScanner.createEnumerable(), resRight.dataScanner.createEnumerable(), JoinKey.keyExtractor(leftKeys), JoinKey.keyExtractor(rightKeys));
    EnumerableDataScanner joinedScanner = new EnumerableDataScanner(resTransactionId, fieldNames, columns, result);
    return new ScanResult(resTransactionId, joinedScanner);
}
Also used : ScanResult(herddb.model.ScanResult) TransactionContext(herddb.model.TransactionContext) DataAccessor(herddb.utils.DataAccessor)

Example 5 with DataAccessor

use of herddb.utils.DataAccessor in project herddb by diennea.

the class SQLRecordFunction method computeNewValue.

@Override
public byte[] computeNewValue(Record previous, StatementEvaluationContext context, TableContext tableContext) throws StatementExecutionException {
    Map<String, Object> res = previous != null ? new HashMap<>(previous.toBean(table)) : new HashMap<>();
    DataAccessor bean = previous != null ? previous.getDataAccessor(table) : DataAccessor.NULL;
    for (int i = 0; i < columns.size(); i++) {
        CompiledSQLExpression e = expressions.get(i);
        String columnName = columns.get(i);
        herddb.model.Column column = table.getColumn(columnName);
        if (column == null) {
            throw new StatementExecutionException("unknown column " + columnName + " in table " + table.name);
        }
        columnName = column.name;
        Object value = RecordSerializer.convert(column.type, e.evaluate(bean, context));
        res.put(columnName, value);
    }
    return RecordSerializer.toRecord(res, table).value.data;
}
Also used : DataAccessor(herddb.utils.DataAccessor) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException)

Aggregations

DataAccessor (herddb.utils.DataAccessor)86 Test (org.junit.Test)53 DataScanner (herddb.model.DataScanner)46 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)37 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)37 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)36 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)34 RawString (herddb.utils.RawString)28 TransactionContext (herddb.model.TransactionContext)20 StatementExecutionException (herddb.model.StatementExecutionException)17 List (java.util.List)14 HashMap (java.util.HashMap)13 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)12 Tuple (herddb.model.Tuple)12 Column (herddb.model.Column)11 ArrayList (java.util.ArrayList)11 ScanResult (herddb.model.ScanResult)10 DataScannerException (herddb.model.DataScannerException)9 StatementEvaluationContext (herddb.model.StatementEvaluationContext)9 Table (herddb.model.Table)9