Skip to content

Commit

Permalink
When deleting a datasource the executions of this datasource will be …
Browse files Browse the repository at this point in the history
…deleted too.
  • Loading branch information
tabergma committed Nov 25, 2015
1 parent 674d1b3 commit 11dac09
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,17 @@

package de.metanome.backend.results_db;

import de.metanome.algorithm_integration.algorithm_types.BasicStatisticsAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.ConditionalUniqueColumnCombinationAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.DatabaseConnectionParameterAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.FileInputParameterAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.FunctionalDependencyAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.InclusionDependencyAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.OrderDependencyAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.RelationalInputParameterAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.TableInputParameterAlgorithm;
import de.metanome.algorithm_integration.algorithm_types.UniqueColumnCombinationsAlgorithm;

import de.metanome.algorithm_integration.algorithm_types.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlTransient;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlTransient;

/**
* Represents an algorithm in the database.
*
Expand Down Expand Up @@ -277,7 +259,7 @@ public Algorithm setBasicStat(boolean isBasicStat) {
}

@XmlTransient
@OneToMany(cascade = CascadeType.ALL, mappedBy = "algorithm")
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "algorithm")
@OnDelete(action = OnDeleteAction.CASCADE)
public List<Execution> getExecutions() {
return executions;
Expand Down
44 changes: 11 additions & 33 deletions backend/src/main/java/de/metanome/backend/results_db/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,15 @@

package de.metanome.backend.results_db;

import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.Type;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.annotations.*;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.*;

/**
* Represents an execution in the database.
Expand Down Expand Up @@ -184,15 +163,14 @@ public Execution setCountResult(Boolean countResult) {
return this;
}

@ManyToMany(fetch = FetchType.EAGER)
@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@Fetch(value = FetchMode.SELECT)
@CollectionId(
columns = @Column(name = "ExecutionInput"),
type = @Type(type = "long"),
generator = "sequence"
@JoinTable(
name = "execution_input",
joinColumns = {@JoinColumn(name = "execution_id")},
inverseJoinColumns = {@JoinColumn(name = "input_id")}
)
@JoinTable
public Collection<Input> getInputs() {
public List<Input> getInputs() {
return inputs;
}

Expand Down
31 changes: 18 additions & 13 deletions backend/src/main/java/de/metanome/backend/results_db/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@

package de.metanome.backend.results_db;

import com.google.common.annotations.GwtCompatible;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.annotations.GwtCompatible;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlTransient;
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Transient;
import java.util.ArrayList;
import java.util.List;

/**
* Represents superclass inputs in the database.
Expand All @@ -50,6 +46,7 @@ public class Input implements Serializable {

protected long id;
protected String name;
protected List<Execution> executions = new ArrayList<>();

// Exists for Serialization
public Input() {
Expand Down Expand Up @@ -79,6 +76,17 @@ public void setName(String name) {
this.name = name;
}

@XmlTransient
@ManyToMany( fetch = FetchType.EAGER, mappedBy = "inputs", cascade = CascadeType.ALL )
public List<Execution> getExecutions() {
return executions;
}

@XmlTransient
public void setExecutions(List<Execution> executions) {
this.executions = executions;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -90,11 +98,8 @@ public boolean equals(Object o) {

Input input = (Input) o;

if (id != input.id) {
return false;
}
return id == input.id;

return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public void testInputsToJson() throws Exception {

List<String> inputsJson = algorithmExecutionResource.inputsToJson(inputValues);

String expectedJson = "{\"type\":\"Input\",\"id\":0,\"name\":\"myFile\",\"identifier\":\"0\"}";
String expectedJson = "{\"type\":\"Input\",\"id\":0,\"name\":\"myFile\",\"executions\":[],\"identifier\":\"0\"}";

assertThat(inputsJson, contains(expectedJson));


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ public void testPersistenceWithAlgorithmAndResultAndInputs()
// Inputs
FileInput expectedFileInput = new FileInput("fileInput");
expectedExecution.addInput(expectedFileInput);
// Inputs can be added twice
expectedExecution.addInput(expectedFileInput);
TableInput expectedTableInput = new TableInput("fileInput");
expectedExecution.addInput(expectedTableInput);

Expand All @@ -193,7 +191,7 @@ public void testPersistenceWithAlgorithmAndResultAndInputs()

Execution actualExecution = executionResource.get(expectedExecution.getId());

Input[] expectedInputs = {expectedTableInput, expectedFileInput, expectedFileInput};
Input[] expectedInputs = {expectedTableInput, expectedFileInput};

Set<Result> actualResults = actualExecution.getResults();
Collection<Input> actualInputs = actualExecution.getInputs();
Expand All @@ -205,7 +203,7 @@ public void testPersistenceWithAlgorithmAndResultAndInputs()
assertTrue(actualResults.contains(expectedResult1));
assertTrue(actualResults.contains(expectedResult2));
// Verify input list
assertEquals(3, actualInputs.size());
assertEquals(2, actualInputs.size());
assertThat(actualInputs, IsIterableContainingInAnyOrder
.containsInAnyOrder(expectedInputs));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@

import au.com.bytecode.opencsv.CSVParser;
import au.com.bytecode.opencsv.CSVReader;

import de.metanome.backend.input.file.FileIterator;
import de.metanome.backend.resources.AlgorithmResource;
import de.metanome.backend.resources.ExecutionResource;
import de.metanome.backend.resources.FileInputResource;
import de.metanome.test_helper.EqualsAndHashCodeTester;

import org.junit.Test;

import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;

/**
Expand Down Expand Up @@ -114,4 +117,39 @@ public void testGetIdentifier() {
// Check
assertEquals(fileName, actualIdentifier);
}

@Test
public void testCascadingDelete() throws EntityStorageException {
AlgorithmResource algorithmResource = new AlgorithmResource();
ExecutionResource executionResource = new ExecutionResource();

// Setup
HibernateUtil.clear();

// Store prerequisite objects in the database
Algorithm algorithm = new Algorithm("example_ind_algorithm.jar");
algorithmResource.store(algorithm);

// Expected values
long begin = new Date().getTime();
Execution expectedExecution = new Execution(algorithm, begin);
// Adding results and inputs
// Results
Result result = new Result("some result file path");
expectedExecution.addResult(result);
// Inputs
FileInput expectedFileInput = new FileInput("fileInput");
expectedExecution.addInput(expectedFileInput);

// Execute functionality
HibernateUtil.store(expectedFileInput);
HibernateUtil.store(expectedExecution);

// Execute
fileInputResource.delete(expectedFileInput.id);

// Check
List<Execution> actualExecutions = executionResource.getAll();
assertEquals(0, actualExecutions.size());
}
}

0 comments on commit 11dac09

Please sign in to comment.