From 042d47787826e1672c531587453e8126b8fe30d1 Mon Sep 17 00:00:00 2001 From: enricoottonello Date: Wed, 20 Mar 2019 15:36:23 +0100 Subject: [PATCH] added versions support --- .../data/mdstore/manager/MainApplication.java | 4 +- .../manager/controller/MDStoreController.java | 136 ++++++++++++++---- .../{ => controller}/SwaggerController.java | 4 +- .../controller/TransactionController.java | 41 ------ .../MDStoreManagerException.java | 6 +- .../exceptions/NoContentException.java | 36 +++++ .../manager/model/MDStoreCurrentVersion.java | 56 ++++++++ .../{Transaction.java => MDStoreVersion.java} | 53 +++---- .../MDStoreCurrentVersionRepository.java | 12 ++ .../repository/MDStoreVersionRepository.java | 17 +++ .../repository/TransactionRepository.java | 14 -- .../src/main/resources/schema.sql | 36 ++--- 12 files changed, 275 insertions(+), 140 deletions(-) rename dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/{ => controller}/SwaggerController.java (65%) delete mode 100644 dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/TransactionController.java rename dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/{ => exceptions}/MDStoreManagerException.java (74%) create mode 100644 dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/exceptions/NoContentException.java create mode 100644 dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/MDStoreCurrentVersion.java rename dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/{Transaction.java => MDStoreVersion.java} (71%) create mode 100644 dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreCurrentVersionRepository.java create mode 100644 dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreVersionRepository.java delete mode 100644 dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/TransactionRepository.java diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/MainApplication.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/MainApplication.java index f23b3529b..a09c438b3 100644 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/MainApplication.java +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/MainApplication.java @@ -1,6 +1,5 @@ package eu.dnetlib.data.mdstore.manager; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; @@ -33,7 +32,7 @@ public class MainApplication { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) - .paths(p -> p.startsWith("/api/")) + .paths(p -> p.startsWith("/mdstores")) .build().apiInfo(new ApiInfoBuilder() .title("MDStore Manager APIs") .description("APIs documentation") @@ -45,5 +44,4 @@ public class MainApplication { } - } diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/MDStoreController.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/MDStoreController.java index a0563856e..d9577d560 100644 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/MDStoreController.java +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/MDStoreController.java @@ -1,79 +1,155 @@ package eu.dnetlib.data.mdstore.manager.controller; +import java.util.Date; import java.util.List; import java.util.stream.Collectors; +import javax.transaction.Transactional; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import eu.dnetlib.data.mdstore.manager.MDStoreManagerException; +import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException; +import eu.dnetlib.data.mdstore.manager.exceptions.NoContentException; import eu.dnetlib.data.mdstore.manager.model.MDStore; +import eu.dnetlib.data.mdstore.manager.model.MDStoreCurrentVersion; +import eu.dnetlib.data.mdstore.manager.model.MDStoreVersion; import eu.dnetlib.data.mdstore.manager.model.MDStoreWithInfo; -import eu.dnetlib.data.mdstore.manager.model.Transaction; +import eu.dnetlib.data.mdstore.manager.repository.MDStoreCurrentVersionRepository; import eu.dnetlib.data.mdstore.manager.repository.MDStoreRepository; +import eu.dnetlib.data.mdstore.manager.repository.MDStoreVersionRepository; import eu.dnetlib.data.mdstore.manager.repository.MDStoreWithInfoRepository; -import eu.dnetlib.data.mdstore.manager.repository.TransactionRepository; @RestController -@RequestMapping("/api/mdstores") -public class MDStoreController { +@RequestMapping("/mdstores") +public class MDStoreController { @Autowired private MDStoreRepository mdstoreRepository; @Autowired - private TransactionRepository transactionRepository; + private MDStoreVersionRepository mdstoreVersionRepository; + @Autowired + private MDStoreCurrentVersionRepository mdstoreCurrentVersionRepository; @Autowired private MDStoreWithInfoRepository mdstoreWithInfoRepository; + @Autowired + private JdbcTemplate jdbcTemplate; @RequestMapping(value = "/", method = RequestMethod.GET) - public final Iterable find() { + public Iterable find() { return mdstoreWithInfoRepository.findAll(); } - @RequestMapping(value = "/identifiers", method = RequestMethod.GET) - public final List findIdentifiers() { + @RequestMapping(value = "/ids", method = RequestMethod.GET) + public List findIdentifiers() { return mdstoreRepository.findAll().stream().map(MDStore::getId).collect(Collectors.toList()); } - @RequestMapping(value = "/byId/{id}", method = RequestMethod.GET) - public final MDStoreWithInfo get(@PathVariable final String id) { - return mdstoreWithInfoRepository.findById(id).orElse(null); + @RequestMapping(value = "/mdstore/{mdId}", method = RequestMethod.GET) + public MDStoreWithInfo get(@PathVariable final String mdId) throws NoContentException { + return mdstoreWithInfoRepository.findById(mdId).orElseThrow(() -> new NoContentException("Missing mdstore: " + mdId)); } @RequestMapping(value = "/count", method = RequestMethod.GET) - public final long count() { + public long count() { return mdstoreRepository.count(); } @RequestMapping(value = "/new/{format}/{layout}/{interpretation}", method = RequestMethod.PUT) - public MDStoreWithInfo createMDStore( - @PathVariable String format, - @PathVariable String layout, - @PathVariable String interpretation, - @RequestParam(required=false) String dsId, - @RequestParam(required=false) String apiId) throws MDStoreManagerException { + public MDStoreWithInfo createMDStore( + @PathVariable final String format, + @PathVariable final String layout, + @PathVariable final String interpretation, + @RequestParam(required = false) final String dsId, + @RequestParam(required = false) final String apiId) throws MDStoreManagerException { + final String id = _createMDStore(format, layout, interpretation, dsId, apiId); + return mdstoreWithInfoRepository.findById(id).orElseThrow(() -> new MDStoreManagerException("MDStore not found")); + } + + @Transactional + private String _createMDStore(final String format, final String layout, final String interpretation, final String dsId, final String apiId) { final MDStore md = MDStore.newInstance(dsId, apiId, format, layout, interpretation); mdstoreRepository.save(md); - final Transaction t = Transaction.newInstance(md.getId()); - t.setCurrent(true); - transactionRepository.save(t); + final MDStoreVersion v = MDStoreVersion.newInstance(md.getId(), false); + v.setLastUpdate(new Date()); + mdstoreVersionRepository.save(v); + mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v)); - return mdstoreWithInfoRepository.findById(md.getId()).orElseThrow(() -> new MDStoreManagerException("MDStore not found")); + return md.getId(); } - @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) - public void delete(@PathVariable final String id) throws MDStoreManagerException { - if (transactionRepository.countByMdstoreAndActive(id, true) == 0) { - transactionRepository.deleteByMdstore(id); - mdstoreRepository.deleteById(id); - } else { - throw new MDStoreManagerException("Active transactions found on mdstore : " + id); + @Transactional + @RequestMapping(value = "/mdstore/{mdId}", method = RequestMethod.DELETE) + public void delete(@PathVariable final String mdId) throws MDStoreManagerException { + if (!mdstoreRepository.existsById(mdId)) { throw new NoContentException("On delete MDStore not found " + "[" + mdId + "]"); } + + if (mdstoreVersionRepository.countByMdstoreAndReadCountGreaterThan(mdId, + 0) > 0) { throw new MDStoreManagerException("Read transactions found on mdstore : " + mdId); } + + if (mdstoreVersionRepository.countByMdstoreAndWriting(mdId, + true) > 0) { throw new MDStoreManagerException("Write transactions found on mdstore : " + mdId); } + + mdstoreCurrentVersionRepository.deleteById(mdId); + mdstoreVersionRepository.deleteByMdstore(mdId); + mdstoreRepository.deleteById(mdId); + } + + @Transactional + @RequestMapping(value = "/mdstore/{mdId}/newVersion", method = RequestMethod.GET) + public MDStoreVersion prepareNewVersion1(@PathVariable final String mdId) { + final MDStoreVersion v = MDStoreVersion.newInstance(mdId, true); + mdstoreVersionRepository.save(v); + return v; + } + + @Transactional + @RequestMapping(value = "/version/{versionId}/commit/{size}", method = RequestMethod.GET) + public void commitVersion(@PathVariable final String versionId, @PathVariable final int size) throws NoContentException { + final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new NoContentException("Invalid version: " + versionId)); + mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v)); + v.setWriting(false); + v.setSize(size); + v.setLastUpdate(new Date()); + mdstoreVersionRepository.save(v); + } + + @RequestMapping(value = "/versions/expired", method = RequestMethod.GET) + public List listExpiredVersions() { + return jdbcTemplate.queryForList( + "select v.id from mdstore_versions v left outer join mdstore_current_versions cv on (v.id = cv.current_version) where v.writing = false and v.readcount = 0 and cv.mdstore is null;", + String.class); + } + + @Transactional + @RequestMapping(value = "/version/{versionId}", method = RequestMethod.DELETE) + public void deleteVersion(@PathVariable final String versionId) throws MDStoreManagerException { + _deleteVersion(versionId); + } + + @Transactional + @RequestMapping(value = "/versions/deleteList", method = RequestMethod.POST) + public void deleteVersions(@RequestBody final List versions) throws MDStoreManagerException { + for (final String v : versions) { + _deleteVersion(v); } } + private void _deleteVersion(final String versionId) throws MDStoreManagerException { + final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found")); + + 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"); } + if (mdstoreCurrentVersionRepository + .countByCurrentVersion(versionId) > 0) { throw new MDStoreManagerException("I cannot delete this version because it is the current version"); } + + mdstoreVersionRepository.delete(v); + } + } diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/SwaggerController.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/SwaggerController.java similarity index 65% rename from dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/SwaggerController.java rename to dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/SwaggerController.java index b6cfb7dc3..0802f4358 100644 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/SwaggerController.java +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/SwaggerController.java @@ -1,4 +1,4 @@ -package eu.dnetlib.data.mdstore.manager; +package eu.dnetlib.data.mdstore.manager.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.RequestMethod; @Controller public class SwaggerController { - @RequestMapping(value = { "/", "/apidoc", "/api-doc", "/doc", "/swagger" }, method = RequestMethod.GET) + @RequestMapping(value = { "/apidoc", "/api-doc", "/doc", "/swagger" }, method = RequestMethod.GET) public String apiDoc() { return "redirect:swagger-ui.html"; } diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/TransactionController.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/TransactionController.java deleted file mode 100644 index d4ae4b1d5..000000000 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/controller/TransactionController.java +++ /dev/null @@ -1,41 +0,0 @@ -package eu.dnetlib.data.mdstore.manager.controller; - -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import eu.dnetlib.data.mdstore.manager.model.Transaction; -import eu.dnetlib.data.mdstore.manager.repository.TransactionRepository; - -@RestController -@RequestMapping("/api/transactions") -public class TransactionController { - - @Autowired - private TransactionRepository repo; - - @RequestMapping(value = "/", method = RequestMethod.GET) - public final List find() { - return repo.findAll(); - } - - @RequestMapping(value = "/identifiers", method = RequestMethod.GET) - public final List findIdentifiers() { - return repo.findAll().stream().map(Transaction::getId).collect(Collectors.toList()); - } - - @RequestMapping(value = "/{id}", method = RequestMethod.GET) - public final Transaction get(@PathVariable final String id) { - return repo.findById(id).orElse(null); - } - - @RequestMapping(value = "/count", method = RequestMethod.GET) - public final long count() { - return repo.count(); - } -} diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/MDStoreManagerException.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/exceptions/MDStoreManagerException.java similarity index 74% rename from dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/MDStoreManagerException.java rename to dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/exceptions/MDStoreManagerException.java index 6ccd5e5fc..c6dd2e96e 100644 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/MDStoreManagerException.java +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/exceptions/MDStoreManagerException.java @@ -1,5 +1,9 @@ -package eu.dnetlib.data.mdstore.manager; +package eu.dnetlib.data.mdstore.manager.exceptions; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public class MDStoreManagerException extends Exception{ /** diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/exceptions/NoContentException.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/exceptions/NoContentException.java new file mode 100644 index 000000000..156fe3820 --- /dev/null +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/exceptions/NoContentException.java @@ -0,0 +1,36 @@ +package eu.dnetlib.data.mdstore.manager.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NO_CONTENT) +public class NoContentException extends MDStoreManagerException{ + + /** + * + */ + private static final long serialVersionUID = -278683963172303773L; + + public NoContentException() { + super(); + } + + public NoContentException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public NoContentException(String message, Throwable cause) { + super(message, cause); + } + + public NoContentException(String message) { + super(message); + } + + public NoContentException(Throwable cause) { + super(cause); + } + + + +} diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/MDStoreCurrentVersion.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/MDStoreCurrentVersion.java new file mode 100644 index 000000000..a22f5cb5b --- /dev/null +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/MDStoreCurrentVersion.java @@ -0,0 +1,56 @@ +package eu.dnetlib.data.mdstore.manager.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "mdstore_current_versions") +public class MDStoreCurrentVersion implements Serializable { + + /** + * + */ + private static final long serialVersionUID = -4757725888593745773L; + + @Id + @Column(name = "mdstore") + private String mdstore; + + @Column(name = "current_version") + private String currentVersion; + + public String getMdstore() { + return mdstore; + } + + public void setMdstore(String mdstore) { + this.mdstore = mdstore; + } + + public String getCurrentVersion() { + return currentVersion; + } + + public void setCurrentVersion(String currentVersion) { + this.currentVersion = currentVersion; + } + + public static long getSerialversionuid() { + return serialVersionUID; + } + + public static MDStoreCurrentVersion newInstance(final String mdId, final String versionId) { + final MDStoreCurrentVersion cv = new MDStoreCurrentVersion(); + cv.setMdstore(mdId); + cv.setCurrentVersion(versionId); + return cv; + } + + public static MDStoreCurrentVersion newInstance(final MDStoreVersion v) { + return newInstance(v.getMdstore(), v.getId()); + } +} diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/Transaction.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/MDStoreVersion.java similarity index 71% rename from dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/Transaction.java rename to dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/MDStoreVersion.java index cc7100955..dbf19f934 100644 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/Transaction.java +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/model/MDStoreVersion.java @@ -11,8 +11,9 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity -@Table(name = "transactions") -public class Transaction implements Serializable{ +@Table(name = "mdstore_versions") +public class MDStoreVersion implements Serializable { + /** * */ @@ -25,11 +26,8 @@ public class Transaction implements Serializable{ @Column(name = "mdstore") private String mdstore; - @Column(name = "current") - private boolean current; - - @Column(name = "active") - private boolean active; + @Column(name = "writing") + private boolean writing; @Column(name = "readcount") private int readCount; @@ -41,6 +39,17 @@ public class Transaction implements Serializable{ @Column(name = "size") private int size; + public static MDStoreVersion newInstance(final String mdId, boolean writing) { + final MDStoreVersion t = new MDStoreVersion(); + t.setId(mdId + "-" + new Date().getTime()); + t.setMdstore(mdId); + t.setLastUpdate(null); + t.setWriting(writing); + t.setReadCount(0); + t.setSize(0); + return t; + } + public String getId() { return id; } @@ -57,20 +66,12 @@ public class Transaction implements Serializable{ this.mdstore = mdstore; } - public boolean isCurrent() { - return current; + public boolean isWriting() { + return writing; } - public void setCurrent(boolean current) { - this.current = current; - } - - public boolean isActive() { - return active; - } - - public void setActive(boolean active) { - this.active = active; + public void setWriting(boolean writing) { + this.writing = writing; } public int getReadCount() { @@ -97,18 +98,4 @@ public class Transaction implements Serializable{ this.size = size; } - public static Transaction newInstance(final String mdId) { - final Transaction t = new Transaction(); - t.setId(mdId + "-" + new Date().getTime()); - t.setMdstore(mdId); - t.setLastUpdate(null); - t.setActive(false); - t.setCurrent(false); - t.setReadCount(0); - t.setSize(0); - return t; - } - - - } diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreCurrentVersionRepository.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreCurrentVersionRepository.java new file mode 100644 index 000000000..aabdac875 --- /dev/null +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreCurrentVersionRepository.java @@ -0,0 +1,12 @@ +package eu.dnetlib.data.mdstore.manager.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import eu.dnetlib.data.mdstore.manager.model.MDStoreCurrentVersion; + +@Repository +public interface MDStoreCurrentVersionRepository extends JpaRepository { + + long countByCurrentVersion(String versionId); +} diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreVersionRepository.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreVersionRepository.java new file mode 100644 index 000000000..0db59f26c --- /dev/null +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/MDStoreVersionRepository.java @@ -0,0 +1,17 @@ +package eu.dnetlib.data.mdstore.manager.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import eu.dnetlib.data.mdstore.manager.model.MDStoreVersion; + +@Repository +public interface MDStoreVersionRepository extends JpaRepository { + + void deleteByMdstore(String id); + + int countByMdstoreAndWriting(String id, boolean b); + + int countByMdstoreAndReadCountGreaterThan(String id, int count); + +} diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/TransactionRepository.java b/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/TransactionRepository.java deleted file mode 100644 index e757955bc..000000000 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/java/eu/dnetlib/data/mdstore/manager/repository/TransactionRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package eu.dnetlib.data.mdstore.manager.repository; - -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -import eu.dnetlib.data.mdstore.manager.model.Transaction; - -@Repository -public interface TransactionRepository extends JpaRepository { - - void deleteByMdstore(String id); - - int countByMdstoreAndActive(String id, boolean active); -} diff --git a/dhp-applications/dhp-mdstore-manager-app/src/main/resources/schema.sql b/dhp-applications/dhp-mdstore-manager-app/src/main/resources/schema.sql index 79abc2568..235bdbb05 100644 --- a/dhp-applications/dhp-mdstore-manager-app/src/main/resources/schema.sql +++ b/dhp-applications/dhp-mdstore-manager-app/src/main/resources/schema.sql @@ -1,5 +1,6 @@ DROP VIEW IF EXISTS mdstores_with_info; -DROP TABLE IF EXISTS transactions; +DROP TABLE IF EXISTS mdstore_current_versions; +DROP TABLE IF EXISTS mdstore_versions; DROP TABLE IF EXISTS mdstores; CREATE TABLE mdstores ( @@ -11,28 +12,31 @@ CREATE TABLE mdstores ( api_id text ); -CREATE TABLE transactions ( +CREATE TABLE mdstore_versions ( id text PRIMARY KEY, mdstore text REFERENCES mdstores(id), - current boolean, - active boolean, + writing boolean, readcount int, lastupdate timestamp, size int ); +CREATE TABLE mdstore_current_versions ( + mdstore text PRIMARY KEY REFERENCES mdstores(id), + current_version text REFERENCES mdstore_versions(id) +); + CREATE VIEW mdstores_with_info AS SELECT - md.id AS id, - md.format AS format, - md.layout AS layout, - md.interpretation AS interpretation, - md.datasource_id AS datasource_id, - md.api_id AS api_id, - t.id AS current_version, - t.lastupdate AS lastupdate, - t.size AS size + md.id AS id, + md.format AS format, + md.layout AS layout, + md.interpretation AS interpretation, + md.datasource_id AS datasource_id, + md.api_id AS api_id, + cv.current_version AS current_version, + v.lastupdate AS lastupdate, + v.size AS size FROM mdstores md - LEFT OUTER JOIN transactions t ON (md.id = t.mdstore) -WHERE - t.current = TRUE; + LEFT OUTER JOIN mdstore_current_versions cv ON (md.id = cv.mdstore) + LEFT OUTER JOIN mdstore_versions v ON (cv.current_version = v.id);