From b250355737c3e23433b848e0f254e10b26341adc Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Thu, 7 Mar 2024 20:21:58 +0100 Subject: [PATCH] improved db repository methods --- .../repository/EntityRecordRepository.java | 35 +++++++------------ .../web/service/EntityRecordService.java | 2 +- .../web/service/ZohoSyncService.java | 2 +- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java b/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java index 70844c39..84b130d0 100644 --- a/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java +++ b/entity-management-mongo/src/main/java/eu/europeana/entitymanagement/mongo/repository/EntityRecordRepository.java @@ -20,7 +20,6 @@ import com.mongodb.client.result.UpdateResult; import dev.morphia.query.FindOptions; import dev.morphia.query.Query; -import dev.morphia.query.Sort; import dev.morphia.query.experimental.filters.Filter; import dev.morphia.query.experimental.updates.UpdateOperators; import eu.europeana.entitymanagement.common.vocabulary.AppConfigConstants; @@ -82,7 +81,7 @@ public List findByEntityIds( fields.add(DISABLED); } - return findEntityRecords(filters.toArray(Filter[]::new), false, -1, -1, null, fields.toArray(String[]::new)); + return findEntityRecords(filters.toArray(Filter[]::new), false, fields.toArray(String[]::new)); } @@ -96,7 +95,7 @@ public EntityRecord findByEntityId(String entityId, String[] fields) { List filters = new ArrayList<>(); filters.add(eq(ENTITY_ID, entityId)); - List recordList=findEntityRecords(filters.toArray(Filter[]::new), false, -1, -1, null, fields); + List recordList=findEntityRecords(filters.toArray(Filter[]::new), false, fields); if(recordList.isEmpty()) { return null; } @@ -105,7 +104,7 @@ public EntityRecord findByEntityId(String entityId, String[] fields) { } } - protected List findEntityRecords(Filter[] filters, boolean disableValidation, int start, int count, Sort[] sorts, String[] fields) { + protected List findEntityRecords(Filter[] filters, boolean disableValidation, String[] fields) { Query query = getDataStore().find(EntityRecord.class); if(disableValidation) { @@ -117,15 +116,6 @@ protected List findEntityRecords(Filter[] filters, boolean disable } FindOptions findOptions = new FindOptions(); - if(start>=0) { - findOptions.skip(start); - } - if(count>0) { - findOptions.limit(count); - } - if(sorts!=null && sorts.length>0) { - findOptions.sort(sorts); - } //array must not be empty, invocation of this method with only one parameter uses and empty array if(fields != null && fields.length > 0) { findOptions.projection().include(fields); @@ -198,7 +188,7 @@ public List findEntitiesByCoreference( } //query the database - return findEntityRecords(filters.toArray(Filter[]::new), true, -1, -1, null, null); + return findEntityRecords(filters.toArray(Filter[]::new), true, null); } public List findByEntityIdsOrCoreference(List uris) { @@ -208,7 +198,7 @@ public List findByEntityIdsOrCoreference(List uris) { // Only fetch active records. Disabled records have a date value filters.add(eq(DISABLED, null)); - return findEntityRecords(filters.toArray(Filter[]::new), true, -1, -1, null, null); + return findEntityRecords(filters.toArray(Filter[]::new), true, null); } @@ -224,16 +214,17 @@ public List saveBulk(List entityRecords) { * @param filters Query filters * @return List with results */ - public List findWithFilters(int start, int count, Filter[] filters) { - List sorts=new ArrayList<>(); - sorts.add(ascending(ENTITY_MODIFIED)); - return findEntityRecords(filters, false, start, count, sorts.toArray(Sort[]::new), null); + public List findWithCount(int start, int count, Filter[] filters) { + return getDataStore().find(EntityRecord.class) + .filter(filters) + .iterator(new FindOptions().skip(start).sort(ascending(ENTITY_MODIFIED)).limit(count)) + .toList(); } public List findAll(int start, int count) { - List sorts=new ArrayList<>(); - sorts.add(ascending(ENTITY_MODIFIED)); - return findEntityRecords(null, false, start, count, sorts.toArray(Sort[]::new), null); + return getDataStore().find(EntityRecord.class) + .iterator(new FindOptions().skip(start).sort(ascending(ENTITY_MODIFIED)).limit(count)) + .toList(); } public long deleteBulk(List entityIds) { diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java index 28e8a69f..124b46e4 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/EntityRecordService.java @@ -847,7 +847,7 @@ public void dropRepository() { * @return */ public List findEntitiesWithFilter(int start, int count, Filter[] queryFilters) { - return this.entityRecordRepository.findWithFilters(start, count, queryFilters); + return this.entityRecordRepository.findWithCount(start, count, queryFilters); } private void updateEntityAggregation(EntityRecord entityRecord, String entityId, Date timestamp) { diff --git a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/ZohoSyncService.java b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/ZohoSyncService.java index 85b617c8..fe5221a5 100644 --- a/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/ZohoSyncService.java +++ b/entity-management-web/src/main/java/eu/europeana/entitymanagement/web/service/ZohoSyncService.java @@ -263,7 +263,7 @@ BatchOperations fillOperations(final List orgList) { */ List findEntityRecordsByProxyId(Set modifiedInZoho) { Filter proxyIdsFilter = Filters.in("proxies.proxyId", modifiedInZoho); - return entityRecordRepository.findWithFilters(0, modifiedInZoho.size(), + return entityRecordRepository.findWithCount(0, modifiedInZoho.size(), new Filter[] {proxyIdsFilter}); }