From 4fd0bdb59077096a80cb7fc4da0e192dd9bd08a1 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 11 Nov 2023 01:00:10 -0700 Subject: [PATCH] delete sync for data entries, better exception for invalid connections --- .../src/main/groovy/nucleodb.library.gradle | 2 +- .../database/tables/ConnectionHandler.java | 44 ++++++++++--------- .../library/database/tables/DataTable.java | 9 ++++ .../utils/InvalidConnectionException.java | 7 +++ 4 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 library/src/main/java/com/nucleocore/library/database/utils/InvalidConnectionException.java diff --git a/buildSrc/src/main/groovy/nucleodb.library.gradle b/buildSrc/src/main/groovy/nucleodb.library.gradle index c416beb..74e29e0 100644 --- a/buildSrc/src/main/groovy/nucleodb.library.gradle +++ b/buildSrc/src/main/groovy/nucleodb.library.gradle @@ -3,7 +3,7 @@ plugins { } group = 'com.nucleodb' -version = '1.9.29' +version = '1.9.30' repositories { mavenCentral() diff --git a/library/src/main/java/com/nucleocore/library/database/tables/ConnectionHandler.java b/library/src/main/java/com/nucleocore/library/database/tables/ConnectionHandler.java index 7c16540..f63ce5c 100644 --- a/library/src/main/java/com/nucleocore/library/database/tables/ConnectionHandler.java +++ b/library/src/main/java/com/nucleocore/library/database/tables/ConnectionHandler.java @@ -9,6 +9,7 @@ import com.nucleocore.library.database.modifications.ConnectionUpdate; import com.nucleocore.library.database.utils.DataEntry; import com.nucleocore.library.database.modifications.Modification; +import com.nucleocore.library.database.utils.InvalidConnectionException; import com.nucleocore.library.database.utils.JsonOperations; import com.nucleocore.library.database.utils.ObjectFileReader; import com.nucleocore.library.database.utils.ObjectFileWriter; @@ -28,6 +29,7 @@ import java.io.Serializable; import java.util.Collections; import java.util.Date; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; @@ -297,34 +299,34 @@ public boolean deleteSync(Connection connection) throws IOException, Interrupted return v; } - public boolean invalidateConnection(Connection c){ - boolean keysHaveNull = c.getFromKey()==null || c.getToKey()==null; - boolean tablesHaveNull = c.getFromTable()==null || c.getToTable()==null; - return keysHaveNull || tablesHaveNull; + public List invalidateConnection(Connection c){ + List invalids = new LinkedList<>(); + if(c.getFromKey()==null) invalids.add("[FromKey]"); + if(c.getToKey()==null) invalids.add("[ToKey]"); + if(c.getFromTable()==null) invalids.add("[FromTable]"); + if(c.getToTable()==null) invalids.add("[ToTable]"); + return invalids; } - public boolean save(Connection connection) { - if(this.invalidateConnection(connection)) { - Serializer.log("ERROR, invalid connection!"); - Serializer.log(connection); - return false; + public boolean save(Connection connection) throws InvalidConnectionException { + List invalids = this.invalidateConnection(connection); + if(invalids.size()>0) { + throw new InvalidConnectionException(invalids.stream().collect(Collectors.joining(", "))); } return saveInternalConsumer(connection, null); } - public boolean save(Connection connection, Consumer consumer) { - if(this.invalidateConnection(connection)) { - Serializer.log("ERROR, invalid connection!"); - Serializer.log(connection); - return false; + public boolean save(Connection connection, Consumer consumer) throws InvalidConnectionException { + List invalids = this.invalidateConnection(connection); + if(invalids.size()>0) { + throw new InvalidConnectionException(invalids.stream().collect(Collectors.joining(", "))); } return saveInternalConsumer(connection, consumer); } - public boolean saveSync(Connection connection) throws InterruptedException { - if(this.invalidateConnection(connection)) { - Serializer.log("ERROR, invalid connection!"); - Serializer.log(connection); - return false; + public boolean saveSync(Connection connection) throws InvalidConnectionException, InterruptedException { + List invalids = this.invalidateConnection(connection); + if(invalids.size()>0) { + throw new InvalidConnectionException(invalids.stream().collect(Collectors.joining(", "))); } CountDownLatch countDownLatch = new CountDownLatch(1); boolean v = saveInternalConsumer(connection, (c)->{ @@ -425,7 +427,7 @@ private boolean saveInternal(Connection connection, String changeUUID) { try { String json = Serializer.getObjectMapper().getOm().writeValueAsString(patch); changes = Serializer.getObjectMapper().getOm().readValue(json, List.class); - Serializer.log(json); + //Serializer.log(json); if (changes != null && changes.size() > 0) { ConnectionUpdate updateEntry = new ConnectionUpdate(connection.getVersion(), json, changeUUID, connection.getUuid()); producer.push(updateEntry, null); @@ -450,7 +452,7 @@ private boolean saveInternalSync(Connection connection, String changeUUID) throw JsonPatch patch = JsonDiff.asJsonPatch(Serializer.getObjectMapper().getOm().valueToTree(oldConnection), Serializer.getObjectMapper().getOm().valueToTree(connection)); try { String json = Serializer.getObjectMapper().getOm().writeValueAsString(patch); - Serializer.log(json); + //Serializer.log(json); changes = Serializer.getObjectMapper().getOm().readValue(json, List.class); if (changes != null && changes.size() > 0) { ConnectionUpdate updateEntry = new ConnectionUpdate(connection.getVersion(), json, changeUUID, connection.getUuid()); diff --git a/library/src/main/java/com/nucleocore/library/database/tables/DataTable.java b/library/src/main/java/com/nucleocore/library/database/tables/DataTable.java index ff7b6f1..7468ff5 100644 --- a/library/src/main/java/com/nucleocore/library/database/tables/DataTable.java +++ b/library/src/main/java/com/nucleocore/library/database/tables/DataTable.java @@ -308,6 +308,15 @@ public int size() { return size; } + public boolean deleteSync(DataEntry obj) throws InterruptedException { + CountDownLatch countDownLatch = new CountDownLatch(1); + boolean v = deleteInternalConsumer(obj, (de)->{ + countDownLatch.countDown(); + }); + countDownLatch.await(); + return v; + } + public boolean delete(DataEntry obj, Consumer consumer) { return deleteInternalConsumer(obj, consumer); } diff --git a/library/src/main/java/com/nucleocore/library/database/utils/InvalidConnectionException.java b/library/src/main/java/com/nucleocore/library/database/utils/InvalidConnectionException.java new file mode 100644 index 0000000..c6d3bac --- /dev/null +++ b/library/src/main/java/com/nucleocore/library/database/utils/InvalidConnectionException.java @@ -0,0 +1,7 @@ +package com.nucleocore.library.database.utils; + +public class InvalidConnectionException extends Exception{ + public InvalidConnectionException(String message) { + super("Invalid Connection Exception, null in connection leaves: "+message); + } +}