SimpleCatalogManager implements CatalogManager { @Override public PaginatedSearchResults<Record> findRecord(Resource catalogId, PaginatedSearchParams searchParams) { try (RepositoryConnection conn = configProvider.getRepository().getConnection()) { Optional<Resource> typeParam = searchParams.getTypeFilter(); Optional<String> searchTextParam = searchParams.getSearchText(); TupleQuery countQuery = conn.prepareTupleQuery(COUNT_RECORDS_QUERY); countQuery.setBinding(CATALOG_BINDING, catalogId); typeParam.ifPresent(resource -> countQuery.setBinding(TYPE_FILTER_BINDING, resource)); searchTextParam.ifPresent(s -> countQuery.setBinding(SEARCH_BINDING, vf.createLiteral(s))); TupleQueryResult countResults = countQuery.evaluate(); int totalCount; BindingSet countBindingSet; if (countResults.hasNext() && (countBindingSet = countResults.next()).getBindingNames().contains(RECORD_COUNT_BINDING)) { totalCount = Bindings.requiredLiteral(countBindingSet, RECORD_COUNT_BINDING).intValue(); countResults.close(); } else { countResults.close(); conn.close(); return SearchResults.emptyResults(); } log.debug("Record count: " + totalCount); int offset = searchParams.getOffset(); int limit = searchParams.getLimit().orElse(totalCount); if (offset > totalCount) { throw new IllegalArgumentException("Offset exceeds total size"); } StringBuilder querySuffix = new StringBuilder("\nORDER BY "); Resource sortByParam = searchParams.getSortBy().orElse(vf.createIRI(_Thing.modified_IRI)); StringBuilder binding = new StringBuilder(); if (sortByParam.equals(vf.createIRI(_Thing.title_IRI))) { binding.append("lcase(?").append(sortingOptions.getOrDefault(sortByParam, "modified")).append(")"); } else { binding.append("?").append(sortingOptions.getOrDefault(sortByParam, "modified")); } Optional<Boolean> ascendingParam = searchParams.getAscending(); if (ascendingParam.isPresent() && ascendingParam.get()) { querySuffix.append(binding); } else { querySuffix.append("DESC(").append(binding).append(")"); } querySuffix.append("\nLIMIT ").append(limit).append("\nOFFSET ").append(offset); String queryString = FIND_RECORDS_QUERY + querySuffix.toString(); TupleQuery query = conn.prepareTupleQuery(queryString); query.setBinding(CATALOG_BINDING, catalogId); typeParam.ifPresent(resource -> query.setBinding(TYPE_FILTER_BINDING, resource)); searchTextParam.ifPresent(searchText -> query.setBinding(SEARCH_BINDING, vf.createLiteral(searchText))); log.debug("Query String:\n" + queryString); log.debug("Query Plan:\n" + query); TupleQueryResult result = query.evaluate(); List<Record> records = new ArrayList<>(); result.forEach(bindings -> { Resource resource = vf.createIRI(Bindings.requiredResource(bindings, RECORD_BINDING) .stringValue()); records.add(utils.getRecord(catalogId, resource, recordFactory, conn)); }); result.close(); log.debug("Result set size: " + records.size()); int pageNumber = (limit > 0) ? (offset / limit) + 1 : 1; return records.size() > 0 ? new SimpleSearchResults<>(records, totalCount, limit, pageNumber) : SearchResults.emptyResults(); } } SimpleCatalogManager(); @Override Catalog getDistributedCatalog(); @Override Catalog getLocalCatalog(); @Override PaginatedSearchResults<Record> findRecord(Resource catalogId, PaginatedSearchParams searchParams); @Override Set<Resource> getRecordIds(Resource catalogId); @Override T createRecord(User user, RecordOperationConfig config, Class<T> recordClass); @Override T createRecord(RecordConfig config, OrmFactory<T> factory); @Override void addRecord(Resource catalogId, T record); @Override void updateRecord(Resource catalogId, T newRecord); @Override T removeRecord(Resource catalogId, Resource recordId, OrmFactory<T> factory); @Override T deleteRecord(User user, Resource recordId, Class<T> recordClass); @Override Optional<T> getRecord(Resource catalogId, Resource recordId, OrmFactory<T> factory); @Override Set<Distribution> getUnversionedDistributions(Resource catalogId, Resource unversionedRecordId); @Override Distribution createDistribution(DistributionConfig config); @Override void addUnversionedDistribution(Resource catalogId, Resource unversionedRecordId,
                                           Distribution distribution); @Override void updateUnversionedDistribution(Resource catalogId, Resource unversionedRecordId,
                                              Distribution newDistribution); @Override void removeUnversionedDistribution(Resource catalogId, Resource unversionedRecordId,
                                              Resource distributionId); @Override Optional<Distribution> getUnversionedDistribution(Resource catalogId, Resource unversionedRecordId,
                                                             Resource distributionId); @Override Set<Version> getVersions(Resource catalogId, Resource versionedRecordId); @Override T createVersion(@Nonnull String title, String description, OrmFactory<T> factory); @Override void addVersion(Resource catalogId, Resource versionedRecordId, T version); @Override void updateVersion(Resource catalogId, Resource versionedRecordId, T newVersion); @Override void removeVersion(Resource catalogId, Resource versionedRecordId, Resource versionId); @Override Optional<T> getVersion(Resource catalogId, Resource versionedRecordId,
                                                      Resource versionId, OrmFactory<T> factory); @Override Optional<T> getLatestVersion(Resource catalogId, Resource versionedRecordId,
                                                            OrmFactory<T> factory); @Override Commit getTaggedCommit(Resource catalogId, Resource versionedRecordId, Resource versionId); @Override Set<Distribution> getVersionedDistributions(Resource catalogId, Resource versionedRecordId,
                                                       Resource versionId); @Override void addVersionedDistribution(Resource catalogId, Resource versionedRecordId, Resource versionId,
                                         Distribution distribution); @Override void updateVersionedDistribution(Resource catalogId, Resource versionedRecordId, Resource versionId,
                                            Distribution newDistribution); @Override void removeVersionedDistribution(Resource catalogId, Resource versionedRecordId, Resource versionId,
                                            Resource distributionId); @Override Optional<Distribution> getVersionedDistribution(Resource catalogId, Resource recordId, Resource versionId,
                                                           Resource distributionId); @Override Set<Branch> getBranches(Resource catalogId, Resource versionedRDFRecordId); @Override T createBranch(@Nonnull String title, String description, OrmFactory<T> factory); @Override void addBranch(Resource catalogId, Resource versionedRDFRecordId, T branch); @Override void addMasterBranch(Resource catalogId, Resource versionedRDFRecordId); @Override void updateBranch(Resource catalogId, Resource versionedRDFRecordId, T newBranch); @Override void updateHead(Resource catalogId, Resource versionedRDFRecordId, Resource branchId, Resource commitId); @Override List<Resource> removeBranch(Resource catalogId, Resource versionedRDFRecordId, Resource branchId); @Override Optional<T> getBranch(Resource catalogId, Resource versionedRDFRecordId,
                                                    Resource branchId, OrmFactory<T> factory); @Override Branch getMasterBranch(Resource catalogId, Resource versionedRDFRecordId); @Override Commit createCommit(@Nonnull InProgressCommit inProgressCommit, @Nonnull String message, Commit baseCommit,
                               Commit auxCommit); @Override InProgressCommit createInProgressCommit(User user); @Override void updateInProgressCommit(Resource catalogId, Resource versionedRDFRecordId, Resource commitId,
                                       @Nullable Model additions, @Nullable Model deletions); @Override void updateInProgressCommit(Resource catalogId, Resource versionedRDFRecordId, User user,
                                       @Nullable Model additions, @Nullable Model deletions); @Override void addInProgressCommit(Resource catalogId, Resource versionedRDFRecordId,
                                    InProgressCommit inProgressCommit); @Override Optional<Commit> getCommit(Resource commitId); @Override Optional<Commit> getCommit(Resource catalogId, Resource versionedRDFRecordId, Resource branchId,
                                      Resource commitId); @Override Commit getHeadCommit(Resource catalogId, Resource versionedRDFRecordId, Resource branchId); @Override Optional<InProgressCommit> getInProgressCommit(Resource catalogId, Resource versionedRDFRecordId,
                                                          User user); @Override Optional<InProgressCommit> getInProgressCommit(Resource catalogId, Resource versionedRDFRecordId,
                                                          Resource inProgressCommitId); @Override Revision getRevision(Resource commitId); @Override Difference getRevisionChanges(Resource commitId); @Override Difference getCommitDifference(Resource commitId); @Override void removeInProgressCommit(Resource catalogId, Resource versionedRDFRecordId,
                                       Resource inProgressCommitId); @Override void removeInProgressCommit(Resource catalogId, Resource versionedRDFRecordId, User user); @Override Model applyInProgressCommit(Resource inProgressCommitId, Model entity); @Override List<Commit> getCommitChain(Resource commitId); @Override List<Commit> getCommitChain(Resource commitId, Resource targetId); @Override List<Commit> getCommitChain(Resource catalogId, Resource versionedRDFRecordId, Resource branchId); @Override List<Commit> getCommitChain(Resource catalogId, Resource versionedRDFRecordId, Resource branchId,
                                       final Resource targetBranchId); @Override List<Commit> getCommitEntityChain(Resource commitId, Resource entityId); @Override List<Commit> getCommitEntityChain(Resource commitId, Resource targetId, Resource entityId); @Override Model getCompiledResource(Resource commitId); @Override Model getCompiledResource(List<Commit> commitList); @Override Model getCompiledResource(Resource versionedRDFRecordId, Resource branchId, Resource commitId); @Override Difference getDifference(Resource sourceCommitId, Resource targetCommitId); @Override Set<Conflict> getConflicts(Resource leftId, Resource rightId); @Override Difference getDiff(Model original, Model changed); @Override void export(Resource recordIRI, RecordOperationConfig config); @Override void export(List<Resource> recordIRIs, RecordOperationConfig config);  }
@Test public void testFindRecordsReturnsCorrectDataFirstPage() throws Exception { int limit = 1; int offset = 0; PaginatedSearchParams searchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).build(); PaginatedSearchResults<Record> records = manager.findRecord(distributedCatalogId, searchParams); assertEquals(1, records.getPage().size()); assertEquals(TOTAL_SIZE, records.getTotalSize()); assertEquals(1, records.getPageSize()); assertEquals(1, records.getPageNumber()); }
@Test public void testFindRecordsReturnsCorrectDataSecondPage() throws Exception { int limit = 1; int offset = 1; PaginatedSearchParams searchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).build(); PaginatedSearchResults<Record> records = manager.findRecord(distributedCatalogId, searchParams); assertEquals(1, records.getPage().size()); assertEquals(TOTAL_SIZE, records.getTotalSize()); assertEquals(1, records.getPageSize()); assertEquals(2, records.getPageNumber()); }
@Test public void testFindRecordsReturnsCorrectDataOnePage() throws Exception { int limit = 1000; int offset = 0; PaginatedSearchParams searchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).build(); PaginatedSearchResults<Record> records = manager.findRecord(distributedCatalogId, searchParams); assertEquals(TOTAL_SIZE, records.getPage().size()); assertEquals(TOTAL_SIZE, records.getTotalSize()); assertEquals(1000, records.getPageSize()); assertEquals(1, records.getPageNumber()); }
@Test public void testFindRecordsWithSearchText() throws Exception { int limit = 10; int offset = 0; PaginatedSearchParams searchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).searchText("Unversioned").build(); PaginatedSearchResults<Record> records = manager.findRecord(distributedCatalogId, searchParams); assertEquals(2, records.getPage().size()); assertEquals(2, records.getTotalSize()); assertEquals(10, records.getPageSize()); assertEquals(1, records.getPageNumber()); }
@Test public void testFindRecordWithEmptyRepository() throws Exception { Repository repo2 = new SesameRepositoryWrapper(new SailRepository(new MemoryStore())); repo2.initialize(); when(configProvider.getRepository()).thenReturn(repo2); int limit = 1; int offset = 0; PaginatedSearchParams searchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).build(); PaginatedSearchResults<Record> records = manager.findRecord(distributedCatalogId, searchParams); assertEquals(0, records.getPage().size()); assertEquals(0, records.getTotalSize()); }
@Test public void testFindRecordWithNoEntries() throws Exception { int limit = 1; int offset = 0; PaginatedSearchParams searchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).build(); PaginatedSearchResults<Record> records = manager.findRecord(localCatalogId, searchParams); assertEquals(0, records.getPage().size()); assertEquals(0, records.getTotalSize()); }
@Test public void testFindRecordsWithTypeFilter() throws Exception { int limit = 1000; int offset = 0; PaginatedSearchParams versionedSearchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).typeFilter(versionedRecordFactory.getTypeIRI()).build(); PaginatedSearchParams unversionedSearchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).typeFilter(unversionedRecordFactory.getTypeIRI()).build(); PaginatedSearchParams fullSearchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).build(); PaginatedSearchResults<Record> versionedRecords = manager.findRecord(distributedCatalogId, versionedSearchParams); PaginatedSearchResults<Record> unversionedRecords = manager.findRecord(distributedCatalogId, unversionedSearchParams); PaginatedSearchResults<Record> fullRecords = manager.findRecord(distributedCatalogId, fullSearchParams); assertTrue(true); assertEquals(6, versionedRecords.getPage().size()); assertEquals(6, versionedRecords.getTotalSize()); assertEquals(2, unversionedRecords.getPage().size()); assertEquals(2, unversionedRecords.getTotalSize()); assertEquals(TOTAL_SIZE, fullRecords.getPage().size()); assertEquals(TOTAL_SIZE, fullRecords.getTotalSize()); }
@Test(expected = IllegalArgumentException.class) public void testFindRecordWithOffsetThatIsTooLarge() { int limit = 10; int offset = 100; PaginatedSearchParams searchParams = new PaginatedSearchParams.Builder().limit(limit).offset(offset).build(); manager.findRecord(distributedCatalogId, searchParams); }