Skip to content

Instantly share code, notes, and snippets.

@Jotschi
Last active April 29, 2016 13:41
Show Gist options
  • Save Jotschi/07e127d87c4c0bd4455c89ed209aaa25 to your computer and use it in GitHub Desktop.
Save Jotschi/07e127d87c4c0bd4455c89ed209aaa25 to your computer and use it in GitHub Desktop.
package com.gentics.mesh.changelog;
import com.tinkerpop.blueprints.TransactionalGraph;
/**
* Interface for a mesh graph database change. A change may alter graph structure, content and indices.
*/
public interface Change {
/**
* Return the uuid of the change.
*
* @return
*/
String getUuid();
/**
* Return the name of the change.
*
* @return
*/
String getName();
/**
* Apply the change to the graph.
*/
void apply();
/**
* Check whether the change already has been applied.
*
* @return
*/
boolean isApplied();
/**
* Return the description of the change.
*
* @return
*/
String getDescription();
/**
* Set the graph instance to be used when handling the change.
*
* @param graph
*/
void setGraph(TransactionalGraph graph);
/**
* Return the graph that is currently set for the change.
*
* @return
*/
TransactionalGraph getGraph();
/**
* Persist the info that the change has been applied to the graph.
*/
void markAsComplete();
/**
* Set the time that passed to execute the change.
*
* @param timeMs
*/
void setDuration(long timeMs);
/**
* Return the time it took to execute the change.
*
* @return
*/
long getDuration();
/**
* Validate the change. A unsuccessful validation will abort the changelog execution.
*
* @return true if validation was successful. Otherwise false.
*/
boolean validate();
}
/**
* Example changelog entry.
*/
public class Change_093BEFB47FA4476FBE37FD27C613F7AA extends AbstractChange {
@Override
public String getName() {
return "Add magic";
}
@Override
public String getDescription() {
return "Adds a magic vertex";
}
@Override
public void apply() {
Vertex meshRootVertex = getMeshRootVertex();
Vertex newMagicVertex = getGraph().addVertex("magic");
newMagicVertex.setProperty("name", "tinkerpop magic");
meshRootVertex.addEdge("HAS_MAGIC", newMagicVertex);
log.info("Added magic vertex");
}
}
/**
* Apply all listed changes.
*
* @param list
* @return Flag which indicates whether all changes were applied successfully
*/
public boolean applyChanges(List<Change> list) {
for (Change change : list) {
// Execute each change in a new transaction
TransactionalGraph graph = db.rawTx();
change.setGraph(graph);
try {
if (!change.isApplied()) {
log.info("Handling change {" + change.getUuid() + "}");
log.info("Name: " + change.getName());
log.info("Description: " + change.getDescription());
long start = System.currentTimeMillis();
change.apply();
change.setDuration(System.currentTimeMillis() - start);
if (!change.validate()) {
throw new Exception("Validation for change {" + change.getUuid() + "/" + change.getName() + "} failed.");
}
change.markAsComplete();
} else {
log.debug("Change {" + change.getUuid() + "} is already applied.");
}
} catch (Exception e) {
log.error("Error while handling change {" + change.getUuid() + "/" + change.getName() + "}. Invoking rollback..", e);
graph.rollback();
return false;
} finally {
graph.shutdown();
}
}
return true;
}
/**
* Typical list of changelog entries.
*/
public final class ChangesList {
public static List<Change> getList() {
List<Change> list = new ArrayList<>();
list.add(new Change_093BEFB47FA4476FBE37FD27C613F7AA());
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment