SecretSeriesDAO { public ImmutableList<SecretSeries> getSecretSeries(@Nullable Long expireMaxTime, @Nullable Group group, @Nullable Long expireMinTime, @Nullable String minName, @Nullable Integer limit) { SelectQuery<Record> select = dslContext .select() .from(SECRETS) .join(SECRETS_CONTENT) .on(SECRETS.CURRENT.equal(SECRETS_CONTENT.ID)) .where(SECRETS.CURRENT.isNotNull()) .getQuery(); select.addOrderBy(SECRETS_CONTENT.EXPIRY.asc().nullsLast(), SECRETS.NAME.asc().nullsLast()); if (expireMaxTime != null && expireMaxTime > 0) { if (expireMinTime == null || expireMinTime == 0) { long now = System.currentTimeMillis() / 1000L; select.addConditions(SECRETS_CONTENT.EXPIRY.greaterOrEqual(now)); } select.addConditions(SECRETS_CONTENT.EXPIRY.lessThan(expireMaxTime)); } if (expireMinTime != null && expireMinTime > 0) { select.addConditions(SECRETS_CONTENT.EXPIRY.greaterThan(expireMinTime) .or(SECRETS_CONTENT.EXPIRY.eq(expireMinTime) .and(SECRETS.NAME.greaterOrEqual(minName)))); } if (group != null) { select.addJoin(ACCESSGRANTS, SECRETS.ID.eq(ACCESSGRANTS.SECRETID)); select.addJoin(GROUPS, GROUPS.ID.eq(ACCESSGRANTS.GROUPID)); select.addConditions(GROUPS.NAME.eq(group.getName())); } if (limit != null && limit >= 0) { select.addLimit(limit); } List<SecretSeries> r = select.fetchInto(SECRETS).map(secretSeriesMapper); return ImmutableList.copyOf(r); } private  SecretSeriesDAO(DSLContext dslContext, ObjectMapper mapper,
      SecretSeriesMapper secretSeriesMapper, RowHmacGenerator rowHmacGenerator); int setExpiration(long secretContentId, Instant expiration); int setHmac(long secretContentId, String hmac); int setCurrentVersion(long secretId, long secretContentId, String updater, long now); Optional<SecretSeries> getSecretSeriesById(long id); Optional<SecretSeries> getSecretSeriesByName(String name); List<SecretSeries> getMultipleSecretSeriesByName(List<String> names); ImmutableList<SecretSeries> getSecretSeries(@Nullable Long expireMaxTime,
      @Nullable Group group, @Nullable Long expireMinTime, @Nullable String minName,
      @Nullable Integer limit); ImmutableList<SecretSeries> getSecretSeriesBatched(int idx, int num, boolean newestFirst); void deleteSecretSeriesByName(String name); void deleteSecretSeriesById(long id); int countDeletedSecretSeries(); List<Long> getIdsForSecretSeriesDeletedBeforeDate(DateTime deleteBefore); long dangerPermanentlyRemoveRecordsForGivenIDs(List<Long> ids);  }
@Test public void getSecretSeries() { long now = OffsetDateTime.now().toEpochSecond(); long firstExpiry = now + 10000; long secondExpiry = now + 20000; long thirdExpiry = now + 30000; long fourthExpiry = now + 40000; long firstId = secretSeriesDAO.createSecretSeries("expiringFirst", "creator", "", null, null, now); long firstContentId = secretContentDAO.createSecretContent(firstId, "blah", "checksum", "creator", null, firstExpiry, now); secretSeriesDAO.setCurrentVersion(firstId, firstContentId, "creator", now); long secondId = secretSeriesDAO.createSecretSeries("expiringSecond", "creator", "", null, null, now); long secondContentId = secretContentDAO.createSecretContent(secondId, "blah", "checksum", "creator", null, secondExpiry, now); secretSeriesDAO.setCurrentVersion(secondId, secondContentId, "creator", now); long fourthId = secretSeriesDAO.createSecretSeries("expiringFourth", "creator", "", null, null, now); long fourthContentId = secretContentDAO.createSecretContent(fourthId, "blah", "checksum", "creator", null, fourthExpiry, now); secretSeriesDAO.setCurrentVersion(fourthId, fourthContentId, "creator", now); long thirdId = secretSeriesDAO.createSecretSeries("expiringThird", "creator", "", null, null, now); long thirdContentId = secretContentDAO.createSecretContent(thirdId, "blah", "checksum", "creator", null, thirdExpiry, now); secretSeriesDAO.setCurrentVersion(thirdId, thirdContentId, "creator", now); long fifthId = secretSeriesDAO.createSecretSeries("laterInAlphabetExpiringFourth", "creator", "", null, null, now); long fifthContentId = secretContentDAO.createSecretContent(fifthId, "blah", "checksum", "creator", null, fourthExpiry, now); secretSeriesDAO.setCurrentVersion(fifthId, fifthContentId, "creator", now); ImmutableList<SecretSeries> retrievedSeries = secretSeriesDAO.getSecretSeries(null, null, null, null, null); assertListContainsSecretsWithIds(retrievedSeries, ImmutableList.of(firstId, secondId, thirdId, fourthId, fifthId)); retrievedSeries = secretSeriesDAO.getSecretSeries(fourthExpiry - 100, null, null,null, null); assertListContainsSecretsWithIds(retrievedSeries, ImmutableList.of(firstId, secondId, thirdId)); assertListDoesNotContainSecretsWithIds(retrievedSeries, ImmutableList.of(fourthId, fifthId)); retrievedSeries = secretSeriesDAO.getSecretSeries(fourthExpiry - 100, null, firstExpiry + 10, null,null); assertListContainsSecretsWithIds(retrievedSeries, ImmutableList.of(secondId, thirdId)); assertListDoesNotContainSecretsWithIds(retrievedSeries, ImmutableList.of(firstId, fourthId, fifthId)); retrievedSeries = secretSeriesDAO.getSecretSeries(fourthExpiry - 100, null, firstExpiry + 10, null,1); assertListContainsSecretsWithIds(retrievedSeries, ImmutableList.of(secondId)); assertListDoesNotContainSecretsWithIds(retrievedSeries, ImmutableList.of(firstId, thirdId, fourthId, fifthId)); retrievedSeries = secretSeriesDAO.getSecretSeries(null, null, fourthExpiry, "laterInAlphabetExpiringFourth", null); assertListContainsSecretsWithIds(retrievedSeries, ImmutableList.of(fifthId)); assertListDoesNotContainSecretsWithIds(retrievedSeries, ImmutableList.of(firstId, secondId, thirdId, fourthId)); }