155 lines
5.8 KiB
Java
155 lines
5.8 KiB
Java
package eu.dnetlib.data.mdstore.manager.utils;
|
|
|
|
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.stereotype.Component;
|
|
|
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStore;
|
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreCurrentVersion;
|
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreVersion;
|
|
import eu.dnetlib.data.mdstore.manager.common.model.MDStoreWithInfo;
|
|
import eu.dnetlib.data.mdstore.manager.exceptions.MDStoreManagerException;
|
|
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;
|
|
|
|
@Component
|
|
public class DatabaseUtils {
|
|
|
|
@Autowired
|
|
private MDStoreRepository mdstoreRepository;
|
|
@Autowired
|
|
private MDStoreVersionRepository mdstoreVersionRepository;
|
|
@Autowired
|
|
private MDStoreCurrentVersionRepository mdstoreCurrentVersionRepository;
|
|
@Autowired
|
|
private MDStoreWithInfoRepository mdstoreWithInfoRepository;
|
|
@Autowired
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
|
public Iterable<MDStoreWithInfo> listMdStores() {
|
|
return mdstoreWithInfoRepository.findAll();
|
|
}
|
|
|
|
public List<String> listMdStoreIDs() {
|
|
return mdstoreRepository.findAll().stream().map(MDStore::getId).collect(Collectors.toList());
|
|
}
|
|
|
|
public long countMdStores() {
|
|
return mdstoreRepository.count();
|
|
}
|
|
|
|
public Iterable<MDStoreVersion> listVersions(final String mdId) {
|
|
return mdstoreVersionRepository.findByMdstore(mdId);
|
|
}
|
|
|
|
public List<String> 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);
|
|
}
|
|
|
|
public MDStoreWithInfo findMdStore(final String mdId) throws MDStoreManagerException {
|
|
return mdstoreWithInfoRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore: " + mdId));
|
|
}
|
|
|
|
@Transactional
|
|
public String createMDStore(final String format,
|
|
final String layout,
|
|
final String interpretation,
|
|
final String dsName,
|
|
final String dsId,
|
|
final String apiId) {
|
|
|
|
final MDStore md = MDStore.newInstance(format, layout, interpretation, dsName, dsId, apiId);
|
|
mdstoreRepository.save(md);
|
|
|
|
final MDStoreVersion v = MDStoreVersion.newInstance(md.getId(), false);
|
|
v.setLastUpdate(new Date());
|
|
mdstoreVersionRepository.save(v);
|
|
mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
|
|
|
|
return md.getId();
|
|
}
|
|
|
|
@Transactional
|
|
public void deleteMdStore(final String mdId) throws MDStoreManagerException {
|
|
if (!mdstoreRepository.existsById(mdId)) { throw new MDStoreManagerException("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
|
|
public MDStoreVersion startReading(final String mdId) throws MDStoreManagerException {
|
|
final MDStoreCurrentVersion cv =
|
|
mdstoreCurrentVersionRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore: " + mdId));
|
|
final MDStoreVersion v = mdstoreVersionRepository.findById(cv.getCurrentVersion())
|
|
.orElseThrow(() -> new MDStoreManagerException("Missing version: " + cv.getCurrentVersion()));
|
|
v.setReadCount(v.getReadCount() + 1);
|
|
mdstoreVersionRepository.save(v);
|
|
return v;
|
|
}
|
|
|
|
@Transactional
|
|
public MDStoreVersion endReading(final String versionId) throws MDStoreManagerException {
|
|
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
|
|
v.setReadCount(v.getReadCount() - 1);
|
|
return v;
|
|
}
|
|
|
|
@Transactional
|
|
public MDStoreVersion prepareMdStoreVersion(final String mdId) {
|
|
final MDStoreVersion v = MDStoreVersion.newInstance(mdId, true);
|
|
mdstoreVersionRepository.save(v);
|
|
return v;
|
|
}
|
|
|
|
@Transactional
|
|
public MDStoreVersion commitMdStoreVersion(final String versionId, final long size) throws MDStoreManagerException {
|
|
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Invalid version: " + versionId));
|
|
mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
|
|
v.setWriting(false);
|
|
v.setSize(size);
|
|
v.setLastUpdate(new Date());
|
|
mdstoreVersionRepository.save(v);
|
|
return v;
|
|
}
|
|
|
|
@Transactional
|
|
public void deleteMdStoreVersions(final boolean force, final String... versions) throws MDStoreManagerException {
|
|
|
|
for (final String versionId : versions) {
|
|
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
|
|
|
|
if (mdstoreCurrentVersionRepository
|
|
.countByCurrentVersion(versionId) > 0) {
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|