use of herddb.model.Column in project herddb by diennea.
the class RecordSerializer method toBean.
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public static Map<String, Object> toBean(Record record, Table table) {
try {
ImmutableMap.Builder<String, Object> res = new ImmutableMap.Builder<>();
if (table.primaryKey.length == 1) {
Object key = deserializeSingleColumnPrimaryKey(record.key, table);
res.put(table.primaryKey[0], key);
} else {
deserializeMultiColumnPrimaryKey(record.key, table, res);
}
if (record.value != null && record.value.getLength() > 0) {
try (ByteArrayCursor din = record.value.newCursor()) {
while (true) {
int serialPosition;
serialPosition = din.readVIntNoEOFException();
if (din.isEof()) {
break;
}
Column col = table.getColumnBySerialPosition(serialPosition);
// we have to deserialize or skip always the value, even the column is no more present
if (col != null) {
Object v = deserializeTypeAndValue(din);
res.put(col.name, v);
} else {
skipTypeAndValue(din);
}
}
}
}
return res.build();
} catch (IOException err) {
throw new IllegalArgumentException("malformed record", err);
}
}
use of herddb.model.Column in project herddb by diennea.
the class TableManager method tableAltered.
@Override
public void tableAltered(Table table, Transaction transaction) throws DDLException {
// compute diff, if some column as been dropped we need to remove the value from each record
List<String> droppedColumns = new ArrayList<>();
for (Column c : this.table.columns) {
if (table.getColumn(c.name) == null) {
droppedColumns.add(c.name);
}
}
this.table = table;
if (!droppedColumns.isEmpty()) {
// no lock is necessary
pages.values().forEach(p -> {
p.flushRecordsCache();
});
}
if (this.table.auto_increment) {
rebuildNextPrimaryKeyValue();
}
// clear FK caches, foreignkeys may change
parentForeignKeyQueries.clear();
childForeignKeyQueries.clear();
}
use of herddb.model.Column 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;
}
use of herddb.model.Column in project herddb by diennea.
the class RunHerdDB070Test method test.
@Test
public void test() throws Exception {
String file = "herddb.070.joinerror.zip";
File dbdatadir = folder.newFolder("dbdata070_" + file);
try (InputStream in = RunHerdDB070Test.class.getResourceAsStream(file)) {
ZIPUtils.unZip(in, dbdatadir);
}
System.out.println("UNZIPPED TO " + dbdatadir);
final Path dbdata = dbdatadir.toPath().resolve("herddb.070.joinerror").resolve("dbdata");
Path metadataPath = dbdata.resolve("metadata");
Path dataPath = dbdata.resolve("data");
Path logsPath = dbdata.resolve("txlog");
Path tmoDir = dbdata.resolve("tmp");
assertTrue(Files.isDirectory(metadataPath));
assertTrue(Files.isDirectory(dataPath));
assertTrue(Files.isDirectory(logsPath));
Path nodeid = dataPath.resolve("nodeid");
assertTrue(Files.isRegularFile(nodeid));
String id = new String(Files.readAllBytes(nodeid), StandardCharsets.UTF_8);
System.out.println("id:" + id);
String expectedNodeId = "asino";
assertTrue(id.endsWith("\n" + expectedNodeId));
try (DBManager manager = new DBManager(expectedNodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
final String tableSpace = "herd";
final String tableName = "testtable";
assertEquals(expectedNodeId, manager.getNodeId());
assertTrue(manager.waitForTablespace(tableSpace, 10000));
AbstractTableManager tableManagerLicense = manager.getTableSpaceManager("herd").getTableManager("license");
Table tableLicense = tableManagerLicense.getTable();
System.out.println("TABLE PK: " + Arrays.toString(tableLicense.primaryKey));
for (Column c : tableLicense.columns) {
System.out.println("COL: " + c.name + " serialPos: " + c.serialPosition);
}
AbstractTableManager tableManagerCustomer = manager.getTableSpaceManager("herd").getTableManager("customer");
Table tableCustomer = tableManagerCustomer.getTable();
System.out.println("TABLE PK: " + Arrays.toString(tableCustomer.primaryKey));
for (Column c : tableCustomer.columns) {
System.out.println("COL: " + c.name + " serialPos: " + c.serialPosition);
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM license", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(15, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM customer", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(7, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT t0.license_id,c.customer_id FROM license t0, customer c WHERE c.customer_id = 3 AND t0.customer_id = 3 AND c.customer_id = t0.customer_id\n" + " ", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(9, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM license t0, customer c WHERE c.customer_id = t0.customer_id", Collections.emptyList(), true, true, false, -1);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = ((ScanResult) manager.executePlan(translated.plan, translated.context, TransactionContext.NO_TRANSACTION)).dataScanner) {
List<DataAccessor> consume = scan1.consume();
System.out.println("NUM " + consume.size());
assertEquals(15, consume.size());
for (DataAccessor r : consume) {
System.out.println("RECORD " + r.toMap());
}
}
}
}
}
use of herddb.model.Column in project herddb by diennea.
the class UpgradeFrom050WithBrinIndexesTest method test.
private void test(String file) throws Exception {
File dbdatadir = folder.newFolder("dbdata050_" + file);
try (InputStream in = UpgradeFrom050WithBrinIndexesTest.class.getResourceAsStream(file)) {
ZIPUtils.unZip(in, dbdatadir);
}
final Path dbdata = dbdatadir.toPath().resolve("dbdata");
Path metadataPath = dbdata.resolve("metadata");
Path dataPath = dbdata.resolve("data");
Path logsPath = dbdata.resolve("txlog");
Path tmoDir = dbdata.resolve("tmp");
assertTrue(Files.isDirectory(metadataPath));
assertTrue(Files.isDirectory(dataPath));
assertTrue(Files.isDirectory(logsPath));
Path nodeid = dataPath.resolve("nodeid");
assertTrue(Files.isRegularFile(nodeid));
String id = new String(Files.readAllBytes(nodeid), StandardCharsets.UTF_8);
System.out.println("id:" + id);
String expectedNodeId = "capra";
assertTrue(id.endsWith("\n" + expectedNodeId));
try (DBManager manager = new DBManager(expectedNodeId, new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
manager.start();
final String tableSpace = "herd";
final String tableName = "testtable";
assertEquals(expectedNodeId, manager.getNodeId());
assertTrue(manager.waitForTablespace(tableSpace, 10000));
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
List<Index> indexes = tableManager.getAvailableIndexes();
for (Index e : indexes) {
System.out.println("INDEX: " + e);
assertEquals(e.type, Index.TYPE_BRIN);
}
assertEquals(4, indexes.size());
for (Column c : tableManager.getTable().getColumns()) {
System.out.println("COLUMN :" + c);
}
{
TranslatedQuery translated = manager.getPlanner().translate(tableSpace, "SELECT * FROM " + tableName + " ORDER BY pk,n1,n2", Collections.emptyList(), true, true, false, -1);
ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
System.out.println("TABLE CONTENTS");
try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
for (DataAccessor r : scan1.consume()) {
System.out.println("RECORD " + r.toMap());
}
}
}
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1", SecondaryIndexSeek.class, 4), manager, tableSpace);
// this could be SecondaryIndexSeek but we have more indexes and the planner is not so smart
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2=3", SecondaryIndexPrefixScan.class, 2), manager, tableSpace);
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n2>=3", SecondaryIndexRangeScan.class, 3), manager, tableSpace);
test(new TestCase("SELECT * FROM " + tableSpace + "." + tableName + " WHERE n1=1 and n2=3", SecondaryIndexPrefixScan.class, 1), manager, tableSpace);
}
}
Aggregations