@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 Set<TableReference> getAllTableNames(DSLContext ctx) { Result<? extends Record> records = ctx .select(TABLE_NAME) .from(METADATA_TABLE) .fetch(); Set<TableReference> tableRefs = Sets.newHashSetWithExpectedSize(records.size()); for (Record record : records) { tableRefs.add(TableReference.createUnsafe(record.getValue(TABLE_NAME))); } return tableRefs; }
public <U> ResultStream<U> selectAll() { // TODO: Decide between defaulting to using cursors or defaulting // to using lists. Cursor<Record> cursor; if (whereConditions != null) cursor = context.dsl.select().from(fromTables).where(whereConditions).fetchLazy(); else cursor = context.dsl.select().from(fromTables).fetchLazy(); RowReader<?>[] readerArray = tableReaders.toArray(new RowReader[tableReaders.size()]); RowReader<U> reader = tableReaders.size() == 1 ? (RowReader<U>)tableReaders.get(0) : new TupleRowReader<>(readerArray); return makeCursorStream(cursor, reader); }
public static JdbcTimestampBoundStore create(final JdbcKeyValueService kvs) { final JdbcTimestampBoundStore store = new JdbcTimestampBoundStore(kvs); kvs.run((Function<DSLContext, Void>) ctx -> { String partialSql = ctx.createTable(store.TABLE) .column(DUMMY_COLUMN, INTEGER.nullable(false)) .column(LATEST_TIMESTAMP, BIGINT.nullable(false)) .getSQL(); int endIndex = partialSql.lastIndexOf(')'); String fullSql = partialSql.substring(0, endIndex) + "," + " CONSTRAINT " + kvs.primaryKey(TIMESTAMP_TABLE) + " PRIMARY KEY (" + DUMMY_COLUMN.getName() + ")" + partialSql.substring(endIndex); try { ctx.execute(fullSql); } catch (DataAccessException e) { kvs.handleTableCreationException(e); } ctx.insertInto(store.TABLE, DUMMY_COLUMN, LATEST_TIMESTAMP) .select(ctx.select(DUMMY_COLUMN, LATEST_TIMESTAMP) .from(kvs.values(ctx, new RowN[] {(RowN) DSL.row(0, 10000L)}, "t", DUMMY_COLUMN.getName(), LATEST_TIMESTAMP.getName())) .whereNotExists(ctx.selectOne() .from(store.TABLE) .where(DUMMY_COLUMN.eq(0)))) .execute(); return null; }); return store; }
int i = 0; for (Cell cell : partCells) { rows[i++] = row(new Object[] {cell.getRowName(), cell.getColumnName(), timestamp, value}); ctx.insertInto(table(tableName(tableRef)), field(ROW_NAME, byte[].class), field(COL_NAME, byte[].class), field(TIMESTAMP, Long.class), field(VALUE, byte[].class)) .select(ctx.select(T1_ROW_NAME, T1_COL_NAME, T1_TIMESTAMP, T1_VALUE) .from(values(ctx, rows, TEMP_TABLE_1, ROW_NAME, COL_NAME, TIMESTAMP, VALUE)) .whereNotExists(ctx.selectOne() .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(A_ROW_NAME.eq(T1_ROW_NAME) .and(A_COL_NAME.eq(T1_COL_NAME)) .and(A_TIMESTAMP.eq(T1_TIMESTAMP))))) .execute(); return null;
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); }
@Override public TIntSet getOutlinkIds(int sourceId) throws DaoException { DSLContext context = getJooq(); try { Cursor<Record> result = context.select() .from(Tables.UNIVERSAL_SKELETAL_LINK) .where(Tables.UNIVERSAL_SKELETAL_LINK.SOURCE_ID.eq(sourceId)) .and(Tables.UNIVERSAL_SKELETAL_LINK.ALGORITHM_ID.eq(algorithmId)) .fetchLazy(getFetchSize()); TIntSet ids = new TIntHashSet(); for (Record record : result){ ids.add(record.getValue(Tables.UNIVERSAL_SKELETAL_LINK.DEST_ID)); } return ids; } finally { freeJooq(context); } }
protected List<? extends ConfigItemStatus> serviceOutOfSyncItems() { return create() .select(CONFIG_ITEM_STATUS.fields()) .from(CONFIG_ITEM_STATUS) .join(SERVICE) .on(SERVICE.ID.eq(CONFIG_ITEM_STATUS.SERVICE_ID)) .where(CONFIG_ITEM_STATUS.REQUESTED_VERSION.ne(CONFIG_ITEM_STATUS.APPLIED_VERSION) .and(SERVICE.REMOVED.isNull())) .limit(BATCH_SIZE.get()) .fetchInto(ConfigItemStatusRecord.class); }
private void putBatch(DSLContext ctx, TableReference tableRef, PutBatch batch, boolean allowReinserts) { InsertValuesStep4<Record, byte[], byte[], Long, byte[]> query = ctx.insertInto(table(tableName(tableRef)), field(ROW_NAME, byte[].class), field(COL_NAME, byte[].class), field(TIMESTAMP, Long.class), field(VALUE, byte[].class)); query = batch.addValuesForInsert(query); try { query.execute(); } catch (DataAccessException e) { if (allowReinserts) { Result<? extends Record> records = ctx .select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP, A_VALUE) .from(atlasTable(tableRef).as(ATLAS_TABLE)) .where(row(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP).in(batch.getRowsForSelect())) .fetch(); if (records.isEmpty()) { throw e; } PutBatch nextBatch = batch.getNextBatch(records); if (nextBatch != null) { putBatch(ctx, tableRef, nextBatch, allowReinserts); return; } } throw new KeyAlreadyExistsException("Conflict on table " + tableRef, e); } }
@Override public Service getServiceByServiceIndexId(long serviceIndexId) { Record record = create() .select(SERVICE.fields()) .from(SERVICE) .join(SERVICE_INDEX).on(SERVICE.ID.eq(SERVICE_INDEX.SERVICE_ID)) .where(SERVICE_INDEX.ID.eq(serviceIndexId)) .fetchAny(); return record == null ? null : record.into(Service.class); }
@Override public String getBody(Language language, int rawLocalPageId) throws DaoException { DSLContext context = getJooq(); try { return context. select(). from(Tables.RAW_PAGE). where(Tables.RAW_PAGE.PAGE_ID.eq(rawLocalPageId)). and(Tables.RAW_PAGE.LANG_ID.eq(language.getId())). fetchOne(). getValue(Tables.RAW_PAGE.BODY); } finally { freeJooq(context); } }
@Override public Set<NamedToroIndex> getAllToroIndexes(DSLContext dsl) { Result<Record1<NamedToroIndex>> storedIndexes = dsl .select(toroIndexTable.indexColumn) .from(toroIndexTable) .fetch(); Set<NamedToroIndex> result = Sets.newHashSet(); for (Record1<NamedToroIndex> storedIndex : storedIndexes) { result.add(storedIndex.value1()); } return result; }
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)); }
public List<IdAndStatus> getCascade(PartialProcessKey parentKey) { UUID parentInstanceId = parentKey.getInstanceId(); try (DSLContext tx = DSL.using(cfg)) { return tx.withRecursive("children").as( select(PROCESS_QUEUE.INSTANCE_ID, PROCESS_QUEUE.CREATED_AT, PROCESS_QUEUE.CURRENT_STATUS).from(PROCESS_QUEUE) .where(PROCESS_QUEUE.INSTANCE_ID.eq(parentInstanceId)) .unionAll( select(PROCESS_QUEUE.INSTANCE_ID, PROCESS_QUEUE.CREATED_AT, PROCESS_QUEUE.CURRENT_STATUS).from(PROCESS_QUEUE) .join(name("children")) .on(PROCESS_QUEUE.PARENT_INSTANCE_ID.eq( field(name("children", "INSTANCE_ID"), UUID.class))))) .select() .from(name("children")) .fetch(r -> new IdAndStatus(new ProcessKey(r.get(0, UUID.class), r.get(1, Timestamp.class)), ProcessStatus.valueOf(r.get(2, String.class)))); } }
public List<ResourceAccessEntry> getAccessLevel(UUID orgId, String name) { List<ResourceAccessEntry> resourceAccessList = new ArrayList<>(); try (DSLContext tx = DSL.using(cfg)) { Result<Record4<UUID, UUID, String, String>> teamAccess = tx.select( SECRET_TEAM_ACCESS.TEAM_ID, SECRET_TEAM_ACCESS.SECRET_ID, TEAMS.TEAM_NAME, SECRET_TEAM_ACCESS.ACCESS_LEVEL) .from(SECRET_TEAM_ACCESS) .leftOuterJoin(TEAMS).on(TEAMS.TEAM_ID.eq(SECRET_TEAM_ACCESS.TEAM_ID)) .where(SECRET_TEAM_ACCESS.SECRET_ID.eq(getByName(orgId, name).getId())) .fetch(); for (Record4<UUID, UUID, String, String> t : teamAccess) { resourceAccessList.add(new ResourceAccessEntry(t.get(SECRET_TEAM_ACCESS.TEAM_ID), null, t.get(TEAMS.TEAM_NAME), ResourceAccessLevel.valueOf(t.get(SECRET_TEAM_ACCESS.ACCESS_LEVEL)))); } } return resourceAccessList; }
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); }
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); }