Skip to content

Commit

Permalink
entities are now checked if they should be inserted, minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
flurfis committed Nov 23, 2023
1 parent 152ea92 commit 7710167
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import lombok.Getter;
import lombok.Setter;
import org.polypheny.db.backup.dependencies.EntityReferencer;
import org.polypheny.db.catalog.entity.LogicalObject;
import org.polypheny.db.catalog.entity.logical.LogicalEntity;

@Getter @Setter
public class BackupEntityWrapper<E extends LogicalObject> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.common.collect.ImmutableMap;
import lombok.Getter;
import lombok.Setter;
import org.polypheny.db.backup.dependencies.BackupEntityType;
import org.polypheny.db.backup.dependencies.EntityReferencer;
import org.polypheny.db.catalog.entity.LogicalConstraint;
import org.polypheny.db.catalog.entity.logical.*;

Expand Down
2 changes: 2 additions & 0 deletions dbms/src/main/java/org/polypheny/db/backup/BackupManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.polypheny.db.backup.datagatherer.GatherEntries;
import org.polypheny.db.backup.datagatherer.GatherSchema;
Expand All @@ -42,6 +43,7 @@ public class BackupManager {
private static BackupManager INSTANCE = null;
private InformationPage informationPage;
private InformationGroup informationGroupOverview;
@Getter
private BackupInformationObject backupInformationObject;
public static TransactionManager transactionManager = null;
//private final Logger logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,15 @@ private void insertCreateNamespace( ImmutableMap<Long, BackupEntityWrapper<Logic

//TODO(FF): check if namespaces is empty, throw error if it is
for ( Map.Entry<Long, BackupEntityWrapper<LogicalNamespace>> ns : namespaces.entrySet() ) {
//query = "CREATE " + ns.getValue().getEntityObject().namespaceType.toString() + " NAMESPACE " + ns.getValue().getEntityObject().name + ";";
query = String.format( "CREATE %s NAMESPACE %s11", ns.getValue().getEntityObject().namespaceType.toString(), ns.getValue().getEntityObject().name );

//TODO(FF): execute query in polypheny, alter owner, set case sensitivity (how?)
if ( !ns.getValue().getEntityObject().name.equals( "public" ) ) {
executeStatementInPolypheny( query, "sql", NamespaceType.RELATIONAL );
//only insert namespaces that are marked to be inserted
if (ns.getValue().getToBeInserted()) {
//query = "CREATE " + ns.getValue().getEntityObject().namespaceType.toString() + " NAMESPACE " + ns.getValue().getEntityObject().name + ";";
query = String.format( "CREATE %s NAMESPACE %s11", ns.getValue().getEntityObject().namespaceType.toString(), ns.getValue().getEntityObject().name );

//TODO(FF): execute query in polypheny, alter owner, set case sensitivity (how?)
if ( !ns.getValue().getEntityObject().name.equals( "public" ) ) {
executeStatementInPolypheny( query, "sql", ns.getValue().getEntityObject().namespaceType );
}
}
}

Expand All @@ -211,11 +214,13 @@ private void insertCreateTable( ImmutableMap<Long, List<BackupEntityWrapper<Logi

// go through each table in the list (of tables for one namespace)
for ( BackupEntityWrapper<LogicalEntity> table : tablesList ) {

// only create tables that don't (exist by default in polypheny)
if ( !(table.getEntityObject().entityType.equals( EntityType.SOURCE )) ) {
query = createTableQuery( table, namespaceName );
executeStatementInPolypheny( query, "sql", NamespaceType.RELATIONAL );
// only create tables that should be inserted
if ( table.getToBeInserted()) {
// only create tables that don't (exist by default in polypheny)
if ( !(table.getEntityObject().entityType.equals( EntityType.SOURCE )) ) {
query = createTableQuery( table, namespaceName );
executeStatementInPolypheny( query, "sql", NamespaceType.RELATIONAL );
}
}
}
}
Expand All @@ -235,8 +240,8 @@ private void insertAlterTableUQ( ImmutableMap<Long, List<BackupEntityWrapper<Log
for ( BackupEntityWrapper<LogicalEntity> table : tablesList ) {
//TODO(FF - cosmetic): exclude source tables (for speed)

// compare the table id with the constraint keys, and if they are the same, create the constraint
if ( constraints.containsKey( table.getEntityObject().unwrap( LogicalTable.class ).getId() ) ) {
// compare the table id with the constraint keys, and if they are the same, create the constraint, and check if it schoult be inserted
if ( (constraints.containsKey( table.getEntityObject().unwrap( LogicalTable.class ).getId() )) && table.getToBeInserted()) {
List<LogicalConstraint> constraintsList = constraints.get( table.getEntityObject().unwrap( LogicalTable.class ).getId() );
List<LogicalColumn> logicalColumns = backupInformationObject.getColumns().get( table.getEntityObject().unwrap( LogicalTable.class ).getId() );

Expand Down Expand Up @@ -266,21 +271,29 @@ private void insertAlterTableFK( ImmutableMap<Long, List<BackupEntityWrapper<Log

// go through foreign key constraints and collect the necessary data
for ( Map.Entry<Long, List<LogicalForeignKey>> fkListPerTable : foreignKeysPerTable.entrySet() ) {
Long tableId = fkListPerTable.getKey();

for ( LogicalForeignKey foreignKey : fkListPerTable.getValue() ) {
String namespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.namespaceId ).getNameForQuery();
String tableName = backupInformationObject.getWrappedTables().get( foreignKey.namespaceId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.tableId ).findFirst().get().getNameForQuery();
String constraintName = foreignKey.name;
String listOfCols = getListOfCol( foreignKey.columnIds, backupInformationObject.getColumns().get( foreignKey.tableId ) );
String referencedNamespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.referencedKeySchemaId ).getNameForQuery();
String referencedTableName = backupInformationObject.getWrappedTables().get( foreignKey.referencedKeySchemaId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.referencedKeyTableId ).findFirst().get().getNameForQuery();
String referencedListOfCols = getListOfCol( foreignKey.referencedKeyColumnIds, backupInformationObject.getColumns().get( foreignKey.referencedKeyTableId ) );
String updateAction = foreignKey.updateRule.foreignKeyOptionToString();
String deleteAction = foreignKey.deleteRule.foreignKeyOptionToString();
//TODO(FF): enforcementTime (on commit) - how to set?? how to change?? possible to change?

query = String.format( "ALTER TABLE %s11.%s11 ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s11.%s11 (%s) ON UPDATE %s ON DELETE %s", namespaceName, tableName, constraintName, listOfCols, referencedNamespaceName, referencedTableName, referencedListOfCols, updateAction, deleteAction );
log.info( query );
executeStatementInPolypheny( query, "sql", NamespaceType.RELATIONAL );
// get the table where the foreign key is safet
BackupEntityWrapper<LogicalEntity> table = bupTables.get( backupInformationObject.getWrappedTables().get( tableId ).get( 0 ).getEntityObject().namespaceId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == tableId ).findFirst().get();

// check if the table is marked to be inserted
if (table.getToBeInserted()) {
String namespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.namespaceId ).getNameForQuery();
String tableName = backupInformationObject.getWrappedTables().get( foreignKey.namespaceId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.tableId ).findFirst().get().getNameForQuery();
String constraintName = foreignKey.name;
String listOfCols = getListOfCol( foreignKey.columnIds, backupInformationObject.getColumns().get( foreignKey.tableId ) );
String referencedNamespaceName = backupInformationObject.getWrappedNamespaces().get( foreignKey.referencedKeySchemaId ).getNameForQuery();
String referencedTableName = backupInformationObject.getWrappedTables().get( foreignKey.referencedKeySchemaId ).stream().filter( e -> e.getEntityObject().unwrap( LogicalTable.class ).getId() == foreignKey.referencedKeyTableId ).findFirst().get().getNameForQuery();
String referencedListOfCols = getListOfCol( foreignKey.referencedKeyColumnIds, backupInformationObject.getColumns().get( foreignKey.referencedKeyTableId ) );
String updateAction = foreignKey.updateRule.foreignKeyOptionToString();
String deleteAction = foreignKey.deleteRule.foreignKeyOptionToString();
//TODO(FF): enforcementTime (on commit) - how to set?? how to change?? possible to change?

query = String.format( "ALTER TABLE %s11.%s11 ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s11.%s11 (%s) ON UPDATE %s ON DELETE %s", namespaceName, tableName, constraintName, listOfCols, referencedNamespaceName, referencedTableName, referencedListOfCols, updateAction, deleteAction );
log.info( query );
executeStatementInPolypheny( query, "sql", NamespaceType.RELATIONAL );
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.polypheny.db.backup;
package org.polypheny.db.backup.dependencies;

public enum BackupEntityType {
NAMESPACE( 1 ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.polypheny.db.backup;
package org.polypheny.db.backup.dependencies;

import java.util.List;
import lombok.Getter;
Expand Down
21 changes: 20 additions & 1 deletion dbms/src/test/java/org/polypheny/db/backup/GeneralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,25 @@
import org.junit.Test;
import org.polypheny.db.TestHelper;
import org.polypheny.db.TestHelper.JdbcConnection;
import org.polypheny.db.transaction.TransactionManager;


@SuppressWarnings({ "SqlDialectInspection", "SqlNoDataSourceInspection" })
@Slf4j
public class GeneralTest {
//TestHelper testHelper;
BackupManager backupManager;

@BeforeClass
public static void start() {
public void start() {
// Ensures that Polypheny-DB is running
//noinspection ResultOfMethodCallIgnored
//this.testHelper = TestHelper.getInstance();
TestHelper.getInstance();
//deleteOldData();
//this.backupManager = new BackupManager( testHelper.getTransactionManager() );
addTestData();

}


Expand Down Expand Up @@ -99,6 +105,19 @@ private static void deleteOldData() {
}
}

/*
@Test
public void testGatherData() {
TransactionManager transactionManager = TestHelper.getTransactionManager();
BackupManager backupManager = new BackupManager( transactionManager );
backupManager.startDataGathering();
Assert.assertEquals( 4, backupManager.getBackupInformationObject().getTables().get( 0 ).size());
}
*/


@Test
public void testGetCatalogs() {
Expand Down

0 comments on commit 7710167

Please sign in to comment.