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 void delete(final TableReference tableRef, final Multimap<Cell, Long> keys) { if (keys.isEmpty()) { return; } for (List<Entry<Cell, Long>> partition : Iterables.partition(keys.entries(), batchSizeForMutations)) { run((Function<DSLContext, Void>) ctx -> { Collection<Row3<byte[], byte[], Long>> rows = Lists.newArrayListWithCapacity(partition.size()); for (Entry<Cell, Long> entry : partition) { rows.add(row(entry.getKey().getRowName(), entry.getKey().getColumnName(), entry.getValue())); } ctx.deleteFrom(atlasTable(tableRef).as(ATLAS_TABLE)) .where(row(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP).in(rows)) .execute(); return null; }); } }
@Override public void putMetadataForTables(final Map<TableReference, byte[]> tableRefToMetadata) { if (tableRefToMetadata.isEmpty()) { return; } run((Function<DSLContext, Void>) ctx -> { Query query = ctx .update(METADATA_TABLE) .set(METADATA, (byte[]) null) .where(TABLE_NAME.eq((String) null)); BatchBindStep batch = ctx.batch(query); for (Entry<TableReference, byte[]> entry : tableRefToMetadata.entrySet()) { batch = batch.bind(entry.getValue(), entry.getKey().getQualifiedName()); } batch.execute(); return null; }); }
@Override public void dropTables(final Set<TableReference> tableRefs) throws InsufficientConsistencyException { if (tableRefs.isEmpty()) { return; } run((Function<DSLContext, Void>) ctx -> { for (TableReference tableRef : tableRefs) { ctx.dropTableIfExists(tableName(tableRef)).execute(); } ctx.deleteFrom(METADATA_TABLE) .where(TABLE_NAME.in(tableRefs)) .execute(); return null; }); }
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); }
@Override public int numUsers(String i) { return dsl .selectCount() .from(DATA).naturalJoin(ITEMS) .where(ITEM_ID.eq(i)) .fetchOne().value1(); }
@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 List<String> getServiceNames() { try (Connection conn = datasource.getConnection()) { return context.get(conn) .selectDistinct(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME) .from(ZIPKIN_ANNOTATIONS) .where(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME.isNotNull() .and(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME.ne(""))) .fetch(ZIPKIN_ANNOTATIONS.ENDPOINT_SERVICE_NAME); } catch (SQLException e) { throw new RuntimeException("Error querying for " + e + ": " + e.getMessage()); } }
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)))))
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; }
private void cleanupServiceEventTable(Date cutoff) { ResultQuery<Record1<Long>> ids = create() .select(SERVICE_EVENT.ID) .from(SERVICE_EVENT) .where(SERVICE_EVENT.CREATED.lt(cutoff)) .and(SERVICE_EVENT.STATE.eq(CommonStatesConstants.CREATED)) .limit(QUERY_LIMIT_ROWS.getValue()); List<Long> toDelete = null; int rowsDeleted = 0; while ((toDelete = ids.fetch().into(Long.class)).size() > 0) { rowsDeleted += create().delete(SERVICE_EVENT) .where(SERVICE_EVENT.ID.in(toDelete)).execute(); } if (rowsDeleted > 0) { log.info("[Rows Deleted] service_event={}", rowsDeleted); } }
@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); }
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; }
@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)); }
public Set<UUID> getOrgIds(UUID userId) { try (DSLContext tx = DSL.using(cfg)) { SelectConditionStep<Record1<UUID>> teamIds = select(USER_TEAMS.TEAM_ID) .from(USER_TEAMS) .where(USER_TEAMS.USER_ID.eq(userId)); return tx.selectDistinct(TEAMS.ORG_ID) .from(TEAMS) .where(TEAMS.TEAM_ID.in(teamIds)) .fetchSet(TEAMS.ORG_ID); } }
@Override public Long incrementRevision(long accountId) { Record1<Long> row = create().select(ACCOUNT.REVISION) .from(ACCOUNT) .where(ACCOUNT.ID.eq(accountId)) .fetchAny(); if (row == null) { return 0L; } create().update(ACCOUNT) .set(ACCOUNT.REVISION, ACCOUNT.REVISION.plus(1)) .where(ACCOUNT.ID.eq(accountId)) .execute(); return row.value1() + 1; } }
@Override public boolean canCreate(Long hostId, String event) { if (!ContainerEventConstants.EVENT_START.equals(event)) { return true; } Record1<Integer> count = create().select(DSL.count()) .from(CONTAINER_EVENT) .where(CONTAINER_EVENT.HOST_ID.eq(hostId) .and(CONTAINER_EVENT.STATE.notEqual(CommonStatesConstants.CREATED))) .fetchAny(); return count.value1() < MAX_EVENTS.get(); }