/** * Converts the wrapped number into an {@link java.time.Instant} by assuming the number represents a * "unix timestamp" in seconds. * * @param defaultValue used if an invalid or non numeric value was found * @return the <tt>Instant</tt> as determined by the wrapped timestamp value or <tt>defaultValue</tt> if no * conversion was possible */ public Instant asInstantOfEpochSeconds(Instant defaultValue) { long epochSeconds = asLong(-1); if (epochSeconds < 0) { return defaultValue; } return Instant.ofEpochSecond(epochSeconds); }
/** * Converts the wrapped number into an {@link java.time.Instant} by assuming the number represents a * "unix timestamp" in milliseconds. * * @param defaultValue used if an invalid or non numeric value was found * @return the <tt>Instant</tt> as determined by the wrapped timestamp value or <tt>defaultValue</tt> if no * conversion was possible */ public Instant asInstantOfEpochMillis(Instant defaultValue) { long epochSeconds = asLong(-1); if (epochSeconds < 0) { return defaultValue; } return Instant.ofEpochMilli(epochSeconds); }
@SuppressWarnings("unchecked") protected <E extends SQLEntity> Optional<E> execFind(Object id, EntityDescriptor ed, Connection c) throws Exception { try (PreparedStatement stmt = c.prepareStatement("SELECT * FROM " + ed.getRelationName() + SQL_WHERE_ID, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { stmt.setLong(1, Value.of(id).asLong(-1)); try (ResultSet rs = stmt.executeQuery()) { if (!rs.next()) { return Optional.empty(); } Set<String> columns = dbs.readColumns(rs); E entity = (E) ed.make(OMA.class, null, key -> { String effeciveKey = key.toUpperCase(); if (!columns.contains(effeciveKey)) { return null; } try { return Value.of(rs.getObject(effeciveKey)); } catch (SQLException e) { throw Exceptions.handle(OMA.LOG, e); } }); if (ed.isVersioned()) { entity.setVersion(rs.getInt(BaseMapper.VERSION.toUpperCase())); } return Optional.of(entity); } } }
/** * Loads an auto generated id from the given row. * * @param entity the target entity to write the id to * @param keys the row to read the id from */ public static void loadCreatedId(SQLEntity entity, Row keys) { if (keys.hasValue("id")) { // Normally the name of the generated column is reported entity.setId(keys.getValue("id").asLong(-1)); } else if (keys.hasValue("GENERATED_KEY")) { // however MySQL reports "GENERATED_KEY"... entity.setId(keys.getValue("GENERATED_KEY").asLong(-1)); } else if (keys.hasValue("INSERT_ID")) { // aaand MariaDB reports "INSERT_ID"... entity.setId(keys.getValue("INSERT_ID").asLong(-1)); } }
@Override public void gather(MetricsCollector collector) { if (redis.isConfigured()) { collector.metric("redis_calls", "redis-calls", "Redis Calls", redis.callDuration.getCount(), "/min"); collector.metric("redis_call_duration", "redis-call-duration", "Redis Call Duration", redis.callDuration.getAndClear(), "ms"); collector.metric("redis_memory_usage", "redis-memory-usage", "Redis Memory Usage", Value.of(redis.getInfo().get(Redis.INFO_USED_MEMORY)).asLong(0) / 1024d / 1024d, "MB"); collector.metric("redis_messages", "redis-messages", "Redis PubSub Messages", redis.messageDuration.getCount(), "/min"); collector.metric("redis_message_duration", "redis-message-duration", "Redis PubSub Message Duration", redis.messageDuration.getAndClear(), "ms"); } } }