SHA256
233 lines
9.2 KiB
Java
233 lines
9.2 KiB
Java
package shine.db.dao;
|
|
|
|
import shine.db.DbController;
|
|
import shine.db.entities.DmSyncOutboxEntry;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Types;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/** Outbox событий DM для единственного второго access-сервера пользователя. */
|
|
public final class DmSyncOutboxDAO {
|
|
private static volatile DmSyncOutboxDAO instance;
|
|
private final DbController db = DbController.getInstance();
|
|
|
|
private DmSyncOutboxDAO() {}
|
|
|
|
public static DmSyncOutboxDAO getInstance() {
|
|
if (instance == null) {
|
|
synchronized (DmSyncOutboxDAO.class) {
|
|
if (instance == null) instance = new DmSyncOutboxDAO();
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public void upsert(
|
|
String ownerLogin,
|
|
String primaryMessageKey,
|
|
String eventId,
|
|
String secondaryMessageKey,
|
|
boolean synced,
|
|
long createdAtMs
|
|
) throws SQLException {
|
|
long now = System.currentTimeMillis();
|
|
String sql = """
|
|
INSERT INTO dm_sync_outbox (
|
|
owner_login, primary_message_key, event_id, secondary_message_key,
|
|
synced, created_at_ms, updated_at_ms
|
|
) VALUES (LOWER(?), ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT (owner_login, primary_message_key) DO UPDATE SET
|
|
event_id = EXCLUDED.event_id,
|
|
secondary_message_key = EXCLUDED.secondary_message_key,
|
|
synced = CASE
|
|
WHEN dm_sync_outbox.event_id = EXCLUDED.event_id
|
|
THEN dm_sync_outbox.synced OR EXCLUDED.synced
|
|
ELSE EXCLUDED.synced
|
|
END,
|
|
created_at_ms = CASE
|
|
WHEN dm_sync_outbox.event_id = EXCLUDED.event_id
|
|
THEN dm_sync_outbox.created_at_ms
|
|
ELSE EXCLUDED.created_at_ms
|
|
END,
|
|
updated_at_ms = EXCLUDED.updated_at_ms
|
|
""";
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, ownerLogin);
|
|
ps.setString(2, primaryMessageKey);
|
|
ps.setString(3, eventId);
|
|
if (secondaryMessageKey == null || secondaryMessageKey.isBlank()) ps.setNull(4, Types.VARCHAR);
|
|
else ps.setString(4, secondaryMessageKey.trim());
|
|
ps.setBoolean(5, synced);
|
|
ps.setLong(6, createdAtMs);
|
|
ps.setLong(7, now);
|
|
ps.executeUpdate();
|
|
}
|
|
}
|
|
|
|
public List<DmSyncOutboxEntry> listUnsynced(String ownerLogin, int limit) throws SQLException {
|
|
return listUnsynced(ownerLogin, 0L, "", limit);
|
|
}
|
|
|
|
public List<DmSyncOutboxEntry> listUnsynced(
|
|
String ownerLogin, long afterCreatedAtMs, String afterPrimaryMessageKey, int limit
|
|
) throws SQLException {
|
|
String sql = """
|
|
SELECT owner_login, primary_message_key, event_id, secondary_message_key,
|
|
synced, created_at_ms, updated_at_ms
|
|
FROM dm_sync_outbox
|
|
WHERE LOWER(owner_login) = LOWER(?) AND synced = FALSE
|
|
AND (created_at_ms > ? OR (created_at_ms = ? AND primary_message_key > ?))
|
|
ORDER BY created_at_ms ASC, primary_message_key ASC
|
|
LIMIT ?
|
|
""";
|
|
List<DmSyncOutboxEntry> out = new ArrayList<>();
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, ownerLogin);
|
|
ps.setLong(2, Math.max(0L, afterCreatedAtMs));
|
|
ps.setLong(3, Math.max(0L, afterCreatedAtMs));
|
|
ps.setString(4, afterPrimaryMessageKey == null ? "" : afterPrimaryMessageKey);
|
|
ps.setInt(5, Math.max(1, limit));
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
while (rs.next()) out.add(mapRow(rs));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
public boolean hasUnsyncedForServer(String serverLogin) throws SQLException {
|
|
String sql = """
|
|
SELECT 1
|
|
FROM dm_sync_outbox o
|
|
JOIN user_access_servers_current r
|
|
ON LOWER(r.user_login) = LOWER(o.owner_login)
|
|
WHERE o.synced = FALSE AND LOWER(r.server_login) = LOWER(?)
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM user_access_servers_current peer
|
|
WHERE LOWER(peer.user_login) = LOWER(o.owner_login)
|
|
AND LOWER(peer.server_login) <> LOWER(?)
|
|
)
|
|
LIMIT 1
|
|
""";
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, serverLogin);
|
|
ps.setString(2, serverLogin);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
return rs.next();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<String> listUnsyncedOwnersForServer(String serverLogin) throws SQLException {
|
|
String sql = """
|
|
SELECT DISTINCT o.owner_login
|
|
FROM dm_sync_outbox o
|
|
JOIN user_access_servers_current r
|
|
ON LOWER(r.user_login) = LOWER(o.owner_login)
|
|
WHERE o.synced = FALSE AND LOWER(r.server_login) = LOWER(?)
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM user_access_servers_current peer
|
|
WHERE LOWER(peer.user_login) = LOWER(o.owner_login)
|
|
AND LOWER(peer.server_login) <> LOWER(?)
|
|
)
|
|
ORDER BY o.owner_login
|
|
""";
|
|
List<String> result = new ArrayList<>();
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setString(1, serverLogin);
|
|
ps.setString(2, serverLogin);
|
|
try (ResultSet rs = ps.executeQuery()) {
|
|
while (rs.next()) result.add(rs.getString("owner_login"));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public int markSynced(String ownerLogin, String eventId) throws SQLException {
|
|
String sql = """
|
|
UPDATE dm_sync_outbox
|
|
SET synced = TRUE, updated_at_ms = ?
|
|
WHERE LOWER(owner_login) = LOWER(?) AND event_id = ?
|
|
""";
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setLong(1, System.currentTimeMillis());
|
|
ps.setString(2, ownerLogin);
|
|
ps.setString(3, eventId);
|
|
return ps.executeUpdate();
|
|
}
|
|
}
|
|
|
|
public int markSynced(String ownerLogin, List<String> eventIds) throws SQLException {
|
|
if (eventIds == null || eventIds.isEmpty()) return 0;
|
|
int total = 0;
|
|
try (Connection c = db.getConnection();
|
|
PreparedStatement ps = c.prepareStatement("""
|
|
UPDATE dm_sync_outbox
|
|
SET synced = TRUE, updated_at_ms = ?
|
|
WHERE LOWER(owner_login) = LOWER(?) AND event_id = ?
|
|
""")) {
|
|
long now = System.currentTimeMillis();
|
|
for (String eventId : eventIds) {
|
|
if (eventId == null || eventId.isBlank()) continue;
|
|
ps.setLong(1, now);
|
|
ps.setString(2, ownerLogin);
|
|
ps.setString(3, eventId.trim());
|
|
ps.addBatch();
|
|
}
|
|
for (int changed : ps.executeBatch()) if (changed > 0) total += changed;
|
|
}
|
|
return total;
|
|
}
|
|
|
|
public int markAllUnsynced(String ownerLogin) throws SQLException {
|
|
String sql = """
|
|
UPDATE dm_sync_outbox
|
|
SET synced = FALSE, updated_at_ms = ?
|
|
WHERE LOWER(owner_login) = LOWER(?)
|
|
""";
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
ps.setLong(1, System.currentTimeMillis());
|
|
ps.setString(2, ownerLogin);
|
|
return ps.executeUpdate();
|
|
}
|
|
}
|
|
|
|
public int markAllUnsynced() throws SQLException {
|
|
try (Connection c = db.getConnection();
|
|
PreparedStatement ps = c.prepareStatement("UPDATE dm_sync_outbox SET synced = FALSE, updated_at_ms = ?")) {
|
|
ps.setLong(1, System.currentTimeMillis());
|
|
return ps.executeUpdate();
|
|
}
|
|
}
|
|
|
|
public int removeMissingMessages() throws SQLException {
|
|
String sql = """
|
|
DELETE FROM dm_sync_outbox o
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM signed_messages m WHERE m.message_key = o.primary_message_key
|
|
)
|
|
""";
|
|
try (Connection c = db.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
|
|
return ps.executeUpdate();
|
|
}
|
|
}
|
|
|
|
private DmSyncOutboxEntry mapRow(ResultSet rs) throws SQLException {
|
|
DmSyncOutboxEntry e = new DmSyncOutboxEntry();
|
|
e.setOwnerLogin(rs.getString("owner_login"));
|
|
e.setPrimaryMessageKey(rs.getString("primary_message_key"));
|
|
e.setEventId(rs.getString("event_id"));
|
|
e.setSecondaryMessageKey(rs.getString("secondary_message_key"));
|
|
e.setSynced(rs.getBoolean("synced"));
|
|
e.setCreatedAtMs(rs.getLong("created_at_ms"));
|
|
e.setUpdatedAtMs(rs.getLong("updated_at_ms"));
|
|
return e;
|
|
}
|
|
}
|