Skip to content

Commit

Permalink
使用 saveAll 代替 upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Oct 9, 2024
1 parent 759e3a0 commit 124014d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import jakarta.persistence.EntityManagerFactory;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.Modifying;
Expand Down Expand Up @@ -83,30 +81,28 @@ public void cleanup() {
@Modifying
public void executeAnnounce(List<PeerAnnounce> announces) {
announceCounter.increment(announces.size());
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
try (StatelessSession statelessSession = sessionFactory.openStatelessSession()) {
for (PeerAnnounce announce : announces) {
var trackedPeer = new TrackedPeer(
new TrackedPeerId(ByteUtil.filterUTF8(ByteUtil.bytesToHex(announce.peerId())), ByteUtil.bytesToHex(announce.infoHash())),
announce.reqIp(),
ByteUtil.filterUTF8(new String(announce.peerId, StandardCharsets.ISO_8859_1)),
announce.peerIp(),
announce.peerPort(),
announce.uploaded(),
announce.uploaded(),
announce.downloaded(),
announce.downloaded(),
announce.left(),
announce.peerEvent(),
announce.userAgent(),
OffsetDateTime.now(),
geoIPManager.geoData(announce.peerIp()),
geoIPManager.geoData(announce.reqIp()),
0
);
statelessSession.upsert(trackedPeer);
}
List<TrackedPeer> trackedPeers = new ArrayList<>();
for (PeerAnnounce announce : announces) {
trackedPeers.add(new TrackedPeer(
new TrackedPeerId(ByteUtil.filterUTF8(ByteUtil.bytesToHex(announce.peerId())), ByteUtil.bytesToHex(announce.infoHash())),
announce.reqIp(),
ByteUtil.filterUTF8(new String(announce.peerId, StandardCharsets.ISO_8859_1)),
announce.peerIp(),
announce.peerPort(),
announce.uploaded(),
announce.uploaded(),
announce.downloaded(),
announce.downloaded(),
announce.left(),
announce.peerEvent(),
announce.userAgent(),
OffsetDateTime.now(),
geoIPManager.geoData(announce.peerIp()),
geoIPManager.geoData(announce.reqIp()),
0
));
}
trackedPeerRepository.saveAll(trackedPeers);
}

@Cacheable(value = {"peers#3000"}, key = "#torrentInfoHash")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Repository
public interface TrackedPeerRepository extends SparkleCommonRepository<TrackedPeer, Long> {


@Query("""
select t from TrackedPeer t
where t.id.torrentInfoHash = ?1 and t.id.peerId <> ?2 and t.peerIp <> ?3
Expand Down

0 comments on commit 124014d

Please sign in to comment.