/
CursorExecutor.java
195 lines (164 loc) · 8.65 KB
/
CursorExecutor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.communication.semistructured;
import jakarta.data.Sort;
import jakarta.data.page.CursoredPage;
import jakarta.data.page.PageRequest;
import jakarta.data.page.impl.CursoredPageRecord;
import org.eclipse.jnosql.communication.CommunicationException;
import org.eclipse.jnosql.communication.TypeReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
enum CursorExecutor {
OFF_SET {
@SuppressWarnings("unchecked")
@Override
public CursoredPage<CommunicationEntity> cursor(SelectQuery query, PageRequest<?> pageRequest, DatabaseManager template) {
var select = new DefaultSelectQuery(pageRequest.size(), 0, query.name(), query.columns(), query.sorts(),
query.condition().orElse(null));
var entities = template.select(select).toList();
var last = entities.isEmpty() ? null : entities.get(entities.size() - 1);
if (last == null) {
return new CursoredPageRecord<>(entities, Collections.emptyList(), -1, (PageRequest<CommunicationEntity>) pageRequest,
null, null);
} else {
PageRequest.Cursor cursor = getCursor(query.sorts(), last);
PageRequest<CommunicationEntity> afterCursor = PageRequest.<CommunicationEntity>ofSize(pageRequest.size()).afterCursor(cursor);
return new CursoredPageRecord<>(entities, List.of(cursor), -1, (PageRequest<CommunicationEntity>)
pageRequest, afterCursor, null);
}
}
}, CURSOR_NEXT {
@SuppressWarnings("unchecked")
@Override
public CursoredPage<CommunicationEntity> cursor(SelectQuery query, PageRequest<?> pageRequest, DatabaseManager template) {
var cursor = pageRequest.cursor().orElseThrow();
var condition = condition(query, cursor);
var select = updateQuery(pageRequest.size(), query, condition);
var entities = template.select(select).toList();
var last = entities.isEmpty() ? null : entities.get(entities.size() - 1);
if (last == null) {
return new CursoredPageRecord<>(entities, Collections.emptyList(), -1, (PageRequest<CommunicationEntity>) pageRequest,
null, null);
} else {
var nextCursor = getCursor(query.sorts(), last);
var afterCursor = PageRequest.<CommunicationEntity>ofSize(pageRequest.size()).afterCursor(nextCursor);
return new CursoredPageRecord<>(entities, List.of(cursor, nextCursor), -1, (PageRequest<CommunicationEntity>)
pageRequest, afterCursor, null);
}
}
private static CriteriaCondition condition(SelectQuery query, PageRequest.Cursor cursor) {
CriteriaCondition condition = null;
CriteriaCondition previousCondition = null;
List<Sort<?>> sorts = query.sorts();
checkCursorKeySizes(cursor, sorts);
for (int index = 0; index < sorts.size(); index++) {
Sort<?> sort = sorts.get(index);
Object key = cursor.get(index);
if(condition == null) {
condition = CriteriaCondition.gt(sort.property(), key);
previousCondition = CriteriaCondition.eq(sort.property(), key);
} else {
condition = condition.or(previousCondition.and(CriteriaCondition.gt(sort.property(), key)));
previousCondition = previousCondition.and(CriteriaCondition.eq(sort.property(), key));
}
}
return condition;
}
}, CURSOR_PREVIOUS {
@SuppressWarnings("unchecked")
@Override
public CursoredPage<CommunicationEntity> cursor(SelectQuery query, PageRequest<?> pageRequest, DatabaseManager template) {
var cursor = pageRequest.cursor().orElseThrow();
var condition = condition(query, cursor);
var select = updateQuery(pageRequest.size(), query, condition);
var entities = template.select(select).toList();
var last = entities.isEmpty() ? null : entities.get(entities.size() - 1);
if (last == null) {
return new CursoredPageRecord<>(entities, Collections.emptyList(), -1, (PageRequest<CommunicationEntity>) pageRequest,
null, null);
} else {
var beforeCursor = getCursor(query.sorts(), last);
var beforeRequest = PageRequest.<CommunicationEntity>ofSize(pageRequest.size()).beforeCursor(beforeCursor);
return new CursoredPageRecord<>(entities, List.of(beforeCursor, cursor), -1, (PageRequest<CommunicationEntity>)
pageRequest, null, beforeRequest);
}
}
private static CriteriaCondition condition(SelectQuery query, PageRequest.Cursor cursor) {
CriteriaCondition condition = null;
CriteriaCondition previousCondition = null;
List<Sort<?>> sorts = query.sorts();
checkCursorKeySizes(cursor, sorts);
for (int index = 0; index < sorts.size(); index++) {
Sort<?> sort = sorts.get(index);
Object key = cursor.get(index);
if(condition == null) {
condition = CriteriaCondition.lt(sort.property(), key);
previousCondition = CriteriaCondition.eq(sort.property(), key);
} else {
condition = condition.or(previousCondition.and(CriteriaCondition.lt(sort.property(), key)));
previousCondition = previousCondition.and(CriteriaCondition.eq(sort.property(), key));
}
}
return condition;
}
};
abstract CursoredPage<CommunicationEntity> cursor(SelectQuery query, PageRequest<?> pageRequest, DatabaseManager template);
public static CursorExecutor of(PageRequest.Mode value) {
return switch (value) {
case CURSOR_NEXT -> CURSOR_NEXT;
case CURSOR_PREVIOUS -> CURSOR_PREVIOUS;
default -> OFF_SET;
};
}
private static PageRequest.Cursor getCursor(List<Sort<?>> sorts, CommunicationEntity entity) {
List<Object> keys = new ArrayList<>(sorts.size());
for (Sort<?> sort : sorts) {
String[] names = sort.property().split("\\.");
keys.add(value(names, entity));
}
return PageRequest.Cursor.forKey(keys.toArray());
}
private static Object value(String[] names, CommunicationEntity entity) {
Element element = entity.find(names[0])
.orElseThrow(() -> new CommunicationException("The sort name does not exist in the entity: " + names[0]));
return value(names, element, 0);
}
private static Object value(String[] names, Element element, int index) {
if (names.length == 1) {
return element.get();
}
List<Element> elements = element.get(new TypeReference<>() {});
Element subElement = elements.stream().filter(e -> e.name().equals(names[index + 1]))
.findFirst().orElseThrow(() -> new CommunicationException("The sort name does not exist in the entity: " + names[index]));
if (names.length == index + 2) {
return subElement.get();
} else {
return value(names, subElement, index + 1);
}
}
private static DefaultSelectQuery updateQuery(int pageRequest, SelectQuery query, CriteriaCondition condition) {
return new DefaultSelectQuery(pageRequest, 0, query.name(), query.columns(), query.sorts(),
query.condition().map(c -> CriteriaCondition.and(c, condition))
.orElse(condition));
}
private static void checkCursorKeySizes(PageRequest.Cursor cursor, List<Sort<?>> sorts) {
if(sorts.size() != cursor.size()) {
throw new IllegalArgumentException("The cursor size is different from the sort size. Cursor: "
+ cursor.size() + " Sort: " + sorts.size());
}
}
}