Java MongoDB 对象版本控制

16

我需要在文档导向数据库(MongoDB)中存储的(简单)Java对象图上执行版本控制。对于关系型数据库和Hibernate,我发现了Envers,对其功能非常惊喜。是否有类似的Spring Data Documents可用的东西?

我发现这篇文章概述了我的思路(以及更多...),即将对象的版本存储到单独的历史集合中,并包含时间戳,但我希望改进它以节省存储空间。因此,我认为我需要实现对象树上的“差异”操作和用于重构旧对象的“合并”操作。是否有任何库可以帮助实现这一点?

编辑:非常感谢与MongoDB和版本控制相关的任何经验!我认为可能不会有Spring Data解决方案。


不是完整的版本控制,但我们已经实现了一个小型审计系统 - 记录谁将哪些旧值更改为新值。我们正在使用Morphia的“prePersist()”方法(仅适用于完整实体保存,而不是特定更新)。可以提供一些代码示例,但这并不复杂... - xeraa
谢谢您的评论!我非常感兴趣了解更多详细信息,以展示您的解决方案。仅跟踪完整实体保存肯定是可以的:这也是我们的主要用例。一个非常有趣的点是您比较旧实体和新实体的方式,识别已更改的属性。我在这里查看了图形比较框架,但没有找到快速简便的解决方案。 - Matthias Wuttke
3个回答

15

以下是我最终实施的 MongoDB 实体版本控制方案。感谢 StackOverflow 社区提供的帮助!

  • 为每个实体单独创建一个历史记录集合,用于保存变更日志。
  • 为了避免保存大量数据,历史记录集合仅存储第一个版本和版本之间的差异(你甚至可以省略第一个版本,在实体的主要集合中从当前版本“向后”重建版本)。
  • 使用 Java Object Diff 生成对象差异。
  • 为了能够正确地使用集合,需要实现实体的 equals 方法,以测试数据库主键而不是子属性。(否则,JavaObjectDiff 将无法识别集合元素中的属性更改。)

这里是我用于版本控制的实体(getter/setter 等已省略):

// 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;
}

这里是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()));
}

这是在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[LetterSection@116a3de]",
          "state" : "ADDED",
          "modified" : {
            "_class" : "LetterSection",
            "_id" : ObjectId("5034c7a83c75c52dddcbd556"),
            "letterId" : "5034c7a83c75c52dddcbd554",
            "sectionIndex" : 2,
            "stringContent" : "BLABLA",
            "contentMimetype" : "text/plain",
            "sectionConfiguration" : "BLUBB"
          }
        }, {
          "path" : "/sections[LetterSection@19546ee]",
          "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[LetterSection@44a38a]/stringContent",
          "state" : "CHANGED",
          "base" : "blub5",
          "modified" : "blub6"
        }]
    },
    }],
  "deleted" : false
}

编辑:以下是访问者模式的代码:

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);
    }
}

}

对于那些几年后查看此内容的其他人 - Visitor :: accept 已更名为 Visitor :: node。 - Kyrstellaine

8

我们正在使用一个基本实体(在其中设置了Id,创建和最后更改日期等)。 在此基础上,我们正在使用通用持久性方法,大致如下:

@Override
public <E extends BaseEntity> ObjectId persist(E entity) {
    delta(entity);
    mongoDataStore.save(entity);
    return entity.getId();
}

Delta方法看起来是这样的(我会尽可能通用):
protected <E extends BaseEntity> void delta(E newEntity) {

    // If the entity is null or has no ID, it hasn't been persisted before,
    // so there's no delta to calculate
    if ((newEntity == null) || (newEntity.getId() == null)) {
        return;
    }

    // Get the original entity
    @SuppressWarnings("unchecked")
    E oldEntity = (E) mongoDataStore.get(newEntity.getClass(), newEntity.getId()); 

    // Ensure that the old entity isn't null
    if (oldEntity == null) {
        LOG.error("Tried to compare and persist null objects - this is not allowed");
        return;
    }

    // Get the current user and ensure it is not null
    String email = ...;

    // Calculate the difference
    // We need to fetch the fields from the parent entity as well as they
    // are not automatically fetched
    Field[] fields = ArrayUtils.addAll(newEntity.getClass().getDeclaredFields(),
            BaseEntity.class.getDeclaredFields());
    Object oldField = null;
    Object newField = null;
    StringBuilder delta = new StringBuilder();
    for (Field field : fields) {
        field.setAccessible(true); // We need to access private fields
        try {
            oldField = field.get(oldEntity);
            newField = field.get(newEntity);
        } catch (IllegalArgumentException e) {
            LOG.error("Bad argument given");
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            LOG.error("Could not access the argument");
            e.printStackTrace();
        }
        if ((oldField != newField)
                && (((oldField != null) && !oldField.equals(newField)) || ((newField != null) && !newField
                        .equals(oldField)))) {
            delta.append(field.getName()).append(": [").append(oldField).append("] -> [")
                    .append(newField).append("]  ");
        }
    }

    // Persist the difference
    if (delta.length() == 0) {
        LOG.warn("The delta is empty - this should not happen");
    } else {
        DeltaEntity deltaEntity = new DeltaEntity(oldEntity.getClass().toString(),
                oldEntity.getId(), oldEntity.getUuid(), email, delta.toString());
        mongoDataStore.save(deltaEntity);
    }
    return;
}

我们的 delta 实体看起来像这样(不包含 getter + setter、toString、hashCode 和 equals 函数):
@Entity(value = "delta", noClassnameStored = true)
public final class DeltaEntity extends BaseEntity {
    private static final long serialVersionUID = -2770175650780701908L;

    private String entityClass; // Do not call this className as Morphia will
                            // try to work some magic on this automatically
    private ObjectId entityId;
    private String entityUuid;
    private String userEmail;
    private String delta;

    public DeltaEntity() {
        super();
    }

    public DeltaEntity(final String entityClass, final ObjectId entityId, final String entityUuid,
            final String userEmail, final String delta) {
        this();
        this.entityClass = entityClass;
        this.entityId = entityId;
        this.entityUuid = entityUuid;
        this.userEmail = userEmail;
        this.delta = delta;
    }

希望这可以帮助你开始入门IT技术方面的学习。 :-)

非常感谢提供的示例。我还发现了一篇关于Java对象差异的帖子(https://dev59.com/TGsz5IYBdhLWcg3wR1zC),提到了这个库:https://github.com/SQiShER/java-object-diff - 也许我可以用这个差异算法来“调味”你的解决方案。我想再把这个问题保持开放状态一段时间,也许会有其他的想法。 - Matthias Wuttke
有趣的项目,期待您的解决方案。在此期间,仍然感谢您的点赞;-) - xeraa

4

2
实际上,我需要修改我之前的评论。我尝试使用Javers,但发现它不可行,因为它总是从基础版本加上所有更改来构建当前对象,这使得读取时间大约比如果它只是在某个地方存储文档的最新版本要长20倍。而且,由于获取文档的最新版本是主要用例,所以在我看来,这是一个无法解决的问题。 - Kira Resari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接