Added pagination for instances listing

This commit is contained in:
luca.frosini 2023-09-22 16:58:23 +02:00
parent 31f0120c50
commit 38668d8a21
4 changed files with 112 additions and 2 deletions

View File

@ -38,6 +38,8 @@ import org.gcube.informationsystem.resourceregistry.instances.base.entities.Enti
import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement; import org.gcube.informationsystem.resourceregistry.instances.model.ERManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.Operation; import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement; import org.gcube.informationsystem.resourceregistry.instances.model.relations.RelationManagement;
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility; import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility;
@ -496,19 +498,34 @@ public abstract class EntityManagement<E extends Entity, ET extends EntityType>
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
ArrayNode arrayNode = objectMapper.createArrayNode(); ArrayNode arrayNode = objectMapper.createArrayNode();
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
int limit = requestInfo.getLimit();
int offset = requestInfo.getOffset();
int position = -1;
int count = 0;
Iterable<ODocument> iterable = oDatabaseDocument.browseClass(typeName, polymorphic); Iterable<ODocument> iterable = oDatabaseDocument.browseClass(typeName, polymorphic);
for(ODocument vertex : iterable) { for(ODocument vertex : iterable) {
if(++position < offset) {
continue;
}
EntityManagement<?,?> entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(), EntityManagement<?,?> entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(),
oDatabaseDocument, (OVertex) vertex); oDatabaseDocument, (OVertex) vertex);
try { try {
entityManagement.setAsEntryPoint(); entityManagement.setAsEntryPoint();
JsonNode jsonNode = entityManagement.serializeAsJsonNode(); JsonNode jsonNode = entityManagement.serializeAsJsonNode();
arrayNode.add(jsonNode); arrayNode.add(jsonNode);
if(++count >= limit) {
break;
}
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
vertex.toString(), OrientDBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE); vertex.toString(), OrientDBUtility.SHOULD_NOT_OCCUR_ERROR_MESSAGE);
} }
} }
try { try {
return objectMapper.writeValueAsString(arrayNode); return objectMapper.writeValueAsString(arrayNode);
} catch(JsonProcessingException e) { } catch(JsonProcessingException e) {

View File

@ -39,6 +39,8 @@ import org.gcube.informationsystem.resourceregistry.instances.model.Operation;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.EntityManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.EntityManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement;
import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement; import org.gcube.informationsystem.resourceregistry.instances.model.entities.ResourceManagement;
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility;
import org.gcube.informationsystem.resourceregistry.requests.ServerRequestInfo;
import org.gcube.informationsystem.resourceregistry.types.TypesCache; import org.gcube.informationsystem.resourceregistry.types.TypesCache;
import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility; import org.gcube.informationsystem.resourceregistry.utils.MetadataUtility;
import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility; import org.gcube.informationsystem.resourceregistry.utils.OrientDBUtility;
@ -777,11 +779,23 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
} }
protected Collection<JsonNode> serializeEdges(Iterable<ODocument> edges, boolean postFilterPolymorphic) private Collection<JsonNode> serializeEdges(Iterable<ODocument> edges, boolean postFilterPolymorphic)
throws ResourceRegistryException { throws ResourceRegistryException {
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
int limit = requestInfo.getLimit();
int offset = requestInfo.getOffset();
int position = -1;
int count = 0;
// Map<String,JsonNode> visitedSourceResources = new HashMap<>(); // Map<String,JsonNode> visitedSourceResources = new HashMap<>();
List<JsonNode> serilizedEdges = new ArrayList<>(); List<JsonNode> serilizedEdges = new ArrayList<>();
for(ODocument d : edges) { for(ODocument d : edges) {
if(++position < offset) {
continue;
}
OEdge edge = (OEdge) d; OEdge edge = (OEdge) d;
if(postFilterPolymorphic && getOClass().isSubClassOf(typeName)) { if(postFilterPolymorphic && getOClass().isSubClassOf(typeName)) {
@ -792,6 +806,9 @@ public abstract class RelationManagement<T extends EntityManagement<? extends En
oDatabaseDocument, edge); oDatabaseDocument, edge);
// visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources); // visitedSourceResources = relationManagement.fullSerialize(visitedSourceResources);
serilizedEdges.add(relationManagement.serializeAsJsonNode()); serilizedEdges.add(relationManagement.serializeAsJsonNode());
if(++count >= limit) {
break;
}
} }
return serilizedEdges; return serilizedEdges;
} }

View File

@ -220,6 +220,7 @@ public class Access extends BaseRest {
ServerRequestInfo serverRequestInfo = initRequestInfo(); ServerRequestInfo serverRequestInfo = initRequestInfo();
serverRequestInfo.checkAllBooleanQueryParameters(); serverRequestInfo.checkAllBooleanQueryParameters();
serverRequestInfo.checkLimitOffset();
ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(type); ElementManagement<?,?> erManagement = ElementManagementUtility.getERManagement(type);
return erManagement.all(polymorphic); return erManagement.all(polymorphic);

View File

@ -3,15 +3,20 @@
*/ */
package org.gcube.informationsystem.resourceregistry.instances; package org.gcube.informationsystem.resourceregistry.instances;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.gcube.com.fasterxml.jackson.core.JsonParseException;
import org.gcube.com.fasterxml.jackson.databind.JsonMappingException;
import org.gcube.common.encryption.encrypter.StringEncrypter; import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.informationsystem.base.reference.IdentifiableElement; import org.gcube.informationsystem.base.reference.IdentifiableElement;
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl; import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
@ -115,6 +120,8 @@ public class ERManagementTest extends ContextTest {
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get(); ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
requestInfo.setIncludeMeta(true); requestInfo.setIncludeMeta(true);
requestInfo.setAllMeta(true); requestInfo.setAllMeta(true);
requestInfo.setLimit(1000);
requestInfo.setOffset(0);
} }
public static SoftwareFacet getSoftwareFacet() { public static SoftwareFacet getSoftwareFacet() {
@ -730,4 +737,72 @@ public class ERManagementTest extends ContextTest {
logger.debug("{}", ret); logger.debug("{}", ret);
} }
@Test
public void testLimitOffsetWithHostingNode() throws ResourceRegistryException, JsonParseException, JsonMappingException, IOException {
ServerRequestInfo requestInfo = RequestUtility.getRequestInfo().get();
int limit = 2;
requestInfo.setLimit(limit);
requestInfo.setOffset(0);
ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(HostingNode.NAME);
String ret = resourceManagement.all(true);
logger.debug("{}", ret);
List<HostingNode> list = ElementMapper.unmarshalList(HostingNode.class, ret);
if(list.size()==0) {
return;
}
Assert.assertTrue(list.size() < 3);
if(list.size()<2) {
return;
}
Set<UUID> uuids = new HashSet<>();
for(HostingNode hn : list) {
UUID uuid = hn.getID();
uuids.add(uuid);
logger.debug("Found {} with UUID {}", HostingNode.NAME, uuid);
}
requestInfo.setOffset(2);
ret = resourceManagement.all(true);
logger.debug("{}", ret);
list = ElementMapper.unmarshalList(HostingNode.class, ret);
if(list.size()>0) {
Assert.assertTrue(list.size() < 3);
for(HostingNode hn : list) {
UUID uuid = hn.getID();
Assert.assertFalse(uuids.contains(uuid));
uuids.add(uuid);
logger.debug("Found {} with UUID {}", HostingNode.NAME, uuid);
}
if(list.size()<2) {
return;
}
int doubleLimit = limit*2;
requestInfo.setOffset(0);
requestInfo.setLimit(doubleLimit);
ret = resourceManagement.all(true);
logger.debug("{}", ret);
list = ElementMapper.unmarshalList(HostingNode.class, ret);
Assert.assertTrue(list.size() <= doubleLimit);
for(HostingNode hn : list) {
UUID uuid = hn.getID();
logger.debug("Checking if {} with UUID {} was containe din the previous queries", HostingNode.NAME, uuid);
Assert.assertTrue(uuids.contains(uuid));
logger.debug("As expected got {} with UUID {}", HostingNode.NAME, uuid);
}
}
}
} }