Example usage for com.google.common.math LongMath checkedSubtract

List of usage examples for com.google.common.math LongMath checkedSubtract

Introduction

In this page you can find the example usage for com.google.common.math LongMath checkedSubtract.

Prototype

@GwtIncompatible("TODO")
public static long checkedSubtract(long a, long b) 

Source Link

Document

Returns the difference of a and b , provided it does not overflow.

Usage

From source file:org.eclipse.che.ide.ext.datasource.server.DatabaseExploreService.java

/**
 * Explores a database.// w  w  w.jav  a 2s  .  c o  m
 * 
 * @param exploreRequest the parameters for the explore request
 * @return a description of the contents of the database
 * @throws DatabaseDefinitionException if the datasource configuration is incorrect
 * @throws SQLException if the database connection could not be created
 * @throws ExploreException on crawling error
 * @throws SchemaCrawlerException if database exploration failed
 */
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
public String getDatabase(final ExploreRequestDTO exploreRequest)
        throws SQLException, DatabaseDefinitionException, ExploreException {

    if (exploreRequest == null) {
        throw new ExploreException("The explore request parameter is null");
    }

    final DatabaseConfigurationDTO databaseConfig = exploreRequest.getDatasourceConfig();
    if (databaseConfig == null) {
        throw new DatabaseDefinitionException("Database definition is null");
    }

    ExploreTableType exploreMode = exploreRequest.getExploreTableType();
    // by default, only crawl simple entities
    if (exploreMode == null) {
        exploreMode = ExploreTableType.STANDARD;
    }

    Database database = null;

    final long startTime = System.currentTimeMillis();
    long endSetupTime;
    try (final Connection connection = this.jdbcConnectionFactory.getDatabaseConnection(databaseConfig)) {
        final SchemaCrawlerOptions options = new SchemaCrawlerOptions();

        // info level set to standard + additional table and column attributes
        SchemaInfoLevel customized = SchemaInfoLevel.standard();
        customized.setRetrieveAdditionalTableAttributes(true);
        customized.setRetrieveAdditionalColumnAttributes(true);
        options.setSchemaInfoLevel(customized);

        // exclude procedures and function for now
        options.setRoutineInclusionRule(new ExcludeAll());

        List<String> tabletypes = null;
        switch (exploreMode) {
        case SIMPLE:
            tabletypes = TABLE_TYPES_SIMPLE;
            break;
        case SYSTEM:
            tabletypes = TABLES_TYPE_WITH_SYSTEM;
            break;
        case ALL:
            tabletypes = TABLES_TYPE_ALL;
            break;
        case STANDARD:
        default:
            tabletypes = TABLE_TYPES_STANDARD;
            break;
        }
        options.setTableTypes(tabletypes);

        endSetupTime = System.currentTimeMillis();
        database = SchemaCrawlerUtility.getDatabase(connection, options);
    } catch (final SchemaCrawlerException e) {
        throw new ExploreException("Couldn't explore database - " + e.getLocalizedMessage());
    }
    final long endCrawlingTime = System.currentTimeMillis();

    DatabaseDTO databaseDTO = DtoFactory.getInstance().createDto(DatabaseDTO.class).withName(database.getName())
            .withLookupKey(database.getLookupKey())
            .withDatabaseProductName(database.getDatabaseInfo().getProductName())
            .withDatabaseProductVersion(database.getDatabaseInfo().getProductVersion())
            .withUserName(database.getDatabaseInfo().getUserName())
            .withJdbcDriverName(database.getJdbcDriverInfo().getDriverName())
            .withJdbcDriverVersion(database.getJdbcDriverInfo().getDriverVersion())
            .withComment(database.getRemarks());
    final Map<String, SchemaDTO> schemaToInject = new HashMap<String, SchemaDTO>();
    databaseDTO = databaseDTO.withSchemas(schemaToInject);
    for (final Schema schema : database.getSchemas()) {
        final SchemaDTO schemaDTO = DtoFactory.getInstance().createDto(SchemaDTO.class)
                // TODO maybe clean up this (schema.getName() can be null with mysql and the json
                // serializer has
                // a constraint on it)
                // TODO do we always want to display the fullname ? rather than the name ?
                .withName((schema.getName() == null) ? schema.getFullName() : schema.getName())
                .withLookupKey(schema.getLookupKey()).withComment(database.getRemarks());
        final Map<String, TableDTO> tables = new HashMap<String, TableDTO>();
        for (final Table table : database.getTables(schema)) {
            TableDTO tableDTO = DtoFactory.getInstance().createDto(TableDTO.class).withName(table.getName())
                    .withLookupKey(table.getLookupKey()).withType(table.getTableType().getTableType())
                    .withComment(database.getRemarks());
            if (table instanceof View) {
                tableDTO = tableDTO.withIsView(true);
            }

            final PrimaryKey primaryKey = table.getPrimaryKey();
            if (primaryKey != null) {
                final List<IndexColumn> primaryKeyColumns = primaryKey.getColumns();
                final List<String> pkColumnNames = new ArrayList<>();
                for (final IndexColumn indexColumn : primaryKeyColumns) {
                    pkColumnNames.add(indexColumn.getName());
                }
                tableDTO.setPrimaryKey(pkColumnNames);
            }

            final Map<String, ColumnDTO> columns = new HashMap<String, ColumnDTO>();
            for (final Column column : table.getColumns()) {
                final ColumnDTO columnDTO = DtoFactory.getInstance().createDto(ColumnDTO.class)
                        .withName(column.getName()).withLookupKey(column.getLookupKey())
                        .withColumnDataType(column.getColumnDataType().getName())
                        .withDefaultValue(column.getDefaultValue()).withNullable(column.isNullable())
                        .withDataSize(column.getSize()).withDecimalDigits(column.getDecimalDigits())
                        .withPartOfForeignKey(column.isPartOfForeignKey())
                        .withPartOfPrimaryKey(column.isPartOfPrimaryKey())
                        .withOrdinalPositionInTable(column.getOrdinalPosition())
                        .withComment(database.getRemarks());
                columns.put(columnDTO.getName(), columnDTO);
            }
            tableDTO = tableDTO.withColumns(columns);
            tables.put(tableDTO.getName(), tableDTO);
        }
        schemaDTO.withTables(tables);
        schemaToInject.put(schemaDTO.getName(), schemaDTO);
    }
    final long endDtoTime = System.currentTimeMillis();
    final String jsonResult = DtoFactory.getInstance().toJson(databaseDTO);
    final long endJsonTime = System.currentTimeMillis();

    try {
        LOG.debug(
                "Schema metadata obtained - setup {}ms ; crawl {}ms ; dto conversion {}ms ; json conversion {}ms - total {}ms",
                LongMath.checkedSubtract(endSetupTime, startTime),
                LongMath.checkedSubtract(endCrawlingTime, endSetupTime),
                LongMath.checkedSubtract(endDtoTime, endCrawlingTime),
                LongMath.checkedSubtract(endJsonTime, endDtoTime),
                LongMath.checkedSubtract(endJsonTime, startTime));
    } catch (final ArithmeticException e) {
        LOG.debug("Schema metadata obtained - no time info");
    }

    return jsonResult;
}

From source file:org.apache.hadoop.hbase.regionserver.compactions.DateTieredCompactionPolicy.java

private static long getOldestToCompact(long maxAgeMillis, long now) {
    try {//from   w  ww  .  ja  v a  2  s  .c  o  m
        return LongMath.checkedSubtract(now, maxAgeMillis);
    } catch (ArithmeticException ae) {
        LOG.warn("Value for " + CompactionConfiguration.DATE_TIERED_MAX_AGE_MILLIS_KEY + ": " + maxAgeMillis
                + ". All the files will be eligible for minor compaction.");
        return Long.MIN_VALUE;
    }
}