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());
}
}
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);
}
}
Aggregations