Skip to content

Commit

Permalink
Merge #104 feature/uncaching-endpoint into test/rpa_uncaching
Browse files Browse the repository at this point in the history
  • Loading branch information
RayPlante committed Mar 5, 2024
2 parents 5f24889 + 4d3c71e commit 9d0196a
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package gov.nist.oar.distrib.web;

import gov.nist.oar.distrib.cachemgr.VolumeStatus;
import gov.nist.oar.distrib.cachemgr.pdr.PDRCacheManager;
import gov.nist.oar.distrib.cachemgr.CacheManagementException;
import gov.nist.oar.distrib.cachemgr.CacheObject;
Expand Down Expand Up @@ -331,6 +332,46 @@ else if (":checked".equals(selector))
return new ResponseEntity<String>("Method not allowed on URL", HttpStatus.METHOD_NOT_ALLOWED);
}

/**
* Endpoint to remove a dataset or specific files within a dataset from the cache.
*
* @param dsid the dataset identifier
* @param request used to extract the optional file path from the URL
* @return ResponseEntity with the result of the operation
*/
@DeleteMapping(value="/objects/{dsid}/**")
public ResponseEntity<String> removeFromCache(@PathVariable("dsid") String dsid, HttpServletRequest request) {
try {

_checkForManager();

// Extract the optional file path from the request URL
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String prefix = "/cache/objects/" + dsid;
String filepath = path.startsWith(prefix) ? path.substring(prefix.length()) : "";

if (filepath.isEmpty() || filepath.equals("/")) {
// Remove the entire dataset from the cache
List<CacheObject> files = mgr.selectDatasetObjects(dsid, VolumeStatus.VOL_FOR_UPDATE);
for (CacheObject file : files) {
mgr.uncache(file.id); // Use the uncache method directly
}
return ResponseEntity.ok("Dataset " + dsid + " removed from cache");
} else {
// Remove specific file or directory within the dataset from the cache
List<CacheObject> files = mgr.selectFileObjects(dsid, filepath, VolumeStatus.VOL_FOR_UPDATE);
for (CacheObject file : files) {
mgr.uncache(file.id);
}
return ResponseEntity.ok("File(s) " + filepath + " in dataset " + dsid + " removed from cache");
}
} catch (NotOperatingException e) {
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body("Cache manager is not operational");
} catch (CacheManagementException e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing cache removal request: " + e.getMessage());
}
}

/**
* return status information about the caching queue. The caching queue is a queue of data items
* waiting to be cached
Expand Down

0 comments on commit 9d0196a

Please sign in to comment.