Skip to content

Commit cc2d716

Browse files
committed
持久化保存时避免集合复制,节约内存
1 parent d04d31a commit cc2d716

File tree

4 files changed

+13
-20
lines changed

4 files changed

+13
-20
lines changed

src/main/java/com/ghostchu/peerbanhelper/database/dao/impl/PeerRecordDao.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import java.sql.SQLException;
1414
import java.sql.Timestamp;
15-
import java.util.List;
15+
import java.util.Deque;
1616

1717
@Component
1818
@Slf4j
@@ -25,15 +25,16 @@ public PeerRecordDao(@Autowired Database database, TorrentDao torrentDao) throws
2525
setObjectCache(true);
2626
}
2727

28-
public void syncPendingTasks(List<BatchHandleTasks> tasks) throws SQLException {
28+
public void syncPendingTasks(Deque<BatchHandleTasks> tasks) throws SQLException {
2929
callBatchTasks(() -> {
30-
tasks.forEach(t -> {
30+
while (!tasks.isEmpty()) {
31+
var t = tasks.pop();
3132
try {
3233
writeToDatabase(t.timestamp, t.downloader, t.torrent, t.peer);
3334
} catch (SQLException e) {
3435
log.error("Unable save peer record to database, please report to developer: {}, {}, {}, {}", t.timestamp, t.downloader, t.torrent, t.peer);
3536
}
36-
});
37+
}
3738
return null;
3839
});
3940
}

src/main/java/com/ghostchu/peerbanhelper/database/dao/impl/ProgressCheatBlockerPersistDao.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.sql.SQLException;
1414
import java.sql.Timestamp;
1515
import java.util.Collections;
16+
import java.util.Deque;
1617
import java.util.List;
1718
import java.util.concurrent.CopyOnWriteArrayList;
1819
import java.util.stream.Collectors;
@@ -26,7 +27,7 @@ public ProgressCheatBlockerPersistDao(@Autowired Database database) throws SQLEx
2627

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

55-
public void flushDatabase(List<ProgressCheatBlocker.ClientTaskRecord> records) throws SQLException {
56+
public void flushDatabase(Deque<ProgressCheatBlocker.ClientTaskRecord> records) throws SQLException {
5657
callBatchTasks(() -> {
57-
records.forEach(record -> {
58+
while (!records.isEmpty()) {
59+
var record = records.pop();
5860
String torrentId = record.client().getTorrentId();
5961
record.task().forEach(task -> {
6062
try {
@@ -88,7 +90,7 @@ public void flushDatabase(List<ProgressCheatBlocker.ClientTaskRecord> records) t
8890
log.error("Unable write PCB persist data into database", e);
8991
}
9092
});
91-
});
93+
}
9294
return null;
9395
});
9496
}

src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/ActiveMonitoringModule.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
import org.springframework.stereotype.Component;
2121

2222
import java.sql.SQLException;
23-
import java.util.ArrayList;
2423
import java.util.Collection;
2524
import java.util.Deque;
26-
import java.util.List;
2725
import java.util.concurrent.*;
2826

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

142140
private void flush() {
143141
try {
144-
List<PeerRecordDao.BatchHandleTasks> tasks = new ArrayList<>();
145-
while (!dataBuffer.isEmpty()) {
146-
tasks.add(dataBuffer.poll());
147-
}
148142
try {
149-
peerRecordDao.syncPendingTasks(tasks);
143+
peerRecordDao.syncPendingTasks(dataBuffer);
150144
} catch (SQLException e) {
151145
log.warn("Unable sync peers data to database", e);
152146
rollbarErrorReporter.warning(e);

src/main/java/com/ghostchu/peerbanhelper/module/impl/rule/ProgressCheatBlocker.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,8 @@ private void cleanDatabase() {
114114

115115
private void flushDatabase() {
116116
try {
117-
List<ClientTaskRecord> records = new ArrayList<>();
118-
while (!pendingPersistQueue.isEmpty()) {
119-
records.add(pendingPersistQueue.poll());
120-
}
121117
try {
122-
progressCheatBlockerPersistDao.flushDatabase(records);
118+
progressCheatBlockerPersistDao.flushDatabase(pendingPersistQueue);
123119
} catch (SQLException e) {
124120
log.error("Unable flush records into database", e);
125121
rollbarErrorReporter.error(e);

0 commit comments

Comments
 (0)