Skip to content

Commit b39240e

Browse files
committed
Shift dataset-level constants out of loops
1 parent 30f3271 commit b39240e

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

src/main/java/edu/harvard/iq/dataverse/search/SolrIndexServiceBean.java

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -416,36 +416,45 @@ public IndexResponse indexPermissionsOnSelfAndChildren(DvObject definitionPoint)
416416
for (Dataset dataset : directChildDatasetsOfDvDefPoint) {
417417
indexPermissionsForOneDvObject(dataset);
418418
numObjects++;
419+
420+
Map<DatasetVersion.VersionState, Boolean> desiredCards = searchPermissionsService.getDesiredCards(dataset);
421+
Set<DatasetVersion> datasetVersions = datasetVersionsToBuildCardsFor(dataset);
422+
419423
for (DatasetVersion version : versionsToReIndexPermissionsFor(dataset)) {
420424
for (FileMetadata fmd : version.getFileMetadatas()) {
421425
filesToReindexAsBatch.add(fmd.getDataFile());
422426
i++;
423427
if (i % 100 == 0) {
424-
reindexFilesInBatches(filesToReindexAsBatch);
428+
reindexFilesInBatches(filesToReindexAsBatch, desiredCards, datasetVersions);
425429
filesToReindexAsBatch.clear();
426430
}
427431
if (i % 1000 == 0) {
428432
logger.info("Progress: " + i + "files permissions reindexed");
429433
}
430434
}
431435
}
436+
reindexFilesInBatches(filesToReindexAsBatch, desiredCards, datasetVersions);
432437
logger.info("Progress : dataset " + dataset.getId() + " permissions reindexed");
433438
}
434439
} else if (definitionPoint.isInstanceofDataset()) {
435440
indexPermissionsForOneDvObject(definitionPoint);
436441
numObjects++;
437442
// index files
438443
Dataset dataset = (Dataset) definitionPoint;
444+
Map<DatasetVersion.VersionState, Boolean> desiredCards = searchPermissionsService.getDesiredCards(dataset);
445+
Set<DatasetVersion> datasetVersions = datasetVersionsToBuildCardsFor(dataset);
446+
439447
for (DatasetVersion version : versionsToReIndexPermissionsFor(dataset)) {
440448
for (FileMetadata fmd : version.getFileMetadatas()) {
441449
filesToReindexAsBatch.add(fmd.getDataFile());
442450
i++;
443451
if (i % 100 == 0) {
444-
reindexFilesInBatches(filesToReindexAsBatch);
452+
reindexFilesInBatches(filesToReindexAsBatch, desiredCards, datasetVersions);
445453
filesToReindexAsBatch.clear();
446454
}
447455
}
448456
}
457+
reindexFilesInBatches(filesToReindexAsBatch, desiredCards, datasetVersions);
449458
} else {
450459
indexPermissionsForOneDvObject(definitionPoint);
451460
numObjects++;
@@ -457,64 +466,50 @@ public IndexResponse indexPermissionsOnSelfAndChildren(DvObject definitionPoint)
457466
* @todo Should update timestamps, probably, even thought these are files, see
458467
* https://github.com/IQSS/dataverse/issues/2421
459468
*/
460-
reindexFilesInBatches(filesToReindexAsBatch);
461469
logger.info("Reindexed permissions for " + i + " files and " + numObjects + "datasets/collections");
462470
return new IndexResponse("Number of dvObject permissions indexed for " + definitionPoint
463471
+ ": " + numObjects);
464472
}
465473

466-
private String reindexFilesInBatches(List<DataFile> filesToReindexPermissionsFor) {
474+
private String reindexFilesInBatches(List<DataFile> filesToReindexPermissionsFor,
475+
Map<DatasetVersion.VersionState, Boolean> desiredCards,
476+
Set<DatasetVersion> datasetVersions) {
467477
List<SolrInputDocument> docs = new ArrayList<>();
468-
Map<Long, List<Long>> byParentId = new HashMap<>();
469478
Map<Long, List<String>> permStringByDatasetVersion = new HashMap<>();
470-
int i = 0;
471479
try {
472-
for (DataFile file : filesToReindexPermissionsFor) {
473-
Dataset dataset = (Dataset) file.getOwner();
474-
Map<DatasetVersion.VersionState, Boolean> desiredCards = searchPermissionsService.getDesiredCards(dataset);
475-
for (DatasetVersion datasetVersionFileIsAttachedTo : datasetVersionsToBuildCardsFor(dataset)) {
476-
boolean cardShouldExist = desiredCards.get(datasetVersionFileIsAttachedTo.getVersionState());
477-
if (cardShouldExist) {
480+
// Assume all files have the same owner
481+
if (filesToReindexPermissionsFor.isEmpty()) {
482+
return "No files to reindex";
483+
}
484+
485+
for (DatasetVersion datasetVersionFileIsAttachedTo : datasetVersions) {
486+
boolean cardShouldExist = desiredCards.get(datasetVersionFileIsAttachedTo.getVersionState());
487+
if (cardShouldExist) {
488+
for (DataFile file : filesToReindexPermissionsFor) {
478489
List<String> cachedPermission = permStringByDatasetVersion.get(datasetVersionFileIsAttachedTo.getId());
479490
if (cachedPermission == null) {
480491
logger.finest("no cached permission! Looking it up...");
481-
List<DvObjectSolrDoc> fileSolrDocs = constructDatafileSolrDocs((DataFile) file, permStringByDatasetVersion);
492+
List<DvObjectSolrDoc> fileSolrDocs = constructDatafileSolrDocs(file, permStringByDatasetVersion);
482493
for (DvObjectSolrDoc fileSolrDoc : fileSolrDocs) {
483494
Long datasetVersionId = fileSolrDoc.getDatasetVersionId();
484495
if (datasetVersionId != null) {
485496
permStringByDatasetVersion.put(datasetVersionId, fileSolrDoc.getPermissions());
486497
SolrInputDocument solrDoc = SearchUtil.createSolrDoc(fileSolrDoc);
487498
docs.add(solrDoc);
488-
i++;
489499
}
490500
}
491501
} else {
492502
logger.finest("cached permission is " + cachedPermission);
493-
List<DvObjectSolrDoc> fileSolrDocsBasedOnCachedPermissions = constructDatafileSolrDocs((DataFile) file, permStringByDatasetVersion);
503+
List<DvObjectSolrDoc> fileSolrDocsBasedOnCachedPermissions = constructDatafileSolrDocs(file, permStringByDatasetVersion);
494504
for (DvObjectSolrDoc fileSolrDoc : fileSolrDocsBasedOnCachedPermissions) {
495505
SolrInputDocument solrDoc = SearchUtil.createSolrDoc(fileSolrDoc);
496506
docs.add(solrDoc);
497-
i++;
498507
}
499508
}
500-
if (i % 20 == 0) {
501-
persistToSolr(docs);
502-
docs = new ArrayList<>();
503-
}
504509
}
505510
}
506-
Long parent = file.getOwner().getId();
507-
List<Long> existingList = byParentId.get(parent);
508-
if (existingList == null) {
509-
List<Long> empty = new ArrayList<>();
510-
byParentId.put(parent, empty);
511-
} else {
512-
List<Long> updatedList = existingList;
513-
updatedList.add(file.getId());
514-
byParentId.put(parent, updatedList);
515-
}
516511
}
517-
512+
518513
persistToSolr(docs);
519514
return " " + filesToReindexPermissionsFor.size() + " files indexed across " + docs.size() + " Solr documents ";
520515
} catch (SolrServerException | IOException ex) {

0 commit comments

Comments
 (0)