Así es como terminé implementando versiones para entidades MongoDB. ¡Gracias a la comunidad de StackOverflow por ayudarme!
- Se guarda un registro de cambios para cada entidad en una colección de historial separada.
- Para evitar guardar una gran cantidad de datos, la colección de historial no almacena instancias completas, sino solo la primera versión y las diferencias entre las versiones. (Incluso podría omitir la primera versión y reconstruir las versiones "al revés" de la versión actual en la colección principal de la entidad.)
- Java Object Diff se utiliza para generar objetos diffs.
- Para poder trabajar con colecciones correctamente, se necesita implementar el método
equals
de las entidades para que pruebe la clave principal de la base de datos y no las subpropiedades. (De lo contrario, JavaObjectDiff no reconocerá los cambios de propiedad en los elementos de colección.)
Estas son las entidades que uso para el control de versiones (getters/setters, etc.eliminado):
// This entity is stored once (1:1) per entity that is to be versioned
// in an own collection
public class MongoDiffHistoryEntry {
/* history id */
private String id;
/* reference to original entity */
private String objectId;
/* copy of original entity (first version) */
private Object originalObject;
/* differences collection */
private List<MongoDiffHistoryChange> differences;
/* delete flag */
private boolean deleted;
}
// changeset for a single version
public class MongoDiffHistoryChange {
private Date historyDate;
private List<MongoDiffHistoryChangeItem> items;
}
// a single property change
public class MongoDiffHistoryChangeItem {
/* path to changed property (PropertyPath) */
private String path;
/* change state (NEW, CHANGED, REMOVED etc.) */
private Node.State state;
/* original value (empty for NEW) */
private Object base;
/* new value (empty for REMOVED) */
private Object modified;
}
Aquí es la operación saveChangeHistory:
private void saveChangeHistory(Object working, Object base) {
assert working != null && base != null;
assert working.getClass().equals(base.getClass());
String baseId = ObjectUtil.getPrimaryKeyValue(base).toString();
String workingId = ObjectUtil.getPrimaryKeyValue(working).toString();
assert baseId != null && workingId != null && baseId.equals(workingId);
MongoDiffHistoryEntry entry = getObjectHistory(base.getClass(), baseId);
if (entry == null) {
//throw new RuntimeException("history not found: " + base.getClass().getName() + "#" + baseId);
logger.warn("history lost - create new base history record: {}#{}", base.getClass().getName(), baseId);
saveNewHistory(base);
saveHistory(working, base);
return;
}
final MongoDiffHistoryChange change = new MongoDiffHistoryChange();
change.setHistoryDate(new Date());
change.setItems(new ArrayList<MongoDiffHistoryChangeItem>());
ObjectDiffer differ = ObjectDifferFactory.getInstance();
Node root = differ.compare(working, base);
root.visit(new MongoDiffHistoryChangeVisitor(change, working, base));
if (entry.getDifferences() == null)
entry.setDifferences(new ArrayList<MongoDiffHistoryChange>());
entry.getDifferences().add(change);
mongoTemplate.save(entry, getHistoryCollectionName(working.getClass()));
}
Ésta es la forma en que se ve como en MongoDB:
{
"_id" : ObjectId("5040a9e73c75ad7e3590e538"),
"_class" : "MongoDiffHistoryEntry",
"objectId" : "5034c7a83c75c52dddcbd554",
"originalObject" : {
BLABLABLA, including sections collection etc.
},
"differences" : [{
"historyDate" : ISODate("2012-08-31T12:11:19.667Z"),
"items" : [{
"path" : "/sections[[email protected]]",
"state" : "ADDED",
"modified" : {
"_class" : "LetterSection",
"_id" : ObjectId("5034c7a83c75c52dddcbd556"),
"letterId" : "5034c7a83c75c52dddcbd554",
"sectionIndex" : 2,
"stringContent" : "BLABLA",
"contentMimetype" : "text/plain",
"sectionConfiguration" : "BLUBB"
}
}, {
"path" : "/sections[[email protected]]",
"state" : "REMOVED",
"base" : {
"_class" : "LetterSection",
"_id" : ObjectId("5034c7a83c75c52dddcbd556"),
"letterId" : "5034c7a83c75c52dddcbd554",
"sectionIndex" : 2,
"stringContent" : "BLABLABLA",
"contentMimetype" : "text/plain",
"sectionConfiguration" : "BLUBB"
}
}]
}, {
"historyDate" : ISODate("2012-08-31T13:15:32.574Z"),
"items" : [{
"path" : "/sections[[email protected]]/stringContent",
"state" : "CHANGED",
"base" : "blub5",
"modified" : "blub6"
}]
},
}],
"deleted" : false
}
EDIT: Aquí está el código de Visitantes:
public class MongoDiffHistoryChangeVisitor implements Visitor {
private MongoDiffHistoryChange change;
private Object working;
private Object base;
public MongoDiffHistoryChangeVisitor(MongoDiffHistoryChange change, Object working, Object base) {
this.change = change;
this.working = working;
this.base = base;
}
public void accept(Node node, Visit visit) {
if (node.isRootNode() && !node.hasChanges() ||
node.hasChanges() && node.getChildren().isEmpty()) {
MongoDiffHistoryChangeItem diffItem = new MongoDiffHistoryChangeItem();
diffItem.setPath(node.getPropertyPath().toString());
diffItem.setState(node.getState());
if (node.getState() != State.UNTOUCHED) {
diffItem.setBase(node.canonicalGet(base));
diffItem.setModified(node.canonicalGet(working));
}
if (change.getItems() == null)
change.setItems(new ArrayList<MongoDiffHistoryChangeItem>());
change.getItems().add(diffItem);
}
}
}
No llena de versiones, pero hemos implementado un pequeño sistema de auditoría - tala quién cambió qué valores antiguos a los nuevos. Estamos utilizando el método '' prePersist() '' de Morphia (que solo funcionará para guardar entidades completas, no actualizaciones específicas). Puede proporcionar algunas muestras de código, pero no es nada sofisticado ... – xeraa
¡Gracias por tu comentario! Me interesaría mucho más detalles para demostrar tu solución. Solo el seguimiento completo de las salvaciones de entidades está definitivamente bien: este es nuestro caso de uso principal, también. Un punto muy interesante es la forma en que se compara la antigua con la nueva entidad, identificando las propiedades modificadas. Eché un vistazo a los marcos de comparación de gráficos aquí, pero no encontré una solución rápida y fácil. –