+
+ @Override
+ public void deleteAllStaleOneTimeEcPreKeys(final long threshold, final int minCount) {
+ final var sql = (
+ """
+ DELETE FROM %s AS p
+ WHERE p.account_id_type = ?1
+ AND p.stale_timestamp < ?2
+ AND p._id NOT IN (
+ SELECT _id
+ FROM %s AS p2
+ WHERE p2.account_id_type = ?1
+ ORDER BY
+ CASE WHEN p2.stale_timestamp IS NULL THEN 1 ELSE 0 END DESC,
+ p2.stale_timestamp DESC,
+ p2._id DESC
+ LIMIT ?3
+ )
+ """
+ ).formatted(TABLE_PRE_KEY, TABLE_PRE_KEY);
+ try (final var connection = database.getConnection()) {
+ try (final var statement = connection.prepareStatement(sql)) {
+ statement.setInt(1, accountIdType);
+ statement.setLong(2, threshold);
+ statement.setInt(3, minCount);
+ final var rowCount = statement.executeUpdate();
+ if (rowCount > 0) {
+ logger.debug("Deleted {} stale one time pre keys", rowCount);
+ }
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException("Failed update pre_key store", e);
+ }
+ }
+
+ @Override
+ public void markAllOneTimeEcPreKeysStaleIfNecessary(final long staleTime) {
+ final var sql = (
+ """
+ UPDATE %s
+ SET stale_timestamp = ?
+ WHERE account_id_type = ? AND stale_timestamp IS NULL
+ """
+ ).formatted(TABLE_PRE_KEY);
+ try (final var connection = database.getConnection()) {
+ try (final var statement = connection.prepareStatement(sql)) {
+ statement.setLong(1, staleTime);
+ statement.setInt(2, accountIdType);
+ statement.executeUpdate();
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException("Failed update pre_key store", e);
+ }
+ }