|
| 1 | +package org.tron.core.manager; |
| 2 | + |
| 3 | +import com.typesafe.config.Config; |
| 4 | +import java.io.BufferedReader; |
| 5 | +import java.io.BufferedWriter; |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileWriter; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.time.format.DateTimeFormatter; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Comparator; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
| 18 | +import org.tron.core.config.Configuration; |
| 19 | +import org.tron.core.dao.BackupRecord; |
| 20 | + |
| 21 | +public class BackupRecordManager { |
| 22 | + private static final String DATA_DIR = "wallet_data"; |
| 23 | + private static final String STORAGE_FILE = DATA_DIR + File.separator + "wallet_backup_records.log"; |
| 24 | + private static final DateTimeFormatter TIMESTAMP_FORMAT = |
| 25 | + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 26 | + private static final int PAGE_SIZE = 10; |
| 27 | + private static int maxRecords; |
| 28 | + private static final int BUFFER_THRESHOLD = 100; |
| 29 | + |
| 30 | + static { |
| 31 | + try { |
| 32 | + Config config = Configuration.getByPath("config.conf"); |
| 33 | + if (config != null && config.hasPath("maxRecords")) { |
| 34 | + int value = config.getInt("maxRecords"); |
| 35 | + if (value <= 0) { |
| 36 | + System.out.println("Invalid maxRecords value " + value + ", must be positive. Using default."); |
| 37 | + } |
| 38 | + maxRecords = value; |
| 39 | + } |
| 40 | + } catch (Exception e) { |
| 41 | + System.out.println("Failed to load maxRecords from config, using default value."); |
| 42 | + maxRecords = 1000; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public BackupRecordManager() { |
| 47 | + initializeStorageFile(); |
| 48 | + } |
| 49 | + |
| 50 | + private void initializeStorageFile() { |
| 51 | + try { |
| 52 | + Path dataDir = Paths.get(DATA_DIR); |
| 53 | + if (!Files.exists(dataDir)) { |
| 54 | + Files.createDirectories(dataDir); |
| 55 | + } |
| 56 | + Path path = Paths.get(STORAGE_FILE); |
| 57 | + if (!Files.exists(path)) { |
| 58 | + Files.createFile(path); |
| 59 | + } |
| 60 | + } catch (IOException e) { |
| 61 | + System.err.println("Failed to initialize backup records file: " + e.getMessage()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void saveRecord(BackupRecord br) { |
| 66 | + try { |
| 67 | + List<BackupRecord> records = loadAllRecords(); |
| 68 | + records.add(br); |
| 69 | + |
| 70 | + if (records.size() > maxRecords + BUFFER_THRESHOLD) { |
| 71 | + records = records.subList(records.size() - maxRecords, records.size()); |
| 72 | + rewriteFile(records); |
| 73 | + } else { |
| 74 | + appendRecord(br); |
| 75 | + } |
| 76 | + } catch (IOException e) { |
| 77 | + System.err.println("Failed to save backup record: " + e.getMessage()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void appendRecord(BackupRecord br) throws IOException { |
| 82 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(STORAGE_FILE, true))) { |
| 83 | + writer.write(recordToCsvLine(br)); |
| 84 | + writer.newLine(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void rewriteFile(List<BackupRecord> records) throws IOException { |
| 89 | + try (BufferedWriter writer = new BufferedWriter(new FileWriter(STORAGE_FILE))) { |
| 90 | + for (BackupRecord br : records) { |
| 91 | + writer.write(recordToCsvLine(br)); |
| 92 | + writer.newLine(); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public List<BackupRecord> loadAllRecords() { |
| 98 | + List<BackupRecord> records = new ArrayList<>(); |
| 99 | + Path path = Paths.get(STORAGE_FILE); |
| 100 | + |
| 101 | + if (!Files.exists(path)) { |
| 102 | + return records; |
| 103 | + } |
| 104 | + |
| 105 | + try (BufferedReader reader = Files.newBufferedReader(path)) { |
| 106 | + String line; |
| 107 | + while ((line = reader.readLine()) != null) { |
| 108 | + if (!line.trim().isEmpty()) { |
| 109 | + BackupRecord br = parseCsvLine(line); |
| 110 | + if (br != null) { |
| 111 | + records.add(br); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } catch (IOException e) { |
| 116 | + System.err.println("Failed to load backup records: " + e.getMessage()); |
| 117 | + } |
| 118 | + return records; |
| 119 | + } |
| 120 | + |
| 121 | + public List<BackupRecord> getRecordsByTimeRange(LocalDateTime start, LocalDateTime end) { |
| 122 | + return loadAllRecords().stream() |
| 123 | + .filter(br -> !br.getTimestamp().isBefore(start)) |
| 124 | + .filter(br -> !br.getTimestamp().isAfter(end)) |
| 125 | + .sorted(Comparator.comparing(BackupRecord::getTimestamp).reversed()) |
| 126 | + .collect(Collectors.toList()); |
| 127 | + } |
| 128 | + |
| 129 | + public int calculateTotalPages(List<BackupRecord> records) { |
| 130 | + if (records == null || records.isEmpty()) { |
| 131 | + return 0; |
| 132 | + } |
| 133 | + return (int) Math.ceil((double) records.size() / PAGE_SIZE); |
| 134 | + } |
| 135 | + |
| 136 | + public int getRecordsTotalPages() { |
| 137 | + List<BackupRecord> backupRecords = loadAllRecords(); |
| 138 | + return calculateTotalPages(backupRecords); |
| 139 | + } |
| 140 | + |
| 141 | + public int getRecordsTotalPagesByTimeRange(LocalDateTime start, LocalDateTime end) { |
| 142 | + List<BackupRecord> list = getRecordsByTimeRange(start, end); |
| 143 | + return calculateTotalPages(list); |
| 144 | + } |
| 145 | + |
| 146 | + public List<BackupRecord> getPaginatedRecords(List<BackupRecord> records, int page) { |
| 147 | + int fromIndex = (page - 1) * PAGE_SIZE; |
| 148 | + if (fromIndex >= records.size()) { |
| 149 | + return new ArrayList<>(); |
| 150 | + } |
| 151 | + |
| 152 | + int toIndex = Math.min(fromIndex + PAGE_SIZE, records.size()); |
| 153 | + return records.subList(fromIndex, toIndex); |
| 154 | + } |
| 155 | + |
| 156 | + private String recordToCsvLine(BackupRecord br) { |
| 157 | + return String.join(",", |
| 158 | + escapeCsvField(br.getCommand()), |
| 159 | + escapeCsvField(br.getWalletName()), |
| 160 | + escapeCsvField(br.getOwnerAddress()), |
| 161 | + br.getTimestamp().format(TIMESTAMP_FORMAT)); |
| 162 | + } |
| 163 | + |
| 164 | + private BackupRecord parseCsvLine(String line) { |
| 165 | + try { |
| 166 | + String[] parts = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1); |
| 167 | + |
| 168 | + if (parts.length != 4) { |
| 169 | + throw new IllegalArgumentException("Invalid CSV line format"); |
| 170 | + } |
| 171 | + |
| 172 | + return new BackupRecord( |
| 173 | + unescapeCsvField(parts[0]), |
| 174 | + unescapeCsvField(parts[1]), |
| 175 | + unescapeCsvField(parts[2]), |
| 176 | + LocalDateTime.parse(parts[3], TIMESTAMP_FORMAT)); |
| 177 | + } catch (Exception e) { |
| 178 | + System.err.println("Failed to parse CSV line: " + line + ", error: " + e.getMessage()); |
| 179 | + return null; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private String escapeCsvField(String field) { |
| 184 | + if (field.contains(",") || field.contains("\"") || field.contains("\n")) { |
| 185 | + return "\"" + field.replace("\"", "\"\"") + "\""; |
| 186 | + } |
| 187 | + return field; |
| 188 | + } |
| 189 | + |
| 190 | + private String unescapeCsvField(String field) { |
| 191 | + if (field.startsWith("\"") && field.endsWith("\"")) { |
| 192 | + return field.substring(1, field.length() - 1).replace("\"\"", "\""); |
| 193 | + } |
| 194 | + return field; |
| 195 | + } |
| 196 | +} |
0 commit comments