Skip to content

Commit

Permalink
- add id to upload range
Browse files Browse the repository at this point in the history
  • Loading branch information
psmagin committed Jul 29, 2024
1 parent e1d5804 commit 65bc576
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package org.folio.search.model.reindex;

import java.sql.Timestamp;
import java.util.UUID;
import lombok.Data;
import org.folio.search.model.types.ReindexEntityType;

@Data
public class UploadRangeEntity {

public static final String UPLOAD_RANGE_TABLE = "upload_range";
public static final String ID_COLUMN = "id";
public static final String ENTITY_TYPE_COLUMN = "entity_type";
public static final String RANGE_LIMIT_COLUMN = "range_limit";
public static final String RANGE_OFFSET_COLUMN = "range_offset";
public static final String CREATED_AT_COLUMN = "created_at";
public static final String FINISHED_AT_COLUMN = "finished_at";

private final UUID id;
private final ReindexEntityType entityType;
private final int limit;
private final int offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.folio.search.model.reindex.UploadRangeEntity.CREATED_AT_COLUMN;
import static org.folio.search.model.reindex.UploadRangeEntity.ENTITY_TYPE_COLUMN;
import static org.folio.search.model.reindex.UploadRangeEntity.FINISHED_AT_COLUMN;
import static org.folio.search.model.reindex.UploadRangeEntity.ID_COLUMN;
import static org.folio.search.model.reindex.UploadRangeEntity.RANGE_LIMIT_COLUMN;
import static org.folio.search.model.reindex.UploadRangeEntity.RANGE_OFFSET_COLUMN;
import static org.folio.search.model.reindex.UploadRangeEntity.UPLOAD_RANGE_TABLE;
Expand All @@ -12,6 +13,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.folio.search.configuration.properties.ReindexConfigurationProperties;
import org.folio.search.model.reindex.UploadRangeEntity;
import org.folio.search.model.types.ReindexEntityType;
Expand All @@ -22,14 +24,14 @@
public abstract class ReindexJdbcRepository {

private static final String UPSERT_UPLOAD_RANGE_SQL = """
INSERT INTO %s (entity_type, range_limit, range_offset, created_at, finished_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT (entity_type, range_limit, range_offset)
INSERT INTO %s (id, entity_type, range_limit, range_offset, created_at, finished_at)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT (id)
DO UPDATE SET finished_at = EXCLUDED.finished_at;
""";

private static final String SELECT_UPLOAD_RANGE_BY_ENTITY_TYPE_SQL = "select * from %s where entity_type = ?;";
private static final String COUNT_SQL = "select count(*) from %s;";
private static final String SELECT_UPLOAD_RANGE_BY_ENTITY_TYPE_SQL = "SELECT * FROM %s WHERE entity_type = ?;";
private static final String COUNT_SQL = "SELECT COUNT(*) FROM %s;";

private final JdbcTemplate jdbcTemplate;
private final FolioExecutionContext context;
Expand Down Expand Up @@ -57,11 +59,12 @@ public void upsertUploadRanges(List<UploadRangeEntity> uploadRanges) {
var fullTableName = getFullTableName(context, UPLOAD_RANGE_TABLE);
jdbcTemplate.batchUpdate(UPSERT_UPLOAD_RANGE_SQL.formatted(fullTableName), uploadRanges, 100,
(statement, entity) -> {
statement.setString(1, entity.getEntityType().name());
statement.setInt(2, entity.getLimit());
statement.setInt(3, entity.getOffset());
statement.setTimestamp(4, entity.getCreatedAt());
statement.setTimestamp(5, entity.getFinishedAt());
statement.setObject(1, entity.getId());
statement.setString(2, entity.getEntityType().name());
statement.setInt(3, entity.getLimit());
statement.setInt(4, entity.getOffset());
statement.setTimestamp(5, entity.getCreatedAt());
statement.setTimestamp(6, entity.getFinishedAt());
});
}

Expand All @@ -78,6 +81,7 @@ public Integer countEntities() {
private RowMapper<UploadRangeEntity> uploadRangeRowMapper() {
return (rs, rowNum) -> {
var uploadRange = new UploadRangeEntity(
UUID.fromString(rs.getString(ID_COLUMN)),
ReindexEntityType.valueOf(rs.getString(ENTITY_TYPE_COLUMN)),
rs.getInt(RANGE_LIMIT_COLUMN),
rs.getInt(RANGE_OFFSET_COLUMN),
Expand All @@ -96,7 +100,7 @@ private List<UploadRangeEntity> prepareAndSaveUploadRanges() {
for (int i = 0; i < pages; i++) {
int offset = i * rangeSize;
int limit = Math.min(rangeSize, totalRecords - offset);
ranges.add(new UploadRangeEntity(entityType(), limit, offset, Timestamp.from(Instant.now())));
ranges.add(new UploadRangeEntity(UUID.randomUUID(), entityType(), limit, offset, Timestamp.from(Instant.now())));
}

upsertUploadRanges(ranges);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,17 @@
<comment>Create upload_ranges table</comment>

<createTable tableName="upload_range">
<column name="entity_type" type="VARCHAR(30)">
<column name="id" type="UUID">
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_upload_ranges"/>
</column>
<column name="entity_type" type="VARCHAR(30)">
<constraints nullable="false"/>
</column>
<column name="range_limit" type="integer">
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_upload_ranges"/>
<constraints nullable="false"/>
</column>
<column name="range_offset" type="integer">
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_upload_ranges"/>
<constraints nullable="false"/>
</column>
<column name="created_at" type="DATETIME" defaultValueComputed="CURRENT_TIMESTAMP">
<constraints nullable="false"/>
Expand Down

0 comments on commit 65bc576

Please sign in to comment.