Skip to content

Commit

Permalink
持久化保存时避免集合复制,节约内存
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Sep 24, 2024
1 parent d04d31a commit cc2d716
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Deque;

@Component
@Slf4j
Expand All @@ -25,15 +25,16 @@ public PeerRecordDao(@Autowired Database database, TorrentDao torrentDao) throws
setObjectCache(true);
}

public void syncPendingTasks(List<BatchHandleTasks> tasks) throws SQLException {
public void syncPendingTasks(Deque<BatchHandleTasks> tasks) throws SQLException {
callBatchTasks(() -> {
tasks.forEach(t -> {
while (!tasks.isEmpty()) {
var t = tasks.pop();
try {
writeToDatabase(t.timestamp, t.downloader, t.torrent, t.peer);
} catch (SQLException e) {
log.error("Unable save peer record to database, please report to developer: {}, {}, {}, {}", t.timestamp, t.downloader, t.torrent, t.peer);
}
});
}
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
Expand All @@ -26,7 +27,7 @@ public ProgressCheatBlockerPersistDao(@Autowired Database database) throws SQLEx

public List<ProgressCheatBlocker.ClientTask> fetchFromDatabase(ProgressCheatBlocker.Client client, Timestamp after) throws SQLException {
IPAddress address = IPAddressUtil.getIPAddress(client.getPeerPrefix());
if(address == null) return Collections.emptyList();
if (address == null) return Collections.emptyList();
List<ProgressCheatBlockerPersistEntity> entities = queryBuilder()
.where()
.eq("torrentId", client.getTorrentId())
Expand All @@ -52,9 +53,10 @@ public List<ProgressCheatBlocker.ClientTask> fetchFromDatabase(ProgressCheatBloc
).collect(Collectors.toCollection(CopyOnWriteArrayList::new)); // 可变 List,需要并发安全
}

public void flushDatabase(List<ProgressCheatBlocker.ClientTaskRecord> records) throws SQLException {
public void flushDatabase(Deque<ProgressCheatBlocker.ClientTaskRecord> records) throws SQLException {
callBatchTasks(() -> {
records.forEach(record -> {
while (!records.isEmpty()) {
var record = records.pop();
String torrentId = record.client().getTorrentId();
record.task().forEach(task -> {
try {
Expand Down Expand Up @@ -88,7 +90,7 @@ public void flushDatabase(List<ProgressCheatBlocker.ClientTaskRecord> records) t
log.error("Unable write PCB persist data into database", e);
}
});
});
}
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.*;

import static com.ghostchu.peerbanhelper.text.TextManager.tlUI;
Expand Down Expand Up @@ -141,12 +139,8 @@ private void writeJournal() {

private void flush() {
try {
List<PeerRecordDao.BatchHandleTasks> tasks = new ArrayList<>();
while (!dataBuffer.isEmpty()) {
tasks.add(dataBuffer.poll());
}
try {
peerRecordDao.syncPendingTasks(tasks);
peerRecordDao.syncPendingTasks(dataBuffer);
} catch (SQLException e) {
log.warn("Unable sync peers data to database", e);
rollbarErrorReporter.warning(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,8 @@ private void cleanDatabase() {

private void flushDatabase() {
try {
List<ClientTaskRecord> records = new ArrayList<>();
while (!pendingPersistQueue.isEmpty()) {
records.add(pendingPersistQueue.poll());
}
try {
progressCheatBlockerPersistDao.flushDatabase(records);
progressCheatBlockerPersistDao.flushDatabase(pendingPersistQueue);
} catch (SQLException e) {
log.error("Unable flush records into database", e);
rollbarErrorReporter.error(e);
Expand Down

0 comments on commit cc2d716

Please sign in to comment.