Search in sources :

Example 1 with InvalidNullValueForKeyException

use of herddb.model.InvalidNullValueForKeyException in project herddb by diennea.

the class ConcurrentMapKeyToPageIndex method scanner.

@Override
public Stream<Map.Entry<Bytes, Long>> scanner(IndexOperation operation, StatementEvaluationContext context, TableContext tableContext, herddb.core.AbstractIndexManager index) throws DataStorageManagerException {
    if (operation instanceof PrimaryIndexSeek) {
        PrimaryIndexSeek seek = (PrimaryIndexSeek) operation;
        byte[] seekValue;
        try {
            seekValue = seek.value.computeNewValue(null, context, tableContext);
        } catch (InvalidNullValueForKeyException nullKey) {
            seekValue = null;
        }
        if (seekValue == null) {
            return Stream.empty();
        }
        Bytes key = Bytes.from_array(seekValue);
        Long pageId = map.get(key);
        if (pageId == null) {
            return Stream.empty();
        }
        return Stream.of(new AbstractMap.SimpleImmutableEntry<>(key, pageId));
    }
    // every predicate (WHEREs...) will always be evaluated anyway on every record, in order to guarantee correctness
    if (index != null) {
        return index.recordSetScanner(operation, context, tableContext, this);
    }
    if (operation == null) {
        Stream<Map.Entry<Bytes, Long>> baseStream = map.entrySet().stream();
        return baseStream;
    } else if (operation instanceof PrimaryIndexPrefixScan) {
        PrimaryIndexPrefixScan scan = (PrimaryIndexPrefixScan) operation;
        byte[] prefix;
        try {
            prefix = scan.value.computeNewValue(null, context, tableContext);
        } catch (InvalidNullValueForKeyException err) {
            return Stream.empty();
        } catch (StatementExecutionException err) {
            throw new RuntimeException(err);
        }
        Predicate<Map.Entry<Bytes, Long>> predicate = (Map.Entry<Bytes, Long> t) -> {
            Bytes fullrecordKey = t.getKey();
            return fullrecordKey.startsWith(prefix.length, prefix);
        };
        Stream<Map.Entry<Bytes, Long>> baseStream = map.entrySet().stream();
        return baseStream.filter(predicate);
    } else if (operation instanceof PrimaryIndexRangeScan) {
        Bytes refminvalue;
        PrimaryIndexRangeScan sis = (PrimaryIndexRangeScan) operation;
        SQLRecordKeyFunction minKey = sis.minValue;
        if (minKey != null) {
            refminvalue = Bytes.from_nullable_array(minKey.computeNewValue(null, context, tableContext));
        } else {
            refminvalue = null;
        }
        Bytes refmaxvalue;
        SQLRecordKeyFunction maxKey = sis.maxValue;
        if (maxKey != null) {
            refmaxvalue = Bytes.from_nullable_array(maxKey.computeNewValue(null, context, tableContext));
        } else {
            refmaxvalue = null;
        }
        Predicate<Map.Entry<Bytes, Long>> predicate;
        if (refminvalue != null && refmaxvalue == null) {
            predicate = (Map.Entry<Bytes, Long> entry) -> {
                Bytes datum = entry.getKey();
                return datum.compareTo(refminvalue) >= 0;
            };
        } else if (refminvalue == null && refmaxvalue != null) {
            predicate = (Map.Entry<Bytes, Long> entry) -> {
                Bytes datum = entry.getKey();
                return datum.compareTo(refmaxvalue) <= 0;
            };
        } else if (refminvalue != null && refmaxvalue != null) {
            predicate = (Map.Entry<Bytes, Long> entry) -> {
                Bytes datum = entry.getKey();
                return datum.compareTo(refmaxvalue) <= 0 && datum.compareTo(refminvalue) >= 0;
            };
        } else {
            predicate = (Map.Entry<Bytes, Long> entry) -> {
                return true;
            };
        }
        Stream<Map.Entry<Bytes, Long>> baseStream = map.entrySet().stream();
        return baseStream.filter(predicate);
    } else {
        throw new DataStorageManagerException("operation " + operation + " not implemented on " + this.getClass());
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) InvalidNullValueForKeyException(herddb.model.InvalidNullValueForKeyException) StatementExecutionException(herddb.model.StatementExecutionException) Predicate(java.util.function.Predicate) AbstractMap(java.util.AbstractMap) Bytes(herddb.utils.Bytes) AtomicLong(java.util.concurrent.atomic.AtomicLong) Stream(java.util.stream.Stream) SQLRecordKeyFunction(herddb.sql.SQLRecordKeyFunction) ConcurrentMap(java.util.concurrent.ConcurrentMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 2 with InvalidNullValueForKeyException

use of herddb.model.InvalidNullValueForKeyException in project herddb by diennea.

the class SQLRecordKeyFunction method computeNewValue.

@Override
@SuppressFBWarnings("BC_UNCONFIRMED_CAST")
public byte[] computeNewValue(Record previous, StatementEvaluationContext context, TableContext tableContext) throws StatementExecutionException {
    SQLStatementEvaluationContext statementEvaluationContext = (SQLStatementEvaluationContext) context;
    if (isConstant) {
        byte[] cachedResult = statementEvaluationContext.getConstant(this);
        if (cachedResult != null) {
            return cachedResult;
        }
    }
    final DataAccessor prevRecord = previous == null ? DataAccessor.NULL : previous.getDataAccessor(tableContext.getTable());
    Map<String, Object> pk = new HashMap<>();
    for (int i = 0; i < columns.length; i++) {
        herddb.model.Column c = columns[i];
        CompiledSQLExpression expression = expressions.get(i);
        Object value = expression.evaluate(prevRecord, context);
        if (value == null) {
            throw new InvalidNullValueForKeyException("error while converting primary key " + pk + ", keys cannot be null");
        }
        try {
            value = RecordSerializer.convert(c.type, value);
        } catch (StatementExecutionException err) {
            throw new StatementExecutionException("error on column " + c.name + " (" + ColumnTypes.typeToString(c.type) + "):" + err.getMessage(), err);
        }
        pk.put(c.name, value);
    }
    try {
        // maybe this is only a partial primary key
        byte[] result = RecordSerializer.serializePrimaryKeyRaw(pk, table, pkColumnNames);
        if (isConstant) {
            statementEvaluationContext.cacheConstant(this, result);
        }
        return result;
    } catch (Exception err) {
        throw new StatementExecutionException("error while converting primary key " + pk + ": " + err, err);
    }
}
Also used : HashMap(java.util.HashMap) DataAccessor(herddb.utils.DataAccessor) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) InvalidNullValueForKeyException(herddb.model.InvalidNullValueForKeyException) StatementExecutionException(herddb.model.StatementExecutionException) StatementExecutionException(herddb.model.StatementExecutionException) InvalidNullValueForKeyException(herddb.model.InvalidNullValueForKeyException) Column(herddb.model.Column) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

InvalidNullValueForKeyException (herddb.model.InvalidNullValueForKeyException)2 StatementExecutionException (herddb.model.StatementExecutionException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Column (herddb.model.Column)1 SQLRecordKeyFunction (herddb.sql.SQLRecordKeyFunction)1 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)1 DataStorageManagerException (herddb.storage.DataStorageManagerException)1 Bytes (herddb.utils.Bytes)1 DataAccessor (herddb.utils.DataAccessor)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1