Skip to content

Commit

Permalink
add reverse lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
firestar committed Oct 27, 2023
1 parent 6807023 commit 0156dd2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/groovy/nucleodb.app.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'com.nucleodb'
version = '1.7.4'
version = '1.7.5'

repositories {
mavenCentral()
Expand Down
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.4'
version = '1.9.5'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
public class ConnectionHandler implements Serializable{
private static final long serialVersionUID = 1;
private transient Map<String, Set<Connection>> connections = new TreeMap<>();
private transient Map<String, Set<Connection>> connectionsReverse = new TreeMap<>();
private transient Map<String, Connection> connectionByUUID = new TreeMap<>();
private Map<Integer, Long> partitionOffsets = new TreeMap<>();

Expand Down Expand Up @@ -149,40 +150,41 @@ public Set<Connection> get(DataEntry de){
}
return null;
}
public Stream<Connection> getStream(DataEntry de){
Set<Connection> tmp = get(de);
if(tmp!=null){
return tmp.stream();

public Set<Connection> getByLabel(DataEntry de, String label){
Set<Connection> tmp = connections.get(de.getKey()+label);
if(tmp!=null) {
return tmp.stream().map(c->c.clone()).collect(Collectors.toSet());
}
return null;
}

public Set<Connection> getByLabel(DataEntry de, String label){
Set<Connection> tmp = connections.get(de.getKey()+label);
public Set<Connection> getByLabelTo(DataEntry de, String label, DataEntry toDe){
Set<Connection> tmp = connections.get(de.getKey()+toDe.getKey()+label);
if(tmp!=null) {
return tmp.stream().map(c->c.clone()).collect(Collectors.toSet());
}
return null;
}
public Stream<Connection> getByLabelStream(DataEntry de, String label){
Set<Connection> tmp = getByLabel(de, label);
if(tmp!=null){
return tmp.stream();
public Set<Connection> getByTo(DataEntry de, DataEntry toDe){
Set<Connection> tmp = connections.get(de.getKey()+toDe.getKey());
if(tmp!=null) {
return tmp.stream().map(c->c.clone()).collect(Collectors.toSet());
}
return null;
}

public Set<Connection> getByLabelTo(DataEntry de, String label, DataEntry toDe){
Set<Connection> tmp = connections.get(de.getKey()+toDe.getKey()+label);
public Set<Connection> getReverseByLabelTo(DataEntry de, String label, DataEntry toDe){
Set<Connection> tmp = connectionsReverse.get(de.getKey()+toDe.getKey()+label);
if(tmp!=null) {
return tmp.stream().map(c->c.clone()).collect(Collectors.toSet());
}
return null;
}
public Stream<Connection> getByLabelToStream(DataEntry de, String label,DataEntry toDe){
Set<Connection> tmp = getByLabelTo(de, label, toDe);
if(tmp!=null){
return tmp.stream();
public Set<Connection> getReverseByTo(DataEntry de, DataEntry toDe){
Set<Connection> tmp = connectionsReverse.get(de.getKey()+toDe.getKey());
if(tmp!=null) {
return tmp.stream().map(c->c.clone()).collect(Collectors.toSet());
}
return null;
}
Expand All @@ -194,13 +196,23 @@ private void putConnectionInKey(String key, Connection connection){
connections.get(key).add(connection);
}

private void putReverseConnectionInKey(String key, Connection connection){
if(!connectionsReverse.containsKey(key)){
connectionsReverse.put(key, new TreeSetExt<>());
}
connectionsReverse.get(key).add(connection);
}

private void addConnection(Connection connection){
connection.connectionHandler = this;
connectionByUUID.put(connection.getUuid(), connection);
String connectionKey = connection.getFromKey();
this.putConnectionInKey(connectionKey, connection);
this.putConnectionInKey(connection.getFromKey()+connection.getLabel(), connection);
this.putConnectionInKey(connection.getFromKey()+connection.getToKey(), connection);
this.putConnectionInKey(connection.getFromKey()+connection.getToKey()+connection.getLabel(), connection);
this.putReverseConnectionInKey(connection.getToKey()+connection.getFromKey()+connection.getLabel(), connection);
this.putReverseConnectionInKey(connection.getToKey()+connection.getFromKey(), connection);
allConnections.add(connection);
}

Expand All @@ -209,13 +221,22 @@ private void removeByKey(String key, Connection connection){
connections.get(key).remove(connection);
}
}
private void removeReverseByKey(String key, Connection connection){
if(connectionsReverse.containsKey(key)){
connectionsReverse.get(key).remove(connection);
}
}

private void removeConnection(Connection connection){
connectionByUUID.remove(connection.getUuid());
String connectionKey = connection.getFromKey();
this.removeByKey(connectionKey, connection);
this.removeByKey(connection.getFromKey()+connection.getLabel(), connection);
this.removeByKey(connection.getFromKey()+connection.getToKey(), connection);
this.removeByKey(connection.getFromKey()+connection.getToKey()+connection.getLabel(), connection);
this.removeReverseByKey(connection.getToKey()+connection.getFromKey()+connection.getLabel(), connection);
this.removeReverseByKey(connection.getToKey()+connection.getFromKey(), connection);

allConnections.remove(connection);
}
public void consume() {
Expand Down
6 changes: 3 additions & 3 deletions testapps/src/main/java/com/nucleocore/test/AnimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public static void main(String[] args) throws IOException, InterruptedException
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Serializer.log(nucleoDB.getConnectionHandler().getByLabelStream(user.get(), "WATCHING").findFirst().get());
Optional<Connection> connectionOptional = nucleoDB.getConnectionHandler().getByLabelStream(user.get(), "WATCHING").findFirst();
Serializer.log(nucleoDB.getConnectionHandler().getByLabel(user.get(), "WATCHING").stream().findFirst().get());
Optional<Connection> connectionOptional = nucleoDB.getConnectionHandler().getByLabel(user.get(), "WATCHING").stream().findFirst();
if(connectionOptional.isPresent()) {
nucleoDB.getConnectionHandler().deleteSync(connectionOptional.get());
}
connectionOptional = nucleoDB.getConnectionHandler().getByLabelStream(user.get(), "WATCHING").findFirst();
connectionOptional = nucleoDB.getConnectionHandler().getByLabel(user.get(), "WATCHING").stream().findFirst();
if(connectionOptional.isPresent()) {
Serializer.log("connection failed to delete.");
Serializer.log(connectionOptional.get());
Expand Down

0 comments on commit 0156dd2

Please sign in to comment.