Skip to content

Commit f25a650

Browse files
authored
Merge pull request #283 from eclipse/550-arangodb-pagination
ArangoDB pagination [issues]
2 parents 5b5e833 + fb2dd0b commit f25a650

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/DefaultArangoDBDocumentManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public void delete(DeleteQuery query) {
123123
@Override
124124
public Stream<CommunicationEntity> select(SelectQuery query) throws NullPointerException {
125125
requireNonNull(query, "query is required");
126-
127126
AQLQueryResult result = QueryAQLConverter.select(query);
127+
LOGGER.finest("Executing AQL: " + result.query());
128128
ArangoCursor<BaseDocument> documents = arangoDB.db(database).query(result.query(),
129129
BaseDocument.class,
130130
result.values(), null);

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/QueryAQLConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ private static AQLQueryResult convert(String documentCollection,
9898
.append(", ").append(maxResult);
9999
} else if (maxResult > 0) {
100100
aql.append(LIMIT).append(maxResult);
101+
} else if(firstResult > 0) {
102+
aql.append(LIMIT).append(firstResult).append(", null");
101103
}
102104

103105
aql.append(conclusion).append(entity);

jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.jnosql.communication.semistructured.Elements;
2525
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
2626
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.Assertions;
2728
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
2930
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
@@ -36,6 +37,7 @@
3637
import java.util.Optional;
3738
import java.util.Random;
3839
import java.util.stream.Collectors;
40+
import java.util.stream.Stream;
3941

4042
import static java.util.Arrays.asList;
4143
import static java.util.Collections.singletonMap;
@@ -278,6 +280,48 @@ void shouldDeleteAll() {
278280
assertThat(entities).isEmpty();
279281
}
280282

283+
284+
@Test
285+
void shouldIncludeLimit() {
286+
for (int index = 0; index < 20; index++) {
287+
var entity = getEntity();
288+
entity.add("index", index);
289+
entityManager.insert(entity);
290+
}
291+
var select = select().from(COLLECTION_NAME).orderBy("index").asc().limit(4).build();
292+
var entities = entityManager.select(select).toList();
293+
var indexes = entities.stream().map(e -> e.find("index").orElseThrow().get()).toList();
294+
org.assertj.core.api.Assertions.assertThat(indexes).hasSize(4).contains(0, 1, 2, 3);
295+
DeleteQuery deleteQuery = delete().from(COLLECTION_NAME).build();
296+
entityManager.delete(deleteQuery);
297+
}
298+
299+
@Test
300+
void shouldIncludeSkipLimit() {
301+
for (int index = 0; index < 20; index++) {
302+
var entity = getEntity();
303+
entity.add("index", index);
304+
entityManager.insert(entity);
305+
}
306+
var select = select().from(COLLECTION_NAME).orderBy("index").asc().skip(3).limit(4).build();
307+
var entities = entityManager.select(select).toList();
308+
var indexes = entities.stream().map(e -> e.find("index").orElseThrow().get()).toList();
309+
org.assertj.core.api.Assertions.assertThat(indexes).hasSize(4).contains(3, 4, 5, 6);
310+
}
311+
312+
@Test
313+
void shouldIncludeSkip() {
314+
for (int index = 0; index < 20; index++) {
315+
var entity = getEntity();
316+
entity.add("index", index);
317+
entityManager.insert(entity);
318+
}
319+
var select = select().from(COLLECTION_NAME).orderBy("index").asc().skip(5).build();
320+
var entities = entityManager.select(select).toList();
321+
var indexes = entities.stream().map(e -> e.find("index").orElseThrow().get()).toList();
322+
org.assertj.core.api.Assertions.assertThat(indexes).hasSize(15);
323+
}
324+
281325
private CommunicationEntity getEntity() {
282326
CommunicationEntity entity = CommunicationEntity.of(COLLECTION_NAME);
283327
Map<String, Object> map = new HashMap<>();
@@ -307,4 +351,4 @@ private CommunicationEntity createDocumentList() {
307351
return entity;
308352
}
309353

310-
}
354+
}

jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/integration/ArangoDBTemplateIntegrationTest.java

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package org.eclipse.jnosql.databases.arangodb.integration;
1616

1717

18+
import jakarta.data.page.CursoredPage;
19+
import jakarta.data.page.PageRequest;
1820
import jakarta.inject.Inject;
1921
import org.assertj.core.api.SoftAssertions;
22+
import org.eclipse.jnosql.communication.semistructured.SelectQuery;
2023
import org.eclipse.jnosql.databases.arangodb.communication.ArangoDBConfigurations;
2124
import org.eclipse.jnosql.databases.arangodb.mapping.ArangoDBTemplate;
2225
import org.eclipse.jnosql.mapping.Database;
@@ -30,9 +33,11 @@
3033
import org.jboss.weld.junit5.auto.AddExtensions;
3134
import org.jboss.weld.junit5.auto.AddPackages;
3235
import org.jboss.weld.junit5.auto.EnableAutoWeld;
36+
import org.junit.jupiter.api.BeforeEach;
3337
import org.junit.jupiter.api.Test;
3438
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3539

40+
import java.util.List;
3641
import java.util.Optional;
3742

3843
import static java.util.UUID.randomUUID;
@@ -61,6 +66,11 @@ class ArangoDBTemplateIntegrationTest {
6166
System.setProperty(MappingConfigurations.DOCUMENT_DATABASE.get(), "library");
6267
}
6368

69+
@BeforeEach
70+
void setUp() {
71+
this.template.delete(Book.class).execute();
72+
}
73+
6474
@Test
6575
void shouldInsert() {
6676
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
@@ -112,7 +122,7 @@ void shouldDelete() {
112122
}
113123

114124
@Test
115-
void shouldDeleteAll(){
125+
void shouldDeleteAll() {
116126
for (int index = 0; index < 20; index++) {
117127
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
118128
assertThat(template.insert(book))
@@ -126,7 +136,7 @@ void shouldDeleteAll(){
126136

127137

128138
@Test
129-
void shouldUpdateNullValues(){
139+
void shouldUpdateNullValues() {
130140
var book = new Book(randomUUID().toString(), "Effective Java", 1);
131141
template.insert(book);
132142
template.update(new Book(book.id(), null, 2));
@@ -138,7 +148,76 @@ void shouldUpdateNullValues(){
138148
softly.assertThat(optional).get().extracting(Book::edition).isEqualTo(2);
139149
});
140150
}
141-
142151

152+
@Test
153+
void shouldExecuteLimit() {
154+
155+
for (int index = 1; index < 10; index++) {
156+
var book = new Book(randomUUID().toString(), "Effective Java", index);
157+
template.insert(book);
158+
}
159+
160+
List<Book> books = template.select(Book.class).orderBy("edition")
161+
.asc().limit(4).result();
162+
163+
SoftAssertions.assertSoftly(soft -> {
164+
soft.assertThat(books).hasSize(4);
165+
var editions = books.stream().map(Book::edition).toList();
166+
soft.assertThat(editions).hasSize(4).contains(1, 2, 3, 4);
167+
});
168+
169+
}
170+
171+
@Test
172+
void shouldExecuteSkip() {
173+
for (int index = 1; index < 10; index++) {
174+
var book = new Book(randomUUID().toString(), "Effective Java", index);
175+
template.insert(book);
176+
}
177+
178+
List<Book> books = template.select(Book.class).orderBy("edition")
179+
.asc().skip(4).result();
180+
181+
SoftAssertions.assertSoftly(soft -> {
182+
soft.assertThat(books).hasSize(5);
183+
var editions = books.stream().map(Book::edition).toList();
184+
soft.assertThat(editions).hasSize(5).contains(5, 6, 7, 8, 9);
185+
});
186+
}
187+
188+
@Test
189+
void shouldExecuteLimitStart() {
190+
for (int index = 1; index < 10; index++) {
191+
var book = new Book(randomUUID().toString(), "Effective Java", index);
192+
template.insert(book);
193+
}
194+
195+
List<Book> books = template.select(Book.class).orderBy("edition")
196+
.asc().skip(4).limit(3).result();
143197

198+
SoftAssertions.assertSoftly(soft -> {
199+
soft.assertThat(books).hasSize(3);
200+
var editions = books.stream().map(Book::edition).toList();
201+
soft.assertThat(editions).hasSize(3).contains(5, 6, 7);
202+
});
203+
}
204+
205+
@Test
206+
void shouldSelectCursorSize() {
207+
for (int index = 1; index < 10; index++) {
208+
var book = new Book(randomUUID().toString(), "Effective Java", index);
209+
template.insert(book);
210+
}
211+
var select = SelectQuery.select().from("Book").orderBy("edition").asc()
212+
.skip(4).limit(3).build();
213+
var pageRequest = PageRequest.ofSize(3);
214+
CursoredPage<Book> entities = template.selectCursor(select, pageRequest);
215+
216+
SoftAssertions.assertSoftly(soft -> {
217+
var content = entities.content();
218+
soft.assertThat(content).hasSize(3);
219+
var editions = content.stream().map(Book::edition).toList();
220+
soft.assertThat(editions).hasSize(3).contains(1, 2, 3);
221+
});
222+
}
144223
}

0 commit comments

Comments
 (0)