Оптимизировано получение пакетов
This commit is contained in:
@@ -5,13 +5,11 @@ 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.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import ru.serega6531.packmate.model.Stream;
|
import ru.serega6531.packmate.model.Packet;
|
||||||
import ru.serega6531.packmate.model.pojo.PacketDto;
|
import ru.serega6531.packmate.model.pojo.PacketDto;
|
||||||
import ru.serega6531.packmate.service.StreamService;
|
import ru.serega6531.packmate.service.StreamService;
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -27,14 +25,10 @@ public class PacketController {
|
|||||||
|
|
||||||
@PostMapping("/{streamId}")
|
@PostMapping("/{streamId}")
|
||||||
public List<PacketDto> getPacketsForStream(@PathVariable long streamId) {
|
public List<PacketDto> getPacketsForStream(@PathVariable long streamId) {
|
||||||
final Optional<Stream> stream = streamService.find(streamId);
|
List<Packet> packets = streamService.getPackets(streamId);
|
||||||
if (stream.isPresent()) {
|
return packets.stream()
|
||||||
return stream.get().getPackets().stream()
|
|
||||||
.map(streamService::packetToDto)
|
.map(streamService::packetToDto)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
} else {
|
|
||||||
return Collections.emptyList();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class Packet {
|
|||||||
@Transient
|
@Transient
|
||||||
private int ttl;
|
private int ttl;
|
||||||
|
|
||||||
@ManyToOne(optional = false)
|
@ManyToOne(fetch = FetchType.LAZY, optional = false)
|
||||||
@JoinColumn(name = "stream_id", nullable = false)
|
@JoinColumn(name = "stream_id", nullable = false)
|
||||||
private Stream stream;
|
private Stream stream;
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import org.springframework.data.jpa.repository.Modifying;
|
|||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import ru.serega6531.packmate.model.Stream;
|
import ru.serega6531.packmate.model.Stream;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface StreamRepository extends JpaRepository<Stream, Long>, JpaSpecificationExecutor<Stream> {
|
public interface StreamRepository extends JpaRepository<Stream, Long>, JpaSpecificationExecutor<Stream> {
|
||||||
|
|
||||||
@Query("UPDATE Stream SET favorite = :favorite WHERE id = :id")
|
@Query("UPDATE Stream SET favorite = :favorite WHERE id = :id")
|
||||||
@@ -14,4 +16,11 @@ public interface StreamRepository extends JpaRepository<Stream, Long>, JpaSpecif
|
|||||||
|
|
||||||
long deleteByEndTimestampBeforeAndFavoriteIsFalse(long threshold);
|
long deleteByEndTimestampBeforeAndFavoriteIsFalse(long threshold);
|
||||||
|
|
||||||
|
@Query("SELECT s FROM Stream s " +
|
||||||
|
"LEFT JOIN FETCH s.packets AS packets " +
|
||||||
|
"LEFT JOIN FETCH packets.matches " +
|
||||||
|
"WHERE s.id = :id"
|
||||||
|
)
|
||||||
|
Optional<Stream> getStreamWithPackets(long id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,12 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
import ru.serega6531.packmate.model.*;
|
import ru.serega6531.packmate.model.*;
|
||||||
import ru.serega6531.packmate.model.enums.PatternActionType;
|
import ru.serega6531.packmate.model.enums.PatternActionType;
|
||||||
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
||||||
@@ -242,8 +244,10 @@ public class StreamService {
|
|||||||
return saved;
|
return saved;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Stream> find(long id) {
|
public List<Packet> getPackets(long streamId) {
|
||||||
return repository.findById(id);
|
return repository.getStreamWithPackets(streamId)
|
||||||
|
.map(Stream::getPackets)
|
||||||
|
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user