MongoDB
 sql >> डेटाबेस >  >> NoSQL >> MongoDB

जावा मोंगोडीबी ऑब्जेक्ट वर्जनिंग

इस तरह मैंने मोंगोडीबी इकाइयों के लिए वर्जनिंग लागू करना समाप्त कर दिया। मदद करने के लिए StackOverflow समुदाय को धन्यवाद!

  • प्रत्येक इकाई के लिए एक अलग इतिहास संग्रह में एक परिवर्तन लॉग रखा जाता है।
  • बहुत सारे डेटा को बचाने से बचने के लिए, इतिहास संग्रह पूर्ण उदाहरणों को संग्रहीत नहीं करता है, लेकिन केवल पहले संस्करण और संस्करणों के बीच अंतर को संग्रहीत करता है। (आप पहले संस्करण को छोड़ भी सकते हैं और इकाई के मुख्य संग्रह में वर्तमान संस्करण से "पीछे की ओर" संस्करणों का पुनर्निर्माण कर सकते हैं।)
  • जावा ऑब्जेक्ट डिफ का उपयोग ऑब्जेक्ट डिफरेंस उत्पन्न करने के लिए किया जाता है।
  • संग्रहों के साथ सही ढंग से काम करने में सक्षम होने के लिए, किसी को equals लागू करने की आवश्यकता है संस्थाओं की विधि ताकि यह डेटाबेस प्राथमिक कुंजी के लिए परीक्षण करे, न कि उप गुणों के लिए। (अन्यथा, JavaObjectDiff संग्रह तत्वों में संपत्ति परिवर्तन की पहचान नहीं करेगा।)

यहाँ वे इकाइयाँ हैं जिनका उपयोग मैं संस्करण बनाने के लिए करता हूँ (गेटर्स/सेटर्स आदि हटा दिए गए):

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

यहां सेव चेंजहिस्ट्री ऑपरेशन है:

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[[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
}

संपादित करें:यहां विज़िटर कोड है:

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

}


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. MongoDB क्वेरी केवल एम्बेडेड दस्तावेज़ वापस करने के लिए

  2. MongoDB के शेल में 20 से अधिक आइटम (दस्तावेज़) कैसे प्रिंट करें?

  3. उल्का और फाइबर/बाइंडएनवायरनमेंट () के साथ क्या हो रहा है?

  4. मोंगोडब अपडेट में एक चर का उपयोग करना

  5. MongoDB में एक तिथि से सप्ताह प्राप्त करने के 3 तरीके