partial implementation of the new delete methods

This commit is contained in:
Michele Artini 2021-01-28 12:59:06 +01:00
parent 9b853732ce
commit 05f5f35fa0
4 changed files with 46 additions and 38 deletions

View File

@ -4,12 +4,13 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -33,6 +34,8 @@ public class MDStoreController extends AbstractDnetController {
@Autowired @Autowired
private DatabaseUtils databaseUtils; private DatabaseUtils databaseUtils;
private static final Logger log = LoggerFactory.getLogger(DatabaseUtils.class);
@ApiOperation("Return all the mdstores") @ApiOperation("Return all the mdstores")
@GetMapping("/") @GetMapping("/")
public Iterable<MDStoreWithInfo> find() { public Iterable<MDStoreWithInfo> find() {
@ -96,18 +99,12 @@ public class MDStoreController extends AbstractDnetController {
return databaseUtils.commitMdStoreVersion(versionId, size); return databaseUtils.commitMdStoreVersion(versionId, size);
} }
@ApiOperation("Return all the expired versions of all the mdstores")
@GetMapping("/versions/expired")
public List<String> listExpiredVersions() {
return databaseUtils.listExpiredVersions();
}
@ApiOperation("Delete a mdstore version") @ApiOperation("Delete a mdstore version")
@DeleteMapping("/version/{versionId}") @DeleteMapping("/version/{versionId}")
public StatusResponse deleteVersion(@ApiParam("the id of the version that has to be deleted") @PathVariable final String versionId, public StatusResponse deleteVersion(@ApiParam("the id of the version that has to be deleted") @PathVariable final String versionId,
@ApiParam("if true, the controls on writing and readcount values will be skipped") @RequestParam(required = false, defaultValue = "false") final boolean force) @ApiParam("if true, the controls on writing and readcount values will be skipped") @RequestParam(required = false, defaultValue = "false") final boolean force)
throws MDStoreManagerException { throws MDStoreManagerException {
databaseUtils.deleteMdStoreVersions(force, versionId); databaseUtils.deleteMdStoreVersion(versionId, force);
return StatusResponse.DELETED; return StatusResponse.DELETED;
} }
@ -125,13 +122,20 @@ public class MDStoreController extends AbstractDnetController {
return databaseUtils.resetReading(versionId); return databaseUtils.resetReading(versionId);
} }
@ApiOperation("Delete multiple mdstore versions") @ApiOperation("Delete expired versions")
@PostMapping("/versions/deleteList") @PostMapping("/versions/expired")
public StatusResponse deleteVersions(@ApiParam("the list of ids of the versions that have to be deleted") @RequestBody final String[] versions, public StatusResponse deleteExpiredVersions() {
@ApiParam("if true, the controls on writing and readcount values will be skipped") @RequestParam(required = false, defaultValue = "false") final boolean force) new Thread(() -> {
throws MDStoreManagerException { for (final String v : databaseUtils.listExpiredVersions()) {
databaseUtils.deleteMdStoreVersions(force, versions); try {
return StatusResponse.DELETED; databaseUtils.deleteMdStoreVersion(v, true);
} catch (final MDStoreManagerException e) {
log.warn("Error deleteting version " + v);
}
}
}).start();
return StatusResponse.DELETING;
} }
@ApiOperation("Show informations") @ApiOperation("Show informations")
@ -141,6 +145,7 @@ public class MDStoreController extends AbstractDnetController {
info.put("number_of_mdstores", databaseUtils.countMdStores()); info.put("number_of_mdstores", databaseUtils.countMdStores());
info.put("hadoop_cluster", databaseUtils.getHadoopCluster()); info.put("hadoop_cluster", databaseUtils.getHadoopCluster());
info.put("hdfs_base_path", databaseUtils.getHdfsBasePath()); info.put("hdfs_base_path", databaseUtils.getHdfsBasePath());
info.put("expired_versions", databaseUtils.listExpiredVersions());
return info; return info;
} }

View File

@ -3,6 +3,7 @@ package eu.dnetlib.data.mdstore.manager.controller;
public class StatusResponse { public class StatusResponse {
public static final StatusResponse DELETED = new StatusResponse("DELETED"); public static final StatusResponse DELETED = new StatusResponse("DELETED");
public static final StatusResponse DELETING = new StatusResponse("DELETING...");
private String status; private String status;

View File

@ -155,23 +155,21 @@ public class DatabaseUtils {
} }
@Transactional @Transactional
public void deleteMdStoreVersions(final boolean force, final String... versions) throws MDStoreManagerException { public void deleteMdStoreVersion(final String versionId, final boolean force) throws MDStoreManagerException {
for (final String versionId : versions) { final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
if (mdstoreCurrentVersionRepository if (mdstoreCurrentVersionRepository
.countByCurrentVersion(versionId) > 0) { .countByCurrentVersion(versionId) > 0) {
throw new MDStoreManagerException("I cannot delete this version because it is the current version"); throw new MDStoreManagerException("I cannot delete this version because it is the current version");
}
if (!force) {
if (v.isWriting()) { throw new MDStoreManagerException("I cannot delete this version because it is in write mode"); }
if (v.getReadCount() > 0) { throw new MDStoreManagerException("I cannot delete this version because it is in read mode"); }
}
mdstoreVersionRepository.delete(v);
} }
if (!force) {
if (v.isWriting()) { throw new MDStoreManagerException("I cannot delete this version because it is in write mode"); }
if (v.getReadCount() > 0) { throw new MDStoreManagerException("I cannot delete this version because it is in read mode"); }
}
mdstoreVersionRepository.delete(v);
} }
public String getHdfsBasePath() { public String getHdfsBasePath() {

View File

@ -14,6 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion; import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion;
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreWithInfo; import eu.dnetlib.data.mdstore.manager.common.model.MDStoreWithInfo;
import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException; import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException;
import eu.dnetlib.data.mdstore.manager.utils.DatabaseUtils;
@Disabled @Disabled
@SpringBootTest @SpringBootTest
@ -22,6 +23,9 @@ class MDStoreControllerTest {
@Autowired @Autowired
private MDStoreController controller; private MDStoreController controller;
@Autowired
private DatabaseUtils databaseUtils;
@Test @Test
void testSimpleCreationAndDelete() throws MDStoreManagerException { void testSimpleCreationAndDelete() throws MDStoreManagerException {
@ -147,19 +151,19 @@ class MDStoreControllerTest {
final String v2 = controller.prepareNewVersion(mdId).getId(); final String v2 = controller.prepareNewVersion(mdId).getId();
final String v3 = controller.prepareNewVersion(mdId).getId(); final String v3 = controller.prepareNewVersion(mdId).getId();
assertEquals(0, controller.listExpiredVersions().size()); assertEquals(0, databaseUtils.listExpiredVersions().size());
controller.commitVersion(v3, 1111l).getId(); controller.commitVersion(v3, 1111l).getId();
assertEquals(v3, controller.getMdStore(mdId).getCurrentVersion()); assertEquals(v3, controller.getMdStore(mdId).getCurrentVersion());
assertEquals(1, controller.listExpiredVersions().size()); assertEquals(1, databaseUtils.listExpiredVersions().size());
controller.commitVersion(v1, 2222l).getId(); controller.commitVersion(v1, 2222l).getId();
assertEquals(v1, controller.getMdStore(mdId).getCurrentVersion()); assertEquals(v1, controller.getMdStore(mdId).getCurrentVersion());
assertEquals(2, controller.listExpiredVersions().size()); assertEquals(2, databaseUtils.listExpiredVersions().size());
controller.commitVersion(v2, 3333l).getId(); controller.commitVersion(v2, 3333l).getId();
assertEquals(v2, controller.getMdStore(mdId).getCurrentVersion()); assertEquals(v2, controller.getMdStore(mdId).getCurrentVersion());
assertEquals(3, controller.listExpiredVersions().size()); assertEquals(3, databaseUtils.listExpiredVersions().size());
controller.delete(mdId); controller.delete(mdId);
} }
@ -185,14 +189,14 @@ class MDStoreControllerTest {
final String vr3 = controller.startReading(mdId).getId(); final String vr3 = controller.startReading(mdId).getId();
assertEquals(v1, vr3); assertEquals(v1, vr3);
assertEquals(0, controller.listExpiredVersions().size()); assertEquals(0, databaseUtils.listExpiredVersions().size());
controller.endReading(vr1); controller.endReading(vr1);
assertEquals(0, controller.listExpiredVersions().size()); assertEquals(0, databaseUtils.listExpiredVersions().size());
controller.endReading(vr3); controller.endReading(vr3);
assertEquals(0, controller.listExpiredVersions().size()); assertEquals(0, databaseUtils.listExpiredVersions().size());
controller.endReading(vr2); controller.endReading(vr2);
assertEquals(1, controller.listExpiredVersions().size()); assertEquals(1, databaseUtils.listExpiredVersions().size());
assertEquals(firstVersion, controller.listExpiredVersions().get(0)); assertEquals(firstVersion, databaseUtils.listExpiredVersions().get(0));
controller.delete(mdId); controller.delete(mdId);
} }