Search in sources :

Example 1 with MockValueGenerator

use of org.jkiss.dbeaver.ext.mockdata.model.MockValueGenerator in project dbeaver by dbeaver.

the class MockDataExecuteWizard method executeProcess.

@Override
public boolean executeProcess(DBRProgressMonitor monitor, DBSDataManipulator dataManipulator) throws IOException {
    DBCExecutionContext context = dataManipulator.getDataSource().getDefaultContext(true);
    try (DBCSession session = context.openSession(monitor, DBCExecutionPurpose.USER, MockDataMessages.tools_mockdata_generate_data_task)) {
        DBCTransactionManager txnManager = DBUtils.getTransactionManager(session.getExecutionContext());
        boolean autoCommit;
        try {
            autoCommit = txnManager == null || txnManager.isAutoCommit();
        } catch (DBCException e) {
            log.error(e);
            autoCommit = true;
        }
        AbstractExecutionSource executionSource = new AbstractExecutionSource(dataManipulator, session.getExecutionContext(), this);
        boolean success = true;
        monitor.beginTask("Generate Mock Data", 3);
        if (mockDataSettings.isRemoveOldData()) {
            logPage.appendLog("Removing old data from the '" + dataManipulator.getName() + "'.\n");
            monitor.subTask("Cleanup old data");
            DBCStatistics deleteStats = new DBCStatistics();
            try {
                // TODO: truncate is much faster than delete
                try (DBSDataManipulator.ExecuteBatch batch = dataManipulator.deleteData(session, new DBSAttributeBase[] {}, executionSource)) {
                    batch.add(new Object[] {});
                    deleteStats.accumulate(batch.execute(session));
                }
                if (txnManager != null && !autoCommit) {
                    txnManager.commit(session);
                }
            } catch (Exception e) {
                success = false;
                String message = "    Error removing the data: " + e.getMessage();
                log.error(message, e);
                logPage.appendLog(message + "\n\n", true);
            }
            logPage.appendLog("    Rows updated: " + deleteStats.getRowsUpdated() + "\n");
            logPage.appendLog("    Duration: " + deleteStats.getExecuteTime() + "ms\n\n");
        } else {
            logPage.appendLog("Old data isn't removed.\n\n");
        }
        if (!success) {
            return true;
        }
        try {
            monitor.subTask("Insert data");
            logPage.appendLog("Inserting mock data into the '" + dataManipulator.getName() + "'.\n");
            DBCStatistics insertStats = new DBCStatistics();
            // build and init the generators
            generators.clear();
            DBSEntity dbsEntity = (DBSEntity) dataManipulator;
            Collection<? extends DBSAttributeBase> attributes = DBUtils.getRealAttributes(dbsEntity.getAttributes(monitor));
            for (DBSAttributeBase attribute : attributes) {
                MockGeneratorDescriptor generatorDescriptor = mockDataSettings.getGeneratorDescriptor(mockDataSettings.getAttributeGeneratorProperties(attribute).getSelectedGeneratorId());
                if (generatorDescriptor != null) {
                    MockValueGenerator generator = generatorDescriptor.createGenerator();
                    MockDataSettings.AttributeGeneratorProperties generatorPropertySource = this.mockDataSettings.getAttributeGeneratorProperties(attribute);
                    String selectedGenerator = generatorPropertySource.getSelectedGeneratorId();
                    Map<Object, Object> generatorProperties = generatorPropertySource.getGeneratorPropertySource(selectedGenerator).getPropertiesWithDefaults();
                    generator.init(dataManipulator, attribute, generatorProperties);
                    generators.put(attribute.getName(), generator);
                }
            }
            monitor.done();
            long rowsNumber = mockDataSettings.getRowsNumber();
            long quotient = rowsNumber / BATCH_SIZE;
            long modulo = rowsNumber % BATCH_SIZE;
            if (modulo > 0) {
                quotient++;
            }
            int counter = 0;
            monitor.beginTask("Insert data", (int) rowsNumber);
            // generate and insert the data
            session.enableLogging(false);
            DBSDataManipulator.ExecuteBatch batch = null;
            for (int q = 0; q < quotient; q++) {
                if (monitor.isCanceled()) {
                    break;
                }
                if (counter > 0) {
                    if (txnManager != null && !autoCommit) {
                        txnManager.commit(session);
                    }
                    monitor.subTask(String.valueOf(counter) + " rows inserted");
                    monitor.worked(BATCH_SIZE);
                }
                try {
                    for (int i = 0; (i < BATCH_SIZE && counter < rowsNumber); i++) {
                        if (monitor.isCanceled()) {
                            break;
                        }
                        List<DBDAttributeValue> attributeValues = new ArrayList<>();
                        try {
                            for (DBSAttributeBase attribute : attributes) {
                                MockValueGenerator generator = generators.get(attribute.getName());
                                if (generator != null) {
                                    // ((AbstractMockValueGenerator) generator).checkUnique(monitor);
                                    Object value = generator.generateValue(monitor);
                                    attributeValues.add(new DBDAttributeValue(attribute, value));
                                }
                            }
                        } catch (DBException e) {
                            processGeneratorException(e);
                            return true;
                        }
                        if (batch == null) {
                            batch = dataManipulator.insertData(session, DBDAttributeValue.getAttributes(attributeValues), null, executionSource);
                        }
                        if (counter++ < rowsNumber) {
                            batch.add(DBDAttributeValue.getValues(attributeValues));
                        }
                    }
                    if (batch != null) {
                        insertStats.accumulate(batch.execute(session));
                    }
                } catch (Exception e) {
                    processGeneratorException(e);
                    if (e instanceof DBException) {
                        throw e;
                    }
                } finally {
                    if (batch != null) {
                        batch.close();
                        batch = null;
                    }
                }
            }
            if (txnManager != null && !autoCommit) {
                txnManager.commit(session);
            }
            logPage.appendLog("    Rows updated: " + insertStats.getRowsUpdated() + "\n");
            logPage.appendLog("    Duration: " + insertStats.getExecuteTime() + "ms\n\n");
        } catch (DBException e) {
            String message = "    Error inserting mock data: " + e.getMessage();
            log.error(message, e);
            logPage.appendLog(message + "\n\n", true);
        }
    } finally {
        monitor.done();
    }
    return true;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDAttributeValue(org.jkiss.dbeaver.model.data.DBDAttributeValue) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) MockGeneratorDescriptor(org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor) MockValueGenerator(org.jkiss.dbeaver.ext.mockdata.model.MockValueGenerator) IOException(java.io.IOException) DBException(org.jkiss.dbeaver.DBException) DBSDataManipulator(org.jkiss.dbeaver.model.struct.DBSDataManipulator) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

IOException (java.io.IOException)1 DBException (org.jkiss.dbeaver.DBException)1 MockGeneratorDescriptor (org.jkiss.dbeaver.ext.mockdata.model.MockGeneratorDescriptor)1 MockValueGenerator (org.jkiss.dbeaver.ext.mockdata.model.MockValueGenerator)1 DBDAttributeValue (org.jkiss.dbeaver.model.data.DBDAttributeValue)1 AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)1 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)1 DBSDataManipulator (org.jkiss.dbeaver.model.struct.DBSDataManipulator)1 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)1