This commit is contained in:
Michele Artini 2024-01-17 15:50:36 +01:00
parent d2fcc9d905
commit 7d327998be
1 changed files with 50 additions and 43 deletions

View File

@ -64,34 +64,34 @@ public class MDStoreService {
public List<MDStoreWithInfo> listMdStores() {
return StreamSupport.stream(mdstoreWithInfoRepository.findAll().spliterator(), false)
return StreamSupport.stream(this.mdstoreWithInfoRepository.findAll().spliterator(), false)
.sorted(Comparator.comparing((Function<MDStoreWithInfo, String>) MDStoreWithInfo::getDatasourceName).thenComparing(MDStoreWithInfo::getId))
.collect(Collectors.toList());
}
public List<String> listMdStoreIDs() {
return mdstoreRepository.findAll().stream().map(MDStore::getId).sorted().collect(Collectors.toList());
return this.mdstoreRepository.findAll().stream().map(MDStore::getId).sorted().collect(Collectors.toList());
}
public long countMdStores() {
return mdstoreRepository.count();
return this.mdstoreRepository.count();
}
public Iterable<MDStoreVersion> listVersions(final String mdId) {
return mdstoreVersionRepository.findByMdstoreOrderById(mdId);
return this.mdstoreVersionRepository.findByMdstoreOrderById(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);
return this.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));
return this.mdstoreWithInfoRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore: " + mdId));
}
public MDStoreVersion findVersion(final String versionId) throws MDStoreManagerException {
return mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore version: " + versionId));
return this.mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore version: " + versionId));
}
@Transactional
@ -104,11 +104,11 @@ public class MDStoreService {
final String apiId) {
final MDStore md = newMDStore(format, layout, interpretation, type, dsName, dsId, apiId, apiId);
mdstoreRepository.save(md);
this.mdstoreRepository.save(md);
final MDStoreVersion v = newMDStoreVersion(md, false);
mdstoreVersionRepository.save(v);
mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
this.mdstoreVersionRepository.save(v);
this.mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
return md.getId();
}
@ -135,21 +135,21 @@ public class MDStoreService {
@Transactional
public void deleteMdStore(final String mdId) throws MDStoreManagerException {
final MDStore md = mdstoreRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("MDStore not found: " + mdId));
final MDStore md = this.mdstoreRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("MDStore not found: " + mdId));
if (mdstoreVersionRepository.countByMdstoreAndReadCountGreaterThan(mdId, 0) > 0) {
if (this.mdstoreVersionRepository.countByMdstoreAndReadCountGreaterThan(mdId, 0) > 0) {
log.error("Read transactions found on mdstore: " + mdId);
throw new MDStoreManagerException("Read transactions found on mdstore: " + mdId);
}
if (mdstoreVersionRepository.countByMdstoreAndWriting(mdId, true) > 0) {
if (this.mdstoreVersionRepository.countByMdstoreAndWriting(mdId, true) > 0) {
log.error("Write transactions found on mdstore: " + mdId);
throw new MDStoreManagerException("Write transactions found on mdstore: " + mdId);
}
mdstoreCurrentVersionRepository.deleteById(mdId);
mdstoreVersionRepository.deleteByMdstore(mdId);
mdstoreRepository.deleteById(mdId);
this.mdstoreCurrentVersionRepository.deleteById(mdId);
this.mdstoreVersionRepository.deleteByMdstore(mdId);
this.mdstoreRepository.deleteById(mdId);
selectBackend(md.getType()).delete(md);
}
@ -157,33 +157,35 @@ public class MDStoreService {
@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())
this.mdstoreCurrentVersionRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("Missing mdstore: " + mdId));
final MDStoreVersion v = this.mdstoreVersionRepository.findById(cv.getCurrentVersion())
.orElseThrow(() -> new MDStoreManagerException("Missing version: " + cv.getCurrentVersion()));
v.setReadCount(v.getReadCount() + 1);
mdstoreVersionRepository.save(v);
this.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"));
final MDStoreVersion v =
this.mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found: " + versionId));
v.setReadCount(Math.max(0, v.getReadCount() - 1));
return v;
}
@Transactional
public MDStoreVersion resetReading(final String versionId) throws MDStoreManagerException {
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
final MDStoreVersion v =
this.mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found: " + versionId));
v.setReadCount(0);
return v;
}
@Transactional
public MDStoreVersion prepareMdStoreVersion(final String mdId) throws MDStoreManagerException {
final MDStore md = mdstoreRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("MDStore not found"));
final MDStore md = this.mdstoreRepository.findById(mdId).orElseThrow(() -> new MDStoreManagerException("MDStore not found: " + mdId));
final MDStoreVersion v = newMDStoreVersion(md, true);
mdstoreVersionRepository.save(v);
this.mdstoreVersionRepository.save(v);
return v;
}
@ -202,33 +204,37 @@ public class MDStoreService {
@Transactional
public void deleteMdStoreVersion(final String versionId, final boolean force) throws MDStoreManagerException {
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
final MDStore md = mdstoreRepository.findById(v.getMdstore()).orElseThrow(() -> new MDStoreManagerException("Version not found"));
final MDStoreVersion v = this.mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
final MDStore md = this.mdstoreRepository.findById(v.getMdstore()).orElseThrow(() -> new MDStoreManagerException("Version not found"));
if (mdstoreCurrentVersionRepository
if (this.mdstoreCurrentVersionRepository
.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: " + versionId);
}
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"); }
if (v.isWriting()) { throw new MDStoreManagerException("I cannot delete this version because it is in write mode: " + versionId); }
if (v.getReadCount() > 0) { throw new MDStoreManagerException("I cannot delete this version because it is in read mode: " + versionId); }
}
mdstoreVersionRepository.delete(v);
this.mdstoreVersionRepository.delete(v);
selectBackend(md.getType()).delete(v);
}
public List<MetadataRecord> listVersionRecords(final String versionId, final long limit) throws MDStoreManagerException {
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
final MDStore md = mdstoreRepository.findById(v.getMdstore()).orElseThrow(() -> new MDStoreManagerException("MDStore not found"));
final MDStoreVersion v =
this.mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found: " + versionId));
final MDStore md =
this.mdstoreRepository.findById(v.getMdstore()).orElseThrow(() -> new MDStoreManagerException("MDStore not found: " + v.getMdstore()));
return selectBackend(md.getType()).listEntries(v, limit);
}
public Stream<MetadataRecord> streamVersionRecords(final String versionId) throws MDStoreManagerException {
final MDStoreVersion v = mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found"));
final MDStore md = mdstoreRepository.findById(v.getMdstore()).orElseThrow(() -> new MDStoreManagerException("MDStore not found"));
final MDStoreVersion v =
this.mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Version not found: " + versionId));
final MDStore md =
this.mdstoreRepository.findById(v.getMdstore()).orElseThrow(() -> new MDStoreManagerException("MDStore not found: " + v.getMdstore()));
return selectBackend(md.getType()).streamEntries(v);
}
@ -263,8 +269,8 @@ public class MDStoreService {
public Map<MDStoreType, Set<String>> fixInconsistencies(final boolean delete) throws MDStoreManagerException {
final Map<MDStoreType, Set<String>> res = new LinkedHashMap<>();
// res.put(MDStoreType.HDFS, hdfsBackend.fixInconsistencies(delete));
res.put(MDStoreType.MOCK, mockBackend.fixInconsistencies(delete));
res.put(MDStoreType.SQL_DB, sqlBackend.fixInconsistencies(delete));
res.put(MDStoreType.MOCK, this.mockBackend.fixInconsistencies(delete));
res.put(MDStoreType.SQL_DB, this.sqlBackend.fixInconsistencies(delete));
// TODO (LOW PRIORITY): ADD HERE THE INVOCATION FOR OTHER MDSTORE TYPE
return res;
@ -272,9 +278,9 @@ public class MDStoreService {
private MDStoreBackend selectBackend(final MDStoreType type) {
return switch (type) {
case MOCK -> mockBackend;
case SQL_DB -> sqlBackend;
default -> defaultBackend;
case MOCK -> this.mockBackend;
case SQL_DB -> this.sqlBackend;
default -> this.defaultBackend;
};
}
@ -286,12 +292,13 @@ public class MDStoreService {
@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));
final MDStoreVersion v =
this.mdstoreVersionRepository.findById(versionId).orElseThrow(() -> new MDStoreManagerException("Invalid version: " + versionId));
this.mdstoreCurrentVersionRepository.save(MDStoreCurrentVersion.newInstance(v));
v.setWriting(false);
v.setSize(size);
v.setLastUpdate(LocalDateTime.now());
mdstoreVersionRepository.save(v);
this.mdstoreVersionRepository.save(v);
return v;
}