Skip to content

Commit

Permalink
delete sync for data entries, better exception for invalid connections
Browse files Browse the repository at this point in the history
  • Loading branch information
firestar committed Nov 11, 2023
1 parent bfd7406 commit 4fd0bdb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/groovy/nucleodb.library.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'com.nucleodb'
version = '1.9.29'
version = '1.9.30'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> invalidateConnection(Connection c){
List<String> 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<String> 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<Connection> consumer) {
if(this.invalidateConnection(connection)) {
Serializer.log("ERROR, invalid connection!");
Serializer.log(connection);
return false;
public boolean save(Connection connection, Consumer<Connection> consumer) throws InvalidConnectionException {
List<String> 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<String> 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)->{
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataEntry> consumer) {
return deleteInternalConsumer(obj, consumer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4fd0bdb

Please sign in to comment.