Search in sources :

Example 1 with AgnosticResultSet

use of com.palantir.nexus.db.sql.AgnosticResultSet in project atlasdb by palantir.

the class DbKvs method getAllTableNames.

@Override
public Set<TableReference> getAllTableNames() {
    return run(conn -> {
        AgnosticResultSet results = conn.selectResultSetUnregisteredQuery("SELECT table_name FROM " + config.metadataTable().getQualifiedName());
        Set<TableReference> ret = Sets.newHashSetWithExpectedSize(results.size());
        for (AgnosticResultRow row : results.rows()) {
            ret.add(TableReference.createUnsafe(row.getString("table_name")));
        }
        return ret;
    });
}
Also used : TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) AgnosticResultRow(com.palantir.nexus.db.sql.AgnosticResultRow)

Example 2 with AgnosticResultSet

use of com.palantir.nexus.db.sql.AgnosticResultSet in project atlasdb by palantir.

the class PostgresDdlTable method getMillisSinceLastCompact.

/**
 * Returns the number of milliseconds since the last compaction, or Long.MAX_VALUE if
 * compaction has never run.
 */
private long getMillisSinceLastCompact() {
    AgnosticResultSet rs = conns.get().selectResultSetUnregisteredQuery("SELECT FLOOR(EXTRACT(EPOCH FROM GREATEST( " + "  last_vacuum, last_autovacuum, last_analyze, last_autoanalyze" + "))*1000) AS last, " + "FLOOR(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP)*1000) AS current " + "FROM pg_stat_user_tables WHERE relname = ?", prefixedTableName());
    AgnosticResultRow row = Iterables.getOnlyElement(rs.rows());
    // last could be null if vacuum has never run
    long last = row.getLong("last", -1);
    if (last == -1) {
        return Long.MAX_VALUE;
    }
    long current = row.getLong("current");
    return current - last;
}
Also used : AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) AgnosticResultRow(com.palantir.nexus.db.sql.AgnosticResultRow)

Example 3 with AgnosticResultSet

use of com.palantir.nexus.db.sql.AgnosticResultSet in project atlasdb by palantir.

the class TableValueStyleCache method getTableType.

public TableValueStyle getTableType(final ConnectionSupplier connectionSupplier, final TableReference tableRef, TableReference metadataTable) {
    try {
        return valueStyleByTableRef.get(tableRef, () -> {
            SqlConnection conn = connectionSupplier.get();
            AgnosticResultSet results = conn.selectResultSetUnregisteredQuery(String.format("SELECT table_size FROM %s WHERE table_name = ?", metadataTable.getQualifiedName()), tableRef.getQualifiedName());
            Preconditions.checkArgument(!results.rows().isEmpty(), "table %s not found", tableRef.getQualifiedName());
            return TableValueStyle.byId(Iterables.getOnlyElement(results.rows()).getInteger("table_size"));
        });
    } catch (ExecutionException e) {
        log.error("TableValueStyle for the table {} could not be retrieved.", tableRef.getQualifiedName());
        throw Throwables.propagate(e);
    }
}
Also used : AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with AgnosticResultSet

use of com.palantir.nexus.db.sql.AgnosticResultSet in project atlasdb by palantir.

the class OracleTableNameUnmapper method getShortTableNameFromMappingTable.

@SuppressWarnings("checkstyle:NestedTryDepth")
public String getShortTableNameFromMappingTable(ConnectionSupplier connectionSupplier, String tablePrefix, TableReference tableRef) throws TableMappingNotFoundException {
    String fullTableName = tablePrefix + DbKvs.internalTableName(tableRef);
    try {
        return unmappingCache.get(fullTableName, () -> {
            SqlConnection conn = connectionSupplier.get();
            AgnosticResultSet results = conn.selectResultSetUnregisteredQuery("SELECT short_table_name " + "FROM " + AtlasDbConstants.ORACLE_NAME_MAPPING_TABLE + " WHERE table_name = ?", fullTableName);
            if (results.size() == 0) {
                throw new TableMappingNotFoundException("The table " + fullTableName + " does not have a mapping." + "This might be because the table does not exist.");
            }
            return Iterables.getOnlyElement(results.rows()).getString("short_table_name");
        });
    } catch (ExecutionException e) {
        throw new TableMappingNotFoundException(e.getCause());
    }
}
Also used : AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) TableMappingNotFoundException(com.palantir.atlasdb.keyvalue.impl.TableMappingNotFoundException) SqlConnection(com.palantir.nexus.db.sql.SqlConnection) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with AgnosticResultSet

use of com.palantir.nexus.db.sql.AgnosticResultSet in project atlasdb by palantir.

the class DbKvs method getMetadataForTables.

@Override
public Map<TableReference, byte[]> getMetadataForTables() {
    return run(conn -> {
        AgnosticResultSet results = conn.selectResultSetUnregisteredQuery("SELECT table_name, value FROM " + config.metadataTable().getQualifiedName());
        Map<TableReference, byte[]> ret = Maps.newHashMapWithExpectedSize(results.size());
        for (AgnosticResultRow row : results.rows()) {
            ret.put(TableReference.createUnsafe(row.getString("table_name")), row.getBytes("value"));
        }
        return ret;
    });
}
Also used : TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) AgnosticResultSet(com.palantir.nexus.db.sql.AgnosticResultSet) AgnosticResultRow(com.palantir.nexus.db.sql.AgnosticResultRow)

Aggregations

AgnosticResultSet (com.palantir.nexus.db.sql.AgnosticResultSet)9 AgnosticResultRow (com.palantir.nexus.db.sql.AgnosticResultRow)5 SqlConnection (com.palantir.nexus.db.sql.SqlConnection)4 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)2 ExecutionException (java.util.concurrent.ExecutionException)2 TableMappingNotFoundException (com.palantir.atlasdb.keyvalue.impl.TableMappingNotFoundException)1 Connection (java.sql.Connection)1 Before (org.junit.Before)1