@Override public Set<Integer> visit(ByStructureDatabaseQuery databaseQuery, Boolean forUpdate) { SelectConditionStep<Record1<Integer>> select = dsl.select(DSL.field("did", Integer.class)) .from(DSL.tableByName(colSchema.getName(), "root")) .where( DSL.field("sid", Integer.class).equal(databaseQuery.getSid()) ); Result<Record1<Integer>> fetched; if (forUpdate) { fetched = select.forUpdate().fetch(); } else { fetched = select.fetch(); } Set<Integer> result = Sets.newHashSetWithExpectedSize(fetched.size()); for (Record1<Integer> record1 : fetched) { result.add(record1.value1()); } return result; }
public List<String> pollStalled(DSLContext tx, Field<Timestamp> cutOff) { return tx.select(TASKS.TASK_ID) .from(TASKS) .where(TASKS.LAST_UPDATED_AT.lessThan(cutOff) .and(TASKS.FINISHED_AT.isNull()) .and(TASKS.TASK_INTERVAL.greaterThan(0L))) .forUpdate() .skipLocked() .fetch(TASKS.TASK_ID); }
public byte[] getOrUpdateSecretKey(UUID projectId, Supplier<byte[]> keySupplier) { return txResult(tx -> { byte[] data = tx.select(PROJECTS.SECRET_KEY).from(PROJECTS) .where(PROJECTS.PROJECT_ID.eq(projectId)) .fetchOne(PROJECTS.SECRET_KEY); // fast path if (data != null) { return data; } // add the key if not exists data = tx.select(PROJECTS.SECRET_KEY).from(PROJECTS) .where(PROJECTS.PROJECT_ID.eq(projectId)) .forUpdate() .fetchOne(PROJECTS.SECRET_KEY); if (data == null) { data = keySupplier.get(); tx.update(PROJECTS) .set(PROJECTS.SECRET_KEY, data) .where(PROJECTS.PROJECT_ID.eq(projectId)) .execute(); } return data; }); }
public List<String> poll() { @SuppressWarnings("unchecked") Field<? extends Number> i = (Field<? extends Number>) PgUtils.interval("1 second"); return txResult(tx -> { List<String> ids = tx.select(TASKS.TASK_ID) .from(TASKS) .where(TASKS.TASK_INTERVAL.greaterThan(0L).and(TASKS.STARTED_AT.isNull() .or(TASKS.FINISHED_AT.isNotNull() .and(TASKS.FINISHED_AT.plus(TASKS.TASK_INTERVAL.mul(i)).lessOrEqual(currentTimestamp()))))) .forUpdate() .skipLocked() .fetch(TASKS.TASK_ID); if (ids.isEmpty()) { return ids; } tx.update(TASKS) .set(TASKS.STARTED_AT, currentTimestamp()) .set(TASKS.TASK_STATUS, value("RUNNING")) .set(TASKS.FINISHED_AT, (Timestamp)null) .set(TASKS.LAST_UPDATED_AT, currentTimestamp()) .where(TASKS.TASK_ID.in(ids)) .execute(); return ids; }); }