mirror of https://github.com/bvn13/ADaStor.git
parent
6dcdb078cb
commit
46fea03041
@ -0,0 +1,31 @@
|
||||
package ru.bvn13.adastor.entities.dtos;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.persistence.Transient;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author boykovn at 12.03.2019
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class StortionDto {
|
||||
|
||||
private String uuid;
|
||||
|
||||
private LocalDateTime storeDate;
|
||||
|
||||
private long size;
|
||||
|
||||
private String path;
|
||||
|
||||
@Transient
|
||||
private long retention;
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package ru.bvn13.adastor.web.controllers;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import ru.bvn13.adastor.entities.Stortion;
|
||||
import ru.bvn13.adastor.entities.dtos.StortionDto;
|
||||
import ru.bvn13.adastor.web.repositories.StortionRepository;
|
||||
import ru.bvn13.adastor.web.services.StortionService;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author boykovn at 12.03.2019
|
||||
*/
|
||||
@Controller
|
||||
public class TestController {
|
||||
|
||||
private StortionService stortionService;
|
||||
|
||||
@Autowired
|
||||
public void setStortionRepository(StortionService stortionService) {
|
||||
this.stortionService = stortionService;
|
||||
}
|
||||
|
||||
@GetMapping("/t")
|
||||
public @ResponseBody String test() {
|
||||
|
||||
Stream<StortionDto> stortions = stortionService.findAllSortedByRetention();
|
||||
stortions.forEach(st -> System.out.println(String.format("%s - %s - %s - %s", st.getUuid(), st.getStoreDate(), st.getSize(), st.getRetention())));
|
||||
|
||||
return "done";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package ru.bvn13.adastor.web.repositories;
|
||||
|
||||
import ru.bvn13.adastor.entities.Stortion;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author boykovn at 12.03.2019
|
||||
*/
|
||||
public interface CustomStortionRepository {
|
||||
|
||||
Stream<Stortion> findAllSortedByRetention();
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package ru.bvn13.adastor.web.repositories.impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.bvn13.adastor.config.Config;
|
||||
import ru.bvn13.adastor.entities.Stortion;
|
||||
import ru.bvn13.adastor.web.repositories.CustomStortionRepository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author boykovn at 12.03.2019
|
||||
*/
|
||||
@Repository
|
||||
public class StortionRepositoryImpl implements CustomStortionRepository {
|
||||
|
||||
private Config config;
|
||||
|
||||
@Autowired
|
||||
public void setConfig(Config config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
/**
|
||||
* RETENTION = min_age + (-max_age + min_age) * pow((file_size / max_size - 1), 3)
|
||||
*
|
||||
* Implemets the query - selecting all stortions ordered by retention ascending
|
||||
*
|
||||
* SELECT * FROM STORTION
|
||||
* ORDER BY 'retention' ASC
|
||||
*
|
||||
* @return queried collection
|
||||
*/
|
||||
@Override
|
||||
public Stream<Stortion> findAllSortedByRetention() {
|
||||
|
||||
long min_age = config.getMinDaysStoring();
|
||||
long max_age = config.getMaxDaysStoring();
|
||||
long max_size = config.getMaxSize();
|
||||
|
||||
Query query = entityManager.createQuery("SELECT s FROM Stortion s ORDER BY "
|
||||
+" (CAST(:min_age as double) + (- CAST(:max_age as double) + CAST(:min_age as double)) * POWER((CAST(s.size as double) / CAST(:max_size as double) - 1), 3)) ASC");
|
||||
query.setParameter("min_age", (double)min_age);
|
||||
query.setParameter("max_age", (double)max_age);
|
||||
query.setParameter("max_size", max_size);
|
||||
|
||||
return (Stream<Stortion>) query.getResultStream();
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue