private Result<? extends Record> fetchValues(DSLContext ctx, TableReference tableRef, Select<? extends Record> subQuery) { return ctx.select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP, A_VALUE) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .join(subQuery.asTable(TEMP_TABLE_2)) .on(A_ROW_NAME.eq(T2_ROW_NAME) .and(A_COL_NAME.eq(T2_COL_NAME)) .and(A_TIMESTAMP.eq(T2_MAX_TIMESTAMP))) .fetch(); }
private Select<? extends Record> getAllTimestampsQueryAllColumns(DSLContext ctx, TableReference tableRef, Select<Record1<byte[]>> subQuery, long timestamp) { return ctx.select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(A_ROW_NAME.in(subQuery) .and(A_TIMESTAMP.lessThan(timestamp))); }
@Override public Multimap<Cell, Long> getAllTimestamps(final TableReference tableRef, final Set<Cell> cells, final long timestamp) throws InsufficientConsistencyException { if (cells.isEmpty()) { return ImmutableMultimap.of(); } Multimap<Cell, Long> toReturn = ArrayListMultimap.create(); for (List<Cell> partition : Iterables.partition(cells, batchSizeForReads)) { toReturn.putAll(run(ctx -> { Result<? extends Record> records = ctx .select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .join(values(ctx, toRows(Sets.newHashSet(partition)), TEMP_TABLE_1, ROW_NAME, COL_NAME)) .on(A_ROW_NAME.eq(T1_ROW_NAME) .and(A_COL_NAME.eq(T1_COL_NAME))) .where(A_TIMESTAMP.lessThan(timestamp)) .fetch(); Multimap<Cell, Long> results = ArrayListMultimap.create(records.size() / 4, 4); for (Record record : records) { results.put( Cell.create(record.getValue(A_ROW_NAME), record.getValue(A_COL_NAME)), record.getValue(A_TIMESTAMP)); } return results; })); } return toReturn; }
private Select<? extends Record> getAllTimestampsQuerySomeColumns(DSLContext ctx, TableReference tableRef, Select<Record1<byte[]>> subQuery, Collection<byte[]> cols, long timestamp) { return ctx.select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(A_ROW_NAME.in(subQuery) .and(A_COL_NAME.in(cols))) .and(A_TIMESTAMP.lessThan(timestamp)); }
private Select<? extends Record> getLatestTimestampQueryAllColumnsSubQuery(DSLContext ctx, TableReference tableRef, Select<Record1<byte[]>> subQuery, long timestamp) { return ctx.select(A_ROW_NAME, A_COL_NAME, DSL.max(A_TIMESTAMP).as(MAX_TIMESTAMP)) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(A_ROW_NAME.in(subQuery) .and(A_TIMESTAMP.lessThan(timestamp))) .groupBy(A_ROW_NAME, A_COL_NAME); }
private Select<? extends Record> getLatestTimestampQueryAllColumns(DSLContext ctx, TableReference tableRef, Collection<byte[]> rows, long timestamp) { return ctx.select(A_ROW_NAME, A_COL_NAME, DSL.max(A_TIMESTAMP).as(MAX_TIMESTAMP)) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(A_ROW_NAME.in(rows) .and(A_TIMESTAMP.lessThan(timestamp))) .groupBy(A_ROW_NAME, A_COL_NAME); }
@Override public synchronized void storeUpperLimit(final long limit) throws MultipleRunningTimestampServiceError { kvs.runInTransaction((Function<DSLContext, Void>) ctx -> { int rowsUpdated = ctx.update(TABLE) .set(LATEST_TIMESTAMP, limit) .where(DUMMY_COLUMN.eq(0).and(LATEST_TIMESTAMP.eq(latestTimestamp))) .execute(); if (rowsUpdated != 1) { long actualLatestTimestamp = getLatestTimestamp(ctx); throw new MultipleRunningTimestampServiceError("Timestamp limit changed underneath " + "us (limit in memory: " + latestTimestamp + ", limit in db: " + actualLatestTimestamp + "). This may indicate that another timestamp service is running against this db!"); } latestTimestamp = limit; return null; }); }
private SelectOffsetStep<Record1<byte[]>> getRangeQuery(DSLContext ctx, TableReference tableRef, RangeRequest rangeRequest, long timestamp, int maxRows) { boolean reverse = rangeRequest.isReverse(); byte[] start = rangeRequest.getStartInclusive(); byte[] end = rangeRequest.getEndExclusive(); Condition cond = R_TIMESTAMP.lessThan(timestamp); if (start.length > 0) { cond = cond.and(reverse ? R_ROW_NAME.lessOrEqual(start) : R_ROW_NAME.greaterOrEqual(start)); } if (end.length > 0) { cond = cond.and(reverse ? R_ROW_NAME.greaterThan(end) : R_ROW_NAME.lessThan(end)); } return ctx.selectDistinct(R_ROW_NAME) .from(atlasTable(tableRef).as(RANGE_TABLE)) .where(cond) .orderBy(reverse ? R_ROW_NAME.desc() : R_ROW_NAME.asc()) .limit(maxRows); }
private Select<? extends Record> getLatestTimestampQuerySomeColumnsSubQuery(DSLContext ctx, TableReference tableRef, Select<Record1<byte[]>> subQuery, Collection<byte[]> cols, long timestamp) { return ctx.select(A_ROW_NAME, A_COL_NAME, DSL.max(A_TIMESTAMP).as(MAX_TIMESTAMP)) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(A_ROW_NAME.in(subQuery) .and(A_COL_NAME.in(cols))) .and(A_TIMESTAMP.lessThan(timestamp)) .groupBy(A_ROW_NAME, A_COL_NAME); }
private Select<? extends Record> getLatestTimestampQuerySomeColumns(DSLContext ctx, TableReference tableRef, Collection<byte[]> rows, Collection<byte[]> cols, long timestamp) { return ctx.select(A_ROW_NAME, A_COL_NAME, DSL.max(A_TIMESTAMP).as(MAX_TIMESTAMP)) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(A_ROW_NAME.in(rows) .and(A_COL_NAME.in(cols))) .and(A_TIMESTAMP.lessThan(timestamp)) .groupBy(A_ROW_NAME, A_COL_NAME); }
private Select<? extends Record> getLatestTimestampQueryManyTimestamps(DSLContext ctx, TableReference tableRef, RowN[] rows) { return ctx.select(A_ROW_NAME, A_COL_NAME, DSL.max(A_TIMESTAMP).as(MAX_TIMESTAMP)) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .join(values(ctx, rows, TEMP_TABLE_1, ROW_NAME, COL_NAME, TIMESTAMP)) .on(A_ROW_NAME.eq(T1_ROW_NAME) .and(A_COL_NAME.eq(T1_COL_NAME))) .where(A_TIMESTAMP.lessThan(T1_TIMESTAMP)) .groupBy(A_ROW_NAME, A_COL_NAME); }
@Override public Agent getAgentByUri(String uri) { return create() .selectFrom(AGENT) .where(AGENT.URI.eq(uri) .and(AGENT.REMOVED.isNull())) .fetchAny(); }
@Override public List<? extends ProjectMember> findBadProjectMembers(int count) { return create().select(PROJECT_MEMBER.fields()) .from(PROJECT_MEMBER) .join(ACCOUNT) .on(ACCOUNT.ID.eq(PROJECT_MEMBER.PROJECT_ID)) .where(ACCOUNT.STATE.eq(CommonStatesConstants.PURGED) .and(PROJECT_MEMBER.REMOVED.isNull()) .and(PROJECT_MEMBER.STATE.notIn(CommonStatesConstants.DEACTIVATING, CommonStatesConstants.REMOVING))) .limit(count) .fetchInto(ProjectMemberRecord.class); }
@Override public List<? extends Volume> findBadVolumes(int count) { return create().select(VOLUME.fields()) .from(VOLUME) .join(ACCOUNT) .on(ACCOUNT.ID.eq(VOLUME.ACCOUNT_ID)) .where(VOLUME.REMOVED.isNull() .and(ACCOUNT.STATE.eq(CommonStatesConstants.PURGED)) .and(VOLUME.STATE.notIn(CommonStatesConstants.DEACTIVATING, CommonStatesConstants.REMOVING))) .limit(count) .fetchInto(VolumeRecord.class); }
@Override public Long getRequestedVersion(Client client, String itemName) { return create() .select(CONFIG_ITEM_STATUS.REQUESTED_VERSION) .from(CONFIG_ITEM_STATUS) .where( CONFIG_ITEM_STATUS.NAME.eq(itemName) .and(targetObjectCondition(client))) .fetchOneInto(Long.class); }
@Override public List<? extends Host> getNonPurgedHosts(long accountId) { return create() .select(HOST.fields()) .from(HOST) .where(HOST.ACCOUNT_ID.eq(accountId) .and(HOST.STATE.notEqual(CommonStatesConstants.PURGED))) .fetchInto(Host.class); }
@Override public Map<String, Host> getHosts(long agentId) { List<? extends Host> hostList = create() .select(HOST.fields()) .from(HOST) .where( HOST.AGENT_ID.eq(agentId) .and(HOST.REMOVED.isNull())) .fetchInto(HostRecord.class); return groupByReportedUUid(hostList); }
@Override public Account getAccountByUuid(String uuid) { return create() .selectFrom(ACCOUNT) .where(ACCOUNT.UUID.eq(uuid) .and(ACCOUNT.STATE.in(getActiveStates()))) .orderBy(ACCOUNT.ID.asc()).limit(1).fetchOne(); }