-
Notifications
You must be signed in to change notification settings - Fork 4
Use repository paging for statement retrieval #404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c485f89
d2ed356
344cc7e
0d7d326
e51d635
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,15 +9,20 @@ | |||||||||||
| import dev.learning.xapi.model.Statement; | ||||||||||||
| import dev.learning.xapi.model.StatementResult; | ||||||||||||
| import java.net.URI; | ||||||||||||
| import java.nio.charset.StandardCharsets; | ||||||||||||
| import java.time.Instant; | ||||||||||||
| import java.util.ArrayList; | ||||||||||||
| import java.util.Base64; | ||||||||||||
| import java.util.Collection; | ||||||||||||
| import java.util.List; | ||||||||||||
| import java.util.Objects; | ||||||||||||
| import java.util.Optional; | ||||||||||||
| import java.util.UUID; | ||||||||||||
| import java.util.stream.StreamSupport; | ||||||||||||
| import org.slf4j.Logger; | ||||||||||||
| import org.slf4j.LoggerFactory; | ||||||||||||
| import org.springframework.data.domain.PageRequest; | ||||||||||||
| import org.springframework.data.domain.Pageable; | ||||||||||||
| import org.springframework.data.domain.Slice; | ||||||||||||
| import org.springframework.stereotype.Service; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
|
|
@@ -29,6 +34,8 @@ | |||||||||||
| @Service | ||||||||||||
| public class StatementService { | ||||||||||||
|
|
||||||||||||
| private static final int PAGE_SIZE = 10; | ||||||||||||
|
|
||||||||||||
| private final Logger log = LoggerFactory.getLogger(StatementService.class); | ||||||||||||
|
|
||||||||||||
| private final StatementRepository repository; | ||||||||||||
|
|
@@ -73,12 +80,62 @@ public StatementResult getStatements() { | |||||||||||
|
|
||||||||||||
| log.info("get statements"); | ||||||||||||
|
|
||||||||||||
| // add custom logic here... | ||||||||||||
| return buildStatementResult(0, null); | ||||||||||||
|
|
||||||||||||
| final var statements = StreamSupport.stream(repository.findAll().spliterator(), false).limit(10) | ||||||||||||
| .map(e -> convertToStatement(e)).toList(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return StatementResult.builder().statements(statements).more(URI.create("")).build(); | ||||||||||||
| /** | ||||||||||||
| * Get multiple Statements since a specific time. | ||||||||||||
| * | ||||||||||||
| * @param since return statements stored since this instant (inclusive) | ||||||||||||
| * | ||||||||||||
| * @return populated StatementResults | ||||||||||||
| */ | ||||||||||||
| public StatementResult getStatementsSince(Instant since) { | ||||||||||||
|
|
||||||||||||
| log.info("get statements since: {}", since); | ||||||||||||
|
|
||||||||||||
| return buildStatementResult(0, since); | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Get multiple Statements using a more token. | ||||||||||||
| * | ||||||||||||
| * @param moreToken the more token indicating where to continue retrieval | ||||||||||||
| * | ||||||||||||
| * @return populated StatementResults | ||||||||||||
| */ | ||||||||||||
| public StatementResult getStatementsMore(String moreToken) { | ||||||||||||
|
|
||||||||||||
| log.info("get statements more: {}", moreToken); | ||||||||||||
|
|
||||||||||||
| final var more = decodeMoreToken(moreToken); | ||||||||||||
|
|
||||||||||||
| return buildStatementResult(more.page(), more.since()); | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private StatementResult buildStatementResult(int page, Instant since) { | ||||||||||||
|
|
||||||||||||
| final Pageable pageable = PageRequest.of(page, PAGE_SIZE); | ||||||||||||
|
|
||||||||||||
| final Slice<StatementEntity> slice; | ||||||||||||
| if (since == null) { | ||||||||||||
| slice = repository.findAllByOrderByStoredAscIdAsc(pageable); | ||||||||||||
| } else { | ||||||||||||
| slice = repository.findByStoredGreaterThanEqualOrderByStoredAscIdAsc(since, pageable); | ||||||||||||
|
Comment on lines
+123
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new Useful? React with 👍 / 👎. |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| final var statements = slice.getContent().stream() | ||||||||||||
| .map(this::convertToStatement) | ||||||||||||
| .filter(Objects::nonNull) | ||||||||||||
| .toList(); | ||||||||||||
|
|
||||||||||||
| final var more = slice.hasNext() ? URI.create("/xapi/statements?more=" | ||||||||||||
| + encodeMoreToken(page + 1, since)) : URI.create(""); | ||||||||||||
|
|
||||||||||||
| return StatementResult.builder().statements(statements).more(more).build(); | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -94,8 +151,10 @@ public void processStatement(UUID statementId, Statement statement) { | |||||||||||
|
|
||||||||||||
| // add custom logic here... | ||||||||||||
|
|
||||||||||||
| final Instant stored = Instant.now(); | ||||||||||||
|
|
||||||||||||
| repository.save(new StatementEntity(statementId, | ||||||||||||
| mapper.valueToTree(statement.withId(statementId).withStored(Instant.now())))); | ||||||||||||
| mapper.valueToTree(statement.withId(statementId).withStored(stored)), stored)); | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
@@ -110,24 +169,61 @@ public Collection<UUID> processStatements(List<Statement> statements) { | |||||||||||
|
|
||||||||||||
| final List<Statement> processedStatements = new ArrayList<>(); | ||||||||||||
|
|
||||||||||||
| final Instant stored = Instant.now(); | ||||||||||||
|
|
||||||||||||
| for (final Statement statement : statements) { | ||||||||||||
| log.info("processing statement: {}", statement); | ||||||||||||
|
|
||||||||||||
| if (statement.getId() == null) { | ||||||||||||
| processedStatements.add(statement.withId(UUID.randomUUID()).withStored(Instant.now())); | ||||||||||||
| processedStatements.add(statement.withId(UUID.randomUUID()).withStored(stored)); | ||||||||||||
| } else { | ||||||||||||
| processedStatements.add(statement.withStored(Instant.now())); | ||||||||||||
| processedStatements.add(statement.withStored(stored)); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // add custom logic here... | ||||||||||||
|
|
||||||||||||
| repository.saveAll(processedStatements.stream() | ||||||||||||
| .map(s -> new StatementEntity(s.getId(), mapper.valueToTree(s))).toList()); | ||||||||||||
| .map(s -> new StatementEntity(s.getId(), mapper.valueToTree(s), s.getStored())).toList()); | ||||||||||||
|
|
||||||||||||
| return processedStatements.stream().map(s -> s.getId()).toList(); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private String encodeMoreToken(int page, Instant since) { | ||||||||||||
|
|
||||||||||||
| final var sinceValue = since == null ? "" : since.toString(); | ||||||||||||
| final var payload = page + "|" + sinceValue; | ||||||||||||
|
|
||||||||||||
| return Base64.getUrlEncoder().encodeToString(payload.getBytes(StandardCharsets.UTF_8)); | ||||||||||||
|
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private MoreToken decodeMoreToken(String token) { | ||||||||||||
|
|
||||||||||||
| try { | ||||||||||||
| final var decoded = new String(Base64.getUrlDecoder().decode(token), StandardCharsets.UTF_8); | ||||||||||||
| final var parts = decoded.split("\\|", -1); | ||||||||||||
|
|
||||||||||||
|
||||||||||||
| if (parts.length < 1 || parts[0].isBlank()) { | |
| throw new IllegalArgumentException("Invalid more token format: missing page number"); | |
| } |
Uh oh!
There was an error while loading. Please reload this page.