use of herddb.utils.Bytes 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);
}
}
use of herddb.utils.Bytes in project herddb by diennea.
the class MemoryHashIndexManager method start.
@Override
public void start(LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
LOGGER.log(Level.SEVERE, "loading in memory all the keys for mem index {0}", new Object[] { index.name });
bootSequenceNumber = sequenceNumber;
if (LogSequenceNumber.START_OF_TIME.equals(sequenceNumber)) {
/* Empty index (booting from the start) */
LOGGER.log(Level.SEVERE, "loaded empty index {0}", new Object[] { index.name });
} else {
IndexStatus status;
try {
status = dataStorageManager.getIndexStatus(tableSpaceUUID, index.uuid, sequenceNumber);
} catch (DataStorageManagerException e) {
LOGGER.log(Level.SEVERE, "cannot load index {0} due to {1}, it will be rebuilt", new Object[] { index.name, e });
rebuild();
return;
}
for (long pageId : status.activePages) {
LOGGER.log(Level.SEVERE, "recovery index " + index.name + ", load " + pageId);
Map<Bytes, List<Bytes>> read = dataStorageManager.readIndexPage(tableSpaceUUID, index.uuid, pageId, in -> {
Map<Bytes, List<Bytes>> deserialized = new HashMap<>();
// version
long version = in.readVLong();
// flags for future implementations
long flags = in.readVLong();
if (version != 1 || flags != 0) {
throw new DataStorageManagerException("corrupted index page");
}
int size = in.readVInt();
for (int i = 0; i < size; i++) {
byte[] indexKey = in.readArray();
int entrySize = in.readVInt();
List<Bytes> value = new ArrayList<>(entrySize);
for (int kk = 0; kk < entrySize; kk++) {
byte[] tableKey = in.readArray();
value.add(Bytes.from_array(tableKey));
}
deserialized.put(Bytes.from_array(indexKey), value);
}
return deserialized;
});
data.putAll(read);
}
newPageId.set(status.newPageId);
LOGGER.log(Level.SEVERE, "loaded {0} keys for index {1}", new Object[] { data.size(), index.name });
}
}
use of herddb.utils.Bytes 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" });
}
use of herddb.utils.Bytes in project herddb by diennea.
the class BRINIndexManager method recordDeleted.
@Override
public void recordDeleted(Bytes key, DataAccessor values) {
Bytes indexKey = RecordSerializer.serializePrimaryKey(values, index, index.columnNames);
data.delete(indexKey, key);
}
use of herddb.utils.Bytes in project herddb by diennea.
the class MemoryDataStorageManager method writeIndexPage.
@Override
public void writeIndexPage(String tableSpace, String indexName, long pageId, DataWriter writer) throws DataStorageManagerException {
Bytes page_wrapper;
try (ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
ExtendedDataOutputStream eout = new ExtendedDataOutputStream(out)) {
writer.write(eout);
eout.flush();
page_wrapper = Bytes.from_array(out.toByteArray());
} catch (IOException ex) {
throw new DataStorageManagerException(ex);
}
Bytes prev = indexpages.putIfAbsent(tableSpace + "." + indexName + "_" + pageId, page_wrapper);
if (prev != null) {
throw new DataStorageManagerException("pages are immutable");
}
}
Aggregations